-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.cpp
371 lines (340 loc) · 11.6 KB
/
main.cpp
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <CSE7766.h>
#include <CAT9554.h>
#include <Wire.h>
#include <EEPROM.h> //导入Flash库文件
#define CAT9554_SDA_PIN 3
#define CAT9554_SCL_PIN 12
#define CAT9554_IRQ_PIN 4
#define CSE7766_RX_PIN 13
#define CSE7766_BAUDRATE 4800
#define LED_PIN 0 // 指示灯
#define LOGO_LED_PIN 14 // Logo指示灯
#define KEY_0_PIN 16 // 总开关
#define KEY_1_PIN 0 // 开关1
#define KEY_2_PIN 1 // 开关2
#define KEY_3_PIN 2 // 开关3
#define REL_0_PIN 7 // 总继电器
#define REL_1_PIN 6 // 继电器1
#define REL_2_PIN 5 // 继电器2
#define REL_3_PIN 4 // 继电器3
String ssid;
String psw;
CSE7766 myCSE7766;
const int httpPort = 80;
String deviceName = "斐讯DC1插排";
String version = "1.1";
ESP8266WebServer server(httpPort);
const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
// 开关的当前状态
bool logLedStatus = true;
bool wifiLedStatus = true;
// 插口
bool plugin4Status = true;
bool plugin5Status = true;
bool plugin6Status = true;
// 总开关
bool plugin7Status = true;
struct config_type
{
char stassid[32];//定义配网得到的WIFI名长度(最大32字节)
char stapsw[64];//定义配网得到的WIFI密码长度(最大64字节)
};
config_type config;//声明定义内容
void saveConfig()//保存函数
{
EEPROM.begin(1024);//向系统申请1024kb ROM
//开始写入
uint8_t *p = (uint8_t*)(&config);
for (int i = 0; i < sizeof(config); i++)
{
EEPROM.write(i, *(p + i)); //在闪存内模拟写入
}
EEPROM.commit();//执行写入ROM
}
void loadConfig()//读取函数
{
EEPROM.begin(1024);
uint8_t *p = (uint8_t*)(&config);
for (int i = 0; i < sizeof(config); i++)
{
*(p + i) = EEPROM.read(i);
}
EEPROM.commit();
ssid = config.stassid;
psw = config.stapsw;
}
void smartConfig()//配网函数
{
// 等待配网
WiFi.beginSmartConfig();
while(1)
{
digitalWrite(LED_PIN, LOW); //加个LED快闪,确认配网是否成功!成功就不闪了。
delay(200);
digitalWrite(LED_PIN, HIGH);
delay(200);
if (WiFi.smartConfigDone())
{
strcpy(config.stassid,WiFi.SSID().c_str());//名称复制
strcpy(config.stapsw,WiFi.psk().c_str());//密码复制
saveConfig();//调用保存函数
WiFi.setAutoConnect(true); // 设置自动连接
break;
}
}
}
// web服务器的根目录
void handleRoot() {
server.send(200, "text/html", "<html><head><title>DC1</title><meta http-equiv=\"content-type\" content=\"text/html;charset=utf-8\"><meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"></head><h1>斐讯DC1插排</h1><a href=\"/ota\">网页OTA</a><br><a href=\"/reboot\">重启</a></html>");
}
void handleReboot() {
ESP.restart();
}
// 操作LED开关状态的API
void handleSwitchStatusChange(){
String message = "{\"code\":0,\"message\":\"success\"}";
for (uint8_t i=0; i<server.args(); i++){
if (server.argName(i)=="on")
{
// 开启
if (server.arg(i)=="logLed")
{
digitalWrite(LOGO_LED_PIN, LOW);
logLedStatus = true;
}else if (server.arg(i)=="wifiLed")
{
digitalWrite(LED_PIN, LOW);
wifiLedStatus = true;
}else if (server.arg(i)=="plugin4")
{
CAT9554.digitalWrite(REL_3_PIN, HIGH);
plugin4Status = true;
}else if (server.arg(i)=="plugin5")
{
CAT9554.digitalWrite(REL_2_PIN, HIGH);
plugin5Status = true;
}else if (server.arg(i)=="plugin6")
{
CAT9554.digitalWrite(REL_1_PIN, HIGH);
plugin6Status = true;
}else if (server.arg(i)=="plugin7")
{
CAT9554.digitalWrite(REL_0_PIN, HIGH);
plugin7Status = true;
}
}else if (server.argName(i)=="off"){
// 关闭
if (server.arg(i)=="logLed")
{
digitalWrite(LOGO_LED_PIN, HIGH);
logLedStatus = false;
}else if (server.arg(i)=="wifiLed")
{
digitalWrite(LED_PIN, HIGH);
wifiLedStatus = false;
}else if (server.arg(i)=="plugin4")
{
CAT9554.digitalWrite(REL_3_PIN, LOW);
plugin4Status = false;
}else if (server.arg(i)=="plugin5")
{
CAT9554.digitalWrite(REL_2_PIN, LOW);
plugin5Status = false;
}else if (server.arg(i)=="plugin6")
{
CAT9554.digitalWrite(REL_1_PIN, LOW);
plugin6Status = false;
}else if (server.arg(i)=="plugin7")
{
CAT9554.digitalWrite(REL_0_PIN, LOW);
plugin7Status = false;
}
}
}
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);
}
// 电力芯片cse7766
void handleCSE7766(){
double value = 0;
for (uint8_t i=0; i<server.args(); i++){
if (server.argName(i)=="type")
{
if (server.arg(i)=="getVoltage"){
value = myCSE7766.getVoltage();
}else if (server.arg(i)=="getCurrent"){
value = myCSE7766.getCurrent();
}else if (server.arg(i)=="getActivePower"){
value = myCSE7766.getActivePower();
}else if (server.arg(i)=="getApparentPower"){
value = myCSE7766.getApparentPower();
}else if (server.arg(i)=="getReactivePower"){
value = myCSE7766.getReactivePower();
}else if (server.arg(i)=="getPowerFactor"){
value = myCSE7766.getPowerFactor();
}else if (server.arg(i)=="getEnergy"){
value = myCSE7766.getEnergy();
}
}
}
String message = "{\"code\":0,\"value\":"+String(value)+",\"message\":\"success\"}";
server.send(200, "application/json", message);
}
// 当前的LED开关状态API
void handleCurrentLEDStatus(){
String message;
message = "{\"logLed\":"+String(logLedStatus)+
",\"wifiLed\":"+String(wifiLedStatus)+
",\"plugin4\":"+String(plugin4Status)+
",\"plugin5\":"+String(plugin5Status)+
",\"plugin6\":"+String(plugin6Status)+
",\"plugin7\":"+String(plugin7Status)+
",\"Voltage\":"+String(myCSE7766.getVoltage())+
",\"Current\":"+String(myCSE7766.getCurrent())+
",\"ActivePower\":"+String(myCSE7766.getActivePower())+
",\"ApparentPower\":"+String(myCSE7766.getApparentPower())+
",\"ReactivePower\":"+String(myCSE7766.getReactivePower())+
",\"PowerFactor\":"+String(myCSE7766.getPowerFactor())+
",\"Energy\":"+String(myCSE7766.getEnergy())+
",\"code\":0,\"message\":\"success\"}";
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){
// 电力芯片
myCSE7766.setRX(CSE7766_RX_PIN);
myCSE7766.begin();
// IO扩展CAT9554
Wire.begin(CAT9554_SDA_PIN, CAT9554_SCL_PIN);
Wire.setClock(4800);
CAT9554.begin();
// CAT9554.begin(SDA_PIN, SCL_PIN);
// 按钮检测,总开关GPIO16,其他插孔:
CAT9554.pinMode(KEY_0_PIN, INPUT);
CAT9554.pinMode(KEY_1_PIN, INPUT);
CAT9554.pinMode(KEY_2_PIN, INPUT);
// 初始化全开
CAT9554.pinMode(REL_3_PIN, OUTPUT);
CAT9554.digitalWrite(REL_3_PIN, HIGH);
CAT9554.pinMode(REL_2_PIN, OUTPUT);
CAT9554.digitalWrite(REL_2_PIN, HIGH);
CAT9554.pinMode(REL_1_PIN, OUTPUT);
CAT9554.digitalWrite(REL_1_PIN, HIGH);
CAT9554.pinMode(REL_0_PIN, OUTPUT);
CAT9554.digitalWrite(REL_0_PIN, HIGH);
// 开关状态初始化为开
pinMode(LOGO_LED_PIN, OUTPUT);
digitalWrite(LOGO_LED_PIN, LOW);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, LOW);
// Serial.begin(115200);
WiFi.mode(WIFI_STA);
delay(500);
// 选取一种连接路由器的方式
// WiFi.begin(ssid, password);
ESP.wdtEnable(10000);//设定看门狗
loadConfig();//读取ROM是否包含密码
//判断ROM是否有密码
if(ssid!=0&&psw!=0){
WiFi.begin(ssid,psw);//如果有密码则自动连接
while(!WiFi.isConnected()){
if(digitalRead(KEY_0_PIN)== LOW){
smartConfig();//如果配网按钮被按下则停止当前连接开始配网
break;//跳出所有循环进入主程序
}
digitalWrite(LED_PIN, LOW); //加个LED慢闪,确认联网是否成功!成功就不闪了。
delay(1000);
digitalWrite(LED_PIN, HIGH);
delay(1000);
}
WiFi.setAutoConnect(true); // 设置自动连接
}else{
smartConfig();//如果ROM没有密码则自动进入配网模式
}
if (MDNS.begin("phicomm-dc1-"+String(ESP.getFlashChipId()))) {
// Serial.println("MDNS responder started");
}
MDNS.addService("iotdevice", "tcp", httpPort);
MDNS.addServiceTxt("iotdevice", "tcp", "name", deviceName);
MDNS.addServiceTxt("iotdevice", "tcp", "model", "com.iotserv.devices.phicomm_dc1");
MDNS.addServiceTxt("iotdevice", "tcp", "mac", WiFi.macAddress());
MDNS.addServiceTxt("iotdevice", "tcp", "id", ESP.getSketchMD5());
// MDNS.addServiceTxt("iotdevice", "tcp", "ui-support", "web,native");
// MDNS.addServiceTxt("iotdevice", "tcp", "ui-first", "native");
MDNS.addServiceTxt("iotdevice", "tcp", "author", "Farry");
MDNS.addServiceTxt("iotdevice", "tcp", "email", "newfarry@126.com");
MDNS.addServiceTxt("iotdevice", "tcp", "home-page", "https://github.com/iotdevice");
MDNS.addServiceTxt("iotdevice", "tcp", "firmware-respository", "https://github.com/iotdevice/phicomm_dc1");
MDNS.addServiceTxt("iotdevice", "tcp", "firmware-version", version);
server.on("/", handleRoot);
server.on("/reboot", handleReboot);
server.on("/switch", handleSwitchStatusChange);
server.on("/rename", handleDeviceRename);
server.on("/status", handleCurrentLEDStatus);
server.on("/cse7766", handleCSE7766);
server.on("/update", HTTP_POST, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/plain", (Update.hasError()) ? "{\"code\":1,\"message\":\"fail\"}" : "{\"code\":0,\"message\":\"success\"}");
ESP.restart();
}, []() {
HTTPUpload& upload = server.upload();
if (upload.status == UPLOAD_FILE_START) {
WiFiUDP::stopAll();
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
if (!Update.begin(maxSketchSpace)) { //start with max available size
}
} else if (upload.status == UPLOAD_FILE_WRITE) {
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
}
} else if (upload.status == UPLOAD_FILE_END) {
if (Update.end(true)) { //true to set the size to the current progress
} else {
}
}
yield();
});
server.on("/ota", HTTP_GET, []() {
server.sendHeader("Connection", "close");
server.send(200, "text/html", serverIndex);
});
server.onNotFound(handleNotFound);
server.begin();
// Serial.println("HTTP server started");
MDNS.addService("iotdevice", "tcp", httpPort);
}
void loop(void){
ESP.wdtFeed();//先喂狗释放资源
if(digitalRead(KEY_0_PIN)== 0)delay(2000);if(digitalRead(KEY_0_PIN)==0)smartConfig();//如果配网按钮被按下则停止所有任务开始重新配网
ESP.wdtFeed();//再喂狗释放资源
MDNS.update();
server.handleClient();
myCSE7766.handle();
}