-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathxiaoaizhimakaideng.ino
87 lines (87 loc) · 2.39 KB
/
xiaoaizhimakaideng.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
#include <ESP8266WiFi.h>
#define LED 0
// 以上烧写程序配网方式可以换成微信扫码配网,等等,具体操作可查看博客相关例子
WiFiServer server(80); // 服务器端口号
void smartConfig()
{
WiFi.mode(WIFI_STA);
Serial.println("\r\nWait for Smartconfig");
WiFi.beginSmartConfig();
while (1)
{
Serial.print(".");
digitalWrite(LED, 0);
delay(500);
digitalWrite(LED, 1);
delay(500);
if (WiFi.smartConfigDone())
{
Serial.println("SmartConfig Success");
Serial.printf("SSID:%s\r\n", WiFi.SSID().c_str());
Serial.printf("PSW:%s\r\n", WiFi.psk().c_str());
break;
}
}
}
void setup() {
Serial.begin(115200);
delay(10);
pinMode(2, OUTPUT);
digitalWrite(2, 1);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
smartConfig(); //微信智能配网
delay(500);
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started @ ");
// Print the IP address & instructions
Serial.println(WiFi.localIP());
//打印出IP地址,后期可以制作显示器来外部硬件显示ip
Serial.println("To control GPIO, open your web browser.");
Serial.println("To set GPIO 0 high, type:");
Serial.print(WiFi.localIP());
Serial.println("/gpio/1");
Serial.println("To set GPIO 0 low, type:");
Serial.print(WiFi.localIP());
Serial.println("/gpio/0");
Serial.println("To toggle GPIO 0, type:");
Serial.print(WiFi.localIP());
Serial.println("/gpio/4");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
delay(100);
return;
}
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();
// Match the request
int val;
// 下面开始判断ip进行动作指令
if (req.indexOf("/gpio/10") != -1)
digitalWrite(2, 1);
else if (req.indexOf("/gpio/11") != -1)
digitalWrite(2, 0);
else {
Serial.println("invalid request");
client.print("HTTP/1.1 404\r\n");
client.stop();
return;
}
client.flush();
// Prepare the response
String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nWelcome wenzheng space! ";
s += "</html>\n";
// Send the response to the client
client.print(s);
delay(1);
Serial.println("Client disonnected");
}