From 617caf345b9be8f6e96020388e8afdcf4dd9325f Mon Sep 17 00:00:00 2001 From: gorec2005 Date: Sun, 28 Jun 2020 11:08:28 +0400 Subject: [PATCH 1/7] Replace SPIFFS with LittleFS --- src/Homie/Boot/BootConfig.cpp | 6 ++--- src/Homie/Boot/BootConfig.hpp | 3 ++- src/Homie/Config.cpp | 48 +++++++++++++++++------------------ src/Homie/Config.hpp | 8 +++--- 4 files changed, 33 insertions(+), 32 deletions(-) diff --git a/src/Homie/Boot/BootConfig.cpp b/src/Homie/Boot/BootConfig.cpp index 6e39152f..21e9dab8 100644 --- a/src/Homie/Boot/BootConfig.cpp +++ b/src/Homie/Boot/BootConfig.cpp @@ -285,15 +285,15 @@ void BootConfig::_onCaptivePortal(AsyncWebServerRequest *request) { Interface::get().getLogger() << F("Proxy") << endl; _proxyHttpRequest(request); } - } else if (request->url() == "/" && !SPIFFS.exists(CONFIG_UI_BUNDLE_PATH)) { + } else if (request->url() == "/" && !LittleFS.exists(CONFIG_UI_BUNDLE_PATH)) { // UI File not found String msg = String(F("UI bundle not loaded. See Configuration API usage: http://homieiot.github.io/homie-esp8266")); Interface::get().getLogger() << msg << endl; request->send(404, F("text/plain"), msg); - } else if (request->url() == "/" && SPIFFS.exists(CONFIG_UI_BUNDLE_PATH)) { + } else if (request->url() == "/" && LittleFS.exists(CONFIG_UI_BUNDLE_PATH)) { // Respond with UI Interface::get().getLogger() << F("UI bundle found") << endl; - AsyncWebServerResponse *response = request->beginResponse(SPIFFS.open(CONFIG_UI_BUNDLE_PATH, "r"), F("index.html"), F("text/html")); + AsyncWebServerResponse *response = request->beginResponse(LittleFS.open(CONFIG_UI_BUNDLE_PATH, "r"), F("index.html"), F("text/html")); request->send(response); } else { // Faild to find request diff --git a/src/Homie/Boot/BootConfig.hpp b/src/Homie/Boot/BootConfig.hpp index 552ab194..b708765f 100644 --- a/src/Homie/Boot/BootConfig.hpp +++ b/src/Homie/Boot/BootConfig.hpp @@ -1,6 +1,7 @@ #pragma once #include "Arduino.h" +#include #include "../Constants.hpp" #if HOMIE_CONFIG @@ -10,7 +11,7 @@ #include #include #include -#include +#include #elif defined(ESP8266) #include #include diff --git a/src/Homie/Config.cpp b/src/Homie/Config.cpp index da37f6d6..c10edd7a 100644 --- a/src/Homie/Config.cpp +++ b/src/Homie/Config.cpp @@ -4,34 +4,34 @@ using namespace HomieInternals; Config::Config() : _configStruct() - , _spiffsBegan(false) + , _littlefsBegan(false) , _valid(false) { } -bool Config::_spiffsBegin() { - if (!_spiffsBegan) { +bool Config::_littlefsBegin() { + if (!_littlefsBegan) { #ifdef ESP32 - _spiffsBegan = SPIFFS.begin(true); + _littlefsBegan = LittleFS.begin(true); #elif defined(ESP8266) - _spiffsBegan = SPIFFS.begin(); + _littlefsBegan = LittleFS.begin(); #endif - if (!_spiffsBegan) Interface::get().getLogger() << F("✖ Cannot mount filesystem") << endl; + if (!_littlefsBegan) Interface::get().getLogger() << F("✖ Cannot mount filesystem") << endl; } - return _spiffsBegan; + return _littlefsBegan; } bool Config::load() { - if (!_spiffsBegin()) { return false; } + if (!_littlefsBegin()) { return false; } _valid = false; - if (!SPIFFS.exists(CONFIG_FILE_PATH)) { + if (!LittleFS.exists(CONFIG_FILE_PATH)) { Interface::get().getLogger() << F("✖ ") << CONFIG_FILE_PATH << F(" doesn't exist") << endl; return false; } - File configFile = SPIFFS.open(CONFIG_FILE_PATH, "r"); + File configFile = LittleFS.open(CONFIG_FILE_PATH, "r"); if (!configFile) { Interface::get().getLogger() << F("✖ Cannot open config file") << endl; return false; @@ -148,7 +148,7 @@ bool Config::load() { } char* Config::getSafeConfigFile() const { - File configFile = SPIFFS.open(CONFIG_FILE_PATH, "r"); + File configFile = LittleFS.open(CONFIG_FILE_PATH, "r"); size_t configSize = configFile.size(); char buf[MAX_JSON_CONFIG_FILE_SIZE]; @@ -170,19 +170,19 @@ char* Config::getSafeConfigFile() const { } void Config::erase() { - if (!_spiffsBegin()) { return; } + if (!_littlefsBegin()) { return; } - SPIFFS.remove(CONFIG_FILE_PATH); - SPIFFS.remove(CONFIG_NEXT_BOOT_MODE_FILE_PATH); + LittleFS.remove(CONFIG_FILE_PATH); + LittleFS.remove(CONFIG_NEXT_BOOT_MODE_FILE_PATH); } void Config::setHomieBootModeOnNextBoot(HomieBootMode bootMode) { - if (!_spiffsBegin()) { return; } + if (!_littlefsBegin()) { return; } if (bootMode == HomieBootMode::UNDEFINED) { - SPIFFS.remove(CONFIG_NEXT_BOOT_MODE_FILE_PATH); + LittleFS.remove(CONFIG_NEXT_BOOT_MODE_FILE_PATH); } else { - File bootModeFile = SPIFFS.open(CONFIG_NEXT_BOOT_MODE_FILE_PATH, "w"); + File bootModeFile = LittleFS.open(CONFIG_NEXT_BOOT_MODE_FILE_PATH, "w"); if (!bootModeFile) { Interface::get().getLogger() << F("✖ Cannot open NEXTMODE file") << endl; return; @@ -195,9 +195,9 @@ void Config::setHomieBootModeOnNextBoot(HomieBootMode bootMode) { } HomieBootMode Config::getHomieBootModeOnNextBoot() { - if (!_spiffsBegin()) { return HomieBootMode::UNDEFINED; } + if (!_littlefsBegin()) { return HomieBootMode::UNDEFINED; } - File bootModeFile = SPIFFS.open(CONFIG_NEXT_BOOT_MODE_FILE_PATH, "r"); + File bootModeFile = LittleFS.open(CONFIG_NEXT_BOOT_MODE_FILE_PATH, "r"); if (bootModeFile) { int v = bootModeFile.parseInt(); bootModeFile.close(); @@ -208,11 +208,11 @@ HomieBootMode Config::getHomieBootModeOnNextBoot() { } void Config::write(const JsonObject config) { - if (!_spiffsBegin()) { return; } + if (!_littlefsBegin()) { return; } - SPIFFS.remove(CONFIG_FILE_PATH); + LittleFS.remove(CONFIG_FILE_PATH); - File configFile = SPIFFS.open(CONFIG_FILE_PATH, "w"); + File configFile = LittleFS.open(CONFIG_FILE_PATH, "w"); if (!configFile) { Interface::get().getLogger() << F("✖ Cannot open config file") << endl; return; @@ -222,7 +222,7 @@ void Config::write(const JsonObject config) { } bool Config::patch(const char* patch) { - if (!_spiffsBegin()) { return false; } + if (!_littlefsBegin()) { return false; } StaticJsonDocument patchJsonDoc; @@ -232,7 +232,7 @@ bool Config::patch(const char* patch) { } JsonObject patchObject = patchJsonDoc.as(); - File configFile = SPIFFS.open(CONFIG_FILE_PATH, "r"); + File configFile = LittleFS.open(CONFIG_FILE_PATH, "r"); if (!configFile) { Interface::get().getLogger() << F("✖ Cannot open config file") << endl; return false; diff --git a/src/Homie/Config.hpp b/src/Homie/Config.hpp index 437f08ea..17702c64 100644 --- a/src/Homie/Config.hpp +++ b/src/Homie/Config.hpp @@ -1,10 +1,10 @@ #pragma once #include "Arduino.h" - +#include #include #ifdef ESP32 -#include +#include #endif // ESP32 #include "FS.h" #include "Datatypes/Interface.hpp" @@ -34,10 +34,10 @@ class Config { private: ConfigStruct _configStruct; - bool _spiffsBegan; + bool _littlefsBegan; bool _valid; - bool _spiffsBegin(); + bool _littlefsBegin(); void _patchJsonObject(JsonObject object, JsonObject patch); }; From ba08a9704cf1880131c19e57a55a9c23290f3820 Mon Sep 17 00:00:00 2001 From: gorec2005 Date: Sun, 28 Jun 2020 11:21:24 +0400 Subject: [PATCH 2/7] fix overincludes --- src/Homie/Boot/BootConfig.hpp | 1 - src/Homie/Config.hpp | 3 --- 2 files changed, 4 deletions(-) diff --git a/src/Homie/Boot/BootConfig.hpp b/src/Homie/Boot/BootConfig.hpp index b708765f..c7b0749b 100644 --- a/src/Homie/Boot/BootConfig.hpp +++ b/src/Homie/Boot/BootConfig.hpp @@ -11,7 +11,6 @@ #include #include #include -#include #elif defined(ESP8266) #include #include diff --git a/src/Homie/Config.hpp b/src/Homie/Config.hpp index 17702c64..8dbe3be5 100644 --- a/src/Homie/Config.hpp +++ b/src/Homie/Config.hpp @@ -3,9 +3,6 @@ #include "Arduino.h" #include #include -#ifdef ESP32 -#include -#endif // ESP32 #include "FS.h" #include "Datatypes/Interface.hpp" #include "Datatypes/ConfigStruct.hpp" From 4248fe934402cb0370dcd72cd02b100a93caa7f4 Mon Sep 17 00:00:00 2001 From: gorec2005 Date: Tue, 29 Sep 2020 09:38:46 +0400 Subject: [PATCH 3/7] comment unused variables --- src/Homie/Config.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Homie/Config.cpp b/src/Homie/Config.cpp index c10edd7a..e6e0c845 100644 --- a/src/Homie/Config.cpp +++ b/src/Homie/Config.cpp @@ -85,11 +85,11 @@ bool Config::load() { const char* reqWifiDns2 = reqWifi["dns2"] | ""; uint16_t reqMqttPort = reqMqtt["port"] | DEFAULT_MQTT_PORT; - bool reqMqttSsl = reqMqtt["ssl"] | false; +// bool reqMqttSsl = reqMqtt["ssl"] | false; bool reqMqttAuth = reqMqtt["auth"] | false; const char* reqMqttUsername = reqMqtt["username"] | ""; const char* reqMqttPassword = reqMqtt["password"] | ""; - const char* reqMqttFingerprint = reqMqtt["ssl_fingerprint"] | ""; +// const char* reqMqttFingerprint = reqMqtt["ssl_fingerprint"] | ""; const char* reqMqttBaseTopic = reqMqtt["base_topic"] | DEFAULT_MQTT_BASE_TOPIC; strlcpy(_configStruct.name, reqName, MAX_FRIENDLY_NAME_LENGTH); From deec2b10311d6c55232e782f4794f36a9efb8883 Mon Sep 17 00:00:00 2001 From: gorec2005 Date: Tue, 29 Sep 2020 20:24:56 +0400 Subject: [PATCH 4/7] fix loop disconnected --- src/Homie/Boot/BootNormal.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index 49622c23..fb13b4eb 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -123,7 +123,7 @@ void BootNormal::loop() { } for (HomieNode* iNode : HomieNode::nodes) { - if (iNode->runLoopDisconnected || (Interface::get().getMqttClient().connected()) && _mqttConnectNotified ) iNode->loop(); + if (iNode->runLoopDisconnected || (Interface::get().getMqttClient().connected() && _mqttConnectNotified) ) iNode->loop(); } if (_mqttReconnectTimer.check()) { _mqttConnect(); From cf806317454301b13416751cf0bd9f859cbf3f78 Mon Sep 17 00:00:00 2001 From: gorec2005 Date: Thu, 10 Dec 2020 23:55:09 +0400 Subject: [PATCH 5/7] avoid duplicate Wifi.begin() by relying on WiFi.setAutoConnect(), only try MQTT connect if WiFi is connected for ESP8266 from matzrh/homie-esp8266 --- src/Homie/Boot/BootNormal.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index fb13b4eb..3fb7a608 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -385,12 +385,17 @@ void BootNormal::_onWifiDisconnected(const WiFiEventStationModeDisconnected& eve Interface::get().event.wifiReason = event.reason; Interface::get().eventHandler(Interface::get().event); - _wifiConnect(); +// _wifiConnect(); } #endif // ESP32 void BootNormal::_mqttConnect() { if (!Interface::get().disable) { + bool fence=!Interface::get().disable;; +#if defined(ESP8266) + fence &= WiFi.isConnected(); +#endif // ESP32 + if (fence) { if (Interface::get().led.enabled) Interface::get().getBlinker().start(LED_MQTT_DELAY); _mqttConnectNotified = false; Interface::get().getLogger() << F("↕ Attempting to connect to MQTT...") << endl; From 151e883c17f69662a883fa8ce57394d6844dba04 Mon Sep 17 00:00:00 2001 From: gorec2005 Date: Fri, 11 Dec 2020 01:20:56 +0400 Subject: [PATCH 6/7] Merge remote-tracking branch 'homieiot/develop' into develop --- scripts/ota_updater/ota_updater.py | 35 ++++++++++++++++++++++++------ 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/scripts/ota_updater/ota_updater.py b/scripts/ota_updater/ota_updater.py index a8c13fc5..bef659d7 100755 --- a/scripts/ota_updater/ota_updater.py +++ b/scripts/ota_updater/ota_updater.py @@ -5,6 +5,9 @@ import base64, sys, math from hashlib import md5 +# Global variable for total bytes to transfer +total = 0 + # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): if rc != 0: @@ -19,9 +22,19 @@ def on_connect(client, userdata, flags, rc): print("Waiting for device to come online...") +# Called from on_message to print a progress bar +def on_progress(progress, total): + g_total = total + bar_width = 30 + bar = int(bar_width*(progress/total)) + print("\r[", '+'*bar, ' '*(bar_width-bar), "] ", progress, "/", total, end='', sep='') + if (progress == total): + print() + sys.stdout.flush() # The callback for when a PUBLISH message is received from the server. def on_message(client, userdata, msg): + global total # decode string for python2/3 compatiblity msg.payload = msg.payload.decode() @@ -29,21 +42,27 @@ def on_message(client, userdata, msg): status = int(msg.payload.split()[0]) if userdata.get("published"): - if status == 206: # in progress + if status == 200: + on_progress(total, total) + print("Firmware uploaded successfully. Waiting for device to come back online.") + sys.stdout.flush() + elif status == 202: + print("Checksum accepted") + elif status == 206: # in progress # state in progress, print progress bar progress, total = [int(x) for x in msg.payload.split()[1].split('/')] - bar_width = 30 - bar = int(bar_width*(progress/total)) - print("\r[", '+'*bar, ' '*(bar_width-bar), "] ", msg.payload.split()[1], end='', sep='') - if (progress == total): - print() - sys.stdout.flush() + on_progress(progress, total) elif status == 304: # not modified print("Device firmware already up to date with md5 checksum: {}".format(userdata.get('md5'))) client.disconnect() elif status == 403: # forbidden print("Device ota disabled, aborting...") client.disconnect() + elif (status > 300) and (status < 500): + print("Other error '" + msg.payload + "', aborting...") + client.disconnect() + else: + print("Other error '" + msg.payload + "'") elif msg.topic.endswith('$fw/checksum'): checksum = msg.payload @@ -127,6 +146,8 @@ def main(broker_host, broker_port, broker_username, broker_password, broker_ca_c if __name__ == '__main__': import argparse + print (sys.argv[1:]) + parser = argparse.ArgumentParser( description='ota firmware update scirpt for ESP8226 implemenation of the Homie mqtt IoT convention.') From db306b021dde441ec34033f795f6760c0e672224 Mon Sep 17 00:00:00 2001 From: gorec2005 Date: Fri, 11 Dec 2020 01:53:03 +0400 Subject: [PATCH 7/7] fix error "lost {" --- src/Homie/Boot/BootNormal.cpp | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/Homie/Boot/BootNormal.cpp b/src/Homie/Boot/BootNormal.cpp index 3fb7a608..44bbe5b5 100644 --- a/src/Homie/Boot/BootNormal.cpp +++ b/src/Homie/Boot/BootNormal.cpp @@ -395,11 +395,12 @@ void BootNormal::_mqttConnect() { #if defined(ESP8266) fence &= WiFi.isConnected(); #endif // ESP32 - if (fence) { - if (Interface::get().led.enabled) Interface::get().getBlinker().start(LED_MQTT_DELAY); - _mqttConnectNotified = false; - Interface::get().getLogger() << F("↕ Attempting to connect to MQTT...") << endl; - Interface::get().getMqttClient().connect(); + if (fence) { + if (Interface::get().led.enabled) Interface::get().getBlinker().start(LED_MQTT_DELAY); + _mqttConnectNotified = false; + Interface::get().getLogger() << F("↕ Attempting to connect to MQTT...") << endl; + Interface::get().getMqttClient().connect(); + } } }