新版固件改进

This commit is contained in:
kerwincui
2022-08-01 23:28:20 +08:00
parent 5171094988
commit fc753c2898
9 changed files with 413 additions and 271 deletions

View File

@@ -1,164 +1,121 @@
// /***********************************************************
// * function 设备配网
// * board: esp8266 core for arduino v3.0.2
// * library PubSubClient2.8.0 & ArduinoJson6.19.1
// * source: https://github.com/kerwincui/wumei-smart
// ***********************************************************/
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
// #include <ESP8266WiFi.h>
// #include <ESP8266WebServer.h>
#include "ApConfig.h"
// String randomName="wumei-device"+(String)random(1000);
// const char *ap_ssid =randomName.c_str();
// const char *ap_password = ""; //开放式网络
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};
char sta_ssid[32] = {0};
char sta_password[64] = {0};
char sta_user_id[32] = {0};
// IPAddress local_IP(192, 168, 4, 1);
// IPAddress gateway(192, 168, 4, 1);
// IPAddress subnet(255, 255, 255, 0);
IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);
// void initApConfig();
// void initWebServer();
// void handleConfig();
// void handleStatus();
// void handleNotFound();
ESP8266WebServer server(80);
// ESP8266WebServer server(80);
/**
* 启动AP配网
*/
void startApConfig()
{
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);
// 启动web服务
startWebServer();
}
// // void setup(void)
// // {
// // //打开串行端口:
// // Serial.begin(115200);
// // // AP模式
// // initApConfig();
// // // web服务
// // initWebServer();
// // }
/**
* 启动Web服务
*/
void startWebServer()
{
isApMode=true;
server.on("/status", HTTP_GET, handleStatus);
server.on("/config", HTTP_POST, handleConfig);
server.onNotFound(handleNotFound);
server.enableCORS(true);
server.begin();
printMsg("HTTP服务已启动");
}
// // void loop(void)
// // {
// // // Web服务端
// // server.handleClient();
// // }
/**
* 检测设备接口
*/
void handleStatus(){
server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪");
}
// /**
// * AP模式
// */
// void initApConfig()
// {
// 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);
// }
/**
* AP配网接口
*/
void handleConfig()
{
printMsg("进入配网......");
// 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);
}
else
{
printMsg("配网必须传递用户编号、WIFI名称和WIFI密码,配网失败");
server.send(500, "text/plain;charset=utf-8", "配网必须传递用户编号、WIFI名称和WIFI密码配网失败");
return;
}
// 可选字段
if (server.hasArg("deviceNum"))
{
printMsg("收到设备编号:" + server.arg("deviceNum"));
}
if (server.hasArg("authCode"))
{
printMsg("收到产品授权码:" + server.arg("authCode"));
}
if (server.hasArg("extra"))
{
printMsg("收到补充信息:" + server.arg("extra"));
}
server.send(200, "text/plain;charset=utf-8", "设备已更新WIFI配置开始连接WIFI...");
// /**
// * 初始化webserver配置
// */
// void initWebServer()
// {
// server.on("/status", HTTP_GET, handleStatus);
// server.on("/config", HTTP_POST, handleConfig);
// server.onNotFound(handleNotFound);
// server.enableCORS(true);
// server.begin();
// printMsg("HTTP服务已启动");
// }
// 连接Wifi关闭web服务
isApMode=false;
connectWifi();
server.close();
}
// /**
// * 连接WIFI
// */
// void connectWifi()
// {
// printMsg("连接WIFI");
// WiFi.begin(sta_ssid, sta_password);
// int cnt = 0;
// while (WiFi.status() != WL_CONNECTED)
// {
// delay(500);
// cnt++;
// Serial.print(".");
// if (cnt >= 30)
// {
// printMsg("设备连接WIFI超时请重新配网");
// return;
// }
// }
// server.close();
// WiFi.softAPdisconnect(false);
// printMsg("Http服务和热点已关闭设备已连接WIFI");
// }
// /**
// * 检测设备状态
// */
// void handleStatus(){
// server.send(200, "text/plain;charset=utf-8", "AP配网已准备就绪");
// }
// /**
// * 配网:下发配置信息
// */
// void handleConfig()
// {
// printMsg("进入配网......");
// // 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);
// }
// else
// {
// printMsg("配网必须传递用户编号、WIFI名称和WIFI密码,配网失败");
// server.send(500, "text/plain;charset=utf-8", "配网必须传递用户编号、WIFI名称和WIFI密码配网失败");
// return;
// }
// // 可选字段
// if (server.hasArg("deviceNum"))
// {
// printMsg("收到设备编号:" + server.arg("deviceNum"));
// }
// if (server.hasArg("extra"))
// {
// printMsg("收到补充信息:" + server.arg("extra"));
// }
// // TODO 可增加设备连接WIFI测试
// server.send(200, "text/plain;charset=utf-8", "设备已更新WIFI配置开始连接WIFI...");
// connectWifi();
// }
// void handleNotFound()
// {
// printMsg("进入预检请求或请求地址找不到");
// if (server.method() == HTTP_OPTIONS)
// {
// // 处理浏览器跨域问题
// server.sendHeader("Access-Control-Max-Age", "10000");
// server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
// server.sendHeader("Access-Control-Allow-Headers", "*");
// server.send(204);
// }
// else
// {
// server.send(404, "text/plain;charset=utf-8", "请求的地址找不到或无法访问");
// }
// }
// //打印提示信息
// void printMsg(String msg)
// {
// Serial.print("\r\n[");
// Serial.print(millis());
// Serial.print("ms]");
// Serial.print(msg);
// }
/**
* 找不到页面和跨域处理
*/
void handleNotFound()
{
printMsg("进入预检请求或请求地址找不到");
if (server.method() == HTTP_OPTIONS)
{
// 处理浏览器跨域问题
server.sendHeader("Access-Control-Max-Age", "10000");
server.sendHeader("Access-Control-Allow-Methods", "PUT,POST,GET,OPTIONS");
server.sendHeader("Access-Control-Allow-Headers", "*");
server.send(204);
}
else
{
server.send(404, "text/plain;charset=utf-8", "请求的地址找不到或无法访问");
}
}

