diff --git a/sdk/Arduino/WumeiArduino/Apconfig.cpp b/sdk/Arduino/WumeiArduino/Apconfig.cpp index f1193e38..85104871 100644 --- a/sdk/Arduino/WumeiArduino/Apconfig.cpp +++ b/sdk/Arduino/WumeiArduino/Apconfig.cpp @@ -8,18 +8,14 @@ #include "ApConfig.h" -String randomName="wumei-device"+(String)random(1000); -const char *ap_ssid =randomName.c_str(); -const char *ap_password = ""; //开放式网络 - -char sta_ssid[32] = {0}; -char sta_password[64] = {0}; -char sta_user_id[32] = {0}; +String randomName = "wumei-device" + (String)random(1000); +const char *ap_ssid = randomName.c_str(); +//开放式网络,不设置密码 +const char *ap_password = ""; IPAddress local_IP(192, 168, 4, 1); IPAddress gateway(192, 168, 4, 1); IPAddress subnet(255, 255, 255, 0); - ESP8266WebServer server(80); /** @@ -27,10 +23,11 @@ ESP8266WebServer server(80); */ void startApConfig() { + ledStatus(true); WiFi.mode(WIFI_AP_STA); WiFi.softAPConfig(local_IP, gateway, subnet); WiFi.softAP(ap_ssid, ap_password); - printMsg("已启动AP配网,IP地址:" + WiFi.softAPIP().toString()+", 热点名称:"+(String)ap_ssid); + printMsg("已启动AP配网,IP地址:" + WiFi.softAPIP().toString() + ", 热点名称:" + (String)ap_ssid); // 启动web服务 startWebServer(); } @@ -40,7 +37,7 @@ void startApConfig() */ void startWebServer() { - isApMode=true; + isApMode = true; server.on("/status", HTTP_GET, handleStatus); server.on("/config", HTTP_POST, handleConfig); server.onNotFound(handleNotFound); @@ -52,25 +49,35 @@ void startWebServer() /** * 检测设备接口 */ - void handleStatus(){ - server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪"); - } - +void handleStatus() +{ + server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪"); +} + /** * AP配网接口 */ void handleConfig() { printMsg("进入配网......"); + config_type config; // wifi名称、wifi密码、用户编号 if (server.hasArg("SSID") && server.hasArg("password") && server.hasArg("userId")) { - strcpy(sta_ssid, server.arg("SSID").c_str()); - strcpy(sta_password, server.arg("password").c_str()); - strcpy(sta_user_id, server.arg("userId").c_str()); - printMsg("收到WIFI名称:" + (String)sta_ssid); - printMsg("收到WIFI密码:" + (String)sta_password); - printMsg("收到用户编号:" + (String)sta_user_id); + // 分配空间 + wifiSsid=(char *)malloc(32*sizeof(char)); + wifiPwd=(char *)malloc(64*sizeof(char)); + userId=(char *)malloc(16*sizeof(char)); + strcpy(config.stassid, server.arg("SSID").c_str()); + strcpy(wifiSsid, server.arg("SSID").c_str()); + strcpy(config.stapsw, server.arg("password").c_str()); + strcpy(wifiPwd, server.arg("password").c_str()); + strcpy(config.userId, server.arg("userId").c_str()); + strcpy(userId, server.arg("userId").c_str()); + + printMsg("收到WIFI名称:" + (String)config.stassid); + printMsg("收到WIFI密码:" + (String)config.stapsw); + printMsg("收到用户编号:" + (String)config.userId); } else { @@ -81,22 +88,28 @@ void handleConfig() // 可选字段 if (server.hasArg("deviceNum")) { + deviceNum=(char *)malloc(32*sizeof(char)); + strcpy(config.deviceNum, server.arg("deviceNum").c_str()); + strcpy(deviceNum, server.arg("deviceNum").c_str()); printMsg("收到设备编号:" + server.arg("deviceNum")); } if (server.hasArg("authCode")) { + authCode=(char *)malloc(32*sizeof(char)); + strcpy(config.authCode, server.arg("authCode").c_str()); + strcpy(authCode, server.arg("authCode").c_str()); printMsg("收到产品授权码:" + server.arg("authCode")); } if (server.hasArg("extra")) { printMsg("收到补充信息:" + server.arg("extra")); - } + } server.send(200, "text/plain;charset=utf-8", "设备已更新WIFI配置,开始连接WIFI..."); - // 连接Wifi,关闭web服务 - isApMode=false; + // 存储配置 + saveConfig(config); + // 连接Wifi connectWifi(); - server.close(); } /** @@ -118,4 +131,3 @@ void handleNotFound() server.send(404, "text/plain;charset=utf-8", "请求的地址找不到或无法访问"); } } - diff --git a/sdk/Arduino/WumeiArduino/Auth.cpp b/sdk/Arduino/WumeiArduino/Auth.cpp index 9aeed9c1..8a8c6117 100644 --- a/sdk/Arduino/WumeiArduino/Auth.cpp +++ b/sdk/Arduino/WumeiArduino/Auth.cpp @@ -24,7 +24,7 @@ void connectMqtt() // 生成mqtt加密密码 String aesPassword = generationAESPwd(); // 连接 设备mqtt客户端Id格式为:认证类型(E=加密、S=简单) & 设备编号 & 产品ID & 用户ID - String clientId = "E&" + deviceNum + "&" + productId + "&" + userId; + String clientId = "E&" + (String)deviceNum + "&" + (String)productId + "&" + (String)userId; printMsg("客户端ID:"+clientId); bool connectResult = mqttClient.connect(clientId.c_str(), mqttUserName, aesPassword.c_str()); if (connectResult) diff --git a/sdk/Arduino/WumeiArduino/Common.cpp b/sdk/Arduino/WumeiArduino/Common.cpp index a42db71c..5c82a025 100644 --- a/sdk/Arduino/WumeiArduino/Common.cpp +++ b/sdk/Arduino/WumeiArduino/Common.cpp @@ -7,28 +7,18 @@ ********************************************************************/ #include "Common.h" +#define BUTTON 14 // 按键引脚 +#define LED 15 // LED灯引脚 WiFiClient wifiClient; PubSubClient mqttClient; - -// 存储的配置类型 -struct config_type -{ - char stassid[32]; // SSID配置项 - char stapsw[64]; // Password配置项 - char deviceNum[64]; // 设备编号配置项 - char userId[32]; // 用户ID配置项 - char authCode[32]; // 授权码配置项 -}; -config_type config; - OneButton button; // 按钮单击事件 static void buttonClick(); // 按钮双击事件 -static void buttonDoubleClick() ; +static void buttonDoubleClick(); // 按钮长按事件 -static void buttonLongPress() ; +static void buttonLongPress(); float rssi = 0; char wumei_iv[17] = "wumei-smart-open"; @@ -36,19 +26,17 @@ int monitorCount = 0; long monitorInterval = 1000; bool isApMode = false; -//==================================== begin 可配置的项 =============================== - -// Wifi配置 +/********************************** begin 可配置的项 **********************************/ +// wifi信息 char *wifiSsid = ""; char *wifiPwd = ""; - -String userId = "1"; +char *userId = "1"; // 产品启用授权码,则授权码不能为空 -String authCode = ""; +char *authCode = ""; // 设备信息配置 -String deviceNum = "D6329VL5668888"; -String productId = "41"; +char *deviceNum = "D6329VL5668888"; +char *productId = "41"; float firmwareVersion = 1.0; // 经度和纬度可选,如果产品使用设备定位,则必须传 float latitude = 0; @@ -64,10 +52,10 @@ char mqttSecret[17] = "K2V5DE28XNUU3497"; // NTP地址(用于获取时间,修改为自己部署项目的接口地址) String ntpServer = "http://wumei.live:8080/iot/tool/ntp?deviceSendTime="; -//==================================== end 可配置的项 =============================== +/********************************** end 可配置的项 **********************************/ -// 订阅的主题 -String prefix = "/" + productId + "/" + deviceNum; +// Mqtt订阅的主题 +String prefix = "/" + (String)productId + "/" + (String)deviceNum; String sInfoTopic = prefix + "/info/get"; String sOtaTopic = prefix + "/ota/get"; String sNtpTopic = prefix + "/ntp/get"; @@ -76,7 +64,7 @@ String sFunctionTopic = prefix + "/function/get"; String sPropertyOnline = prefix + "/property-online/get"; String sFunctionOnline = prefix + "/function-online/get"; String sMonitorTopic = prefix + "/monitor/get"; -// 发布的主题 +// Mqtt发布的主题 String pInfoTopic = prefix + "/info/post"; String pNtpTopic = prefix + "/ntp/post"; String pPropertyTopic = prefix + "/property/post"; @@ -91,57 +79,64 @@ void initWumeiSmart() Serial.begin(115200); printMsg("wumei smart device starting..."); - // 初始化按键为GND,并添加单击、双击、长按事件 - button = OneButton(14, true, true); + // 初始化按键为低电平,并添加单击、双击、长按事件 + button = OneButton(BUTTON, true, true); button.attachClick(buttonClick); button.attachDoubleClick(buttonDoubleClick); button.attachLongPressStart(buttonLongPress); - saveConfig(); + // 加载配置 loadConfig(); } // 按钮单击事件 -static void buttonClick() { - Serial.println("Clicked!"); +static void buttonClick() +{ + printMsg("检测到按键单击"); + ledStatus(true); } // 按钮双击事件 -static void buttonDoubleClick() { - Serial.println("double Clicked!"); +static void buttonDoubleClick() +{ + printMsg("检测到按键双击"); + ledStatus(false); } -// 按钮长按事件 -static void buttonLongPress() { - Serial.println("long Clicked!"); +// 按钮长按事件,进入配网模式 +static void buttonLongPress() +{ + if (isApMode) + { + printMsg("设备重启..."); + ESP.restart(); + } + else + { + printMsg("开始AP配网"); + startApConfig(); + } } // 连接wifi void connectWifi() { - printMsg("连接 "); + isApMode = false; + printMsg("连接Wifi... "); Serial.print(wifiSsid); WiFi.mode(WIFI_STA); WiFi.begin(wifiSsid, wifiPwd); - while (WiFi.status() != WL_CONNECTED) - { - delay(500); - Serial.print("."); - } - printMsg("WiFi连接成功"); - printMsg("IP地址: "); - Serial.print(WiFi.localIP()); + // 关闭AP配网模式 + server.stop(); + ledStatus(false); } // 存储配置 -void saveConfig() +void saveConfig(config_type config) { + // 标识为已经存储数据 + config.flag = 1; EEPROM.begin(240); - strcpy(config.stassid, "tp-six"); //名称复制 - strcpy(config.stapsw, "clh15108665817"); //密码复制 - strcpy(config.userId, "1000"); //密码复制 - strcpy(config.authCode, "8kjfsfjjkjfjljdfldjfsdlfjsdlfjl8"); //密码复制 - strcpy(config.deviceNum, "6qqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq6"); //密码复制 printMsg("存储配置..."); uint8_t *p = (uint8_t *)(&config); for (int i = 0; i < sizeof(config); i++) @@ -154,6 +149,7 @@ void saveConfig() // 加载配置 void loadConfig() { + config_type config; EEPROM.begin(240); printMsg("加载配置..."); uint8_t *p = (uint8_t *)(&config); @@ -161,14 +157,36 @@ void loadConfig() { *(p + i) = EEPROM.read(i); } - EEPROM.commit(); - wifiSsid = config.stassid; - wifiPwd = config.stapsw; - printMsg("SSID: " + (String)config.stassid); - printMsg("SSID: " + (String)config.authCode); - printMsg("SSID: " + (String)config.deviceNum); - printMsg("SSID: " + (String)config.userId); - printMsg("Password: " + (String)wifiPwd); + if (config.flag != 1) + { + printMsg("flash暂无数据"); + return; + } + // wifi名称 + if (strlen(config.stassid) != 0) + { + strcpy(wifiSsid, config.stassid); + } + // wifi密码 + if (strlen(config.stapsw) != 0) + { + strcpy(wifiPwd, config.stapsw); + } + // 设备编号 + if (strlen(config.deviceNum) != 0) + { + strcpy(deviceNum, config.deviceNum); + } + // 用户编号 + if (strlen(config.userId) != 0) + { + strcpy(userId, config.userId); + } + // 授权码 + if (strlen(config.authCode) != 0) + { + strcpy(authCode, config.authCode); + } } // 清空配置 @@ -233,13 +251,26 @@ void printMsg(String msg) void blink() { printMsg("指示灯闪烁..."); - int led = 15; - pinMode(led, OUTPUT); + pinMode(LED, OUTPUT); for (int i = 0; i < 2; i++) { - digitalWrite(led, HIGH); + digitalWrite(LED, HIGH); delay(100); - digitalWrite(led, LOW); + digitalWrite(LED, LOW); delay(100); } } + +// 控制指示灯状态 +void ledStatus(bool status) +{ + pinMode(LED, OUTPUT); + if (status) + { + digitalWrite(LED, HIGH); + } + else + { + digitalWrite(LED, LOW); + } +} diff --git a/sdk/Arduino/WumeiArduino/Common.h b/sdk/Arduino/WumeiArduino/Common.h index 7693c368..60886ffc 100644 --- a/sdk/Arduino/WumeiArduino/Common.h +++ b/sdk/Arduino/WumeiArduino/Common.h @@ -9,6 +9,7 @@ #ifndef _COMMON_H #define _COMMON_H +#include "Apconfig.h" #include "Base64.h" #include #include @@ -16,13 +17,24 @@ #include // 版本6.19.1 #include // 版本2.0.4 +// 存储的配置类型结构 +struct config_type +{ + char flag; // 是否有数据标识,等于1表示有数据 + char stassid[32]; // SSID配置项 + char stapsw[64]; // Password配置项 + char deviceNum[32]; // 设备编号配置项 + char userId[16]; // 用户ID配置项 + char authCode[32]; // 授权码配置项 +}; + extern WiFiClient wifiClient; extern PubSubClient mqttClient; extern OneButton button; -extern String deviceNum ; // 设备编号(重要,同时是Mqtt的clientId) -extern String userId; // 用户ID -extern String productId; // 产品ID +extern char *deviceNum ; // 设备编号(重要,同时是Mqtt的clientId) +extern char *userId; // 用户ID +extern char *productId; // 产品ID extern float rssi; // 信号强度(信号极好4格[-55— 0],信号好3格[-70— -55],信号一般2格[-85— -70],信号差1格[-100— -85]) extern float firmwareVersion; // 固件版本 extern float latitude; // 设备精度 @@ -35,7 +47,7 @@ extern char *mqttUserName; // Mqtt消息服务器账号 extern char *mqttPwd; // Mqtt消息服务器密码 extern char mqttSecret[17]; // Mqtt秘钥,16位 extern char wumei_iv[17]; // AES加密偏移量,固定值16位 -extern String authCode; // 产品授权码,产品未启用时为空字符串 +extern char *authCode; // 产品授权码,产品未启用时为空字符串 extern String ntpServer; // NTP服务地址,用于获取当前时间 extern int monitorCount; // 发布监测数据的最大次数 extern long monitorInterval; // 发布监测数据的间隔,默认1000毫秒 @@ -66,7 +78,7 @@ void connectWifi(); // 加载配置 void loadConfig(); // 保存配置 -void saveConfig(); +void saveConfig(config_type config); // 清空配置 void clearConfig(); // 随机生成监测值 @@ -75,6 +87,8 @@ String randomPropertyData(); void printMsg(String tips); // 控制指示灯闪烁 void blink(); +// 控制指示灯状态 +void ledStatus(bool status); #endif diff --git a/sdk/Arduino/WumeiArduino/WumeiArduino.ino b/sdk/Arduino/WumeiArduino/WumeiArduino.ino index 348eee09..15481697 100644 --- a/sdk/Arduino/WumeiArduino/WumeiArduino.ino +++ b/sdk/Arduino/WumeiArduino/WumeiArduino.ino @@ -10,6 +10,7 @@ #include "Auth.h" #include "Apconfig.h" +long lastWifiConn; // 上次wifi连接时间 long lastMqttConn; // 上次mqtt连接时间 long lastPublishMonitor; // 上次发布监测数据时间 long lastPublishSimulateData; // 上次发布测试数据时间 @@ -19,6 +20,7 @@ long lastPublishSimulateData; // 上次发布测试数据时间 */ void setup() { + clearConfig(); // 初始化配置 initWumeiSmart(); @@ -32,7 +34,6 @@ void setup() connectMqtt(); } - } /** @@ -65,13 +66,18 @@ void loop() } /* - * Wifi掉线重连 + * Wifi掉线重连(非阻塞,间隔10s) */ void wifiReconnectionClient() { + long now = millis(); if (WiFi.status() != WL_CONNECTED) { - connectWifi(); + if (now - lastWifiConn > 10000) + { + lastWifiConn = now; + WiFi.reconnect(); + } } }