From a39f4ea8186e93705ecc047c4083ec63ebc7e302 Mon Sep 17 00:00:00 2001 From: kuba Date: Sat, 8 Feb 2020 16:45:14 +0100 Subject: [PATCH] Arduino OTA integration --- README.md | 3 ++ mqtt_esp8266_light/config-sample.h | 4 +++ mqtt_esp8266_light/mqtt_esp8266_light.ino | 12 +++++++ mqtt_esp8266_light/ota.h | 39 +++++++++++++++++++++++ 4 files changed, 58 insertions(+) create mode 100644 mqtt_esp8266_light/ota.h diff --git a/README.md b/README.md index 43d1d09..e194082 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,9 @@ I'm using ESP8266-01 microcontrollers for my lights because they are so cheap an 4. Save the configuration file as `config.h`. 5. Open the `.ino` file in the Arduino IDE and upload to an ESP with the correct connections. +#### OTA updates +The sketch includes `ota.h` if you prefer to disable the OTA updates, change the `OTA_ENABLED` constant to `false`. + #### About the `DEPRECATED` Folder Originally, there were separate Arduino sketches for each of the supported light types. This quickly became redundant and hard-to-maintain, so the new, unified sketch was created. diff --git a/mqtt_esp8266_light/config-sample.h b/mqtt_esp8266_light/config-sample.h index ffbd069..c6be510 100644 --- a/mqtt_esp8266_light/config-sample.h +++ b/mqtt_esp8266_light/config-sample.h @@ -64,3 +64,7 @@ enum strip { // Enables Serial and print statements #define CONFIG_DEBUG false + +// Constants for OTA updates +#define OTA_ENABLED true +#define OTA_HOSTNAME "LED-OTA" diff --git a/mqtt_esp8266_light/mqtt_esp8266_light.ino b/mqtt_esp8266_light/mqtt_esp8266_light.ino index 9c06058..f0726ea 100644 --- a/mqtt_esp8266_light/mqtt_esp8266_light.ino +++ b/mqtt_esp8266_light/mqtt_esp8266_light.ino @@ -21,6 +21,9 @@ // http://pubsubclient.knolleary.net/ #include +// OTA firmware update +#include "ota.h" + const bool rgb = (CONFIG_STRIP == RGB) || (CONFIG_STRIP == RGBW); const bool includeWhite = (CONFIG_STRIP == BRIGHTNESS) || (CONFIG_STRIP == RGBW); @@ -112,6 +115,10 @@ void setup() { setup_wifi(); client.setServer(CONFIG_MQTT_HOST, CONFIG_MQTT_PORT); client.setCallback(callback); + + if (OTA_ENABLED) { + setupOta(); + } } void setup_wifi() { @@ -401,6 +408,11 @@ void setColor(int inR, int inG, int inB, int inW) { } void loop() { + + if (OTA_ENABLED) { + ArduinoOTA.handle(); + } + if (!client.connected()) { reconnect(); } diff --git a/mqtt_esp8266_light/ota.h b/mqtt_esp8266_light/ota.h new file mode 100644 index 0000000..3e87dc7 --- /dev/null +++ b/mqtt_esp8266_light/ota.h @@ -0,0 +1,39 @@ +#ifndef ota_h +#define ota_h + +#include +#include +#include +#include + +/** + * @brief mDNS and OTA Constants + * @{ + */ +#define HOSTNAME OTA_HOSTNAME ///< Hostname. The setup function adds the Chip ID at the end. +/// @} + + +void setupOta() { + + delay(100); + + Serial.println("\r\n"); + Serial.print("Chip ID: 0x"); + Serial.println(ESP.getChipId(), HEX); + + // Set Hostname. + String hostname(HOSTNAME); + hostname += String("-OTA-"); + hostname += String(ESP.getChipId(), HEX); + WiFi.hostname(hostname); + + // Print hostname. + Serial.println("Hostname: " + hostname); + + // Start OTA server. + ArduinoOTA.setHostname((const char *)hostname.c_str()); + ArduinoOTA.begin(); +} + +#endif