-
Notifications
You must be signed in to change notification settings - Fork 42
/
10.7-ArduinoWebServer.ino
136 lines (126 loc) · 4 KB
/
10.7-ArduinoWebServer.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
OpenJumper WebServer Example
显示室内照度+开关灯控制
通过手机、平板、计算机等设备访问
Arduino Server,就看到当前室内光线照度
在A0引脚连接光敏模块,用于采集室内光线;在2号引脚连接LED模块。
http://www.openjumper.com/
http://x.openjumper.com/ethernet/
*/
#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=2;
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");
// 一次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”,有则开灯
if(readString.indexOf("/?on") >0)//checks for on
{
digitalWrite(Light, HIGH);
Serial.println("Led On");
}
// 检查收到的信息中是否有”/?off”,有则关灯
if(readString.indexOf("/?off") >0)//checks for off
{
digitalWrite(Light, LOW); // set pin 4 low
Serial.println("Led 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>OpenJumper!</title></head>");
// 每十秒刷新一次
client.println("<meta http-equiv=\"refresh\" content=\"10\">");
client.println("<body>");
client.println("<div align=\"center\" style=\"border-style: dotted; font-size: 36px;\">");
client.println("<div align=\"center\">");
client.println("<strong>OpenJumper!<br />");
client.println("Arduino Web Server<br /></strong>");
client.println("</div><br />");
client.println("<div style=\"font-size: 30px;\">");
//显示室内光照度
client.println("Light intensity: ");
// 即将A0采集到的模拟值输出
client.println(analogRead(A0));
client.println("<br />");
// on按钮
client.println("<a href=\"/?on\" target=\"inlineframe\"><button>on</button></a>");
client.println(" ");
// off按钮
client.println("<a href=\"/?off\" target=\"inlineframe\"><button>off</button></a>");
client.println("<IFRAME name=inlineframe style=\"display:none\" >");
client.println("</IFRAME>");
client.println("<br /> ");
client.println("</div><br />");
client.println("<ahref=\"http://x.openjumper.com/ethernet/\">");
client.println("<imgsrc=\"http://www.openjumper.com/ad.jpg\"></a>");
client.println("</div><p>");
client.println("<a href=\"http://weibo.com/coloz/\">zhou</a>@");
client.println("<a href=\"http://www.openjumper.com/\">OpenJumper</a></p>");
client.println("</body>");
client.println("</html>");
}