From f487a691fb244e6a2940d2bdf1ada60ae4ce770c Mon Sep 17 00:00:00 2001 From: Dmitry Borisenko <49808844+DmitryBorisenko33@users.noreply.github.com> Date: Thu, 10 Dec 2020 19:12:15 +0300 Subject: [PATCH] uart working version --- data/set.device.json | 2 +- include/Clock.h | 2 +- include/Cmd.h | 2 +- include/Consts.h | 2 +- include/Init.h | 4 +- include/Utils/TimeUtils.h | 2 +- include/Utils/WebUtils.h | 3 +- src/BufferExecute.cpp | 1 + src/Bus.cpp | 7 ++- src/Clock.cpp | 4 +- src/Init.cpp | 12 ++++-- src/ItemsList.cpp | 3 +- src/RemoteOrdersUdp.cpp | 2 + src/SSDP.cpp | 1 + src/SoftUART.cpp | 7 +-- src/Telegram.cpp | 1 + src/UpgradeFirm.cpp | 1 + src/Utils/FileUtils.cpp | 3 +- src/Utils/TimeUtils.cpp | 28 +++++++----- src/Utils/WebUtils.cpp | 40 +++++++++++++++-- src/Utils/WiFiUtils.cpp | 6 ++- src/Utils/statUtils.cpp | 1 + src/Web.cpp | 4 +- src/WebServer.cpp | 2 + src/items/vButtonOut.cpp | 6 ++- src/main.cpp | 91 +++++---------------------------------- 26 files changed, 119 insertions(+), 118 deletions(-) diff --git a/data/set.device.json b/data/set.device.json index 5f566c11..c1163c58 100644 --- a/data/set.device.json +++ b/data/set.device.json @@ -43,7 +43,7 @@ }, { "type": "h4", - "title": "Версия файловой системы: 268" + "title": "Версия файловой системы: 270" }, { "type": "h4", diff --git a/include/Clock.h b/include/Clock.h index dea567c8..fdb55cbe 100644 --- a/include/Clock.h +++ b/include/Clock.h @@ -5,7 +5,7 @@ #include "Utils/TimeUtils.h" #include "Utils\SerialPrint.h" -extern void clock_init(); +extern void clockInit(); #ifdef ESP8266 #include "sntp.h" diff --git a/include/Cmd.h b/include/Cmd.h index eb27380d..2fec07d9 100644 --- a/include/Cmd.h +++ b/include/Cmd.h @@ -42,7 +42,7 @@ extern void bme280A_reading(); //extern void dhtC_reading(); //extern void dhtD_reading(); -extern void handle_time_init(); +extern void timeInit(); extern void stepper(); extern void stepperSet(); extern void servo_(); diff --git a/include/Consts.h b/include/Consts.h index 7fb57c28..0d41037f 100644 --- a/include/Consts.h +++ b/include/Consts.h @@ -1,7 +1,7 @@ #pragma once //===========Firmware============================================================================================================================================= -#define FIRMWARE_VERSION 268 +#define FIRMWARE_VERSION 270 //#define FLASH_SIZE_1MB #ifdef ESP8266 #define FIRMWARE_NAME "esp8266-iotm" diff --git a/include/Init.h b/include/Init.h index bb2b2ca7..e7fc3157 100644 --- a/include/Init.h +++ b/include/Init.h @@ -1,10 +1,10 @@ #pragma once extern void loadConfig(); -extern void all_init(); +extern void espInit(); extern void statistics_init(); extern void loadScenario(); -extern void Device_init(); +extern void deviceInit(); extern void prsets_init(); extern void handle_uptime(); extern void handle_statistics(); diff --git a/include/Utils/TimeUtils.h b/include/Utils/TimeUtils.h index 024d1bc8..60bf405f 100644 --- a/include/Utils/TimeUtils.h +++ b/include/Utils/TimeUtils.h @@ -50,4 +50,4 @@ int getOffsetInMinutes(int timezone); */ void breakEpochToTime(unsigned long epoch, Time_t& tm); -void handle_time_init(); \ No newline at end of file +void timeInit(); \ No newline at end of file diff --git a/include/Utils/WebUtils.h b/include/Utils/WebUtils.h index 5479314c..15e412db 100644 --- a/include/Utils/WebUtils.h +++ b/include/Utils/WebUtils.h @@ -4,4 +4,5 @@ extern String getURL(const String& urls); extern const String getMethodName(AsyncWebServerRequest* request); -extern const String getRequestInfo(AsyncWebServerRequest* request); \ No newline at end of file +extern const String getRequestInfo(AsyncWebServerRequest* request); +extern void wifiSignalInit(); \ No newline at end of file diff --git a/src/BufferExecute.cpp b/src/BufferExecute.cpp index ad33b79c..4af187a8 100644 --- a/src/BufferExecute.cpp +++ b/src/BufferExecute.cpp @@ -144,6 +144,7 @@ void sensorsInit() { } }, nullptr, true); + SerialPrint("I", F("Sensors"), F("Sensors Init")); } void addKey(String& key, String& keyNumberTable, int number) { diff --git a/src/Bus.cpp b/src/Bus.cpp index 12cbcf6a..eeff8108 100644 --- a/src/Bus.cpp +++ b/src/Bus.cpp @@ -10,12 +10,14 @@ void busInit() { tmp = i2c_scan(); Serial.println(tmp); jsonWriteStr(configLiveJson, "i2c", tmp); - } else { + } + else { Serial.println(tmp); jsonWriteStr(configLiveJson, "i2c", tmp); } }, nullptr); + SerialPrint("I", F("Bus"), F("Bus Init")); } String i2c_scan() { @@ -32,7 +34,8 @@ String i2c_scan() { } if (count == 0) { return "error"; - } else { + } + else { return out; } } \ No newline at end of file diff --git a/src/Clock.cpp b/src/Clock.cpp index 888f9d5b..2ae4132a 100644 --- a/src/Clock.cpp +++ b/src/Clock.cpp @@ -3,14 +3,14 @@ #include "Global.h" Clock* timeNow; -void clock_init() { +void clockInit() { timeNow = new Clock; timeNow->setNtpPool(jsonReadStr(configSetupJson, "ntp")); timeNow->setTimezone(jsonReadStr(configSetupJson, "timezone").toInt()); - ts.add( TIME_SYNC, 30000, [&](void*) { timeNow->hasSync(); }, nullptr, true); + SerialPrint("I", F("Time"), F("Clock Init")); } diff --git a/src/Init.cpp b/src/Init.cpp index 98586aa3..25885189 100644 --- a/src/Init.cpp +++ b/src/Init.cpp @@ -26,17 +26,20 @@ void loadConfig() { prex = jsonReadStr(configSetupJson, "mqttPrefix") + "/" + chipId; - Serial.println(configSetupJson); + //Serial.println(configSetupJson); serverIP = jsonReadStr(configSetupJson, "serverip"); + + SerialPrint("I", F("Conf"), F("Config Json Init")); } -void all_init() { - Device_init(); +void espInit() { + deviceInit(); loadScenario(); + SerialPrint("I", F("esp"), F("esp Init")); } -void Device_init() { +void deviceInit() { sensorReadingMap10sec = ""; @@ -110,6 +113,7 @@ void uptime_init() { handle_uptime(); }, nullptr, true); + SerialPrint("I", F("Uptime"), F("Uptime Init")); } void handle_uptime() { diff --git a/src/ItemsList.cpp b/src/ItemsList.cpp index b18d8f16..d80c11c0 100644 --- a/src/ItemsList.cpp +++ b/src/ItemsList.cpp @@ -9,7 +9,7 @@ static const char* firstLine PROGMEM = "Удалить;Тип элемента;I void itemsListInit() { myNotAsyncActions->add( do_deviceInit, [&](void*) { - Device_init(); + deviceInit(); }, nullptr); @@ -33,6 +33,7 @@ void itemsListInit() { }, nullptr); #endif + SerialPrint("I", F("Items"), F("Items Init")); } void addItem(String name) { diff --git a/src/RemoteOrdersUdp.cpp b/src/RemoteOrdersUdp.cpp index c3428fec..70ff516c 100644 --- a/src/RemoteOrdersUdp.cpp +++ b/src/RemoteOrdersUdp.cpp @@ -75,6 +75,8 @@ void asyncUdpInit() { //}, //nullptr, true); + SerialPrint("I", F("UDP"), "Udp Init"); + } bool udpPacketValidation(String& data) { diff --git a/src/SSDP.cpp b/src/SSDP.cpp index c84d99ca..fb431bbd 100644 --- a/src/SSDP.cpp +++ b/src/SSDP.cpp @@ -47,6 +47,7 @@ void SsdpInit() { SSDP.setDeviceType(F("upnp:rootdevice")); SSDP.setSchemaURL(F("description.xml")); SSDP.begin(); + SerialPrint("I", F("SSDP"), F("Ssdp Init")); } String xmlNode(String tags, String data) { diff --git a/src/SoftUART.cpp b/src/SoftUART.cpp index 70cf2680..91ba781d 100644 --- a/src/SoftUART.cpp +++ b/src/SoftUART.cpp @@ -20,10 +20,11 @@ void uartInit() { myUART->begin(4, 5); #endif } + SerialPrint("I", F("UART"), F("UART Init")); } void uartHandle() { - if (myUART != nullptr) { + if (myUART) { if (!jsonReadBool(configSetupJson, "uart")) { return; } @@ -34,7 +35,7 @@ void uartHandle() { incStr += inc; if (inc == '\n') { parse(incStr); - incStr = ""; + incStr = ""; } } } @@ -46,7 +47,7 @@ void parse(String& incStr) { incStr.replace("\n", ""); if (incStr.indexOf("set") != -1) { incStr = deleteBeforeDelimiter(incStr, " "); - SerialPrint("I", "UART", incStr); orderBuf += incStr; + SerialPrint("I", "=>UART", incStr); } } \ No newline at end of file diff --git a/src/Telegram.cpp b/src/Telegram.cpp index cc560330..6a5237ca 100644 --- a/src/Telegram.cpp +++ b/src/Telegram.cpp @@ -19,6 +19,7 @@ void telegramInit() { else { SerialPrint("E", "Telegram", "Not connected"); } + SerialPrint("I", F("Telegram"), F("Telegram Init")); } } diff --git a/src/UpgradeFirm.cpp b/src/UpgradeFirm.cpp index 9583a818..63e7168f 100644 --- a/src/UpgradeFirm.cpp +++ b/src/UpgradeFirm.cpp @@ -31,6 +31,7 @@ void upgradeInit() { } } }; + SerialPrint("I", F("Update"), F("Updater Init")); } void getLastVersion() { diff --git a/src/Utils/FileUtils.cpp b/src/Utils/FileUtils.cpp index dd13682f..70955088 100644 --- a/src/Utils/FileUtils.cpp +++ b/src/Utils/FileUtils.cpp @@ -10,9 +10,10 @@ const String filepath(const String& filename) { bool fileSystemInit() { if (!LittleFS.begin()) { - SerialPrint("[E]","Files","init"); + SerialPrint("E", F("FS"), F("FS Init ERROR, may be FS was not flashed")); return false; } + SerialPrint("I", F("FS"), F("FS Init")); return true; } diff --git a/src/Utils/TimeUtils.cpp b/src/Utils/TimeUtils.cpp index 5420b4a1..b377965a 100644 --- a/src/Utils/TimeUtils.cpp +++ b/src/Utils/TimeUtils.cpp @@ -2,8 +2,8 @@ #include "Global.h" #include "Utils/StringUtils.h" -static const uint8_t days_in_month[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; -static const char* week_days[7] = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"}; +static const uint8_t days_in_month[] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; +static const char* week_days[7] = { "Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat" }; // String getTimeUnix() { // time_t t; @@ -118,7 +118,8 @@ const String prettySeconds(unsigned long time_s) { if (days) { sprintf_P(buf, TIME_FORMAT_WITH_DAYS, days, hours, minutes, seconds); - } else { + } + else { sprintf_P(buf, TIME_FORMAT, hours, minutes, seconds); } return String(buf); @@ -138,15 +139,18 @@ unsigned long millis_passed(unsigned long start, unsigned long finish) { unsigned long passed = finish - start; if (passed <= __LONG_MAX__) { result = static_cast(passed); - } else { + } + else { result = static_cast((__LONG_MAX__ - finish) + start + 1u); } - } else { + } + else { unsigned long passed = start - finish; if (passed <= __LONG_MAX__) { result = static_cast(passed); result = -1 * result; - } else { + } + else { result = static_cast((__LONG_MAX__ - start) + finish + 1u); result = -1 * result; } @@ -194,16 +198,19 @@ void breakEpochToTime(unsigned long epoch, Time_t& tm) { if (1 == month) { // february if (LEAP_YEAR(year)) { month_length = 29; - } else { + } + else { month_length = 28; } - } else { + } + else { month_length = days_in_month[month]; } if (time >= month_length) { time -= month_length; - } else { + } + else { break; } } @@ -212,7 +219,7 @@ void breakEpochToTime(unsigned long epoch, Time_t& tm) { tm.valid = (epoch > MIN_DATETIME); } -void handle_time_init() { +void timeInit() { ts.add( TIME, 1000, [&](void*) { String timenow = timeNow->getTimeWOsec(); @@ -224,4 +231,5 @@ void handle_time_init() { } }, nullptr, true); + SerialPrint("I", F("Time"), F("Handle time init")); } diff --git a/src/Utils/WebUtils.cpp b/src/Utils/WebUtils.cpp index 8c766b0e..f3807912 100644 --- a/src/Utils/WebUtils.cpp +++ b/src/Utils/WebUtils.cpp @@ -8,7 +8,8 @@ String getURL(const String& urls) { int httpCode = http.GET(); if (httpCode == HTTP_CODE_OK) { res = http.getString(); - } else { + } + else { res = "error"; } http.end(); @@ -68,9 +69,11 @@ const String getRequestInfo(AsyncWebServerRequest* request) { AsyncWebParameter* p = request->getParam(i); if (p->isFile()) { res += "FILE"; - } else if (p->isPost()) { + } + else if (p->isPost()) { res += "POST"; - } else { + } + else { res += "GET"; } res += ' '; @@ -86,3 +89,34 @@ const String getRequestInfo(AsyncWebServerRequest* request) { } return res; } + +void wifiSignalInit() { + ts.add( + SYGNAL, 1000 * 60, [&](void*) { + SerialPrint("I", "System", printMemoryStatus()); + switch (RSSIquality()) { + case 0: + jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: не подключено к роутеру")); + break; + case 1: + jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: нет сигнала")); + break; + case 2: + jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: очень низкий")); + break; + case 3: + jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: низкий")); + break; + case 4: + jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: хороший")); + break; + case 5: + jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: очень хороший")); + break; + case 6: + jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: отличный")); + break; + } + }, + nullptr, true); +} diff --git a/src/Utils/WiFiUtils.cpp b/src/Utils/WiFiUtils.cpp index 5b163c93..10ecd39c 100644 --- a/src/Utils/WiFiUtils.cpp +++ b/src/Utils/WiFiUtils.cpp @@ -1,13 +1,16 @@ #include "Utils/WiFiUtils.h" void routerConnect() { + + WiFi.setAutoConnect(false); + WiFi.persistent(false); + setLedStatus(LED_SLOW); WiFi.mode(WIFI_STA); byte tries = 20; String _ssid = jsonReadStr(configSetupJson, "routerssid"); String _password = jsonReadStr(configSetupJson, "routerpass"); - //WiFi.persistent(false); if (_ssid == "" && _password == "") { WiFi.begin(); @@ -39,6 +42,7 @@ void routerConnect() { setLedStatus(LED_OFF); mqttInit(); } + SerialPrint("I", F("WIFI"), F("Network Init")); } bool startAPMode() { diff --git a/src/Utils/statUtils.cpp b/src/Utils/statUtils.cpp index fad0a29d..b7e7c690 100644 --- a/src/Utils/statUtils.cpp +++ b/src/Utils/statUtils.cpp @@ -25,6 +25,7 @@ void initSt() { }, nullptr, true); } + SerialPrint("I", F("Stat"), F("Stat Init")); } void decide() { diff --git a/src/Web.cpp b/src/Web.cpp index 613b930c..8e4b6eb9 100644 --- a/src/Web.cpp +++ b/src/Web.cpp @@ -311,7 +311,7 @@ void web_init() { // itemsLine = request->getParam("line")->value(); // } // delElementFlag = true; - // Device_init(); + // deviceInit(); // request->redirect("/?setn.device"); //}); @@ -360,6 +360,8 @@ void web_init() { myNotAsyncActions->make(do_UPGRADE); request->send(200, "text/html"); }); + + SerialPrint("I", F("Web"), F("WebAdmin Init")); } void setConfigParam(const char* param, const String& value) { diff --git a/src/WebServer.cpp b/src/WebServer.cpp index 9a4611f4..c955e9a2 100644 --- a/src/WebServer.cpp +++ b/src/WebServer.cpp @@ -75,6 +75,8 @@ void init() { initOta(); initMDNS(); initWS(); + + SerialPrint("I", F("HTTP"), F("HttpServer Init")); } void onWsEvent(AsyncWebSocket *server, AsyncWebSocketClient *client, AwsEventType type, void *arg, uint8_t *data, size_t len) { diff --git a/src/items/vButtonOut.cpp b/src/items/vButtonOut.cpp index bc1f6e2c..ff98e2ee 100644 --- a/src/items/vButtonOut.cpp +++ b/src/items/vButtonOut.cpp @@ -22,8 +22,10 @@ ButtonOut::~ButtonOut() {} void ButtonOut::execute(String state) { if (_type == "UART") { if (jsonReadBool(configSetupJson, "uart")) { - if (myUART != nullptr) { - myUART->print(_key + " " + state); + if (myUART) { + String msg = _key + " " + state; + myUART->print(msg); + SerialPrint("I", "<=UART", msg); } } } diff --git a/src/main.cpp b/src/main.cpp index 44c52ff1..45398eef 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,116 +29,48 @@ Timings metric; boolean initialized = false; void setup() { - WiFi.setAutoConnect(false); - WiFi.persistent(false); - Serial.begin(115200); Serial.flush(); Serial.println(); Serial.println(F("--------------started----------------")); - setChipId(); - myNotAsyncActions = new NotAsync(do_LAST); myScenario = new Scenario(); + //=========================================initialisation============================================================== + setChipId(); fileSystemInit(); - SerialPrint("I", F("FS"), F("FS Init")); - - SerialPrint("I", F("UART"), F("UART Init")); - uartInit(); - loadConfig(); - SerialPrint("I", F("Conf"), F("Config Init")); - - clock_init(); - SerialPrint("I", F("Time"), F("Clock Init")); - - handle_time_init(); - SerialPrint("I", F("Time"), F("Handle time init")); - - sensorsInit(); - SerialPrint("I", F("Sensors"), F("Sensors Init")); - + uartInit(); + clockInit(); + timeInit(); + sensorsInit(); //Will be remooved itemsListInit(); - SerialPrint("I", F("Items"), F("Items Init")); - - all_init(); - SerialPrint("I", F("Init"), F("Init Init")); - + espInit(); routerConnect(); - SerialPrint("I", F("WIFI"), F("Network Init")); - telegramInit(); - SerialPrint("I", F("Telegram"), F("Telegram Init")); - uptime_init(); - SerialPrint("I", F("Uptime"), F("Uptime Init")); - upgradeInit(); - SerialPrint("I", F("Update"), F("Updater Init")); - HttpServer::init(); - SerialPrint("I", F("HTTP"), F("HttpServer Init")); - web_init(); - SerialPrint("I", F("Web"), F("WebAdmin Init")); - initSt(); - SerialPrint("I", F("Stat"), F("Stat Init")); - + busInit(); #ifdef UDP_ENABLED - SerialPrint("I", F("UDP"), "Udp Init"); asyncUdpInit(); #endif - - SerialPrint("I", F("Bus"), F("Bus Init")); - busInit(); - - - #ifdef SSDP_ENABLED - SerialPrint("I", F("SSDP"), F("Ssdp Init")); SsdpInit(); #endif //esp_log_level_set("esp_littlefs", ESP_LOG_NONE); - ts.add( - SYGNAL, 1000 * 60, [&](void*) { - SerialPrint("I", "System", printMemoryStatus()); - switch (RSSIquality()) { - case 0: - jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: не подключено к роутеру")); - break; - case 1: - jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: нет сигнала")); - break; - case 2: - jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: очень низкий")); - break; - case 3: - jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: низкий")); - break; - case 4: - jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: хороший")); - break; - case 5: - jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: очень хороший")); - break; - case 6: - jsonWriteStr(configSetupJson, F("signal"), F("Уровень WiFi сигнала: отличный")); - break; - } - }, - nullptr, true); - - - just_load = false; initialized = true; } + + + void loop() { if (!initialized) { return; @@ -154,7 +86,6 @@ void loop() { myButtonIn.loop(); myScenario->loop(); loopCmdExecute(); - //loopSerial(); myNotAsyncActions->loop(); ts.update();