mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-30 21:51:52 +00:00
Added firmware upload from UI
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
#define _AMSDATA_H
|
||||
|
||||
#include "Arduino.h"
|
||||
#include <Timezone.h>
|
||||
#include "HanReader.h"
|
||||
|
||||
#define METER_TYPE_KAIFA 1
|
||||
|
||||
@@ -10,6 +10,8 @@
|
||||
#include <ESP8266WiFi.h>
|
||||
#elif defined(ESP32)
|
||||
#include <WiFi.h>
|
||||
#include "SPIFFS.h"
|
||||
#include "Update.h"
|
||||
#endif
|
||||
|
||||
// Build settings for custom hardware by Roar Fredriksen
|
||||
|
||||
@@ -86,7 +86,7 @@ void setup() {
|
||||
debugger->println("Started...");
|
||||
debugger->print("Voltage: ");
|
||||
debugger->print(vcc);
|
||||
debugger->println("mV");
|
||||
debugger->println("V");
|
||||
}
|
||||
|
||||
if (vcc > 0 && vcc < 3.1) {
|
||||
@@ -112,6 +112,60 @@ void setup() {
|
||||
WiFi.softAPdisconnect(true);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
|
||||
if(SPIFFS.begin()) {
|
||||
bool flashed = false;
|
||||
if(SPIFFS.exists("/firmware.bin")) {
|
||||
if(debugger) debugger->println("Found firmware");
|
||||
#if defined(ESP8266)
|
||||
WiFi.setSleepMode(WIFI_LIGHT_SLEEP);
|
||||
WiFi.forceSleepBegin();
|
||||
#endif
|
||||
int i = 0;
|
||||
while(hw.getVcc() < 3.3 && i < 3) {
|
||||
if(debugger) debugger->println(" vcc not optimal, light sleep 10s");
|
||||
#if defined(ESP8266)
|
||||
delay(10000);
|
||||
#elif defined(ESP32)
|
||||
esp_sleep_enable_timer_wakeup(10000000);
|
||||
esp_light_sleep_start();
|
||||
#endif
|
||||
i++;
|
||||
}
|
||||
|
||||
if(debugger) debugger->println(" flashing");
|
||||
File firmwareFile = SPIFFS.open("/firmware.bin", "r");
|
||||
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
|
||||
if (!Update.begin(maxSketchSpace, U_FLASH)) {
|
||||
if(debugger) {
|
||||
debugger->println("Unable to start firmware update");
|
||||
Update.printError(*debugger);
|
||||
}
|
||||
} else {
|
||||
while (firmwareFile.available()) {
|
||||
uint8_t ibuffer[128];
|
||||
firmwareFile.read((uint8_t *)ibuffer, 128);
|
||||
Update.write(ibuffer, sizeof(ibuffer));
|
||||
}
|
||||
flashed = Update.end(true);
|
||||
}
|
||||
firmwareFile.close();
|
||||
SPIFFS.remove("/firmware.bin");
|
||||
}
|
||||
SPIFFS.end();
|
||||
if(flashed) {
|
||||
if(debugger) {
|
||||
debugger->println("Firmware update complete, restarting");
|
||||
debugger->flush();
|
||||
}
|
||||
#if defined(ESP8266)
|
||||
ESP.reset();
|
||||
#elif defined(ESP32)
|
||||
ESP.restart();
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if(config.hasConfig()) {
|
||||
if(debugger) config.print(debugger);
|
||||
WiFi_connect();
|
||||
|
||||
@@ -6,6 +6,8 @@
|
||||
#include "root/configwifi_html.h"
|
||||
#include "root/configmqtt_html.h"
|
||||
#include "root/configweb_html.h"
|
||||
#include "root/configsystem_html.h"
|
||||
#include "root/firmwarewait_html.h"
|
||||
#include "root/boot_css.h"
|
||||
#include "root/gaugemeter_js.h"
|
||||
|
||||
@@ -18,16 +20,20 @@ void AmsWebServer::setup(AmsConfiguration* config, Stream* debugger, MQTTClient*
|
||||
this->mqtt = mqtt;
|
||||
|
||||
server.on("/", std::bind(&AmsWebServer::indexHtml, this));
|
||||
server.on("/config-meter", std::bind(&AmsWebServer::configMeterHtml, this));
|
||||
server.on("/config-wifi", std::bind(&AmsWebServer::configWifiHtml, this));
|
||||
server.on("/config-mqtt", std::bind(&AmsWebServer::configMqttHtml, this));
|
||||
server.on("/config-web", std::bind(&AmsWebServer::configWebHtml, this));
|
||||
server.on("/boot.css", std::bind(&AmsWebServer::bootCss, this));
|
||||
server.on("/gaugemeter.js", std::bind(&AmsWebServer::gaugemeterJs, this));
|
||||
server.on("/data.json", std::bind(&AmsWebServer::dataJson, this));
|
||||
server.on("/config-meter", HTTP_GET, std::bind(&AmsWebServer::configMeterHtml, this));
|
||||
server.on("/config-wifi", HTTP_GET, std::bind(&AmsWebServer::configWifiHtml, this));
|
||||
server.on("/config-mqtt", HTTP_GET, std::bind(&AmsWebServer::configMqttHtml, this));
|
||||
server.on("/config-web", HTTP_GET, std::bind(&AmsWebServer::configWebHtml, this));
|
||||
server.on("/boot.css", HTTP_GET, std::bind(&AmsWebServer::bootCss, this));
|
||||
server.on("/gaugemeter.js", HTTP_GET, std::bind(&AmsWebServer::gaugemeterJs, this));
|
||||
server.on("/data.json", HTTP_GET, std::bind(&AmsWebServer::dataJson, this));
|
||||
|
||||
server.on("/save", std::bind(&AmsWebServer::handleSave, this));
|
||||
|
||||
server.on("/config-system", HTTP_GET, std::bind(&AmsWebServer::configSystemHtml, this));
|
||||
server.on("/config-system", HTTP_POST, std::bind(&AmsWebServer::configSystemPost, this), std::bind(&AmsWebServer::configSystemUpload, this));
|
||||
server.on("/firmware-wait", HTTP_GET, std::bind(&AmsWebServer::firmwareWaitHtml, this));
|
||||
|
||||
server.begin(); // Web server start
|
||||
}
|
||||
|
||||
@@ -148,7 +154,7 @@ void AmsWebServer::indexHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configMeterHtml() {
|
||||
println("Serving /config/meter.html over http...");
|
||||
println("Serving /config-meter.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -182,7 +188,7 @@ void AmsWebServer::configMeterHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configWifiHtml() {
|
||||
println("Serving /config/wifi.html over http...");
|
||||
println("Serving /config-wifi.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -209,7 +215,7 @@ void AmsWebServer::configWifiHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configMqttHtml() {
|
||||
println("Serving /config/mqtt.html over http...");
|
||||
println("Serving /config-mqtt.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -243,7 +249,7 @@ void AmsWebServer::configMqttHtml() {
|
||||
}
|
||||
|
||||
void AmsWebServer::configWebHtml() {
|
||||
println("Serving /config/web.html over http...");
|
||||
println("Serving /config-web.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
@@ -514,6 +520,92 @@ void AmsWebServer::handleSave() {
|
||||
}
|
||||
}
|
||||
|
||||
void AmsWebServer::configSystemHtml() {
|
||||
println("Serving /config-system.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
|
||||
String html = String((const __FlashStringHelper*) CONFIGSYSTEM_HTML);
|
||||
html.replace("${version}", VERSION);
|
||||
|
||||
if(WiFi.getMode() != WIFI_AP) {
|
||||
html.replace("boot.css", BOOTSTRAP_URL);
|
||||
}
|
||||
|
||||
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server.sendHeader("Pragma", "no-cache");
|
||||
|
||||
server.setContentLength(html.length());
|
||||
server.send(200, "text/html", html);
|
||||
}
|
||||
|
||||
void AmsWebServer::configSystemPost() {
|
||||
server.send(200);
|
||||
}
|
||||
|
||||
void AmsWebServer::configSystemUpload() {
|
||||
HTTPUpload& upload = server.upload();
|
||||
if(upload.status == UPLOAD_FILE_START){
|
||||
String filename = upload.filename;
|
||||
if(!filename.endsWith(".bin")) {
|
||||
server.send(500, "text/plain", "500: couldn't create file");
|
||||
} else if (!SPIFFS.begin()) {
|
||||
println("An Error has occurred while mounting SPIFFS");
|
||||
String html = "<html><body><h1>Error uploading!</h1></form>";
|
||||
server.send(500, "text/html", html);
|
||||
} else {
|
||||
print("handleFileUpload Name: "); println(filename.c_str());
|
||||
firmwareFile = SPIFFS.open("/firmware.bin", "w");
|
||||
filename = String();
|
||||
}
|
||||
} else if(upload.status == UPLOAD_FILE_WRITE) {
|
||||
if(firmwareFile)
|
||||
firmwareFile.write(upload.buf, upload.currentSize);
|
||||
} else if(upload.status == UPLOAD_FILE_END) {
|
||||
if(firmwareFile) {
|
||||
firmwareFile.close();
|
||||
SPIFFS.end();
|
||||
print("handleFileUpload Size: "); println(String(upload.totalSize).c_str());
|
||||
server.sendHeader("Location","/firmware-wait");
|
||||
server.send(303);
|
||||
} else {
|
||||
server.send(500, "text/plain", "500: couldn't create file");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void AmsWebServer::firmwareWaitHtml() {
|
||||
println("Serving /firmware-wait.html over http...");
|
||||
|
||||
if(!checkSecurity(1))
|
||||
return;
|
||||
|
||||
String html = String((const __FlashStringHelper*) FIRMWAREWAIT_HTML);
|
||||
html.replace("${version}", VERSION);
|
||||
|
||||
if(WiFi.getMode() != WIFI_AP) {
|
||||
html.replace("boot.css", BOOTSTRAP_URL);
|
||||
}
|
||||
|
||||
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
|
||||
server.sendHeader("Pragma", "no-cache");
|
||||
|
||||
server.setContentLength(html.length());
|
||||
server.send(200, "text/html", html);
|
||||
|
||||
yield();
|
||||
if(SPIFFS.begin() && SPIFFS.exists("/firmware.bin")) {
|
||||
SPIFFS.end();
|
||||
println("Firmware uploaded, rebooting");
|
||||
delay(1000);
|
||||
#if defined(ESP8266)
|
||||
ESP.reset();
|
||||
#elif defined(ESP32)
|
||||
ESP.restart();
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
size_t AmsWebServer::print(const char* text)
|
||||
{
|
||||
|
||||
@@ -22,6 +22,8 @@
|
||||
#elif defined(ESP32) // ARDUINO_ARCH_ESP32
|
||||
#include <WiFi.h>
|
||||
#include <WebServer.h>
|
||||
#include "SPIFFS.h"
|
||||
#include "Update.h"
|
||||
#else
|
||||
#warning "Unsupported board type"
|
||||
#endif
|
||||
@@ -40,6 +42,7 @@ private:
|
||||
AmsData data;
|
||||
Stream* debugger;
|
||||
MQTTClient* mqtt;
|
||||
File firmwareFile;
|
||||
|
||||
#if defined(ESP8266)
|
||||
ESP8266WebServer server;
|
||||
@@ -60,6 +63,11 @@ private:
|
||||
|
||||
void handleSave();
|
||||
|
||||
void configSystemHtml();
|
||||
void configSystemPost();
|
||||
void configSystemUpload();
|
||||
void firmwareWaitHtml();
|
||||
|
||||
size_t print(const char* text);
|
||||
size_t println(const char* text);
|
||||
size_t print(const Printable& data);
|
||||
|
||||
Reference in New Issue
Block a user