OTA updates

master v0.0.4
Kenneth Barbour 2020-01-08 20:01:55 -05:00
parent fd715431ca
commit 32c1258919
3 changed files with 93 additions and 8 deletions

View File

@ -1,7 +1,9 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <FS.h>
#include "version.h"
#include "web.h"
#include "update.h"
#ifndef SERIAL_BAUD
#define SERIAL_BAUD 74880
@ -43,21 +45,28 @@ void setup()
WiFi.softAP(softApSSID);
Serial.printf_P( PSTR("Created Soft AP for setup: %s\r\n"), softApSSID);
} else {
Serial.println( F("Connected"));
Serial.print( F("Connected, IPv4: "));
Serial.println(WiFi.localIP());
connected = true;
}
// Start Filesystem
SPIFFS.begin();
// Setup Web Server
uweather_web_init();
// if disabled and connected, shutdown web server
if (connected) { //TODO: if connected and disabled
uweather_web_shutdown();
//uweather_web_shutdown();
}
uweather_update_init();
}
void loop()
{
uweather_web_handle();
uweather_update_handle();
delay(10);
}

50
src/update.h 100644
View File

@ -0,0 +1,50 @@
#ifndef _UWEATHER_UPDATE_H_
#define _UWEATHER_UPDATE_H_
#include <ArduinoOTA.h>
#include <FS.h>
void uweather_update_init()
{
ArduinoOTA.onStart([]() {
if (ArduinoOTA.getCommand() == U_FLASH) {
Serial.println("Starting firmware update");
} else {
Serial.println("Starting filesystem update");
SPIFFS.end();
}
});
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();
}
void uweather_update_handle()
{
ArduinoOTA.handle();
}
#endif /** _UWEATHER_UPDATE_H_ */

View File

@ -2,6 +2,7 @@
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <FS.h>
#include <QueryString.h>
/**
* How long to wait after the OK to shutdown before no longer
@ -25,16 +26,30 @@ File contentFile;
// Initialize Connection
void web_init(HttpRequest& req, HttpResponse& res)
{
SPIFFS.begin();
if (contentFile)
contentFile.close();
content.clear(); // clear content buffer
res.content = &content; // response content buffer
Serial.printf("[%s] %s ", req.getMethod(), req.getUrl());
}
// Terminate Connection
void web_terminate(const HttpRequest& req, const HttpResponse& res)
{
if (contentFile) contentFile.close();
Serial.printf("[%s] %s %d\r\n", req.getMethod(), req.getUrl(), res.code);
Serial.printf("%d\r\n", res.code);
}
/**
* Helper for sending errors to the client
*/
void client_error(const char* messg, HttpResponse& res)
{
if (res.code == 200) res.code = 400;
res.headers.set("Content-Type","application/json");
content.print("{ \"error\": true, \"message\": \"");
content.print(messg);
content.print("\" }");
}
void handle_index(HttpRequest& req, HttpResponse& res)
@ -66,11 +81,22 @@ void handle_wifi_state(HttpRequest& req, HttpResponse& res)
{
res.code = 200; // assume OK unless otherwise specified
if (req.method[0] == 'P') { // PUT | POST
// Attempt to connect to connection from request
res.code = 501;
if (req.getMessageLength() > 128)
return client_error("SSID and passphrase too long", res);
QueryString qs(req.getMessage());
if (!qs.has("ssid")) return client_error("Missing ssid", res);
Serial.print(req.getMessage());
const char* ssid = qs.key("ssid");
const char* pass = qs.key("key");
if (pass == nullptr) pass = "";
Serial.println("Changing WiFi SSID");
WiFi.begin(ssid, pass);
WiFi.mode(WIFI_AP_STA);
uint8_t connectResult = WiFi.waitForConnectResult();
} else if (req.method[0] == 'D') { // DELETE
// Disconnect, start softAP, disable shutdown
res.code = 501;
Serial.println("Disconnecting from WiFi");
WiFi.mode(WIFI_AP);
delay(100);
}
res.headers.set("Content-Type","application/json"); //TODO: flash string