From b03df134c5313050925fef18d7ec1b4ebbb92748 Mon Sep 17 00:00:00 2001 From: klaute Date: Sat, 23 Jun 2018 21:49:11 +0200 Subject: [PATCH] Added the webbased OTA update mechanism. --- firmware/config.h | 2 +- firmware/firmware.ino | 63 +++++++++-------------------------------- firmware/webUpdater.ino | 34 ++++++++++++++++++++++ firmware/webserver.ino | 27 ++++++++++++++++++ 4 files changed, 76 insertions(+), 50 deletions(-) create mode 100644 firmware/webUpdater.ino create mode 100644 firmware/webserver.ino diff --git a/firmware/config.h b/firmware/config.h index b25c5ad..57f67ca 100755 --- a/firmware/config.h +++ b/firmware/config.h @@ -9,7 +9,7 @@ #define UPDATE_INTERVAL 4 #define STATUS_LED_PIN BUILTIN_LED -#define ANEMOMETER_PIN D6 +#define ANEMOMETER_PIN D7 #define BME_SCK 13 #define BME_MISO 12 diff --git a/firmware/firmware.ino b/firmware/firmware.ino index 68ff60e..aa8db14 100755 --- a/firmware/firmware.ino +++ b/firmware/firmware.ino @@ -1,4 +1,7 @@ +#include +#include +#include #include #include #include @@ -11,22 +14,6 @@ #include "config.h" /** -<<<<<<< HEAD - Whishlist: - - Webserver for /metrics endpoint (Prometheus) - - Show current sensor values over simple webpage - - Push sensor values to various 3rd party services (https://openweathermap.org/) - - MQTT? - - - Buffer sensor values if there is no WIFI connection - - Configure weather station over http webinterface - - OTA firmware update - - TODO: - - https://sminghub.github.io/Sming/about/ - - https://github.com/marvinroger/homie-esp8266 -*/ -======= * Whishlist: * - Webserver for /metrics endpoint (Prometheus) * - Show current sensor values over simple webpage @@ -41,24 +28,22 @@ * - https://sminghub.github.io/Sming/about/ * - https://github.com/marvinroger/homie-esp8266 */ ->>>>>>> d5aee1ca54127dd46df8934c2512bcf1b9cc1bfa float currentSensorData[5] = {0.0, 0.0, 0.0, 0.0, 0.0}; - WiFiManager wifiManager; Influxdb influxdb(INFLUXDB_HOST, INFLUXDB_PORT); Adafruit_APDS9960 apds; Adafruit_BME280 bme; +boolean webUpdaterEnabled = false; + void setup() { #ifdef DEBUG Serial.begin(115200); #endif - //WiFi.disconnect(); // enable to remove the wifi credentials - // Pin settings pinMode(STATUS_LED_PIN, OUTPUT); //pinMode(ANEMOMETER_PIN, INPUT_PULLUP); @@ -96,6 +81,14 @@ void setup() { void loop() { + if (webUpdaterEnabled == true) + { + doWebUpdater(); + return; + } + + doWebserver(); + digitalWrite(STATUS_LED_PIN, LOW); #ifdef DEBUG @@ -103,7 +96,6 @@ void loop() { digitalWrite(STATUS_LED_PIN, HIGH); #endif currentSensorData[SENSOR_TEMPERATURE] = fetchTemperature(); -<<<<<<< HEAD currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); currentSensorData[SENSOR_LIGHT] = fetchLight(); currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed(); @@ -122,31 +114,4 @@ void loop() { pushToInfluxDB(DEVICE_NAME, currentSensorData); delay(UPDATE_INTERVAL * 1000); -} - -======= - Serial.print("*"); - currentSensorData[SENSOR_HUMIDITY] = fetchHumidity(); - Serial.print("*"); - currentSensorData[SENSOR_LIGHT] = fetchLight(); - Serial.print("*"); - currentSensorData[SENSOR_WINDSPEED] = fetchWindspeed(); - Serial.print("*"); - currentSensorData[SENSOR_PRESSURE] = fetchPressure(); - Serial.println("*"); - - #ifdef DEBUG - Serial.println(""); - Serial.println("Current readings:"); - Serial.println("Temperature: " + String(currentSensorData[SENSOR_TEMPERATURE]) + " °C"); - Serial.println("Humidity: " + String(currentSensorData[SENSOR_HUMIDITY]) + " %"); - Serial.println("Light: " + String(currentSensorData[SENSOR_LIGHT]) + " Lumen"); - Serial.println("Windspeed: " + String(currentSensorData[SENSOR_WINDSPEED]) + " Km/h"); - Serial.println("Pressure: " + String(currentSensorData[SENSOR_PRESSURE]) + " hPa"); - #endif - - pushToInfluxDB(DEVICE_NAME, currentSensorData); - - delay(UPDATE_INTERVAL*1000); -} ->>>>>>> d5aee1ca54127dd46df8934c2512bcf1b9cc1bfa +} diff --git a/firmware/webUpdater.ino b/firmware/webUpdater.ino new file mode 100644 index 0000000..9d02762 --- /dev/null +++ b/firmware/webUpdater.ino @@ -0,0 +1,34 @@ +/* + To upload through terminal you can use: curl -F "image=@firmware.bin" esp8266-webupdate.local/update +*/ + +#include +#include +#include +#include +#include + +ESP8266WebServer httpServer(8080); +ESP8266HTTPUpdateServer httpUpdater; + +void setupWebUpdater(void) +{ +#ifdef DEBUG + Serial.println(); + Serial.println("Starting WebUpdater..."); +#endif + + httpUpdater.setup(&httpServer); + httpServer.begin(); + +#ifdef DEBUG + Serial.println("HTTPUpdateServer ready!"); +#endif +} + +void doWebUpdater(void) +{ + digitalWrite(D0, HIGH); + httpServer.handleClient(); +} + diff --git a/firmware/webserver.ino b/firmware/webserver.ino new file mode 100644 index 0000000..98aaab2 --- /dev/null +++ b/firmware/webserver.ino @@ -0,0 +1,27 @@ + +//*************************************************************************// + +WiFiServer server(80); + +void doWebserver(void) +{ + WiFiClient client = server.available(); + if (client) + { + String request = client.readStringUntil('\r'); + + if (request.indexOf("/WU") != -1) + { + setupWebUpdater(); + webUpdaterEnabled = true; // enable the web update mechanism + + } else if (request.indexOf("/RW") != -1) + { + WiFi.disconnect(); // erase the wifi credentials + while(1); // trigger the watchdog + } + + } +} + +