sdk完善

This commit is contained in:
kerwincui
2022-08-03 15:47:44 +08:00
parent fc753c2898
commit 339e065031
5 changed files with 160 additions and 97 deletions

View File

@@ -8,18 +8,14 @@
#include "ApConfig.h" #include "ApConfig.h"
String randomName="wumei-device"+(String)random(1000); String randomName = "wumei-device" + (String)random(1000);
const char *ap_ssid =randomName.c_str(); const char *ap_ssid = randomName.c_str();
const char *ap_password = ""; //开放式网络 //开放式网络,不设置密码
const char *ap_password = "";
char sta_ssid[32] = {0};
char sta_password[64] = {0};
char sta_user_id[32] = {0};
IPAddress local_IP(192, 168, 4, 1); IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1); IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0); IPAddress subnet(255, 255, 255, 0);
ESP8266WebServer server(80); ESP8266WebServer server(80);
/** /**
@@ -27,10 +23,11 @@ ESP8266WebServer server(80);
*/ */
void startApConfig() void startApConfig()
{ {
ledStatus(true);
WiFi.mode(WIFI_AP_STA); WiFi.mode(WIFI_AP_STA);
WiFi.softAPConfig(local_IP, gateway, subnet); WiFi.softAPConfig(local_IP, gateway, subnet);
WiFi.softAP(ap_ssid, ap_password); 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服务 // 启动web服务
startWebServer(); startWebServer();
} }
@@ -40,7 +37,7 @@ void startApConfig()
*/ */
void startWebServer() void startWebServer()
{ {
isApMode=true; isApMode = true;
server.on("/status", HTTP_GET, handleStatus); server.on("/status", HTTP_GET, handleStatus);
server.on("/config", HTTP_POST, handleConfig); server.on("/config", HTTP_POST, handleConfig);
server.onNotFound(handleNotFound); server.onNotFound(handleNotFound);
@@ -52,9 +49,10 @@ void startWebServer()
/** /**
* 检测设备接口 * 检测设备接口
*/ */
void handleStatus(){ void handleStatus()
{
server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪"); server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪");
} }
/** /**
* AP配网接口 * AP配网接口
@@ -62,15 +60,24 @@ void startWebServer()
void handleConfig() void handleConfig()
{ {
printMsg("进入配网......"); printMsg("进入配网......");
config_type config;
// wifi名称、wifi密码、用户编号 // wifi名称、wifi密码、用户编号
if (server.hasArg("SSID") && server.hasArg("password") && server.hasArg("userId")) 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()); wifiSsid=(char *)malloc(32*sizeof(char));
strcpy(sta_user_id, server.arg("userId").c_str()); wifiPwd=(char *)malloc(64*sizeof(char));
printMsg("收到WIFI名称" + (String)sta_ssid); userId=(char *)malloc(16*sizeof(char));
printMsg("收到WIFI密码" + (String)sta_password); strcpy(config.stassid, server.arg("SSID").c_str());
printMsg("收到用户编号:" + (String)sta_user_id); 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 else
{ {
@@ -81,10 +88,16 @@ void handleConfig()
// 可选字段 // 可选字段
if (server.hasArg("deviceNum")) 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")); printMsg("收到设备编号:" + server.arg("deviceNum"));
} }
if (server.hasArg("authCode")) 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")); printMsg("收到产品授权码:" + server.arg("authCode"));
} }
if (server.hasArg("extra")) if (server.hasArg("extra"))
@@ -93,10 +106,10 @@ void handleConfig()
} }
server.send(200, "text/plain;charset=utf-8", "设备已更新WIFI配置开始连接WIFI..."); server.send(200, "text/plain;charset=utf-8", "设备已更新WIFI配置开始连接WIFI...");
// 连接Wifi关闭web服务 // 存储配置
isApMode=false; saveConfig(config);
// 连接Wifi
connectWifi(); connectWifi();
server.close();
} }
/** /**
@@ -118,4 +131,3 @@ void handleNotFound()
server.send(404, "text/plain;charset=utf-8", "请求的地址找不到或无法访问"); server.send(404, "text/plain;charset=utf-8", "请求的地址找不到或无法访问");
} }
} }

View File

@@ -24,7 +24,7 @@ void connectMqtt()
// 生成mqtt加密密码 // 生成mqtt加密密码
String aesPassword = generationAESPwd(); String aesPassword = generationAESPwd();
// 连接 设备mqtt客户端Id格式为认证类型(E=加密、S=简单) & 设备编号 & 产品ID & 用户ID // 连接 设备mqtt客户端Id格式为认证类型(E=加密、S=简单) & 设备编号 & 产品ID & 用户ID
String clientId = "E&" + deviceNum + "&" + productId + "&" + userId; String clientId = "E&" + (String)deviceNum + "&" + (String)productId + "&" + (String)userId;
printMsg("客户端ID"+clientId); printMsg("客户端ID"+clientId);
bool connectResult = mqttClient.connect(clientId.c_str(), mqttUserName, aesPassword.c_str()); bool connectResult = mqttClient.connect(clientId.c_str(), mqttUserName, aesPassword.c_str());
if (connectResult) if (connectResult)

View File

@@ -7,28 +7,18 @@
********************************************************************/ ********************************************************************/
#include "Common.h" #include "Common.h"
#define BUTTON 14 // 按键引脚
#define LED 15 // LED灯引脚
WiFiClient wifiClient; WiFiClient wifiClient;
PubSubClient mqttClient; 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; OneButton button;
// 按钮单击事件 // 按钮单击事件
static void buttonClick(); static void buttonClick();
// 按钮双击事件 // 按钮双击事件
static void buttonDoubleClick() ; static void buttonDoubleClick();
// 按钮长按事件 // 按钮长按事件
static void buttonLongPress() ; static void buttonLongPress();
float rssi = 0; float rssi = 0;
char wumei_iv[17] = "wumei-smart-open"; char wumei_iv[17] = "wumei-smart-open";
@@ -36,19 +26,17 @@ int monitorCount = 0;
long monitorInterval = 1000; long monitorInterval = 1000;
bool isApMode = false; bool isApMode = false;
//==================================== begin 可配置的项 =============================== /********************************** begin 可配置的项 **********************************/
// wifi信息
// Wifi配置
char *wifiSsid = ""; char *wifiSsid = "";
char *wifiPwd = ""; char *wifiPwd = "";
char *userId = "1";
String userId = "1";
// 产品启用授权码,则授权码不能为空 // 产品启用授权码,则授权码不能为空
String authCode = ""; char *authCode = "";
// 设备信息配置 // 设备信息配置
String deviceNum = "D6329VL5668888"; char *deviceNum = "D6329VL5668888";
String productId = "41"; char *productId = "41";
float firmwareVersion = 1.0; float firmwareVersion = 1.0;
// 经度和纬度可选,如果产品使用设备定位,则必须传 // 经度和纬度可选,如果产品使用设备定位,则必须传
float latitude = 0; float latitude = 0;
@@ -64,10 +52,10 @@ char mqttSecret[17] = "K2V5DE28XNUU3497";
// NTP地址用于获取时间,修改为自己部署项目的接口地址) // NTP地址用于获取时间,修改为自己部署项目的接口地址)
String ntpServer = "http://wumei.live:8080/iot/tool/ntp?deviceSendTime="; String ntpServer = "http://wumei.live:8080/iot/tool/ntp?deviceSendTime=";
//==================================== end 可配置的项 =============================== /********************************** end 可配置的项 **********************************/
// 订阅的主题 // Mqtt订阅的主题
String prefix = "/" + productId + "/" + deviceNum; String prefix = "/" + (String)productId + "/" + (String)deviceNum;
String sInfoTopic = prefix + "/info/get"; String sInfoTopic = prefix + "/info/get";
String sOtaTopic = prefix + "/ota/get"; String sOtaTopic = prefix + "/ota/get";
String sNtpTopic = prefix + "/ntp/get"; String sNtpTopic = prefix + "/ntp/get";
@@ -76,7 +64,7 @@ String sFunctionTopic = prefix + "/function/get";
String sPropertyOnline = prefix + "/property-online/get"; String sPropertyOnline = prefix + "/property-online/get";
String sFunctionOnline = prefix + "/function-online/get"; String sFunctionOnline = prefix + "/function-online/get";
String sMonitorTopic = prefix + "/monitor/get"; String sMonitorTopic = prefix + "/monitor/get";
// 发布的主题 // Mqtt发布的主题
String pInfoTopic = prefix + "/info/post"; String pInfoTopic = prefix + "/info/post";
String pNtpTopic = prefix + "/ntp/post"; String pNtpTopic = prefix + "/ntp/post";
String pPropertyTopic = prefix + "/property/post"; String pPropertyTopic = prefix + "/property/post";
@@ -91,57 +79,64 @@ void initWumeiSmart()
Serial.begin(115200); Serial.begin(115200);
printMsg("wumei smart device starting..."); printMsg("wumei smart device starting...");
// 初始化按键为GND,并添加单击、双击、长按事件 // 初始化按键为低电平,并添加单击、双击、长按事件
button = OneButton(14, true, true); button = OneButton(BUTTON, true, true);
button.attachClick(buttonClick); button.attachClick(buttonClick);
button.attachDoubleClick(buttonDoubleClick); button.attachDoubleClick(buttonDoubleClick);
button.attachLongPressStart(buttonLongPress); button.attachLongPressStart(buttonLongPress);
saveConfig(); // 加载配置
loadConfig(); loadConfig();
} }
// 按钮单击事件 // 按钮单击事件
static void buttonClick() { static void buttonClick()
Serial.println("Clicked!"); {
printMsg("检测到按键单击");
ledStatus(true);
} }
// 按钮双击事件 // 按钮双击事件
static void buttonDoubleClick() { static void buttonDoubleClick()
Serial.println("double Clicked!"); {
printMsg("检测到按键双击");
ledStatus(false);
} }
// 按钮长按事件 // 按钮长按事件,进入配网模式
static void buttonLongPress() { static void buttonLongPress()
Serial.println("long Clicked!"); {
if (isApMode)
{
printMsg("设备重启...");
ESP.restart();
}
else
{
printMsg("开始AP配网");
startApConfig();
}
} }
// 连接wifi // 连接wifi
void connectWifi() void connectWifi()
{ {
printMsg("连接 "); isApMode = false;
printMsg("连接Wifi... ");
Serial.print(wifiSsid); Serial.print(wifiSsid);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(wifiSsid, wifiPwd); WiFi.begin(wifiSsid, wifiPwd);
while (WiFi.status() != WL_CONNECTED) // 关闭AP配网模式
{ server.stop();
delay(500); ledStatus(false);
Serial.print(".");
}
printMsg("WiFi连接成功");
printMsg("IP地址: ");
Serial.print(WiFi.localIP());
} }
// 存储配置 // 存储配置
void saveConfig() void saveConfig(config_type config)
{ {
// 标识为已经存储数据
config.flag = 1;
EEPROM.begin(240); 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("存储配置..."); printMsg("存储配置...");
uint8_t *p = (uint8_t *)(&config); uint8_t *p = (uint8_t *)(&config);
for (int i = 0; i < sizeof(config); i++) for (int i = 0; i < sizeof(config); i++)
@@ -154,6 +149,7 @@ void saveConfig()
// 加载配置 // 加载配置
void loadConfig() void loadConfig()
{ {
config_type config;
EEPROM.begin(240); EEPROM.begin(240);
printMsg("加载配置..."); printMsg("加载配置...");
uint8_t *p = (uint8_t *)(&config); uint8_t *p = (uint8_t *)(&config);
@@ -161,14 +157,36 @@ void loadConfig()
{ {
*(p + i) = EEPROM.read(i); *(p + i) = EEPROM.read(i);
} }
EEPROM.commit(); if (config.flag != 1)
wifiSsid = config.stassid; {
wifiPwd = config.stapsw; printMsg("flash暂无数据");
printMsg("SSID: " + (String)config.stassid); return;
printMsg("SSID: " + (String)config.authCode); }
printMsg("SSID: " + (String)config.deviceNum); // wifi名称
printMsg("SSID: " + (String)config.userId); if (strlen(config.stassid) != 0)
printMsg("Password: " + (String)wifiPwd); {
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() void blink()
{ {
printMsg("指示灯闪烁..."); printMsg("指示灯闪烁...");
int led = 15; pinMode(LED, OUTPUT);
pinMode(led, OUTPUT);
for (int i = 0; i < 2; i++) for (int i = 0; i < 2; i++)
{ {
digitalWrite(led, HIGH); digitalWrite(LED, HIGH);
delay(100); delay(100);
digitalWrite(led, LOW); digitalWrite(LED, LOW);
delay(100); delay(100);
} }
} }
// 控制指示灯状态
void ledStatus(bool status)
{
pinMode(LED, OUTPUT);
if (status)
{
digitalWrite(LED, HIGH);
}
else
{
digitalWrite(LED, LOW);
}
}

View File

@@ -9,6 +9,7 @@
#ifndef _COMMON_H #ifndef _COMMON_H
#define _COMMON_H #define _COMMON_H
#include "Apconfig.h"
#include "Base64.h" #include "Base64.h"
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <EEPROM.h> #include <EEPROM.h>
@@ -16,13 +17,24 @@
#include <ArduinoJson.h> // 版本6.19.1 #include <ArduinoJson.h> // 版本6.19.1
#include <OneButton.h> // 版本2.0.4 #include <OneButton.h> // 版本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 WiFiClient wifiClient;
extern PubSubClient mqttClient; extern PubSubClient mqttClient;
extern OneButton button; extern OneButton button;
extern String deviceNum ; // 设备编号重要同时是Mqtt的clientId extern char *deviceNum ; // 设备编号重要同时是Mqtt的clientId
extern String userId; // 用户ID extern char *userId; // 用户ID
extern String productId; // 产品ID extern char *productId; // 产品ID
extern float rssi; // 信号强度信号极好4格[-55— 0]信号好3格[-70— -55]信号一般2格[-85— -70]信号差1格[-100— -85] extern float rssi; // 信号强度信号极好4格[-55— 0]信号好3格[-70— -55]信号一般2格[-85— -70]信号差1格[-100— -85]
extern float firmwareVersion; // 固件版本 extern float firmwareVersion; // 固件版本
extern float latitude; // 设备精度 extern float latitude; // 设备精度
@@ -35,7 +47,7 @@ extern char *mqttUserName; // Mqtt消息服务器账号
extern char *mqttPwd; // Mqtt消息服务器密码 extern char *mqttPwd; // Mqtt消息服务器密码
extern char mqttSecret[17]; // Mqtt秘钥,16位 extern char mqttSecret[17]; // Mqtt秘钥,16位
extern char wumei_iv[17]; // AES加密偏移量固定值16位 extern char wumei_iv[17]; // AES加密偏移量固定值16位
extern String authCode; // 产品授权码,产品未启用时为空字符串 extern char *authCode; // 产品授权码,产品未启用时为空字符串
extern String ntpServer; // NTP服务地址用于获取当前时间 extern String ntpServer; // NTP服务地址用于获取当前时间
extern int monitorCount; // 发布监测数据的最大次数 extern int monitorCount; // 发布监测数据的最大次数
extern long monitorInterval; // 发布监测数据的间隔默认1000毫秒 extern long monitorInterval; // 发布监测数据的间隔默认1000毫秒
@@ -66,7 +78,7 @@ void connectWifi();
// 加载配置 // 加载配置
void loadConfig(); void loadConfig();
// 保存配置 // 保存配置
void saveConfig(); void saveConfig(config_type config);
// 清空配置 // 清空配置
void clearConfig(); void clearConfig();
// 随机生成监测值 // 随机生成监测值
@@ -75,6 +87,8 @@ String randomPropertyData();
void printMsg(String tips); void printMsg(String tips);
// 控制指示灯闪烁 // 控制指示灯闪烁
void blink(); void blink();
// 控制指示灯状态
void ledStatus(bool status);
#endif #endif

View File

@@ -10,6 +10,7 @@
#include "Auth.h" #include "Auth.h"
#include "Apconfig.h" #include "Apconfig.h"
long lastWifiConn; // 上次wifi连接时间
long lastMqttConn; // 上次mqtt连接时间 long lastMqttConn; // 上次mqtt连接时间
long lastPublishMonitor; // 上次发布监测数据时间 long lastPublishMonitor; // 上次发布监测数据时间
long lastPublishSimulateData; // 上次发布测试数据时间 long lastPublishSimulateData; // 上次发布测试数据时间
@@ -19,6 +20,7 @@ long lastPublishSimulateData; // 上次发布测试数据时间
*/ */
void setup() void setup()
{ {
clearConfig();
// 初始化配置 // 初始化配置
initWumeiSmart(); initWumeiSmart();
@@ -32,7 +34,6 @@ void setup()
connectMqtt(); connectMqtt();
} }
} }
/** /**
@@ -65,13 +66,18 @@ void loop()
} }
/* /*
* Wifi掉线重连 * Wifi掉线重连(非阻塞间隔10s)
*/ */
void wifiReconnectionClient() void wifiReconnectionClient()
{ {
long now = millis();
if (WiFi.status() != WL_CONNECTED) if (WiFi.status() != WL_CONNECTED)
{ {
connectWifi(); if (now - lastWifiConn > 10000)
{
lastWifiConn = now;
WiFi.reconnect();
}
} }
} }