Added OTA logic - enable with homeSpan.enableOTA();

TO DO:  Add password for OTA (and require it)
This commit is contained in:
Gregg 2021-02-06 18:10:48 -06:00
parent 170f972d3b
commit 208905419c
4 changed files with 50 additions and 2 deletions

View File

@ -29,6 +29,7 @@
#include <nvs_flash.h>
#include <sodium.h>
#include <WiFi.h>
#include <ArduinoOTA.h>
#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);

View File

@ -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
};
///////////////////////////////

View File

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

View File

@ -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");