diff --git a/src/HomeSpan.cpp b/src/HomeSpan.cpp index 1267e84..d3b300d 100644 --- a/src/HomeSpan.cpp +++ b/src/HomeSpan.cpp @@ -29,6 +29,7 @@ #include #include #include +#include #include "HomeSpan.h" #include "HAP.h" @@ -207,6 +208,9 @@ void Span::poll() { HAPClient::checkNotifications(); HAPClient::checkTimedWrites(); + if(otaEnabled) + ArduinoOTA.handle(); + if(controlButton.primed()){ statusLED.start(LED_ALERT); } @@ -425,9 +429,48 @@ void Span::checkConnect(){ Serial.print("\nStarting Web (HTTP) Server supporting up to "); Serial.print(maxConnections); - Serial.print(" simultaneous connections...\n\n"); + Serial.print(" simultaneous connections...\n"); hapServer->begin(); + if(otaEnabled){ + ArduinoOTA.setHostname(hostName); + + ArduinoOTA + .onStart([]() { + String type; + if (ArduinoOTA.getCommand() == U_FLASH) + type = "sketch"; + else // U_SPIFFS + type = "filesystem"; + Serial.println("\n*** OTA Starting:" + type); + homeSpan.statusLED.start(LED_OTA_STARTED); + }) + .onEnd([]() { + Serial.println("\n*** OTA Completed. Rebooting..."); + homeSpan.statusLED.off(); + }) + .onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("*** Progress: %u%%\r", (progress / (total / 100))); + }) + .onError([](ota_error_t error) { + Serial.printf("*** OTA Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed\n"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed\n"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed\n"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed\n"); + else if (error == OTA_END_ERROR) Serial.println("End Failed\n"); + }); + + ArduinoOTA.begin(); + Serial.print("Starting OTA: "); + Serial.print(displayName); + Serial.print(" at "); + Serial.print(WiFi.localIP()); + Serial.print("\n"); + } + + Serial.print("\n"); + if(!HAPClient::nAdminControllers()){ Serial.print("DEVICE NOT YET PAIRED -- PLEASE PAIR WITH HOMEKIT APP\n\n"); statusLED.start(LED_PAIRING_NEEDED); diff --git a/src/HomeSpan.h b/src/HomeSpan.h index 5960bb0..7a8777f 100644 --- a/src/HomeSpan.h +++ b/src/HomeSpan.h @@ -100,6 +100,7 @@ struct Span{ unsigned long comModeLife=DEFAULT_COMMAND_TIMEOUT*1000; // length of time (in milliseconds) to keep Command Mode alive before resuming normal operations uint16_t tcpPortNum=DEFAULT_TCP_PORT; // port for TCP communications between HomeKit and HomeSpan char qrID[5]=""; // Setup ID used for pairing with QR Code + boolean otaEnabled=false; // enables Over-the-Air updates WiFiServer *hapServer; // pointer to the HAP Server connection Blinker statusLED; // indicates HomeSpan status @@ -150,6 +151,7 @@ struct Span{ void setHostNameSuffix(const char *suffix){hostNameSuffix=suffix;} // sets the hostName suffix to be used instead of the 6-byte AccessoryID void setPortNum(uint16_t port){tcpPortNum=port;} // sets the TCP port number to use for communications between HomeKit and HomeSpan void setQRID(const char *id); // sets the Setup ID for optional pairing with a QR Code + void enableOTA(){otaEnabled=true;} // enables Over-the-Air updates }; /////////////////////////////// diff --git a/src/Settings.h b/src/Settings.h index 9b1ccc6..fb1417b 100644 --- a/src/Settings.h +++ b/src/Settings.h @@ -87,6 +87,7 @@ #define LED_WIFI_CONNECTING 2000 // slow flashing #define LED_AP_STARTED 100,0.5,2,300 // rapid double-blink #define LED_AP_CONNECTED 300,0.5,2,400 // medium double-blink +#define LED_OTA_STARTED 300,0.5,5,500 // rapid 5-blink ///////////////////////////////////////////////////// // Message Log Level Control Macros // diff --git a/src/src.ino b/src/src.ino index 54b38c0..d337fbf 100644 --- a/src/src.ino +++ b/src/src.ino @@ -10,9 +10,11 @@ void setup() { homeSpan.setLogLevel(2); - homeSpan.setHostNameSuffix(""); +// homeSpan.setHostNameSuffix(""); homeSpan.setPortNum(1200); + homeSpan.setMaxConnections(16); // homeSpan.setQRID("One1"); + homeSpan.enableOTA(); homeSpan.begin(Category::Lighting,"HomeSpanTest");