implement OTA

This commit is contained in:
Mathieu Maret 2016-03-04 00:49:16 +01:00
parent 2fccb805df
commit aec6e58fed
1 changed files with 77 additions and 14 deletions

View File

@ -25,6 +25,8 @@ char eeprom[EEPROM_SIZE];
#define BOOTMODE_NORMAL 1 #define BOOTMODE_NORMAL 1
#define BOOTMODE_OTA 2 #define BOOTMODE_OTA 2
int mode;
/* EEPROM LAYOUT /* EEPROM LAYOUT
"BOOTMODE;SSID;PASSWORD;HOSTNAME;" "BOOTMODE;SSID;PASSWORD;HOSTNAME;"
BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA BOOTMODE could be 0 for Setup, 1 for normal use, 2 for OTA
@ -43,6 +45,7 @@ ESP8266WebServer server(80);
void handleRoot() { void handleRoot() {
server.send(200, "text/html", "<h1>You are connected</h1><br/>" server.send(200, "text/html", "<h1>You are connected</h1><br/>"
"<a href=\"/setup\">Setup</a><br/>" "<a href=\"/setup\">Setup</a><br/>"
"<a href=\"/otamode\">OTA mode</a><br/>"
"<a href=\"/gpio?gpio=2&amp;value=1\">ON</a><br/>" "<a href=\"/gpio?gpio=2&amp;value=1\">ON</a><br/>"
"<a href=\"/gpio?gpio=2&value=0\">OFF</a><br/>" "<a href=\"/gpio?gpio=2&value=0\">OFF</a><br/>"
); );
@ -114,6 +117,13 @@ void handleSave() {
"You can reboot now"); "You can reboot now");
} }
void handleOTA() {
Serial.println("Boot mode Set to OTA");
saveBootMode(BOOTMODE_OTA);
server.send(200, "text/html", "<h1>OTA Mode set</h1><br/>"
"You can reboot now");
}
void handleNotFound() { void handleNotFound() {
String message = "File Not Found\n\n"; String message = "File Not Found\n\n";
message += "URI: "; message += "URI: ";
@ -202,10 +212,61 @@ void setupWifi(int bootmode, int forceSetup, char *confSsid, char *confPassword,
Serial.println(myIP); 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() { void setup() {
pinMode(3, OUTPUT); pinMode(3, OUTPUT);
int bootMode;
char *confSsid; char *confSsid;
char *confPassword; char *confPassword;
char *confHost; char *confHost;
@ -221,10 +282,10 @@ void setup() {
Serial.swap(); // Switch back Serial.swap(); // Switch back
EEPROM.begin(EEPROM_SIZE); EEPROM.begin(EEPROM_SIZE);
readEEPROM(bootMode, &confSsid, &confPassword, &confHost); readEEPROM(mode, &confSsid, &confPassword, &confHost);
if (bootMode == BOOTMODE_NORMAL || bootMode == BOOTMODE_OTA) { if (mode == BOOTMODE_NORMAL || mode == BOOTMODE_OTA) {
Serial.println("Configuration Found !:"); Serial.println("Configuration Found !:");
Serial.println(bootMode); Serial.println(mode);
Serial.println(confSsid); Serial.println(confSsid);
Serial.println(confPassword); Serial.println(confPassword);
Serial.println(confHost); Serial.println(confHost);
@ -237,18 +298,20 @@ void setup() {
Serial.print(txStatus?"No":"Yes"); Serial.print(txStatus?"No":"Yes");
Serial.println(); Serial.println();
setupWifi(bootMode, txStatus == 0, confSsid, confPassword, confHost); setupWifi(mode, txStatus == 0, confSsid, confPassword, confHost);
server.on("/", handleRoot); if (mode == BOOTMODE_OTA) {
server.on("/setup", handleSetup); setupOTA();
server.on("/save", handleSave); } else {
if (bootMode == BOOTMODE_NORMAL) setupWebServer();
server.on("/gpio", handleGpio); }
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
} }
void loop() { void loop() {
server.handleClient();
if (mode == BOOTMODE_OTA) {
ArduinoOTA.handle();
} else {
server.handleClient();
}
} }