Add checks for HASP_USE_MQTT

This commit is contained in:
fvanroie 2020-03-25 13:45:43 +01:00
parent 3a965b18e5
commit dc82075a96
2 changed files with 80 additions and 50 deletions

View File

@ -12,11 +12,14 @@
#include "hasp_log.h" #include "hasp_log.h"
#include "hasp_mdns.h" #include "hasp_mdns.h"
#include "hasp_mqtt.h"
#include "hasp_config.h" #include "hasp_config.h"
uint8_t mdnsEnabled = true; #include "hasp_conf.h"
const float haspVersion = 0.38; #if HASP_USE_MQTT
#include "hasp_mqtt.h"
#endif
uint8_t mdnsEnabled = true;
void mdnsSetup(const JsonObject & settings) void mdnsSetup(const JsonObject & settings)
{ {
@ -27,7 +30,11 @@ void mdnsSetup(const JsonObject & settings)
void mdnsStart() void mdnsStart()
{ {
if(mdnsEnabled) { if(mdnsEnabled) {
#if HASP_USE_MQTT > 0
String hasp2Node = mqttGetNodename(); String hasp2Node = mqttGetNodename();
#else
String hasp2Node = "unknown";
#endif
// Setup mDNS service discovery if enabled // Setup mDNS service discovery if enabled
/*if(debugTelnetEnabled) { /*if(debugTelnetEnabled) {
} }
@ -46,8 +53,7 @@ void mdnsStart()
addServiceTxt("arduino", "tcp", "tcp_check", "no"); addServiceTxt("arduino", "tcp", "tcp_check", "no");
addServiceTxt("arduino", "tcp", "ssh_upload", "no"); addServiceTxt("arduino", "tcp", "ssh_upload", "no");
addServiceTxt("arduino", "tcp", "board", ARDUINO_BOARD); addServiceTxt("arduino", "tcp", "board", ARDUINO_BOARD);
addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no"); addServiceTxt("arduino", "tcp", "auth_upload", (auth) ? "yes" : "no");*/
*/
} else { } else {
errorPrintln(String(F("MDNS: %sResponder failed to start ")) + hasp2Node); errorPrintln(String(F("MDNS: %sResponder failed to start ")) + hasp2Node);
}; };

View File

@ -6,12 +6,17 @@
#include "hasp_debug.h" #include "hasp_debug.h"
#include "hasp_dispatch.h" #include "hasp_dispatch.h"
#include "hasp_ota.h" #include "hasp_ota.h"
#include "hasp_conf.h"
#if HASP_USE_MQTT
#include "hasp_mqtt.h" #include "hasp_mqtt.h"
#endif
#define F_OTA_URL F("otaurl") #define F_OTA_URL F("otaurl")
std::string otaUrl = "http://10.1.0.3"; std::string otaUrl = "http://10.1.0.3";
int8_t otaPrecentageComplete = -1; int8_t otaPrecentageComplete = -1;
int16_t otaPort = 3232;
void otaProgress() void otaProgress()
{ {
@ -21,7 +26,6 @@ void otaProgress()
void otaSetup(JsonObject settings) void otaSetup(JsonObject settings)
{ {
if(!settings[F_OTA_URL].isNull()) { if(!settings[F_OTA_URL].isNull()) {
char buffer[128]; char buffer[128];
otaUrl = settings[F_OTA_URL].as<String>().c_str(); otaUrl = settings[F_OTA_URL].as<String>().c_str();
@ -29,53 +33,73 @@ void otaSetup(JsonObject settings)
debugPrintln(buffer); debugPrintln(buffer);
} }
ArduinoOTA.setHostname(String(mqttGetNodename()).c_str()); if(otaPort > 0) {
// ArduinoOTA.setPassword(configPassword); 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([]() { debugPrintln(F("OTA: Start update"));
if(ArduinoOTA.getCommand() == U_FLASH) { dispatchCommand("page 0");
} else { // U_SPIFFS otaPrecentageComplete = 0;
// NOTE: if updating SPIFFS this would be the place to unmount SPIFFS using SPIFFS.end() // 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")); // haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rProgress: " + String(progress / (total / 100)) + "%\"");
dispatchCommand("page 0"); });
otaPrecentageComplete = 0; ArduinoOTA.onError([](ota_error_t error) {
// haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\""); otaPrecentageComplete = -1;
}); debugPrintln(String(F("OTA: ERROR code ")) + String(error));
ArduinoOTA.onEnd([]() { if(error == OTA_AUTH_ERROR)
otaPrecentageComplete = 100; debugPrintln(F("OTA: ERROR - Auth Failed"));
otaProgress(); else if(error == OTA_BEGIN_ERROR)
otaPrecentageComplete = -1; debugPrintln(F("OTA: ERROR - Begin Failed"));
dispatchPage("0"); else if(error == OTA_CONNECT_ERROR)
// haspSetAttr("p[0].b[1].txt", "\"ESP OTA Update\\rComplete!\""); debugPrintln(F("OTA: ERROR - Connect Failed"));
dispatchReboot(true); else if(error == OTA_RECEIVE_ERROR)
}); debugPrintln(F("OTA: ERROR - Receive Failed"));
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { else if(error == OTA_END_ERROR)
if(total != 0) otaPrecentageComplete = progress * 100 / total; 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)) + "%\""); #if HASP_USE_MQTT > 0
}); ArduinoOTA.setHostname(String(mqttGetNodename()).c_str());
ArduinoOTA.onError([](ota_error_t error) { #else
otaPrecentageComplete = -1; ArduinoOTA.setHostname(String(mqttGetNodename()).c_str());
debugPrintln(String(F("OTA: ERROR code ")) + String(error)); #endif
if(error == OTA_AUTH_ERROR) // ArduinoOTA.setPassword(configPassword);
debugPrintln(F("OTA: ERROR - Auth Failed")); ArduinoOTA.setPort(otaPort);
else if(error == OTA_BEGIN_ERROR)
debugPrintln(F("OTA: ERROR - Begin Failed")); #if ESP32
else if(error == OTA_CONNECT_ERROR) #if HASP_USE_MDNS > 0
debugPrintln(F("OTA: ERROR - Connect Failed")); ArduinoOTA.setMdnsEnabled(true);
else if(error == OTA_RECEIVE_ERROR) #else
debugPrintln(F("OTA: ERROR - Receive Failed")); ArduinoOTA.setMdnsEnabled(false);
else if(error == OTA_END_ERROR) #endif
debugPrintln(F("OTA: ERROR - End Failed")); // ArduinoOTA.setTimeout(1000);
// haspSetAttr("p[0].b[1].txt", "\"ESP OTA FAILED\""); #endif
delay(5000); ArduinoOTA.setRebootOnSuccess(true);
// haspSendCmd("page " + String(nextionActivePage));
}); ArduinoOTA.begin();
ArduinoOTA.begin(); debugPrintln(F("OTA: Over the Air firmware update ready"));
debugPrintln(F("OTA: Over the Air firmware update ready")); debugPrintln(F("OTA: Setup Complete"));
debugPrintln(F("OTA: Setup Complete")); } else {
debugPrintln(F("OTA: Disabled"));
}
} }
void otaLoop() void otaLoop()