From aec6e58fedf48fcca857e167587ea06f0bf3ab0c Mon Sep 17 00:00:00 2001 From: Mathieu Maret Date: Fri, 4 Mar 2016 00:49:16 +0100 Subject: [PATCH] implement OTA --- .../WiFiAccessPointConfigurator.ino | 91 ++++++++++++++++--- 1 file changed, 77 insertions(+), 14 deletions(-) diff --git a/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino b/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino index 82fd2f5..56a2f2a 100644 --- a/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino +++ b/WiFiAccessPointConfigurator/WiFiAccessPointConfigurator.ino @@ -25,6 +25,8 @@ char eeprom[EEPROM_SIZE]; #define BOOTMODE_NORMAL 1 #define BOOTMODE_OTA 2 +int mode; + /* EEPROM LAYOUT "BOOTMODE;SSID;PASSWORD;HOSTNAME;" BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA @@ -43,6 +45,7 @@ ESP8266WebServer server(80); void handleRoot() { server.send(200, "text/html", "

You are connected


" "Setup
" + "OTA mode
" "ON
" "OFF
" ); @@ -114,6 +117,13 @@ void handleSave() { "You can reboot now"); } +void handleOTA() { + Serial.println("Boot mode Set to OTA"); + saveBootMode(BOOTMODE_OTA); + server.send(200, "text/html", "

OTA Mode set


" + "You can reboot now"); +} + void handleNotFound() { String message = "File Not Found\n\n"; message += "URI: "; @@ -202,10 +212,61 @@ void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword, Serial.println(myIP); } +void setupOTA() { + // Port defaults to 8266 + // ArduinoOTA.setPort(8266); + + // Hostname defaults to esp8266-[ChipID] + // ArduinoOTA.setHostname("myesp8266"); + + // No authentication by default + // ArduinoOTA.setPassword((const char *)"123"); + + //Disable OTA mode to avoid forever loop + //Force BOOTMODE_SETUP in case eeprom layout have changed + saveConfig(BOOTMODE_SETUP, "", "", "" ); + + ArduinoOTA.onStart([]() { + Serial.println("Start"); + }); + ArduinoOTA.onEnd([]() { + Serial.println("\nEnd"); + }); + ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) { + Serial.printf("Progress: %u%%\r", (progress / (total / 100))); + }); + ArduinoOTA.onError([](ota_error_t error) { + 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(); + Serial.println("Ready"); + Serial.print("IP address: "); + Serial.println(WiFi.localIP()); + Serial.print("Free Space: "); + Serial.println(ESP.getFreeSketchSpace()); +} + +void setupWebServer() { + server.on("/", handleRoot); + server.on("/setup", handleSetup); + server.on("/save", handleSave); + if (mode == BOOTMODE_NORMAL){ + server.on("/gpio", handleGpio); + server.on("/otamode", handleOTA); + } + server.onNotFound(handleNotFound); + server.begin(); + Serial.println("HTTP server started"); +} + void setup() { pinMode(3, OUTPUT); - int bootMode; char *confSsid; char *confPassword; char *confHost; @@ -221,10 +282,10 @@ void setup() { Serial.swap(); // Switch back EEPROM.begin(EEPROM_SIZE); - readEEPROM(bootMode, &confSsid, &confPassword, &confHost); - if (bootMode == BOOTMODE_NORMAL || bootMode == BOOTMODE_OTA) { + readEEPROM(mode, &confSsid, &confPassword, &confHost); + if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) { Serial.println("Configuration Found !:"); - Serial.println(bootMode); + Serial.println(mode); Serial.println(confSsid); Serial.println(confPassword); Serial.println(confHost); @@ -237,18 +298,20 @@ void setup() { Serial.print(txStatus?"No":"Yes"); Serial.println(); - setupWifi(bootMode, txStatus == 0, confSsid, confPassword, confHost); + setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost); - server.on("/", handleRoot); - server.on("/setup", handleSetup); - server.on("/save", handleSave); - if (bootMode == BOOTMODE_NORMAL) - server.on("/gpio", handleGpio); - server.onNotFound(handleNotFound); - server.begin(); - Serial.println("HTTP server started"); + if (mode == BOOTMODE_OTA) { + setupOTA(); + } else { + setupWebServer(); + } } void loop() { - server.handleClient(); + + if (mode == BOOTMODE_OTA) { + ArduinoOTA.handle(); + } else { + server.handleClient(); + } }