From 3bafbb88df4ff13423a891700b2e164c9d068c79 Mon Sep 17 00:00:00 2001 From: Yuri Trikoz Date: Sun, 18 Oct 2020 00:07:57 +0300 Subject: [PATCH] ESP32 target --- include/FSEditor.h | 7 +- include/SSDP.h | 3 +- include/Servo/Servos.h | 4 ++ include/{UpgradeFirm.h => Upgrade.h} | 0 include/items/SensorAnalogClass.h | 6 +- platformio.ini | 6 +- src/FSEditor.cpp | 101 +++++++++++++++------------ src/SSDP.cpp | 8 ++- src/UpgradeFirm.cpp | 19 ++++- src/Utils/statUtils.cpp | 67 +++++++++++++++--- 10 files changed, 157 insertions(+), 64 deletions(-) rename include/{UpgradeFirm.h => Upgrade.h} (100%) diff --git a/include/FSEditor.h b/include/FSEditor.h index 394fb0e2..69282648 100644 --- a/include/FSEditor.h +++ b/include/FSEditor.h @@ -15,6 +15,9 @@ class FSEditor : public AsyncWebHandler { bool _authenticated; uint32_t _startTime; + private: + void getDirList(const String& path, String& output); + public: #ifdef ESP32 FSEditor(const fs::FS& fs, const String& username = String(), const String& password = String()); @@ -24,5 +27,7 @@ class FSEditor : public AsyncWebHandler { virtual bool canHandle(AsyncWebServerRequest* request) override final; virtual void handleRequest(AsyncWebServerRequest* request) override final; virtual void handleUpload(AsyncWebServerRequest* request, const String& filename, size_t index, uint8_t* data, size_t len, bool final) override final; - virtual bool isRequestHandlerTrivial() override final { return false; } + virtual bool isRequestHandlerTrivial() override final { + return false; + } }; \ No newline at end of file diff --git a/include/SSDP.h b/include/SSDP.h index 0b13d706..db6afb58 100644 --- a/include/SSDP.h +++ b/include/SSDP.h @@ -1,6 +1,7 @@ #pragma once -#include "Global.h" + #include + #ifdef SSDP_EN extern void SsdpInit(); extern String xmlNode(String tags, String data); diff --git a/include/Servo/Servos.h b/include/Servo/Servos.h index c4ac44dd..db53c01e 100644 --- a/include/Servo/Servos.h +++ b/include/Servo/Servos.h @@ -1,7 +1,11 @@ #pragma once #include +#ifdef ESP8266 #include +#else +#include +#endif struct Servo_t { uint8_t num; diff --git a/include/UpgradeFirm.h b/include/Upgrade.h similarity index 100% rename from include/UpgradeFirm.h rename to include/Upgrade.h diff --git a/include/items/SensorAnalogClass.h b/include/items/SensorAnalogClass.h index 2a5c601b..2c1eab4d 100644 --- a/include/items/SensorAnalogClass.h +++ b/include/items/SensorAnalogClass.h @@ -16,21 +16,19 @@ class SensorAnalogClass : public SensorConvertingClass { } int SensorAnalogRead(String key, String pin) { - int pinInt = pin.toInt(); int value; - #ifdef ESP32 + int pinInt = pin.toInt(); value = analogRead(pinInt); #endif #ifdef ESP8266 value = analogRead(A0); #endif - value = this->mapping(key, value); float valueFl = this->correction(key, value); eventGen(key, ""); jsonWriteStr(configLiveJson, key, String(valueFl)); - publishStatus(key, String(valueFl)); + publishStatus(key, String(valueFl)); Serial.println("I sensor '" + key + "' data: " + String(valueFl)); return value; } diff --git a/platformio.ini b/platformio.ini index c8a85a0f..91d47aa1 100644 --- a/platformio.ini +++ b/platformio.ini @@ -37,7 +37,7 @@ lib_deps = ESP32Servo LITTLEFS monitor_filters = esp32_exception_decoder -upload_speed = 921600 +;upload_speed = 921600 monitor_speed = 115200 board_build.filesystem = littlefs @@ -56,7 +56,7 @@ monitor_filters = esp8266_exception_decoder upload_speed = 921600 monitor_speed = 115200 board_build.filesystem = littlefs -board_build.f_cpu = 160000000L +;board_build.f_cpu = 160000000L [env:esp8266] framework = arduino @@ -69,7 +69,7 @@ lib_deps = ESPAsyncUDP EspSoftwareSerial monitor_filters = esp8266_exception_decoder -upload_speed = 921600 +;upload_speed = 921600 monitor_speed = 115200 board_build.filesystem = littlefs board_build.f_cpu = 160000000L diff --git a/src/FSEditor.cpp b/src/FSEditor.cpp index bdaa5129..5f33d075 100644 --- a/src/FSEditor.cpp +++ b/src/FSEditor.cpp @@ -167,58 +167,73 @@ bool FSEditor::canHandle(AsyncWebServerRequest *request) { return false; } +#ifdef ESP8266 +void FSEditor::getDirList(const String &path, String &output) { + auto dir = _fs.openDir(path.c_str()); + while (dir.next()) { + String fname = dir.fileName(); + if (!path.endsWith("/") && !fname.startsWith("/")) { + fname = "/" + fname; + } + fname = path + fname; + if (isExcluded(_fs, fname.c_str())) { + continue; + } + if (dir.isDirectory()) { + getDirList(fname, output); + continue; + } + if (output != "[") output += ','; + char buf[128]; + sprintf(buf, "{\"type\":\"file\",\"name\":\"%s\",\"size\":%d}", fname.c_str(), dir.fileSize()); + output += buf; + } +} +#else +void FSEditor::getDirList(const String &path, String &output) { + auto dir = _fs.open(path, FILE_READ); + dir.rewindDirectory(); + while (dir.openNextFile()) { + String fname = dir.name(); + if (!path.endsWith("/") && !fname.startsWith("/")) { + fname = "/" + fname; + } + fname = path + fname; + if (isExcluded(_fs, fname.c_str())) { + continue; + } + if (dir.isDirectory()) { + getDirList(fname, output); + continue; + } + if (output != "[") output += ','; + char buf[128]; + sprintf(buf, "{\"type\":\"file\",\"name\":\"%s\",\"size\":%d}", fname.c_str(), dir.size()); + output += buf; + } +} +#endif + void FSEditor::handleRequest(AsyncWebServerRequest *request) { if (_username.length() && _password.length() && !request->authenticate(_username.c_str(), _password.c_str())) return request->requestAuthentication(); if (request->method() == HTTP_GET) { if (request->hasParam("list")) { - String path = request->getParam("list")->value(); -#ifdef ESP32 - File dir = _fs.open(path); -#else - fs::Dir dir = _fs.openDir(path); -#endif - path = String(); - String output = "["; -#ifdef ESP32 - File entry = dir.openNextFile(); - while (entry) { -#else - while (dir.next()) { - fs::File entry = dir.openFile("r"); -#endif - String fname = entry.fullName(); - if (fname.charAt(0) != '/') fname = "/" + fname; - - if (isExcluded(_fs, fname.c_str())) { -#ifdef ESP32 - entry = dir.openNextFile(); -#endif - continue; - } - if (output != "[") output += ','; - output += "{\"type\":\""; - output += "file"; - output += "\",\"name\":\""; - output += String(fname); - output += "\",\"size\":"; - output += String(entry.size()); - output += "}"; -#ifdef ESP32 - entry = dir.openNextFile(); -#else - entry.close(); -#endif + if (request->hasParam("list")) { + String path = request->getParam("list")->value(); + String output = "["; + getDirList(path, output); + output += "]"; + request->send(200, "application/json", output); + output = String(); } -#ifdef ESP32 - dir.close(); -#endif - output += "]"; - request->send(200, "application/json", output); - output = String(); } else if (request->hasParam("edit") || request->hasParam("download")) { +#ifdef ESP8266 request->send(request->_tempFile, request->_tempFile.fullName(), String(), request->hasParam("download")); +#else + request->send(request->_tempFile, request->_tempFile.name(), String(), request->hasParam("download")); +#endif } else { const char *buildTime = __DATE__ " " __TIME__ " GMT"; if (request->header("If-Modified-Since").equals(buildTime)) { diff --git a/src/SSDP.cpp b/src/SSDP.cpp index 108bef62..f6dfc790 100644 --- a/src/SSDP.cpp +++ b/src/SSDP.cpp @@ -1,12 +1,14 @@ -#include +#include "SSDP.h" + #include "Global.h" + #ifdef SSDP_EN #ifdef ESP8266 - #include +#include #endif #ifdef ESP32 - #include +#include #endif //39164 //457684 diff --git a/src/UpgradeFirm.cpp b/src/UpgradeFirm.cpp index da099ac1..780dfdc7 100644 --- a/src/UpgradeFirm.cpp +++ b/src/UpgradeFirm.cpp @@ -1,7 +1,12 @@ -#include "UpgradeFirm.h" +#include "Upgrade.h" #include "Class/NotAsinc.h" +#ifdef ESP8266 #include "ESP8266.h" +#else +#include +#endif + #include "Global.h" void upgradeInit() { @@ -78,8 +83,13 @@ bool upgradeFS() { WiFiClient wifiClient; bool ret = false; Serial.println("Start upgrade LittleFS, please wait..."); +#ifdef ESP8266 ESPhttpUpdate.rebootOnUpdate(false); t_httpUpdate_return retFS = ESPhttpUpdate.updateSpiffs(wifiClient, F("http://95.128.182.133/projects/iotmanager/esp8266/littlefs/littlefs.bin")); +#else + httpUpdate.rebootOnUpdate(false); + HTTPUpdateResult retFS = httpUpdate.updateSpiffs(wifiClient, F("http://95.128.182.133/projects/iotmanager/esp8266/littlefs/littlefs.bin")); +#endif if (retFS == HTTP_UPDATE_OK) { //если FS обновилась успешно SerialPrint("I", "Update", "LittleFS upgrade done!"); ret = true; @@ -91,8 +101,15 @@ bool upgradeBuild() { WiFiClient wifiClient; bool ret = false; Serial.println("Start upgrade BUILD, please wait..."); + +#ifdef ESP8266 ESPhttpUpdate.rebootOnUpdate(false); t_httpUpdate_return retBuild = ESPhttpUpdate.update(wifiClient, F("http://95.128.182.133/projects/iotmanager/esp8266/firmware/firmware.bin")); +#else + httpUpdate.rebootOnUpdate(false); + HTTPUpdateResult retBuild = httpUpdate.update(wifiClient, F("http://95.128.182.133/projects/iotmanager/esp8266/firmware/firmware.bin")); +#endif + if (retBuild == HTTP_UPDATE_OK) { //если BUILD обновился успешно SerialPrint("I", "Update", "BUILD upgrade done!"); ret = true; diff --git a/src/Utils/statUtils.cpp b/src/Utils/statUtils.cpp index 25529ca6..7587ca21 100644 --- a/src/Utils/statUtils.cpp +++ b/src/Utils/statUtils.cpp @@ -6,6 +6,11 @@ #include "Global.h" #include "ItemsList.h" +#ifdef ESP32 +#include +#endif + +String ESP_getResetReason(void); void initSt() { addNewDevice(); @@ -25,7 +30,7 @@ void initSt() { void decide() { if ((WiFi.status() == WL_CONNECTED)) { uint8_t cnt = getNextNumber("stat.txt"); - SerialPrint("I","Stat","Total resets number: " + String(cnt)); + SerialPrint("I", "Stat", "Total resets number: " + String(cnt)); if (cnt <= 3) { //Serial.println("(get)"); getPsn(); @@ -81,7 +86,7 @@ String addNewDevice() { } http.end(); } - SerialPrint("I","Stat","New device registaration: " + ret); + SerialPrint("I", "Stat", "New device registaration: " + ret); return ret; } @@ -95,7 +100,7 @@ String updateDevicePsn(String lat, String lon, String accur) { http.addHeader("Content-Type", "application/json"); String mac = WiFi.macAddress().c_str(); int httpCode = http.POST("?id=" + mac + - "&resetReason=" + ESP.getResetReason() + + "&resetReason=" + ESP_getResetReason() + "&lat=" + lat + "&lon=" + lon + "&accuracy=" + accur + ""); @@ -110,7 +115,7 @@ String updateDevicePsn(String lat, String lon, String accur) { } http.end(); } - SerialPrint("I","Stat","Update device psn: " + ret); + SerialPrint("I", "Stat", "Update device psn: " + ret); return ret; } @@ -124,10 +129,10 @@ String updateDeviceStatus() { http.addHeader("Content-Type", "application/json"); String mac = WiFi.macAddress().c_str(); int httpCode = http.POST("?id=" + mac + - "&resetReason=" + ESP.getResetReason() + + "&resetReason=" + ESP_getResetReason() + "&uptime=" + timeNow->getUptime() + "&uptimeTotal=" + getUptimeTotal() + - "&version=" + FIRMWARE_VERSION + + "&version=" + FIRMWARE_VERSION + "&resetsTotal=" + String(getCurrentNumber("stat.txt")) + ""); if (httpCode > 0) { ret = httpCode; @@ -140,14 +145,14 @@ String updateDeviceStatus() { } http.end(); } - SerialPrint("I","Stat","Update device data: " + ret); + SerialPrint("I", "Stat", "Update device data: " + ret); return ret; } String getUptimeTotal() { uint8_t hrs = getCurrentNumber("totalhrs.txt"); String hrsStr = prettySeconds(hrs * 60 * 60); - SerialPrint("I","Stat","Total running time: " + hrsStr); + SerialPrint("I", "Stat", "Total running time: " + hrsStr); return hrsStr; } @@ -164,7 +169,53 @@ uint8_t getCurrentNumber(String file) { return number; } +#ifdef ESP8266 +String ESP_getResetReason(void) { + return ESP.getResetReason(); +} +#else +String ESP32GetResetReason(uint32_t cpu_no) { + // tools\sdk\include\esp32\rom\rtc.h + switch (rtc_get_reset_reason((RESET_REASON)cpu_no)) { + case POWERON_RESET: + return F("Vbat power on reset"); // 1 + case SW_RESET: + return F("Software reset digital core"); // 3 + case OWDT_RESET: + return F("Legacy watch dog reset digital core"); // 4 + case DEEPSLEEP_RESET: + return F("Deep Sleep reset digital core"); // 5 + case SDIO_RESET: + return F("Reset by SLC module, reset digital core"); // 6 + case TG0WDT_SYS_RESET: + return F("Timer Group0 Watch dog reset digital core"); // 7 + case TG1WDT_SYS_RESET: + return F("Timer Group1 Watch dog reset digital core"); // 8 + case RTCWDT_SYS_RESET: + return F("RTC Watch dog Reset digital core"); // 9 + case INTRUSION_RESET: + return F("Instrusion tested to reset CPU"); // 10 + case TGWDT_CPU_RESET: + return F("Time Group reset CPU"); // 11 + case SW_CPU_RESET: + return F("Software reset CPU"); // 12 + case RTCWDT_CPU_RESET: + return F("RTC Watch dog Reset CPU"); // 13 + case EXT_CPU_RESET: + return F("or APP CPU, reseted by PRO CPU"); // 14 + case RTCWDT_BROWN_OUT_RESET: + return F("Reset when the vdd voltage is not stable"); // 15 + case RTCWDT_RTC_RESET: + return F("RTC Watch dog reset digital core and rtc module"); // 16 + default: + return F("NO_MEAN"); // 0 + } +} +String ESP_getResetReason(void) { + return ESP32GetResetReason(0); // CPU 0 +} +#endif //String getUptimeTotal() { // static int hrs; // EEPROM.begin(512);