View File

@@ -1,15 +1,28 @@
/***********************************************************
* function 设备配网
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#ifndef _APCONFIG_H
#define _APCONFIG_H
#include "Base64.h"
#include <ESP8266WiFi.h>
#include "Common.h"
#include <ESP8266WebServer.h>
extern ESP8266WebServer server;
// 启动AP配网
void startApConfig();
// 启动Web服务
void startWebServer();
// 配网处理接口
void handleConfig();
// 检测设备接口
void handleStatus();
// 找不到页面和浏览器跨域处理
void handleNotFound();
#endif

View File

@@ -1,9 +1,10 @@
/***********************************************************
* function 设备认证
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#include "Auth.h"
@@ -24,6 +25,7 @@ void connectMqtt()
String aesPassword = generationAESPwd();
// 连接 设备mqtt客户端Id格式为认证类型(E=加密、S=简单) & 设备编号 & 产品ID & 用户ID
String clientId = "E&" + deviceNum + "&" + productId + "&" + userId;
printMsg("客户端ID"+clientId);
bool connectResult = mqttClient.connect(clientId.c_str(), mqttUserName, aesPassword.c_str());
if (connectResult)
{

View File

@@ -1,16 +1,16 @@
/***********************************************************
* function 设备认证
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#ifndef _AUTH9_H
#define _AUTH9_H
#ifndef _AUTH_H
#define _AUTH_H
#include "Common.h"
#include "Mqtt.h"
#include "Base64.h"
#include <Ethernet.h>
#include <ESP8266HTTPClient.h>

View File

@@ -1,27 +1,53 @@
/***********************************************************
* function 设备交互
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#include "Common.h"
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 buttonLongPress() ;
float rssi = 0;
char wumei_iv[17] = "wumei-smart-open";
int monitorCount = 0;
long monitorInterval = 1000;
bool isApMode = false;
//==================================== begin 可配置的项 ===============================
//==================================== 这是需要配置的项 ===============================
// Wifi配置
char *wifiSsid = "wumei";
char *wifiPwd = "wumei-smart";
char *wifiSsid = "";
char *wifiPwd = "";
String userId = "1";
// 产品启用授权码,则授权码不能为空
String authCode = "";
// 设备信息配置
String deviceNum = "D6329VL548866";
String userId = "1";
String deviceNum = "D6329VL5668888";
String productId = "41";
float firmwareVersion = 1.0;
// 经度和纬度可选,如果产品使用设备定位,则必须传
@@ -34,12 +60,61 @@ int mqttPort = 1883;
char *mqttUserName = "wumei-smart";
char *mqttPwd = "PHYFED93WSFF1DAS";
char mqttSecret[17] = "K2V5DE28XNUU3497";
// 产品启用授权码,则授权码不能为空
String authCode="";
// NTP地址用于获取时间,修改为自己部署项目的接口地址)
String ntpServer = "http://wumei.live:8080/iot/tool/ntp?deviceSendTime=";
//==================================== end 可配置的项 ===============================
// 订阅的主题
String prefix = "/" + productId + "/" + deviceNum;
String sInfoTopic = prefix + "/info/get";
String sOtaTopic = prefix + "/ota/get";
String sNtpTopic = prefix + "/ntp/get";
String sPropertyTopic = prefix + "/property/get";
String sFunctionTopic = prefix + "/function/get";
String sPropertyOnline = prefix + "/property-online/get";
String sFunctionOnline = prefix + "/function-online/get";
String sMonitorTopic = prefix + "/monitor/get";
// 发布的主题
String pInfoTopic = prefix + "/info/post";
String pNtpTopic = prefix + "/ntp/post";
String pPropertyTopic = prefix + "/property/post";
String pFunctionTopic = prefix + "/function/post";
String pMonitorTopic = prefix + "/monitor/post";
String pEventTopic = prefix + "/event/post";
// 初始化项目
void initWumeiSmart()
{
//打开串行端口:
Serial.begin(115200);
printMsg("wumei smart device starting...");
// 初始化按键为GND并添加单击、双击、长按事件
button = OneButton(14, true, true);
button.attachClick(buttonClick);
button.attachDoubleClick(buttonDoubleClick);
button.attachLongPressStart(buttonLongPress);
saveConfig();
loadConfig();
}
// 按钮单击事件
static void buttonClick() {
Serial.println("Clicked!");
}
// 按钮双击事件
static void buttonDoubleClick() {
Serial.println("double Clicked!");
}
// 按钮长按事件
static void buttonLongPress() {
Serial.println("long Clicked!");
}
// 连接wifi
void connectWifi()
@@ -58,8 +133,58 @@ void connectWifi()
Serial.print(WiFi.localIP());
}
// 存储配置
void saveConfig()
{
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++)
{
EEPROM.write(i, *(p + i));
}
EEPROM.end();
}
// 加载配置
void loadConfig()
{
EEPROM.begin(240);
printMsg("加载配置...");
uint8_t *p = (uint8_t *)(&config);
for (int i = 0; i < sizeof(config); i++)
{
*(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);
}
// 清空配置
void clearConfig()
{
EEPROM.begin(240);
for (int i = 0; i < 240; i++)
{
EEPROM.write(i, 0);
}
EEPROM.end();
}
// 随机生成监测值
String randomPropertyData(){
String randomPropertyData()
{
// 匹配云端定义的监测数据,随机数代替监测结果
float randFloat = 0;
int randInt = 0;

View File

@@ -1,22 +1,24 @@
/***********************************************************
* function 设备交互
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#ifndef _COMMON_H
#define _COMMON_H
#include "Base64.h"
#include <ESP8266WiFi.h>
#include <Ethernet.h>
#include <ESP8266HTTPClient.h>
#include <EEPROM.h>
#include <PubSubClient.h> // 版本2.8.0
#include <ArduinoJson.h> // 版本6.19.1
#include <OneButton.h> // 版本2.0.4
extern WiFiClient wifiClient;
extern PubSubClient mqttClient;
extern OneButton button;
extern String deviceNum ; // 设备编号重要同时是Mqtt的clientId
extern String userId; // 用户ID
@@ -37,9 +39,36 @@ extern String authCode; // 产品授权码,产品未启用时为空字
extern String ntpServer; // NTP服务地址用于获取当前时间
extern int monitorCount; // 发布监测数据的最大次数
extern long monitorInterval; // 发布监测数据的间隔默认1000毫秒
extern bool isApMode; // 是否进入AP配网模式
// 订阅的主题
extern String sInfoTopic; // 订阅设备信息
extern String sOtaTopic; // 订阅OTA升级
extern String sNtpTopic; // 订阅NTP时间
extern String sPropertyTopic; // 订阅属性
extern String sFunctionTopic; // 订阅功能
extern String sPropertyOnline; // 订阅属性-在线模式
extern String sFunctionOnline; // 订阅功能-在线模式
extern String sMonitorTopic; // 订阅实时监测
// 发布的主题
extern String pInfoTopic; // 发布设备信息
extern String pNtpTopic; // 发布NTP时间
extern String pPropertyTopic; // 发布属性
extern String pFunctionTopic; // 发布功能
extern String pMonitorTopic; // 发布实时监测数据
extern String pEventTopic; // 发布事件
// 初始化项目
void initWumeiSmart();
// 连接WIFI
void connectWifi();
// 加载配置
void loadConfig();
// 保存配置
void saveConfig();
// 清空配置
void clearConfig();
// 随机生成监测值
String randomPropertyData();
//打印提示信息
@@ -47,4 +76,5 @@ void printMsg(String tips);
// 控制指示灯闪烁
void blink();
#endif

View File

@@ -1,30 +1,13 @@
/***********************************************************
* function 设备交互
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#include "Mqtt.h"
// 订阅的主题
String prefix = "/" + productId + "/" + deviceNum;
String sInfoTopic = prefix + "/info/get";
String sOtaTopic = prefix + "/ota/get";
String sNtpTopic = prefix + "/ntp/get";
String sPropertyTopic = prefix + "/property/get";
String sFunctionTopic = prefix + "/function/get";
String sPropertyOnline = prefix + "/property-online/get";
String sFunctionOnline = prefix + "/function-online/get";
String sMonitorTopic = prefix + "/monitor/get";
// 发布的主题
String pInfoTopic = prefix + "/info/post";
String pNtpTopic = prefix + "/ntp/post";
String pPropertyTopic = prefix + "/property/post";
String pFunctionTopic = prefix + "/function/post";
String pMonitorTopic = prefix + "/monitor/post";
String pEventTopic = prefix + "/event/post";
// 物模型-属性处理
void processProperty(String payload)
{
@@ -188,6 +171,7 @@ void publishInfo()
serializeJson(doc, Serial);
String output;
serializeJson(doc, output);
printMsg("主题为:" + pPropertyTopic);
mqttClient.publish(pInfoTopic.c_str(), output.c_str());
}
@@ -201,6 +185,7 @@ void publishNtp()
serializeJson(doc, Serial);
String output;
serializeJson(doc, output);
printMsg("主题为:" + pPropertyTopic);
mqttClient.publish(pNtpTopic.c_str(), output.c_str());
}
@@ -208,6 +193,7 @@ void publishNtp()
void publishProperty(String msg)
{
printMsg("发布属性:" + msg);
printMsg("主题为:" + pPropertyTopic);
mqttClient.publish(pPropertyTopic.c_str(), msg.c_str());
}
@@ -215,6 +201,7 @@ void publishProperty(String msg)
void publishFunction(String msg)
{
printMsg("发布功能:" + msg);
printMsg("主题为:" + pPropertyTopic);
mqttClient.publish(pFunctionTopic.c_str(), msg.c_str());
}
@@ -237,6 +224,7 @@ void publishEvent()
serializeJson(doc, Serial);
String output;
serializeJson(doc, output);
printMsg("主题为:" + pPropertyTopic);
mqttClient.publish(pEventTopic.c_str(), output.c_str());
}
@@ -246,5 +234,6 @@ void publishMonitor()
String msg=randomPropertyData();
// 发布为实时监测数据,不会存储
printMsg("发布实时监测数据:"+msg);
printMsg("主题为:" + pPropertyTopic);
mqttClient.publish(pMonitorTopic.c_str(), msg.c_str());
}

View File

@@ -1,36 +1,15 @@
/***********************************************************
* function 设备交互
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#ifndef _MQTT_H
#define _MQTT_H
#include "Common.h"
#include <ESP8266WiFi.h>
#include <Ethernet.h>
#include <ESP8266HTTPClient.h>
#include <PubSubClient.h> // 版本2.8.0
#include <ArduinoJson.h> // 版本6.19.1
// 订阅的主题
extern String sInfoTopic; // 订阅设备信息
extern String sOtaTopic; // 订阅OTA升级
extern String sNtpTopic; // 订阅NTP时间
extern String sPropertyTopic; // 订阅属性
extern String sFunctionTopic; // 订阅功能
extern String sPropertyOnline; // 订阅属性-在线模式
extern String sFunctionOnline; // 订阅功能-在线模式
extern String sMonitorTopic; // 订阅实时监测
// 发布的主题
extern String pInfoTopic; // 发布设备信息
extern String pNtpTopic; // 发布NTP时间
extern String pPropertyTopic; // 发布属性
extern String pFunctionTopic; // 发布功能
extern String pMonitorTopic; // 发布实时监测数据
extern String pEventTopic; // 发布事件
// 发布设备信息
void publishInfo();

View File

@@ -1,13 +1,14 @@
/***********************************************************
/*********************************************************************
* function 程序入口
* board: esp8266 core for arduino v3.0.2
* library PubSubClient2.8.0 & ArduinoJson6.19.1
* source: https://github.com/kerwincui/wumei-smart
***********************************************************/
* library PubSubClient2.8.0 & ArduinoJson6.19.1 & OneButton2.0.4
* source: https://gitee.com/kerwincui/wumei-smart
* copyright: wumei-smart and kerwincui all rights reserved.
********************************************************************/
#include "Common.h"
#include "Auth.h"
#include "Mqtt.h"
#include "Apconfig.h"
long lastMqttConn; // 上次mqtt连接时间
long lastPublishMonitor; // 上次发布监测数据时间
@@ -18,27 +19,50 @@ long lastPublishSimulateData; // 上次发布测试数据时间
*/
void setup()
{
//打开串行端口:
Serial.begin(115200);
printMsg("wumei smart device starting...");
// 初始化配置
initWumeiSmart();
if(strcmp(wifiSsid, "") == 0){
// 启动配网
startApConfig();
}else{
// 连接Wifi
connectWifi();
// 连接Mqtt
// 连接Mqtt(加密认证)
connectMqtt();
}
}
/**
* 循环执行
*/
void loop()
{
// 监测按钮
button.tick();
if (isApMode)
{
// 配网时的Web服务
server.handleClient();
}
else
{
// Wifi重连
wifiReconnectionClient();
// Mqtt重连
mqttReconnectionClient();
// 发布实时监测数据
publicMonitorClient();
// 发布模拟数据,测试用
publishSimulateDataClient();
}
}
/*
* Wifi掉线重连
@@ -51,6 +75,29 @@ void wifiReconnectionClient()
}
}
/*
* mqtt掉线重连(非阻塞、间隔30s)
*/
void mqttReconnectionClient()
{
if (WiFi.status() == WL_CONNECTED)
{
long now = millis();
if (!mqttClient.connected())
{
if (now - lastMqttConn > 30000)
{
lastMqttConn = now;
connectMqtt();
}
}
else
{
mqttClient.loop();
}
}
}
/*
* 发布实时监测数据非阻塞、间隔默认1秒
*/