From dc82075a967702bffd894448f1d3bd614527be30 Mon Sep 17 00:00:00 2001 From: fvanroie Date: Wed, 25 Mar 2020 13:45:43 +0100 Subject: [PATCH] Add checks for HASP_USE_MQTT --- src/hasp_mdns.cpp | 16 +++++-- src/hasp_ota.cpp | 114 ++++++++++++++++++++++++++++------------------ 2 files changed, 80 insertions(+), 50 deletions(-) diff --git a/src/hasp_mdns.cpp b/src/hasp_mdns.cpp index f76f6ab3..943caaf8 100644 --- a/src/hasp_mdns.cpp +++ b/src/hasp_mdns.cpp @@ -12,11 +12,14 @@ #include "hasp_log.h" #include "hasp_mdns.h" -#include "hasp_mqtt.h" #include "hasp_config.h" -uint8_t mdnsEnabled = true; -const float haspVersion = 0.38; +#include "hasp_conf.h" +#if HASP_USE_MQTT +#include "hasp_mqtt.h" +#endif + +uint8_t mdnsEnabled = true; void mdnsSetup(const JsonObject & settings) { @@ -27,7 +30,11 @@ void mdnsSetup(const JsonObject & settings) void mdnsStart() { if(mdnsEnabled) { +#if HASP_USE_MQTT > 0 String hasp2Node = mqttGetNodename(); +#else + String hasp2Node = "unknown"; +#endif // Setup mDNS service discovery if enabled /*if(debugTelnetEnabled) { } @@ -46,8 +53,7 @@ void mdnsStart() addServiceTxt("arduino", "tcp", "tcp_check", "no"); addServiceTxt("arduino", "tcp", "ssh_upload", "no"); addServiceTxt("arduino", "tcp", "board", ARDUINO_BOARD); - addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no"); - */ + addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no");*/ } else { errorPrintln(String(F("MDNS: %sResponder failed to start ")) + hasp2Node); }; diff --git a/src/hasp_ota.cpp b/src/hasp_ota.cpp index 81bca3f2..460eeabf 100644 --- a/src/hasp_ota.cpp +++ b/src/hasp_ota.cpp @@ -6,12 +6,17 @@ #include "hasp_debug.h" #include "hasp_dispatch.h" #include "hasp_ota.h" + +#include "hasp_conf.h" +#if HASP_USE_MQTT #include "hasp_mqtt.h" +#endif #define F_OTA_URL F("otaurl") std::string otaUrl = "http://10.1.0.3"; int8_t otaPrecentageComplete = -1; +int16_t otaPort = 3232; void otaProgress() { @@ -21,7 +26,6 @@ void otaProgress() void otaSetup(JsonObject settings) { - if(!settings[F_OTA_URL].isNull()) { char buffer[128]; otaUrl = settings[F_OTA_URL].as().c_str(); @@ -29,53 +33,73 @@ void otaSetup(JsonObject settings) debugPrintln(buffer); } - ArduinoOTA.setHostname(String(mqttGetNodename()).c_str()); - // ArduinoOTA.setPassword(configPassword); + if(otaPort > 0) { + ArduinoOTA.onStart([]() { + if(ArduinoOTA.getCommand() == U_FLASH) { + } else { // U_SPIFFS + // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() + } - ArduinoOTA.onStart([]() { - if(ArduinoOTA.getCommand() == U_FLASH) { - } else { // U_SPIFFS - // NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() - } + debugPrintln(F("OTA: Start update")); + dispatchCommand("page 0"); + otaPrecentageComplete = 0; + // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\""); + }); + ArduinoOTA.onEnd([]() { + otaPrecentageComplete = 100; + otaProgress(); + otaPrecentageComplete = -1; + dispatchPage("0"); + // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rComplete!\""); + dispatchReboot(true); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + if(total != 0) otaPrecentageComplete = progress * 100 / total; - debugPrintln(F("OTA: Start update")); - dispatchCommand("page 0"); - otaPrecentageComplete = 0; - // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\""); - }); - ArduinoOTA.onEnd([]() { - otaPrecentageComplete = 100; - otaProgress(); - otaPrecentageComplete = -1; - dispatchPage("0"); - // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rComplete!\""); - dispatchReboot(true); - }); - ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { - if(total != 0) otaPrecentageComplete = progress * 100 / total; + // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rProgress: " + String(progress / (total / 100)) + "%\""); + }); + ArduinoOTA.onError([](ota_error_t error) { + otaPrecentageComplete = -1; + debugPrintln(String(F("OTA: ERROR code ")) + String(error)); + if(error == OTA_AUTH_ERROR) + debugPrintln(F("OTA: ERROR - Auth Failed")); + else if(error == OTA_BEGIN_ERROR) + debugPrintln(F("OTA: ERROR - Begin Failed")); + else if(error == OTA_CONNECT_ERROR) + debugPrintln(F("OTA: ERROR - Connect Failed")); + else if(error == OTA_RECEIVE_ERROR) + debugPrintln(F("OTA: ERROR - Receive Failed")); + else if(error == OTA_END_ERROR) + debugPrintln(F("OTA: ERROR - End Failed")); + // haspSetAttr("p[0].b[1].txt", "\"ESP OTA FAILED\""); + delay(5000); + // haspSendCmd("page " + String(nextionActivePage)); + }); - // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rProgress: " + String(progress / (total / 100)) + "%\""); - }); - ArduinoOTA.onError([](ota_error_t error) { - otaPrecentageComplete = -1; - debugPrintln(String(F("OTA: ERROR code ")) + String(error)); - if(error == OTA_AUTH_ERROR) - debugPrintln(F("OTA: ERROR - Auth Failed")); - else if(error == OTA_BEGIN_ERROR) - debugPrintln(F("OTA: ERROR - Begin Failed")); - else if(error == OTA_CONNECT_ERROR) - debugPrintln(F("OTA: ERROR - Connect Failed")); - else if(error == OTA_RECEIVE_ERROR) - debugPrintln(F("OTA: ERROR - Receive Failed")); - else if(error == OTA_END_ERROR) - debugPrintln(F("OTA: ERROR - End Failed")); - // haspSetAttr("p[0].b[1].txt", "\"ESP OTA FAILED\""); - delay(5000); - // haspSendCmd("page " + String(nextionActivePage)); - }); - ArduinoOTA.begin(); - debugPrintln(F("OTA: Over the Air firmware update ready")); - debugPrintln(F("OTA: Setup Complete")); +#if HASP_USE_MQTT > 0 + ArduinoOTA.setHostname(String(mqttGetNodename()).c_str()); +#else + ArduinoOTA.setHostname(String(mqttGetNodename()).c_str()); +#endif + // ArduinoOTA.setPassword(configPassword); + ArduinoOTA.setPort(otaPort); + +#if ESP32 +#if HASP_USE_MDNS > 0 + ArduinoOTA.setMdnsEnabled(true); +#else + ArduinoOTA.setMdnsEnabled(false); +#endif + // ArduinoOTA.setTimeout(1000); +#endif + ArduinoOTA.setRebootOnSuccess(true); + + ArduinoOTA.begin(); + debugPrintln(F("OTA: Over the Air firmware update ready")); + debugPrintln(F("OTA: Setup Complete")); + } else { + debugPrintln(F("OTA: Disabled")); + } } void otaLoop()