mirror of
https://github.com/UtilitechAS/amsreader-firmware.git
synced 2026-01-29 05:20:58 +00:00
Various changes for version updates and some debugging for upload
This commit is contained in:
@@ -7,6 +7,8 @@
|
||||
|
||||
#define EPOCH_2021_01_01 1609459200
|
||||
|
||||
#define MAX_PEM_SIZE 4096
|
||||
|
||||
#include <SoftwareSerial.h>
|
||||
|
||||
#if defined(ESP8266)
|
||||
|
||||
@@ -45,7 +45,6 @@ ADC_MODE(ADC_VCC);
|
||||
|
||||
#include "Uptime.h"
|
||||
|
||||
#define WEBSOCKET_DISABLED true
|
||||
#include "RemoteDebug.h"
|
||||
|
||||
HwTools hw;
|
||||
@@ -188,17 +187,18 @@ void setup() {
|
||||
WiFi.softAPdisconnect(true);
|
||||
WiFi.mode(WIFI_OFF);
|
||||
|
||||
bool spiffs = false;
|
||||
bool hasFs = false;
|
||||
#if defined(ESP32)
|
||||
debugD("ESP32 SPIFFS");
|
||||
spiffs = SPIFFS.begin(true);
|
||||
hasFs = SPIFFS.begin(true);
|
||||
debugD(" size: %d", SPIFFS.totalBytes());
|
||||
#else
|
||||
debugD("ESP8266 SPIFFS");
|
||||
spiffs = SPIFFS.begin();
|
||||
hasFs = SPIFFS.begin();
|
||||
#endif
|
||||
delay(1);
|
||||
|
||||
if(spiffs) {
|
||||
if(hasFs) {
|
||||
bool flashed = false;
|
||||
if(SPIFFS.exists(FILE_FIRMWARE)) {
|
||||
if(Debug.isActive(RemoteDebug::INFO)) debugI("Found firmware");
|
||||
@@ -798,21 +798,41 @@ void MQTT_connect() {
|
||||
char *ca = NULL;
|
||||
char *cert = NULL;
|
||||
char *key = NULL;
|
||||
File file;
|
||||
|
||||
if(SPIFFS.exists(FILE_MQTT_CA)) {
|
||||
debugI("Found MQTT CA file");
|
||||
File file = SPIFFS.open(FILE_MQTT_CA, "r");
|
||||
secureClient->loadCACert(file, file.size());
|
||||
file = SPIFFS.open(FILE_MQTT_CA, "r");
|
||||
#if defined(ESP8266)
|
||||
char caStr[MAX_PEM_SIZE];
|
||||
file.readBytes(caStr, file.size());
|
||||
BearSSL::X509List *serverTrustedCA = new BearSSL::X509List(caStr);
|
||||
secureClient->setTrustAnchors(serverTrustedCA);
|
||||
#elif defined(ESP32)
|
||||
secureClient->loadCACert(file, file.size());
|
||||
#endif
|
||||
}
|
||||
if(SPIFFS.exists(FILE_MQTT_CERT)) {
|
||||
debugI("Found MQTT certificate file");
|
||||
File file = SPIFFS.open(FILE_MQTT_CERT, "r");
|
||||
secureClient->loadCertificate(file, file.size());
|
||||
}
|
||||
if(SPIFFS.exists(FILE_MQTT_KEY)) {
|
||||
debugI("Found MQTT key file");
|
||||
File file = SPIFFS.open(FILE_MQTT_KEY, "r");
|
||||
secureClient->loadPrivateKey(file, file.size());
|
||||
|
||||
if(SPIFFS.exists(FILE_MQTT_CERT) && SPIFFS.exists(FILE_MQTT_KEY)) {
|
||||
#if defined(ESP8266)
|
||||
char certStr[MAX_PEM_SIZE];
|
||||
file = SPIFFS.open(FILE_MQTT_CERT, "r");
|
||||
file.readBytes(certStr, file.size());
|
||||
BearSSL::X509List *serverCertList = new BearSSL::X509List(certStr);
|
||||
char keyStr[MAX_PEM_SIZE];
|
||||
file = SPIFFS.open(FILE_MQTT_KEY, "r");
|
||||
file.readBytes(keyStr, file.size());
|
||||
BearSSL::PrivateKey *serverPrivKey = new BearSSL::PrivateKey(keyStr);
|
||||
secureClient->setClientRSACert(serverCertList, serverPrivKey);
|
||||
#elif defined(ESP32)
|
||||
debugI("Found MQTT certificate file");
|
||||
file = SPIFFS.open(FILE_MQTT_CERT, "r");
|
||||
secureClient->loadCertificate(file, file.size());
|
||||
|
||||
debugI("Found MQTT key file");
|
||||
file = SPIFFS.open(FILE_MQTT_KEY, "r");
|
||||
secureClient->loadPrivateKey(file, file.size());
|
||||
#endif
|
||||
}
|
||||
SPIFFS.end();
|
||||
}
|
||||
|
||||
@@ -217,6 +217,7 @@ bool HwTools::ledBlink(uint8_t color, uint8_t blink) {
|
||||
if(i != blink)
|
||||
delay(50);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
bool HwTools::writeLedPin(uint8_t color, uint8_t state) {
|
||||
|
||||
@@ -184,9 +184,7 @@ bool EntsoeApi::retrieve(const char* url, Stream* doc) {
|
||||
#endif
|
||||
|
||||
HTTPClient https;
|
||||
#if defined(ESP8266)
|
||||
https.setFollowRedirects(true);
|
||||
#endif
|
||||
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
||||
|
||||
if(https.begin(client, url)) {
|
||||
printD("Connection established");
|
||||
|
||||
@@ -80,6 +80,7 @@ bool JsonMqttHandler::publish(AmsData* data, AmsData* previousState) {
|
||||
);
|
||||
return mqtt->publish(topic, json);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool JsonMqttHandler::publishTemperatures(AmsConfiguration* config, HwTools* hw) {
|
||||
|
||||
@@ -198,7 +198,7 @@ void AmsWebServer::temperaturePost() {
|
||||
server.send (302, "text/plain", "");
|
||||
} else {
|
||||
printE("Error saving configuration");
|
||||
String html = "<html><body><h1>Error saving configuration!</h1></form>";
|
||||
String html = "<html><body><h1>Error saving configuration!</h1></body></html>";
|
||||
server.send(500, "text/html", html);
|
||||
}
|
||||
}
|
||||
@@ -895,7 +895,7 @@ void AmsWebServer::handleSetup() {
|
||||
server.send(303);
|
||||
} else {
|
||||
printE("Error saving configuration");
|
||||
String html = "<html><body><h1>Error saving configuration!</h1></form>";
|
||||
String html = "<html><body><h1>Error saving configuration!</h1></body></html>";
|
||||
server.send(500, "text/html", html);
|
||||
}
|
||||
}
|
||||
@@ -1083,7 +1083,7 @@ void AmsWebServer::handleSave() {
|
||||
}
|
||||
} else {
|
||||
printE("Error saving configuration");
|
||||
String html = "<html><body><h1>Error saving configuration!</h1></form>";
|
||||
String html = "<html><body><h1>Error saving configuration!</h1></body></html>";
|
||||
server.send(500, "text/html", html);
|
||||
}
|
||||
}
|
||||
@@ -1235,31 +1235,75 @@ void AmsWebServer::uploadFile(const char* path) {
|
||||
if(upload.status == UPLOAD_FILE_START){
|
||||
if(uploading) {
|
||||
printE("Upload already in progress");
|
||||
String html = "<html><body><h1>Upload already in progress!</h1></form>";
|
||||
String html = "<html><body><h1>Upload already in progress!</h1></body></html>";
|
||||
server.send(500, "text/html", html);
|
||||
} else if (!SPIFFS.begin()) {
|
||||
printE("An Error has occurred while mounting SPIFFS");
|
||||
String html = "<html><body><h1>Unable to mount SPIFFS!</h1></form>";
|
||||
String html = "<html><body><h1>Unable to mount SPIFFS!</h1></body></html>";
|
||||
server.send(500, "text/html", html);
|
||||
} else {
|
||||
uploading = true;
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload file: %s\n", path);
|
||||
}
|
||||
#if defined(ESP32)
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload Free heap: %lu\n", ESP.getFreeHeap());
|
||||
debugger->printf("handleFileUpload SPIFFS size: %lu\n", SPIFFS.totalBytes());
|
||||
debugger->printf("handleFileUpload SPIFFS used: %lu\n", SPIFFS.usedBytes());
|
||||
debugger->printf("handleFileUpload SPIFFS free: %lu\n", SPIFFS.totalBytes()-SPIFFS.usedBytes());
|
||||
}
|
||||
#endif
|
||||
file = SPIFFS.open(path, "w");
|
||||
file.write(upload.buf, upload.currentSize);
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload Open file and write: %lu\n", upload.currentSize);
|
||||
}
|
||||
size_t written = file.write(upload.buf, upload.currentSize);
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload Written: %lu\n", written);
|
||||
}
|
||||
}
|
||||
} else if(upload.status == UPLOAD_FILE_WRITE) {
|
||||
if(file)
|
||||
file.write(upload.buf, upload.currentSize);
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload Writing: %lu\n", upload.currentSize);
|
||||
}
|
||||
if(file) {
|
||||
size_t written = file.write(upload.buf, upload.currentSize);
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload Written: %lu\n", written);
|
||||
}
|
||||
delay(1);
|
||||
if(written != upload.currentSize) {
|
||||
#if defined(ESP32)
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload Free heap: %lu\n", ESP.getFreeHeap());
|
||||
debugger->printf("handleFileUpload SPIFFS size: %lu\n", SPIFFS.totalBytes());
|
||||
debugger->printf("handleFileUpload SPIFFS used: %lu\n", SPIFFS.usedBytes());
|
||||
debugger->printf("handleFileUpload SPIFFS free: %lu\n", SPIFFS.totalBytes()-SPIFFS.usedBytes());
|
||||
}
|
||||
#endif
|
||||
|
||||
file.flush();
|
||||
file.close();
|
||||
SPIFFS.remove(path);
|
||||
SPIFFS.end();
|
||||
|
||||
printE("An Error has occurred while writing file");
|
||||
String html = "<html><body><h1>Unable to write file!</h1></body></html>";
|
||||
server.send(500, "text/html", html);
|
||||
}
|
||||
}
|
||||
} else if(upload.status == UPLOAD_FILE_END) {
|
||||
if(file) {
|
||||
file.flush();
|
||||
file.close();
|
||||
SPIFFS.end();
|
||||
file = SPIFFS.open(path, "r");
|
||||
if(debugger->isActive(RemoteDebug::DEBUG)) {
|
||||
debugger->printf("handleFileUpload Size: %lu\n", upload.totalSize);
|
||||
debugger->printf("handleFileUpload File size: %lu\n", file.size());
|
||||
}
|
||||
file.close();
|
||||
SPIFFS.end();
|
||||
} else {
|
||||
server.send(500, "text/plain", "500: couldn't create file");
|
||||
}
|
||||
@@ -1315,16 +1359,16 @@ void AmsWebServer::firmwareDownload() {
|
||||
WiFiClientSecure client;
|
||||
#if defined(ESP8266)
|
||||
client.setBufferSizes(512, 512);
|
||||
String url = "https://github.com/gskjold/AmsToMqttBridge/releases/download/" + version + "/ams2mqtt-esp8266-" + versionStripped + ".bin";
|
||||
#elif defined(ESP32)
|
||||
String url = "https://github.com/gskjold/AmsToMqttBridge/releases/download/" + version + "/ams2mqtt-esp32-" + versionStripped + ".bin";
|
||||
#endif
|
||||
client.setInsecure();
|
||||
#endif
|
||||
String url = "https://github.com/gskjold/AmsToMqttBridge/releases/download/" + version + "/ams2mqtt-esp12e-" + versionStripped + ".bin";
|
||||
HTTPClient https;
|
||||
#if defined(ESP8266)
|
||||
https.setFollowRedirects(true);
|
||||
#endif
|
||||
https.setFollowRedirects(HTTPC_STRICT_FOLLOW_REDIRECTS);
|
||||
|
||||
https.addHeader("Referer", "https://github.com/gskjold/AmsToMqttBridge/releases");
|
||||
if(https.begin(client, url)) {
|
||||
https.addHeader("Referer", "https://github.com/gskjold/AmsToMqttBridge/releases");
|
||||
printD("HTTP client setup successful");
|
||||
int status = https.GET();
|
||||
if(status == HTTP_CODE_OK) {
|
||||
@@ -1332,7 +1376,6 @@ void AmsWebServer::firmwareDownload() {
|
||||
if(SPIFFS.begin()) {
|
||||
printI("Downloading firmware to SPIFFS");
|
||||
file = SPIFFS.open(FILE_FIRMWARE, "w");
|
||||
// The following does not work... Maybe someone will make it work in the future? It seems to be disconnected at this point.
|
||||
int len = https.writeToStream(&file);
|
||||
file.close();
|
||||
SPIFFS.end();
|
||||
@@ -1365,6 +1408,7 @@ void AmsWebServer::firmwareDownload() {
|
||||
server.send(303);
|
||||
}
|
||||
https.end();
|
||||
client.stop();
|
||||
} else {
|
||||
printI("No firmware version specified...");
|
||||
server.sendHeader("Location","/");
|
||||
|
||||
Reference in New Issue
Block a user