《Arduino程序设计基础》第二版 arduino-web-server程序修改

1657

添加AJAX 获取数据并发送指令

<!--例程网页控制Arduino -->
<!--奈何col  2014.12.24 v3.0 -->

<html>
<head>
<meta charset="UTF-8">
<title>OpenJumper!Arduino Web Server</title>
<script type="text/javascript">
function send2arduino(){
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}

	element=document.getElementById("light");
	if (element.innerHTML.match("Turn on"))
	  { 
	  	element.innerHTML="Turn off"; 
	  	xmlhttp.open("GET","on",true);
	  }
	else
	  { 
	  	element.innerHTML="Turn on";
	  	xmlhttp.open("GET","off",true); 
	  }
	xmlhttp.send();	
}
function getBrightness(){
	var xmlhttp;
	if (window.XMLHttpRequest)
	{// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	}
	else
	{// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
    {
    	if (xmlhttp.readyState==4 && xmlhttp.status==200)
    	{
    		document.getElementById("brightness").innerHTML=xmlhttp.responseText;
    	}
    }
	xmlhttp.open("GET","getBrightness",true); 
	xmlhttp.send();	
}
window.setInterval(getBrightness,5000); 
</script>
</head>
<body>
<div align="center">
<h1>Arduino Web Server</h1>
<div>brightness:</div>
<div id="brightness">??</div>

<button id="light" type="button" onclick="send2arduino()">Turn on</button>
<button type="button" onclick="alert('OpenJumper Web Server')">About</button>
</div>
</body>
</html>

Arduino直接输出版本:

#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
EthernetServer server(80);
EthernetClient client;
String readString=""; 
int Light=6;
void setup() {
// 初始化串口通信
Serial.begin(9600);
// 初始化Ethernet通信
Ethernet.begin(mac, ip);
server.begin();
pinMode(Light,OUTPUT);
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// 监听连入的客户端
client = server.available();
if (client) {
Serial.println("new client");
boolean currentLineIsBlank = false;
while (client.connected()) {
if (client.available()) {
char c = client.read();
readString += c;
if (c == '\n') {
Serial.println("1111111111:");
Serial.println(readString);
//检查收到的信息中是否有”on”,有则开灯
Serial.println("check On");
Serial.println(readString.indexOf("?on"));
if(readString.indexOf("?on") >0) {
digitalWrite(Light, HIGH);
Serial.println("Led On");
break;
}
//检查收到的信息中是否有”off”,有则关灯
Serial.println("check Off");
Serial.println(readString.indexOf("?off"));
if(readString.indexOf("?off") >0) {
digitalWrite(Light, LOW);
Serial.println("Led Off");
break;
}
//检查收到的信息中是否有”getBrightness”,有则读取光敏模拟值,并返回给浏览器
Serial.println("check Get");
Serial.println(readString.indexOf("?getBrightness"));
if(readString.indexOf("?getBrightness") >0) {
Serial.println("33333333333");
client.println(analogRead(A0));
break;
}
//发送HTML文本
SendHTML();
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
}
else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
Serial.println("2222222222:");
Serial.println(readString);
delay(1);
client.stop();
Serial.println("client disonnected");
readString=""; 
}
}
// 用于输出HTML文本的函数
void SendHTML()
{
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html><head><meta charset=\"UTF-8\"><title>OpenJumper!Arduino Web Server</title><script type=\"text/javascript\">");
client.println("function send2arduino(){var xmlhttp;if (window.XMLHttpRequest)xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");element=document.getElementById(\"light\");if (element.innerHTML.match(\"Turn on\")){element.innerHTML=\"Turn off\"; xmlhttp.open(\"GET\",\"/?on\",true);}else{ element.innerHTML=\"Turn on\";xmlhttp.open(\"GET\",\"/?off\",true); }xmlhttp.send();}");
client.println("function getBrightness(){var xmlhttp;if (window.XMLHttpRequest)xmlhttp=new XMLHttpRequest();else xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");xmlhttp.onreadystatechange=function(){if (xmlhttp.readyState==4 && xmlhttp.status==200)document.getElementById(\"brightness\").innerHTML=xmlhttp.responseText;};xmlhttp.open(\"GET\",\"/?getBrightness\",true); xmlhttp.send();}window.setInterval(getBrightness,1000);</script>");
client.println("</head><body><div align=\"center\"><h1>Arduino Web Server</h1><div>brightness:</div><div id=\"brightness\">??</div><button id=\"light\" type=\"button\" onclick=\"send2arduino()\">Turn on</button><button type=\"button\" onclick=\"alert('OpenJumper Web Server')\">About</button></div></body></html>");  
}

在SD卡中读取并输出:

留下一个答复

Please enter your comment!
Please enter your name here