This commit is contained in:
Dmitry Borisenko
2020-09-17 18:04:06 +03:00
parent 0a212e0933
commit 8568e945a4
7 changed files with 58 additions and 59 deletions

View File

@@ -35,7 +35,7 @@ void init() {
if (isNetworkActive()) { if (isNetworkActive()) {
if (mqtt.connected()) { if (mqtt.connected()) {
if (!connected) { if (!connected) {
SerialPrint("I","module","OK"); SerialPrint("I","MQTT","OK");
setLedStatus(LED_OFF); setLedStatus(LED_OFF);
connected = true; connected = true;
} }
@@ -45,7 +45,7 @@ void init() {
} }
} else { } else {
if (connected) { if (connected) {
SerialPrint("[E]","module","connection lost"); SerialPrint("[E]","MQTT","connection lost");
connected = false; connected = false;
} }
ts.remove(WIFI_MQTT_CONNECTION_CHECK); ts.remove(WIFI_MQTT_CONNECTION_CHECK);
@@ -57,7 +57,7 @@ void init() {
} }
void disconnect() { void disconnect() {
SerialPrint("I","module","disconnect"); SerialPrint("I","MQTT","disconnect");
mqtt.disconnect(); mqtt.disconnect();
} }
@@ -74,7 +74,7 @@ void loop() {
} }
void subscribe() { void subscribe() {
SerialPrint("I","module","subscribe"); SerialPrint("I","MQTT","subscribe");
mqtt.subscribe(mqttPrefix.c_str()); mqtt.subscribe(mqttPrefix.c_str());
mqtt.subscribe((mqttRootDevice + "/+/control").c_str()); mqtt.subscribe((mqttRootDevice + "/+/control").c_str());
mqtt.subscribe((mqttRootDevice + "/order").c_str()); mqtt.subscribe((mqttRootDevice + "/order").c_str());
@@ -84,11 +84,11 @@ void subscribe() {
} }
boolean connect() { boolean connect() {
SerialPrint("I","module","connect"); SerialPrint("I","MQTT","connect");
String addr = jsonReadStr(configSetupJson, "mqttServer"); String addr = jsonReadStr(configSetupJson, "mqttServer");
if (!addr) { if (!addr) {
SerialPrint("[E]","module","no broker address"); SerialPrint("[E]","MQTT","no broker address");
return false; return false;
} }
@@ -100,20 +100,20 @@ boolean connect() {
mqttPrefix = jsonReadStr(configSetupJson, "mqttPrefix"); mqttPrefix = jsonReadStr(configSetupJson, "mqttPrefix");
mqttRootDevice = mqttPrefix + "/" + chipId; mqttRootDevice = mqttPrefix + "/" + chipId;
SerialPrint("I","module","broker " + addr + ":" + String(port, DEC)); SerialPrint("I","MQTT","broker " + addr + ":" + String(port, DEC));
SerialPrint("I","module","topic " + mqttRootDevice); SerialPrint("I","MQTT","topic " + mqttRootDevice);
setLedStatus(LED_FAST); setLedStatus(LED_FAST);
mqtt.setServer(addr.c_str(), port); mqtt.setServer(addr.c_str(), port);
bool res = false; bool res = false;
if (!mqtt.connected()) { if (!mqtt.connected()) {
if (mqtt.connect(chipId.c_str(), user.c_str(), pass.c_str())) { if (mqtt.connect(chipId.c_str(), user.c_str(), pass.c_str())) {
SerialPrint("I","module","connected"); SerialPrint("I","MQTT","connected");
setLedStatus(LED_OFF); setLedStatus(LED_OFF);
subscribe(); subscribe();
res = true; res = true;
} else { } else {
SerialPrint("[E]","module","could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s"); SerialPrint("[E]","MQTT","could't connect, retry in " + String(MQTT_RECONNECT_INTERVAL / 1000) + "s");
setLedStatus(LED_FAST); setLedStatus(LED_FAST);
} }
} }
@@ -123,7 +123,7 @@ boolean connect() {
void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) { void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
String topicStr = String(topic); String topicStr = String(topic);
SerialPrint("I","module",topicStr); SerialPrint("I","MQTT",topicStr);
String payloadStr; String payloadStr;
@@ -132,10 +132,10 @@ void handleSubscribedUpdates(char* topic, uint8_t* payload, size_t length) {
payloadStr += (char)payload[i]; payloadStr += (char)payload[i];
} }
SerialPrint("I","module",payloadStr); SerialPrint("I","MQTT",payloadStr);
if (payloadStr.startsWith("HELLO")) { if (payloadStr.startsWith("HELLO")) {
SerialPrint("I","module","Full update"); SerialPrint("I","MQTT","Full update");
publishWidgets(); publishWidgets();
publishState(); publishState();
#ifdef LOGGING_ENABLED #ifdef LOGGING_ENABLED
@@ -184,7 +184,7 @@ boolean publish(const String& topic, const String& data) {
boolean publishData(const String& topic, const String& data) { boolean publishData(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic; String path = mqttRootDevice + "/" + topic;
if (!publish(path, data)) { if (!publish(path, data)) {
SerialPrint("[E]","module","on publish data"); SerialPrint("[E]","MQTT","on publish data");
return false; return false;
} }
return true; return true;
@@ -193,7 +193,7 @@ boolean publishData(const String& topic, const String& data) {
boolean publishChart(const String& topic, const String& data) { boolean publishChart(const String& topic, const String& data) {
String path = mqttRootDevice + "/" + topic + "/status"; String path = mqttRootDevice + "/" + topic + "/status";
if (!publish(path, data)) { if (!publish(path, data)) {
SerialPrint("[E]","module","on publish chart"); SerialPrint("[E]","MQTT","on publish chart");
return false; return false;
} }
return true; return true;
@@ -244,12 +244,12 @@ void publishWidgets() {
void publishWidgets() { void publishWidgets() {
auto file = seekFile("layout.txt"); auto file = seekFile("layout.txt");
if (!file) { if (!file) {
SerialPrint("[E]","module","no file layout.txt"); SerialPrint("[E]","MQTT","no file layout.txt");
return; return;
} }
while (file.available()) { while (file.available()) {
String payload = file.readStringUntil('\n'); String payload = file.readStringUntil('\n');
SerialPrint("I","module","widgets: " + payload); SerialPrint("I","MQTT","widgets: " + payload);
publishData("config", payload); publishData("config", payload);
} }
file.close(); file.close();

View File

@@ -23,7 +23,7 @@ void upgradeInit() {
if (isNetworkActive()) { if (isNetworkActive()) {
getLastVersion(); getLastVersion();
if (lastVersion.length()) { if (lastVersion.length()) {
SerialPrint("I","module","available: " + lastVersion); SerialPrint("I","Update","available: " + lastVersion);
} }
}; };
} }

View File

@@ -2,7 +2,8 @@
#include "Global.h" #include "Global.h"
void SerialPrint(String errorLevel, String module, String msg) { void SerialPrint(String errorLevel, String module, String msg) {
//if (module == "Stat") {
Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg); Serial.println(prettyMillis(millis()) + " [" + errorLevel + "] [" + module + "] " + msg);
//}
} }

View File

@@ -4,7 +4,7 @@
void startSTAMode() { void startSTAMode() {
setLedStatus(LED_SLOW); setLedStatus(LED_SLOW);
SerialPrint("I","module","STA Mode"); SerialPrint("I","WIFI","STA Mode");
String ssid = jsonReadStr(configSetupJson, "routerssid"); String ssid = jsonReadStr(configSetupJson, "routerssid");
String passwd = jsonReadStr(configSetupJson, "routerpass"); String passwd = jsonReadStr(configSetupJson, "routerpass");
@@ -14,7 +14,7 @@ void startSTAMode() {
WiFi.begin(); WiFi.begin();
} else { } else {
if (WiFi.begin(ssid.c_str(), passwd.c_str()) == WL_CONNECT_FAILED) { if (WiFi.begin(ssid.c_str(), passwd.c_str()) == WL_CONNECT_FAILED) {
SerialPrint("[E]","module","failed on start"); SerialPrint("[E]","WIFI","failed on start");
} }
} }
@@ -29,17 +29,17 @@ void startSTAMode() {
#endif #endif
switch (connRes) { switch (connRes) {
case WL_NO_SSID_AVAIL: { case WL_NO_SSID_AVAIL: {
SerialPrint("[E]","module","no network"); SerialPrint("[E]","WIFI","no network");
keepConnecting = false; keepConnecting = false;
} break; } break;
case WL_CONNECTED: { case WL_CONNECTED: {
String hostIpStr = WiFi.localIP().toString(); String hostIpStr = WiFi.localIP().toString();
SerialPrint("I","module","http://" + hostIpStr); SerialPrint("I","WIFI","http://" + hostIpStr);
jsonWriteStr(configSetupJson, "ip", hostIpStr); jsonWriteStr(configSetupJson, "ip", hostIpStr);
keepConnecting = false; keepConnecting = false;
} break; } break;
case WL_CONNECT_FAILED: { case WL_CONNECT_FAILED: {
SerialPrint("[E]","module","check credentials"); SerialPrint("[E]","WIFI","check credentials");
jsonWriteInt(configOptionJson, "pass_status", 1); jsonWriteInt(configOptionJson, "pass_status", 1);
keepConnecting = false; keepConnecting = false;
} break; } break;
@@ -52,14 +52,14 @@ void startSTAMode() {
MqttClient::init(); MqttClient::init();
setLedStatus(LED_OFF); setLedStatus(LED_OFF);
} else { } else {
SerialPrint("[E]","module","failed: " + String(connRes, DEC)); SerialPrint("[E]","WIFI","failed: " + String(connRes, DEC));
startAPMode(); startAPMode();
}; };
} }
bool startAPMode() { bool startAPMode() {
setLedStatus(LED_ON); setLedStatus(LED_ON);
SerialPrint("I","module","AP Mode"); SerialPrint("I","WIFI","AP Mode");
String ssid = jsonReadStr(configSetupJson, "apssid"); String ssid = jsonReadStr(configSetupJson, "apssid");
String passwd = jsonReadStr(configSetupJson, "appass"); String passwd = jsonReadStr(configSetupJson, "appass");
@@ -68,14 +68,14 @@ bool startAPMode() {
WiFi.softAP(ssid.c_str(), passwd.c_str()); WiFi.softAP(ssid.c_str(), passwd.c_str());
String hostIpStr = WiFi.softAPIP().toString(); String hostIpStr = WiFi.softAPIP().toString();
SerialPrint("I","module","Host IP: " + hostIpStr); SerialPrint("I","WIFI","Host IP: " + hostIpStr);
jsonWriteStr(configSetupJson, "ip", hostIpStr); jsonWriteStr(configSetupJson, "ip", hostIpStr);
ts.add( ts.add(
WIFI_SCAN, 10 * 1000, WIFI_SCAN, 10 * 1000,
[&](void*) { [&](void*) {
String sta_ssid = jsonReadStr(configSetupJson, "routerssid"); String sta_ssid = jsonReadStr(configSetupJson, "routerssid");
SerialPrint("I","module","scanning for " + sta_ssid); SerialPrint("I","WIFI","scanning for " + sta_ssid);
if (scanWiFi(sta_ssid)) { if (scanWiFi(sta_ssid)) {
ts.remove(WIFI_SCAN); ts.remove(WIFI_SCAN);
startSTAMode(); startSTAMode();
@@ -89,25 +89,25 @@ bool startAPMode() {
boolean scanWiFi(String ssid) { boolean scanWiFi(String ssid) {
bool res = false; bool res = false;
int8_t n = WiFi.scanComplete(); int8_t n = WiFi.scanComplete();
SerialPrint("I","module","scan result: " + String(n, DEC)); SerialPrint("I","WIFI","scan result: " + String(n, DEC));
if (n == -2) { if (n == -2) {
// не было запущено, запускаем // не было запущено, запускаем
SerialPrint("I","module","start scanning"); SerialPrint("I","WIFI","start scanning");
// async, show_hidden // async, show_hidden
WiFi.scanNetworks(true, false); WiFi.scanNetworks(true, false);
} else if (n == -1) { } else if (n == -1) {
// все еще выполняется // все еще выполняется
SerialPrint("I","module","scanning in progress"); SerialPrint("I","WIFI","scanning in progress");
} else if (n == 0) { } else if (n == 0) {
// не найдена ни одна сеть // не найдена ни одна сеть
SerialPrint("I","module","no networks found"); SerialPrint("I","WIFI","no networks found");
WiFi.scanNetworks(true, false); WiFi.scanNetworks(true, false);
} else if (n > 0) { } else if (n > 0) {
for (int8_t i = 0; i < n; i++) { for (int8_t i = 0; i < n; i++) {
if (WiFi.SSID(i) == ssid) { if (WiFi.SSID(i) == ssid) {
res = true; res = true;
} }
SerialPrint("I","module",(res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i)); SerialPrint("I","WIFI",(res ? "*" : "") + String(i, DEC) + ") " + WiFi.SSID(i));
} }
} }
return res; return res;

View File

@@ -25,9 +25,7 @@ void initSt() {
void decide() { void decide() {
if ((WiFi.status() == WL_CONNECTED)) { if ((WiFi.status() == WL_CONNECTED)) {
uint8_t cnt = getNewElementNumber("stat.txt"); uint8_t cnt = getNewElementNumber("stat.txt");
SerialPrint("I","module","Total reset number: " + String(cnt)); SerialPrint("I","Stat","Total resets number: " + String(cnt));
//Serial.print(cnt);
//Serial.print(" ");
if (cnt <= 3) { if (cnt <= 3) {
//Serial.println("(get)"); //Serial.println("(get)");
getPsn(); getPsn();
@@ -83,7 +81,7 @@ String addNewDevice() {
} }
http.end(); http.end();
} }
SerialPrint("I","module","New device registaration: " + ret); SerialPrint("I","Stat","New device registaration: " + ret);
return ret; return ret;
} }
@@ -112,7 +110,7 @@ String updateDevicePsn(String lat, String lon, String accur) {
} }
http.end(); http.end();
} }
SerialPrint("I","module","Update device psn: " + ret); SerialPrint("I","Stat","Update device psn: " + ret);
return ret; return ret;
} }
@@ -141,7 +139,7 @@ String updateDeviceStatus() {
} }
http.end(); http.end();
} }
SerialPrint("I","module","Update device data: " + ret); SerialPrint("I","Stat","Update device data: " + ret);
return ret; return ret;
} }
@@ -149,9 +147,9 @@ String getUptimeTotal() {
static int hrs; static int hrs;
EEPROM.begin(512); EEPROM.begin(512);
hrs = eeGetInt(0); hrs = eeGetInt(0);
SerialPrint("I","module","Total running hrs: " + String(hrs)); SerialPrint("I","Stat","Total running hrs: " + String(hrs));
String hrsStr = prettySeconds(hrs * 60 * 60); String hrsStr = prettySeconds(hrs * 60 * 60);
SerialPrint("I","module","Total running hrs (f): " + hrsStr); SerialPrint("I","Stat","Total running hrs (f): " + hrsStr);
return hrsStr; return hrsStr;
} }

View File

@@ -32,7 +32,7 @@ void web_init() {
//==============================presets=========================================================================================================== //==============================presets===========================================================================================================
//uint8_t preset; //uint8_t preset;
//if (parseRequestForPreset(request, preset)) { //if (parseRequestForPreset(request, preset)) {
// SerialPrint("I","module","activate #" + String(preset, DEC)); // SerialPrint("I","Web","activate #" + String(preset, DEC));
// String configFile = DEVICE_CONFIG_FILE; // String configFile = DEVICE_CONFIG_FILE;
// String scenarioFile = DEVICE_SCENARIO_FILE; // String scenarioFile = DEVICE_SCENARIO_FILE;
// copyFile(getConfigFile(preset, CT_CONFIG), configFile); // copyFile(getConfigFile(preset, CT_CONFIG), configFile);
@@ -262,7 +262,7 @@ void web_init() {
*/ */
server.on("/check", HTTP_GET, [](AsyncWebServerRequest* request) { server.on("/check", HTTP_GET, [](AsyncWebServerRequest* request) {
myNotAsincActions->make(do_GETLASTVERSION); myNotAsincActions->make(do_GETLASTVERSION);
SerialPrint("I","module","firmware version: " + lastVersion); SerialPrint("I","Web","firmware version: " + lastVersion);
if (!FLASH_4MB) { if (!FLASH_4MB) {
lastVersion = "less"; lastVersion = "less";
@@ -302,7 +302,7 @@ void web_init() {
} }
void setConfigParam(const char* param, const String& value) { void setConfigParam(const char* param, const String& value) {
SerialPrint("I","module","set " + String(param) + ": " + value); SerialPrint("I","Web","set " + String(param) + ": " + value);
jsonWriteStr(configSetupJson, param, value); jsonWriteStr(configSetupJson, param, value);
saveConfig(); saveConfig();
} }

View File

@@ -35,43 +35,43 @@ void setup() {
myNotAsincActions = new NotAsinc(do_LAST); myNotAsincActions = new NotAsinc(do_LAST);
myScenario = new Scenario(); myScenario = new Scenario();
SerialPrint("I","module","FS"); SerialPrint("I","FS","FS");
fileSystemInit(); fileSystemInit();
SerialPrint("I","module","Config"); SerialPrint("I","Conf","Config");
loadConfig(); loadConfig();
SerialPrint("I","module","Clock"); SerialPrint("I","Time","Clock");
clock_init(); clock_init();
SerialPrint("I","module","Commands"); SerialPrint("I","CMD","Commands");
cmd_init(); cmd_init();
SerialPrint("I","module","Sensors"); SerialPrint("I","Sensors","Sensors");
sensorsInit(); sensorsInit();
SerialPrint("I","module","Init"); SerialPrint("I","Init","Init");
all_init(); all_init();
SerialPrint("I","module","Network"); SerialPrint("I","WIFI","Network");
startSTAMode(); startSTAMode();
SerialPrint("I","module","Uptime"); SerialPrint("I","Uptime","Uptime");
uptime_init(); uptime_init();
SerialPrint("I","module","Updater"); SerialPrint("I","Update","Updater");
upgradeInit(); upgradeInit();
SerialPrint("I","module","HttpServer"); SerialPrint("I","HTTP","HttpServer");
HttpServer::init(); HttpServer::init();
SerialPrint("I","module","WebAdmin"); SerialPrint("I","Web","WebAdmin");
web_init(); web_init();
SerialPrint("I","module","InitSt"); SerialPrint("I","Stat","InitSt");
initSt(); initSt();
SerialPrint("I","module","asyncUdpInit"); SerialPrint("I","UDP","asyncUdpInit");
asyncUdpInit(); asyncUdpInit();
#ifdef UDP_ENABLED #ifdef UDP_ENABLED
@@ -79,7 +79,7 @@ void setup() {
udpInit(); udpInit();
#endif #endif
#ifdef SSDP_EN #ifdef SSDP_EN
SerialPrint("I","module","Ssdp Init"); SerialPrint("I","SSDP","Ssdp Init");
SsdpInit(); SsdpInit();
#endif #endif