From 3e5e8480f3f2f0af56731471cc967404efe0c921 Mon Sep 17 00:00:00 2001 From: Steven Date: Wed, 6 Oct 2021 00:52:14 -0400 Subject: andy cleanup --- ota.h | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 ota.h (limited to 'ota.h') diff --git a/ota.h b/ota.h new file mode 100644 index 0000000..28fb37b --- /dev/null +++ b/ota.h @@ -0,0 +1,98 @@ +// Include what is needed to implement Over The Air Update +// The WiFi connection is not done on this include file, so must be done +// in the main program file +// +// This include file provides the functions +// setupWifi(hostname) +// hostname can be an empty string; this function will +// establish the wifi connections, WiFi credentials are supplied +// in the file "wifiinfo.h", see "wifiinfo.h.sample" +// setupOTA(hostname) +// hostname can be an empty string; will be used to retrieve the +// correct board to upgrade Over The Air. +// If an empty string the default name is esp8266-[ChipID] +// The above 2 functions must be called in the setup() function +// +// in the loop function the function +// ArduinoOTA.handle() +// must be called + +#ifndef ota_h +#define ota_h +// needed include files +#include +#include +#include +#include + +// include our Network SSID and PASSWORD +#include "wifiinfo.h" + +// ================================================================== +// Establish WiFi connection +// ================================================================== +void setupWifi(char hostname[]) { + int i = 0; + WiFi.mode(WIFI_STA); + if (sizeof(hostname) > 0) { + WiFi.hostname(hostname); + } + if (Serial) {Serial.println("Connecting ");} + WiFi.begin(ssid, password); + while (WiFi.status() != WL_CONNECTED) { + if (Serial) {Serial.print(".");} + delay(500); + } + if (Serial) { + Serial.println("."); + Serial.print("Connected! IP: "); + Serial.println(WiFi.localIP()); + } +} + + +// ================================================================== +// Setup for OTA update +// ================================================================== + +void setupOTA(char hostname[]) { + // Port defaults to 8266 + // ArduinoOTA.setPort(8266); + + // Hostname defaults to esp8266-[ChipID] + if (sizeof(hostname) > 0) { + ArduinoOTA.setHostname(hostname); + } + + // No authentication by default + // ArduinoOTA.setPassword((const char *)"123"); + + ArduinoOTA.onStart([]() { + if (Serial) {Serial.println("Start");} + }); + ArduinoOTA.onEnd([]() { + if (Serial) {Serial.println("\nEnd");} + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + if (Serial) {Serial.printf("Progress: %u%%\r", (progress / (total / 100)));} + }); + ArduinoOTA.onError([](ota_error_t error) { + if (Serial) { + Serial.printf("Error[%u]: ", error); + if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed"); + else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed"); + else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed"); + else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed"); + else if (error == OTA_END_ERROR) Serial.println("End Failed"); + } + }); + ArduinoOTA.begin(); + if (Serial) { + Serial.println("Ready"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + } +} + + +#endif -- cgit v1.2.3