Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Arduino OTA integration #58

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
4 changes: 4 additions & 0 deletions mqtt_esp8266_light/config-sample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
12 changes: 12 additions & 0 deletions mqtt_esp8266_light/mqtt_esp8266_light.ino
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
// http://pubsubclient.knolleary.net/
#include <PubSubClient.h>

// OTA firmware update
#include "ota.h"

const bool rgb = (CONFIG_STRIP == RGB) || (CONFIG_STRIP == RGBW);
const bool includeWhite = (CONFIG_STRIP == BRIGHTNESS) || (CONFIG_STRIP == RGBW);

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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();
}
Expand Down
39 changes: 39 additions & 0 deletions mqtt_esp8266_light/ota.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#ifndef ota_h
#define ota_h

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>

/**
* @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