-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.ino
148 lines (138 loc) · 4.14 KB
/
main.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
137
138
139
140
141
142
143
144
145
146
147
148
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
const int httpPort = 80;
String deviceName = "esp8266 Switch";
String version = "1.0";
ESP8266WebServer server(httpPort);
// 看你的继电器是连接那个io,默认gpio0,gpio2,请自行修改下面的Wifi账号和密码
const int led1 = 0;
const int led2 = 2;
// 如果是gpio0
// const int led = 0;
// 开关的状态表示
const int on = 1;
const int off = 0;
// 开关的当前状态
int led1status = off;
int led2status = off;
// digitalWrite(led1, on);
// web服务器的根目录
void handleRoot() {
server.send(200, "text/html", "<h1>this is index page from esp8266!</h1>");
}
// 操作LED开关状态的API
void handleLEDStatusChange(){
String message = "{\"code\":0,\"message\":\"success\"}";
for (uint8_t i=0; i<server.args(); i++){
if (server.argName(i)=="pin")
{
if (server.arg(i)=="ON1")
{
digitalWrite(led1, on);
led1status = on;
}else if (server.arg(i)=="OFF1")
{
digitalWrite(led1, off);
led1status = off;
}else if (server.arg(i)=="ON2")
{
digitalWrite(led2, on);
led2status = on;
}else if (server.arg(i)=="OFF2")
{
digitalWrite(led2, off);
led2status = off;
}
}
}
server.send(200, "application/json", message);
}
// 设备改名的API
void handleDeviceRename(){
String message = "{\"code\":0,\"message\":\"success\"}";
for (uint8_t i=0; i<server.args(); i++){
if (server.argName(i)=="name")
{
deviceName = server.arg(i);
}
}
server.send(200, "application/json", message);
}
// 当前的LED开关状态API
void handleCurrentLEDStatus(){
String message;
message = "{\"led1\":"+String(led1status)+",\"led2\":"+String(led2status)+",\"code\":0,\"message\":\"success\"}";
server.send(200, "application/json", message);
}
// 设备信息
void handleDeviceInfo(){
String message;
message = "{\n";
message += "\"name\":\""+deviceName +"\",\n";
message += "\"model\":\"com.iotserv.devices.esp8266-switch\",\n";
message += "\"ui-support\":[\"web\",\"native\"],\n";
message += "\"ui-first\":\"native\",\n";
message += "\"author\":\"Farry\",\n";
message += "\"email\":\"[email protected]\",\n";
message += "\"home-page\":\"https://github.com/iotdevice\",\n";
message += "\"firmware-respository\":\"https://github.com/iotdevice/esp8266-switch\",\n";
message += "\"firmware-version\":\""+version+"\"\n";
message +="}";
server.send(200, "application/json", message);
}
// 页面或者api没有找到
void handleNotFound(){
String message = "File Not Found\n\n";
message += "URI: ";
message += server.uri();
message += "\nMethod: ";
message += (server.method() == HTTP_GET)?"GET":"POST";
message += "\nArguments: ";
message += server.args();
message += "\n";
for (uint8_t i=0; i<server.args(); i++){
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
}
server.send(404, "text/plain", message);
}
void setup(void){
pinMode(led1, OUTPUT);
digitalWrite(led1, off);
pinMode(led2, OUTPUT);
digitalWrite(led2, off);
// Serial.begin(115200);
WiFi.mode(WIFI_STA);
// 选取一种连接路由器的方式 ,填写自建的wifi账号或者使用下面注释掉的smartconfig配网
WiFi.begin("ssid", "password");
// WiFi.beginSmartConfig();
// Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
}
// Serial.println("");
// Serial.print("Connected to ");
// Serial.println(ssid);
// Serial.print("IP address: ");
// Serial.println(WiFi.localIP());
if (MDNS.begin("esp8266")) {
// Serial.println("MDNS responder started");
}
server.on("/", handleLEDStatusChange);
server.on("/led", handleLEDStatusChange);
server.on("/rename", handleDeviceRename);
server.on("/status", handleCurrentLEDStatus);
// about this device
server.on("/info", handleDeviceInfo);
server.onNotFound(handleNotFound);
server.begin();
// Serial.println("HTTP server started");
MDNS.addService("iotdevice", "tcp", httpPort);
}
void loop(void){
MDNS.update();
server.handleClient();
}