From ad2c8898890f6cb90c272d4117e8a9ada197c895 Mon Sep 17 00:00:00 2001 From: cyberman54 Date: Wed, 3 Mar 2021 19:16:14 +0100 Subject: [PATCH] maintenance mode adjustments --- include/globals.h | 1 - src/main.cpp | 7 ------- src/mqttclient.cpp | 7 +++++++ src/ota.cpp | 16 ++++++++++------ 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/include/globals.h b/include/globals.h index e236998f..1cb8b349 100644 --- a/include/globals.h +++ b/include/globals.h @@ -149,6 +149,5 @@ extern TaskHandle_t irqHandlerTask, ClockTask, macProcessTask; extern TimerHandle_t WifiChanTimer; extern Timezone myTZ; extern RTC_DATA_ATTR runmode_t RTC_runmode; -extern char clientId[20]; // generated device name #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index e3b2b59f..850a0123 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -93,7 +93,6 @@ uint8_t batt_level = 0; // display value uint8_t volatile channel = WIFI_CHANNEL_MIN; // channel rotation counter uint8_t volatile rf_load = 0; // RF traffic indicator uint16_t volatile macs_wifi = 0, macs_ble = 0; // globals for display -char clientId[20]; // generated device name hw_timer_t *ppsIRQ = NULL, *displayIRQ = NULL, *matrixDisplayIRQ = NULL; @@ -143,12 +142,6 @@ void setup() { do_after_reset(); - // generate unique clientId from device's MAC - uint8_t mac[6]; - esp_eth_get_mac(mac); - const uint32_t hashedmac = hash((const char *)mac, 6); - snprintf(clientId, 20, "paxcounter_%08x", hashedmac); - // print chip information on startup if in verbose mode after coldstart #if (VERBOSE) diff --git a/src/mqttclient.cpp b/src/mqttclient.cpp index 4948409b..89d54fde 100644 --- a/src/mqttclient.cpp +++ b/src/mqttclient.cpp @@ -43,6 +43,13 @@ esp_err_t mqtt_init(void) { int mqtt_connect(const char *my_host, const uint16_t my_port) { IPAddress mqtt_server_ip; + uint8_t mac[6]; + char clientId[20]; + + // hash 6 byte MAC to 4 byte hash + esp_eth_get_mac(mac); + const uint32_t hashedmac = hash((const char *)mac, 6); + snprintf(clientId, 20, "paxcounter_%08x", hashedmac); ESP_LOGI(TAG, "MQTT name is %s", MQTT_CLIENTNAME); diff --git a/src/ota.cpp b/src/ota.cpp index bfc1d4a9..215d72d6 100644 --- a/src/ota.cpp +++ b/src/ota.cpp @@ -330,15 +330,15 @@ void show_progress(unsigned long current, unsigned long size) { #endif } -// start local web user with user interface for maintenance mode -// currently used only for manually uploading a firmware file via wifi +// start local web server with user interface for maintenance mode +// used for manually uploading a firmware file via wifi void start_maintenance(void) { // code snippets taken from - // https://github.com/espressif/arduino-esp32/blob/master/libraries/ArduinoOTA/examples/OTAWebUpdater/OTAWebUpdater.ino + // github.com/espressif/arduino-esp32/blob/master/libraries/ArduinoOTA/examples/OTAWebUpdater/OTAWebUpdater.ino - const char *host = MQTT_CLIENTNAME; + const char *host = "paxcounter"; const char *ssid = WIFI_SSID; const char *password = WIFI_PASS; @@ -388,10 +388,11 @@ void start_maintenance(void) { WiFi.begin(ssid, password); // Wait for connection - while (WiFi.status() != WL_CONNECTED) { + while (WiFi.status() != WL_CONNECTED) delay(500); - } + ESP_LOGI(TAG, "Connected to %s", ssid); + ESP_LOGI(TAG, "Open http://%s.local in your browser", host); // use mdns for host name resolution if (!MDNS.begin(host)) { @@ -399,6 +400,7 @@ void start_maintenance(void) { delay(3000); do_reset(false); } + server.on("/", HTTP_GET, [&server, &serverIndex]() { server.sendHeader("Connection", "close"); server.send(200, "text/html", serverIndex); @@ -439,7 +441,9 @@ void start_maintenance(void) { do_reset(false); } }); + server.begin(); + MDNS.addService("http", "tcp", 80); while (1) { server.handleClient();