multiple changes

This commit is contained in:
2022-08-14 17:38:45 +02:00
parent ad6332acd4
commit 0b9ef67c39
10 changed files with 132 additions and 30 deletions

View File

@@ -21,6 +21,8 @@
#define OTA_DELAY 50 // ticks -> 10ms / tick
#endif
#define ATOMIC_FS_UPDATE
#ifndef ADMIN_PASSWORD
#error "You need to define ADMIN_PASSWORD for OTA-Update"
#endif

View File

@@ -12,6 +12,7 @@ typedef enum DTCNums_e
DTC_EEPROM_CFG_BAD,
DTC_EEPROM_PDS_BAD,
DTC_EEPROM_VERSION_BAD,
DTC_FLASHFS_ERROR,
#ifdef FEATURE_ENABLE_GPS
DTC_NO_GPS_SERIAL,
#endif

View File

@@ -29,6 +29,7 @@ typedef struct Globals_s
char systemStatustxt[16] = "";
uint8_t purgePulses = 0;
eEERequest requestEEAction = EE_IDLE;
char DeviceName[33];
} Globals_t;
extern Globals_t globals;

View File

@@ -2,7 +2,6 @@
#include <Wire.h>
#include <U8g2lib.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <ArduinoOTA.h>
#include <FastLED.h>
@@ -41,7 +40,6 @@ ESP8266WiFiMulti wifiMulti;
#endif
bool startSetupMode = false;
char DeviceName[33];
Globals_t globals;
uint32_t TravelDistance_highRes;
@@ -84,14 +82,14 @@ Ticker EEPROMCyclicPDSTicker(EEPROMCyclicPDS_callback, 60000, 0, MILLIS);
void setup()
{
system_update_cpu_freq(SYS_CPU_80MHZ);
snprintf(DeviceName, 32, HOST_NAME, ESP.getChipId());
snprintf(globals.DeviceName, 32, HOST_NAME, ESP.getChipId());
WiFi.persistent(false);
ClearAllDTC(); // Init DTC-Storage
#ifdef FEATURE_ENABLE_WIFI_CLIENT
WiFi.mode(WIFI_STA);
WiFi.setHostname(DeviceName);
WiFi.setHostname(globals.DeviceName);
wifiMulti.addAP(QUOTE(WIFI_SSID), QUOTE(WIFI_PASSWORD));
WiFiMaintainConnectionTicker.start();
#else
@@ -102,7 +100,7 @@ void setup()
Serial.setDebugOutput(true);
Serial.println("Souko's ChainLube Mk1");
Serial.println(DeviceName);
Serial.println(globals.DeviceName);
InitEEPROM();
GetConfig_EEPROM();
@@ -141,10 +139,7 @@ void setup()
pinMode(GPIO_PUMP, OUTPUT);
#ifdef FEATURE_ENABLE_REMOTE_DEBUG
if (MDNS.begin(DeviceName))
MDNS.addService("telnet", "tcp", 23);
Debug.begin(DeviceName);
Debug.begin(globals.DeviceName);
Debug.setResetCmdEnabled(true);
Debug.showProfiler(false);
Debug.showColors(true);
@@ -157,7 +152,7 @@ void setup()
#endif
ArduinoOTA.setPort(8266);
ArduinoOTA.setHostname(DeviceName);
ArduinoOTA.setHostname(globals.DeviceName);
ArduinoOTA.setPassword(QUOTE(ADMIN_PASSWORD));
ArduinoOTA.onStart([]()
@@ -302,7 +297,7 @@ void RemoteDebug_formatPersistence()
void RemotDebug_printSystemInfo()
{
debugA("Souko's ChainOiler Mk1");
debugA("Hostname: %s", DeviceName);
debugA("Hostname: %s", globals.DeviceName);
FlashMode_t ideMode = ESP.getFlashChipMode();
debugA("Sdk version: %s", ESP.getSdkVersion());
@@ -719,7 +714,7 @@ void toggleWiFiAP(boolean shutdown)
{
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(IPAddress(WIFI_AP_IP_GW), IPAddress(WIFI_AP_IP_GW), IPAddress(255, 255, 255, 0));
WiFi.softAP(DeviceName, QUOTE(WIFI_AP_PASSWORD));
WiFi.softAP(globals.DeviceName, QUOTE(WIFI_AP_PASSWORD));
#ifdef FEATURE_ENABLE_WIFI_CLIENT
WiFiMaintainConnectionTicker.stop();
debugV("WiFi AP started, stopped Maintain-Timer");

View File

@@ -8,21 +8,30 @@ String processor(const String &var);
void WebserverPOST_Callback(AsyncWebServerRequest *request);
void WebserverNotFound_Callback(AsyncWebServerRequest *request);
void Webserver_Callback(AsyncWebServerRequest *request);
void WebserverFirmwareUpdate_Callback(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final);
uint32_t GetFlashVersion();
void initWebUI()
{
if (!LittleFS.begin())
{
Serial.println("An Error has occurred while mounting LittleFS");
MaintainDTC(DTC_FLASHFS_ERROR, true);
return;
}
MDNS.begin(globals.DeviceName);
MDNS.addService("telnet", "tcp", 23);
MDNS.addService("http", "tcp", 80);
webServer.serveStatic("/static/", LittleFS, "/static/").setCacheControl("max-age=360000");
webServer.on("/", HTTP_GET, [](AsyncWebServerRequest *request)
{ request->redirect("/index.htm"); });
webServer.onNotFound(WebserverNotFound_Callback);
webServer.on("/index.htm", HTTP_GET, Webserver_Callback);
webServer.on("/post.htm", HTTP_POST, WebserverPOST_Callback);
webServer.on(
"/doUpdate", HTTP_POST, [](AsyncWebServerRequest *request) {}, WebserverFirmwareUpdate_Callback);
webServer.begin();
}
@@ -176,6 +185,8 @@ String processor(const String &var)
sprintf(buffer, "%d.%02d", SW_VERSION_MAJOR, SW_VERSION_MINOR);
return String(buffer);
}
if (var == "FS_VERSION")
return String(GetFlashVersion());
if (var == "PLACEHOLDER")
return "placeholder";
@@ -275,4 +286,65 @@ void WebserverPOST_Callback(AsyncWebServerRequest *request)
void WebserverNotFound_Callback(AsyncWebServerRequest *request)
{
request->send(404, "text/html", "Not found");
}
}
uint32_t GetFlashVersion()
{
char buffer[20];
File this_file = LittleFS.open("version", "r");
if (!this_file)
{ // failed to open the file, retrn empty result
return 0;
}
while (this_file.available())
{
this_file.readBytesUntil('\r', buffer, sizeof(buffer));
}
this_file.close();
return atoi(buffer);
}
void WebserverFirmwareUpdate_Callback(AsyncWebServerRequest *request, const String &filename, size_t index, uint8_t *data, size_t len, bool final)
{
if (!index)
{
Serial.println("Update");
size_t content_len = request->contentLength();
// if filename includes spiffs, update the spiffs partition
int cmd = (filename.indexOf(".fs") > -1) ? U_FS : U_FLASH;
Update.runAsync(true);
if (!Update.begin(content_len, cmd))
{
Update.printError(Serial);
}
}
if (Update.write(data, len) != len)
{
Update.printError(Serial);
}
else
{
Serial.printf("Progress: %d%%\n", (Update.progress() * 100) / Update.size());
}
if (final)
{
AsyncWebServerResponse *response = request->beginResponse(302, "text/plain", "Please wait while the device reboots");
response->addHeader("Refresh", "20");
response->addHeader("Location", "/");
request->send(response);
if (!Update.end(true))
{
Update.printError(Serial);
}
else
{
Serial.println("Update complete");
Serial.flush();
ESP.restart();
}
}
}

View File

@@ -6,6 +6,9 @@
#include <LittleFS.h>
#include <ESPAsyncTCP.h>
#include <ESPAsyncWebServer.h>
#include <Updater.h>
#include <ESP8266mDNS.h>
#include "config.h"
#include "globals.h"
#include "dtc.h"