diff --git a/code/espurna/config/general.h b/code/espurna/config/general.h index a790d825..d956b80f 100644 --- a/code/espurna/config/general.h +++ b/code/espurna/config/general.h @@ -537,6 +537,10 @@ #define OTA_PORT 8266 // OTA port #endif +#ifndef OTA_MQTT_SUPPORT +#define OTA_MQTT_SUPPORT 0 // No support by default +#endif + #define OTA_GITHUB_FP "D7:9F:07:61:10:B3:92:93:E3:49:AC:89:84:5B:03:80:C1:9E:2F:8B" // ----------------------------------------------------------------------------- @@ -763,6 +767,7 @@ #define MQTT_TOPIC_SPEED "speed" #define MQTT_TOPIC_IRIN "irin" #define MQTT_TOPIC_IROUT "irout" +#define MQTT_TOPIC_OTA "ota" // Light module #define MQTT_TOPIC_CHANNEL "channel" diff --git a/code/espurna/ota.ino b/code/espurna/ota.ino index c1370d53..4dc015a9 100644 --- a/code/espurna/ota.ino +++ b/code/espurna/ota.ino @@ -158,6 +158,10 @@ void _otaFrom(const char * host, unsigned int port, const char * url) { } void _otaFrom(String url) { + if (!url.startsWith("http://") && !url.startsWith("https://")) { + DEBUG_MSG_P(PSTR("[OTA] Incorrect URL specified\n")); + return; + } // Port from protocol unsigned int port = 80; @@ -197,6 +201,25 @@ void _otaInitCommands() { #endif // TERMINAL_SUPPORT +#if OTA_MQTT_SUPPORT + +void _otaMQTTCallback(unsigned int type, const char * topic, const char * payload) { + if (type == MQTT_CONNECT_EVENT) { + mqttSubscribe(MQTT_TOPIC_OTA); + } + + if (type == MQTT_MESSAGE_EVENT) { + // Match topic + String t = mqttMagnitude((char *) topic); + if (t.equals(MQTT_TOPIC_OTA)) { + DEBUG_MSG_P(PSTR("[OTA] Initiating from URL: %s\n"), payload); + _otaFrom(payload); + } + } +} + +#endif // OTA_MQTT_SUPPORT + // ----------------------------------------------------------------------------- void otaSetup() { @@ -207,6 +230,10 @@ void otaSetup() { _otaInitCommands(); #endif + #if OTA_MQTT_SUPPORT + mqttRegister(_otaMQTTCallback); + #endif + // Main callbacks espurnaRegisterLoop(_otaLoop); espurnaRegisterReload(_otaConfigure);