学习《Arduino程序设计基础》项目:网页控制Arduino
实验板子用的是OpenJumper进阶套件的Zduino Ethernet 。设想能够控制 8 路LED的开关,在测试过程中单路输出都没问题,2路、3路、今天甚至做到4路输出(IO 2~5),但都不稳定,有时候可以更多时候不行。另外如果网页设置 8 路开关(16个button),输入ip后:服务器无响应!附上代码如下:
#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 Light2=2;/* 2-9, Total 8 line OUT */
int Light3=3;
int Light4=4;
int Light5=5;
int Light6=6;
int Light7=7;
int Light8=8;
int Light9=9;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
pinMode(Light2,OUTPUT);
pinMode(Light3,OUTPUT);
pinMode(Light4,OUTPUT);
pinMode(Light5,OUTPUT);
pinMode(Light6,OUTPUT);
pinMode(Light7,OUTPUT);
pinMode(Light8,OUTPUT);
pinMode(Light9,OUTPUT);
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
client = server.available();// 监听连入的客户端
if (client)
{
Serial.println("new client");// 一次http请求结束都带有一个空行
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
//将收到的信息都保存在readString函数中
readString += c;
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank)
{
Serial.print(readString);
// 发送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;
}
}
}
// 等待浏览器接收数据
delay(1);
//关闭连接
client.stop();
Serial.println("client disonnected");
/*====================== On ==========================*/
// 检查收到的信息中是否有”/2?on”,有则开灯
if(readString.indexOf("/2?on") >0)//checks for on
{
digitalWrite(Light2, HIGH);
Serial.println("Led 2 On");
}
// 检查收到的信息中是否有”/3?on”,有则开灯
if(readString.indexOf("/3?on") >0)//checks for on
{
digitalWrite(Light3, HIGH);
Serial.println("Led 3 On");
}
// 检查收到的信息中是否有”/4?on”,有则开灯
if(readString.indexOf("/4?on") >0)//checks for on
{
digitalWrite(Light4, HIGH);
Serial.println("Led 4 On");
}
// 检查收到的信息中是否有”/5?on”,有则开灯
if(readString.indexOf("/5?on") >0)//checks for on
{
digitalWrite(Light5, HIGH);
Serial.println("Led 5 On");
}
// 检查收到的信息中是否有”/6?on”,有则开灯
if(readString.indexOf("/6?on") >0)//checks for on
{
digitalWrite(Light6, HIGH);
Serial.println("Led 6 On");
}
// 检查收到的信息中是否有”/7?on”,有则开灯
if(readString.indexOf("/7?on") >0)//checks for on
{
digitalWrite(Light7, HIGH);
Serial.println("Led 7 On");
}
// 检查收到的信息中是否有”/8?on”,有则开灯
if(readString.indexOf("/8?on") >0)//checks for on
{
digitalWrite(Light8, HIGH);
Serial.println("Led 8 On");
}
// 检查收到的信息中是否有”/9?on”,有则开灯
if(readString.indexOf("/9?on") >0)//checks for on
{
digitalWrite(Light9, HIGH);
Serial.println("Led 9 On");
}
/*====================== Off ==========================*/
// 检查收到的信息中是否有”/2?off”,有则关灯
if(readString.indexOf("/2?off") >0)//checks for off
{
digitalWrite(Light2, LOW); // set pin 2 low
Serial.println("Led 2 Off");
}
// 检查收到的信息中是否有”/3?off”,有则关灯
if(readString.indexOf("/3?off") >0)//checks for off
{
digitalWrite(Light3, LOW); // set pin 3 low
Serial.println("Led 3 Off");
}
// 检查收到的信息中是否有”/4?off”,有则关灯
if(readString.indexOf("/4?off") >0)//checks for off
{
digitalWrite(Light4, LOW); // set pin 4 low
Serial.println("Led 4 Off");
}
// 检查收到的信息中是否有”/5?off”,有则关灯
if(readString.indexOf("/5?off") >0)//checks for off
{
digitalWrite(Light5, LOW); // set pin 5 low
Serial.println("Led 5 Off");
}
// 检查收到的信息中是否有”/6?off”,有则关灯
if(readString.indexOf("/6?off") >0)//checks for off
{
digitalWrite(Light6, LOW); // set pin 6 low
Serial.println("Led 6 Off");
}
// 检查收到的信息中是否有”/7?off”,有则关灯
if(readString.indexOf("/7?off") >0)//checks for off
{
digitalWrite(Light7, LOW); // set pin 7 low
Serial.println("Led 7 Off");
}
// 检查收到的信息中是否有”/8?off”,有则关灯
if(readString.indexOf("/8?off") >0)//checks for off
{
digitalWrite(Light8, LOW); // set pin 8 low
Serial.println("Led 8 Off");
}
// 检查收到的信息中是否有”/9?off”,有则关灯
if(readString.indexOf("/9?off") >0)//checks for off
{
digitalWrite(Light9, LOW); // set pin 9 low
Serial.println("Led 9 Off");
}
readString="";
}
}
// 用于输出HTML文本的函数
void SendHTML()
{
// 发送标准的HTTP响应
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>");
client.println("<head><title>TEST !</title></head>");
// 每十秒刷新一次
client.println("<meta http-equiv=\"refresh\" content=\"10\">");
client.println("<body>");
client.println("<div style=\"font-size: 30px;\">");
client.println("Light intensity: "); //显示室内光照度
client.println(analogRead(A0)); // 即将A0采集到的模拟值输出
client.println("<br />");
// 2 on按钮
client.println("<a href=\"/2?on\" target=\"inlineframe\"><button>2 on</button></a>");
// 2 off按钮
client.println("<a href=\"/2?off\" target=\"inlineframe\"><button>2 off</button></a>");
client.println(" ");
// 3 on按钮
client.println("<a href=\"/3?on\" target=\"inlineframe\"><button>3 on</button></a>");
// 3 off按钮
client.println("<a href=\"/3?off\" target=\"inlineframe\"><button>3 off</button></a>");
client.println(" ");
// 4 on按钮
client.println("<a href=\"/4?on\" target=\"inlineframe\"><button>4 on</button></a>");
// 4 off按钮
client.println("<a href=\"/4?off\" target=\"inlineframe\"><button>4 off</button></a>");
client.println(" ");
// 5 on按钮
client.println("<a href=\"/5?on\" target=\"inlineframe\"><button>5 on</button></a>");
// 5 off按钮
client.println("<a href=\"/5?off\" target=\"inlineframe\"><button>5 off</button></a>");
client.println(" ");
// 6 on按钮
client.println("<a href=\"/6?on\" target=\"inlineframe\"><button>6 on</button></a>");
// 6 off按钮
client.println("<a href=\"/6?off\" target=\"inlineframe\"><button>6 off</button></a>");
client.println(" ");
// 7 on按钮
client.println("<a href=\"/7?on\" target=\"inlineframe\"><button>7 on</button></a>");
// 7 off按钮
client.println("<a href=\"/7?off\" target=\"inlineframe\"><button>7 off</button></a>");
client.println(" ");
// 8 on按钮
client.println("<a href=\"/8?on\" target=\"inlineframe\"><button>8 on</button></a>");
// 8 off按钮
client.println("<a href=\"/8?off\" target=\"inlineframe\"><button>8 off</button></a>");
client.println(" ");
// 9 on按钮
client.println("<a href=\"/9?on\" target=\"inlineframe\"><button>9 on</button></a>");
// 9 off按钮
client.println("<a href=\"/9?off\" target=\"inlineframe\"><button>9 off</button></a>");
client.println(" ");
client.println("<IFRAME name=inlineframe style=\"display:none\" >");
client.println("</IFRAME>");
client.println("<br /> ");
client.println("</body>");
client.println("</html>");
} :Q为啥为啥为啥呀..... 本帖最后由 nbsp 于 2014-6-9 21:08 编辑
修改内容:
if( readString.charAt(6)=='T' )
digitalWrite((readString.charAt(5)-'0'), HIGH);
if( readString.charAt(6)=='F' )
digitalWrite((readString.charAt(5)-'0'), LOW);
带回开关按钮原来是“/2?on”,现在改成“/2T”,T-On、F-Off
我靠~~~~~~~~~~~~~~~~~~~~~~~~~~~就改了上面的if语句,现在测试8路完全无压力啊
附上代码:(明天去掉不必要的串口通讯和A0输入,再测测)
#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;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
for( int i=2; i<=9; i++ )
pinMode(i,OUTPUT);
Serial.print("Server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
int switchButton;
client = server.available();// 监听连入的客户端
if (client)
{
Serial.println("new client"); // 一次http请求结束都带有一个空行
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{
char c = client.read();
readString += c; //将收到的信息都保存在readString函数中
if (c == '\n' && currentLineIsBlank)
{
Serial.println(readString);
SendHTML(); // 发送HTML文本
break;
}
if (c == '\n') // 检测是否有换行符
currentLineIsBlank = true;
else if (c != '\r') // 检测是否有回车符
currentLineIsBlank = false;
}
}
delay(1); // 等待浏览器接收数据
client.stop(); //关闭连接
Serial.println("client disonnected");
if( readString.charAt(6)=='T' )
digitalWrite((readString.charAt(5)-'0'), HIGH);
if( readString.charAt(6)=='F' )
digitalWrite((readString.charAt(5)-'0'), LOW);
readString="";
}
}
// 用于输出HTML文本的函数
void SendHTML()
{
// 发送标准的HTTP响应
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>");
client.println("<head><title>TEST !</title></head>");
// 每十秒刷新一次
client.println("<meta http-equiv=\"refresh\" content=\"10\">");
client.println("<body>");
client.println("<div style=\"font-size: 30px;\">");
client.println("Light intensity: "); //显示室内光照度
client.println(analogRead(A0)); // 即将A0采集到的模拟值输出
client.println("<br />");
// 2 on按钮
client.println("<a href=\"/2T\" target=\"inlineframe\"><button>2 on</button></a>");
// 2 off按钮
client.println("<a href=\"/2F\" target=\"inlineframe\"><button>2 off</button></a>");
// 3 on按钮
client.println("<a href=\"/3T\" target=\"inlineframe\"><button>3 on</button></a>");
// 3 off按钮
client.println("<a href=\"/3F\" target=\"inlineframe\"><button>3 off</button></a>");
// 4 on按钮
client.println("<a href=\"/4T\" target=\"inlineframe\"><button>4 on</button></a>");
// 4 off按钮
client.println("<a href=\"/4F\" target=\"inlineframe\"><button>4 off</button></a>");
// 5 on按钮
client.println("<a href=\"/5T\" target=\"inlineframe\"><button>5 on</button></a>");
// 5 off按钮
client.println("<a href=\"/5F\" target=\"inlineframe\"><button>5 off</button></a>");
// 6 on按钮
client.println("<a href=\"/6T\" target=\"inlineframe\"><button>6 on</button></a>");
// 6 off按钮
client.println("<a href=\"/6F\" target=\"inlineframe\"><button>6 off</button></a>");
// 7 on按钮
client.println("<a href=\"/7T\" target=\"inlineframe\"><button>7 on</button></a>");
// 7 off按钮
client.println("<a href=\"/7F\" target=\"inlineframe\"><button>7 off</button></a>");
// 8 on按钮
client.println("<a href=\"/8T\" target=\"inlineframe\"><button>8 on</button></a>");
// 8 off按钮
client.println("<a href=\"/8F\" target=\"inlineframe\"><button>8 off</button></a>");
// 9 on按钮
client.println("<a href=\"/9T\" target=\"inlineframe\"><button>9 on</button></a>");
// 9 off按钮
client.println("<a href=\"/9F\" target=\"inlineframe\"><button>9 off</button></a>");
client.println("<IFRAME name=inlineframe style=\"display:none\" >");
client.println("</IFRAME>");
client.println("<br /> ");
client.println("</body>");
client.println("</html>");
} 目前估计是String实体的indexOf( )方法对于处理器工作压力大,一般来说if并列多条应该影响不大,有时间我会再去测测if并列情况(比如同时支持几条?) 已解决,地址:http://www.arduino.cn/thread-5984-1-1.html
页:
[1]