diff --git a/Arduino/McLighting/McLighting.ino b/Arduino/McLighting/McLighting.ino index c65fb64..62c2037 100644 --- a/Arduino/McLighting/McLighting.ino +++ b/Arduino/McLighting/McLighting.ino @@ -17,6 +17,11 @@ #include //https://github.com/Links2004/arduinoWebSockets #include +// OTA +#ifdef ENABLE_OTA + #include + #include +#endif // *************************************************************************** // Instanciate HTTP(80) / WebSockets(81) Server @@ -147,6 +152,48 @@ void setup() { digitalWrite(BUILTIN_LED, LOW); + // *************************************************************************** + // Configure OTA + // *************************************************************************** + #ifdef ENABLE_OTA + DBG_OUTPUT_PORT.println("Arduino OTA activated."); + + // Port defaults to 8266 + ArduinoOTA.setPort(8266); + + // Hostname defaults to esp8266-[ChipID] + ArduinoOTA.setHostname(HOSTNAME); + + // No authentication by default + // ArduinoOTA.setPassword("admin"); + + // Password can be set with it's md5 value as well + // MD5(admin) = 21232f297a57a5a743894a0e4a801fc3 + // ArduinoOTA.setPasswordHash("21232f297a57a5a743894a0e4a801fc3"); + + ArduinoOTA.onStart([]() { + DBG_OUTPUT_PORT.println("Arduino OTA: Start updating"); + }); + ArduinoOTA.onEnd([]() { + DBG_OUTPUT_PORT.println("Arduino OTA: End"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + DBG_OUTPUT_PORT.printf("Arduino OTA Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + DBG_OUTPUT_PORT.printf("Arduino OTA Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) DBG_OUTPUT_PORT.println("Arduino OTA: Auth Failed"); + else if (error == OTA_BEGIN_ERROR) DBG_OUTPUT_PORT.println("Arduino OTA: Begin Failed"); + else if (error == OTA_CONNECT_ERROR) DBG_OUTPUT_PORT.println("Arduino OTA: Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) DBG_OUTPUT_PORT.println("Arduino OTA: Receive Failed"); + else if (error == OTA_END_ERROR) DBG_OUTPUT_PORT.println("Arduino OTA: End Failed"); + }); + + ArduinoOTA.begin(); + DBG_OUTPUT_PORT.println(""); + #endif + + // *************************************************************************** // Setup: MDNS responder // *************************************************************************** @@ -356,9 +403,13 @@ void setup() { server.begin(); } + void loop() { server.handleClient(); webSocket.loop(); + #ifdef ENABLE_OTA + ArduinoOTA.handle(); + #endif // Simple statemachine that handles the different modes if (mode == SET_MODE) { diff --git a/Arduino/McLighting/definitions.h b/Arduino/McLighting/definitions.h index 18bb514..85e3923 100644 --- a/Arduino/McLighting/definitions.h +++ b/Arduino/McLighting/definitions.h @@ -5,6 +5,8 @@ #define HOSTNAME "ESP8266_02" // Friedly hostname +// #define ENABLE_OTA // If defined, enable Arduino OTA code. + // *************************************************************************** // Global variables / definitions