Added optional Arduino OTA code.

This commit is contained in:
Tobias Blum 2017-02-18 23:58:00 +01:00
parent 51c87aab7c
commit 7daa7a9166
2 changed files with 53 additions and 0 deletions

View file

@ -17,6 +17,11 @@
#include <WebSockets.h> //https://github.com/Links2004/arduinoWebSockets
#include <WebSocketsServer.h>
// OTA
#ifdef ENABLE_OTA
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#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) {

View file

@ -5,6 +5,8 @@
#define HOSTNAME "ESP8266_02" // Friedly hostname
// #define ENABLE_OTA // If defined, enable Arduino OTA code.
// ***************************************************************************
// Global variables / definitions