附上修改后的代码:
#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>");
} |