From 66069dc3e49b4b1298da90a6aa8c99510f121c3a Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Thu, 1 Feb 2024 14:37:29 +0100 Subject: [PATCH 01/10] big update --- fullflash.sh | 7 ++++--- platformio.ini | 12 +++++++++--- src/arbitration.cpp | 2 +- src/bus.cpp | 24 ++++++++++++------------ src/client.cpp | 4 ++-- src/main.cpp | 27 +++++++++++++++------------ 6 files changed, 43 insertions(+), 33 deletions(-) diff --git a/fullflash.sh b/fullflash.sh index 24c740a..dd0ce1b 100755 --- a/fullflash.sh +++ b/fullflash.sh @@ -2,8 +2,9 @@ OF=firmware-fullflash-HW_v5.x.bin -dd if=.pio/build/esp32-c3/bootloader.bin of=$OF -dd if=.pio/build/esp32-c3/partitions.bin of=$OF bs=1 seek=$((0x8000)) -dd if=$HOME/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin of=$OF bs=1 seek=$((0xe000)) +tr '\0' '\377' < /dev/zero | dd bs=1 count=$((0x10000)) of=$OF +dd if=.pio/build/esp32-c3/bootloader.bin of=$OF conv=notrunc +dd if=.pio/build/esp32-c3/partitions.bin of=$OF bs=1 seek=$((0x8000)) conv=notrunc +dd if=$HOME/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin of=$OF bs=1 seek=$((0xe000)) conv=notrunc dd if=.pio/build/esp32-c3/firmware.bin of=$OF bs=1 seek=$((0x10000)) diff --git a/platformio.ini b/platformio.ini index 52921d9..eed54d4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,7 +11,7 @@ [env] framework = arduino lib_deps = - https://github.com/tzapu/WiFiManager#v2.0.14-beta + https://github.com/tzapu/WiFiManager#0d84861 extra_scripts = pre:auto_firmware_version.py @@ -23,8 +23,10 @@ upload_speed = 921600 build_flags = -DRESET_PIN=5 -DTX_DISABLE_PIN=5 + -DBusSer=Serial + -DDebugSer=Serial1 lib_deps = - https://github.com/tzapu/WiFiManager#v2.0.14-beta + ${env.lib_deps} https://github.com/marvinroger/ESP8266TrueRandom vshymanskyy/Preferences@^2.1.0 @@ -50,9 +52,13 @@ board = esp32-c3-devkitm-1 build_flags = -DRESET_PIN=20 -DPWM_PIN=6 + -DARDUINO_USB_MODE=1 + -DARDUINO_USB_CDC_ON_BOOT=1 + -DBusSer=Serial1 + -DDebugSer=HWCDCSerial lib_deps = - https://github.com/tzapu/WiFiManager#v2.0.14-beta + ${env.lib_deps} https://github.com/guido4096/espsoftwareserial.git#add-startbit-timestamp [env:esp32-c3-ota] diff --git a/src/arbitration.cpp b/src/arbitration.cpp index a37b9e5..82a7397 100644 --- a/src/arbitration.cpp +++ b/src/arbitration.cpp @@ -27,7 +27,7 @@ Arbitration::result Arbitration::start(BusState& busstate, uint8_t master, unsig if (timeSinceStartBit > 4456 || Bus.available()) { // if we are too late, don't try to participate and retry next round - DEBUG_LOG("ARB LATE 0x%02x %lu us\n", Serial.peek(), timeSinceStartBit); + DEBUG_LOG("ARB LATE 0x%02x %lu us\n", BusSer.peek(), timeSinceStartBit); return late; } #if USE_ASYNCHRONOUS diff --git a/src/bus.cpp b/src/bus.cpp index 0fc4b0e..9ffa3f3 100644 --- a/src/bus.cpp +++ b/src/bus.cpp @@ -119,21 +119,21 @@ void BusType::begin() { #if USE_SOFTWARE_SERIAL #if defined(ESP32) - Serial.begin(2400, SERIAL_8N1, -1, UART_TX); // used for writing + BusSer.begin(2400, SERIAL_8N1, -1, UART_TX); // used for writing #elif defined(ESP8266) - Serial.begin(2400, SERIAL_8N1, SERIAL_TX_ONLY, UART_TX); + BusSer.begin(2400, SERIAL_8N1, SERIAL_TX_ONLY, UART_TX); #endif mySerial.enableStartBitTimeStampRecording(true); mySerial.enableTx(false); mySerial.enableIntTx(false); mySerial.begin(2400, SWSERIAL_8N1, UART_RX, -1, false, RXBUFFERSIZE); // used for reading #else - Serial.setRxBufferSize(RXBUFFERSIZE); + BusSer.setRxBufferSize(RXBUFFERSIZE); #if defined(ESP32) - Serial.begin(2400, SERIAL_8N1, UART_RX, UART_TX); // used for writing - Serial.setRxFIFOFull(1); + BusSer.begin(2400, SERIAL_8N1, UART_RX, UART_TX); // used for writing + BusSer.setRxFIFOFull(1); #elif defined(ESP8266) - Serial.begin(2400); + BusSer.begin(2400); #endif #endif @@ -145,7 +145,7 @@ void BusType::begin() { } void BusType::end() { - Serial.end(); + BusSer.end(); #if USE_SOFTWARE_SERIAL mySerial.end(); #endif @@ -161,11 +161,11 @@ void BusType::end() { int BusType::availableForWrite() { - return Serial.availableForWrite(); + return BusSer.availableForWrite(); } size_t BusType::write(uint8_t symbol) { - return Serial.write(symbol); + return BusSer.write(symbol); } bool BusType::read(data& d) { @@ -178,8 +178,8 @@ bool BusType::read(data& d) { receive(symbol, mySerial.readStartBitTimeStamp()); } #else - if (Serial.available()){ - uint8_t symbol = Serial.read(); + if (BusSer.available()){ + uint8_t symbol = BusSer.read(); receive(symbol, micros()); } #endif @@ -196,7 +196,7 @@ int BusType::available() { #if USE_SOFTWARE_SERIAL return mySerial.available(); #else - return Serial.available(); + return BusSer.available(); #endif } diff --git a/src/client.cpp b/src/client.cpp index b4cc8af..cc49ccf 100644 --- a/src/client.cpp +++ b/src/client.cpp @@ -9,7 +9,7 @@ bool handleNewClient(WiFiServer &server, WiFiClient clients[]) { int i; for (i = 0; i < MAX_SRV_CLIENTS; i++) { if (!clients[i]) { // equivalent to !serverClients[i].connected() - clients[i] = server.available(); + clients[i] = server.accept(); clients[i].setNoDelay(true); break; } @@ -17,7 +17,7 @@ bool handleNewClient(WiFiServer &server, WiFiClient clients[]) { // No free/disconnected slot so reject if (i == MAX_SRV_CLIENTS) { - server.available().println("busy"); + server.accept().println("busy"); // hints: server.available() is a WiFiClient with short-term scope // when out of scope, a WiFiClient will // - flush() - all data will be sent diff --git a/src/main.cpp b/src/main.cpp index 052d03d..bbdea91 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,7 +18,6 @@ Preferences preferences; #define ALPHA 0.3 -#define PWM_CHANNEL 0 #define PWM_FREQ 10000 #define PWM_RESOLUTION 8 @@ -29,7 +28,7 @@ Preferences preferences; TaskHandle_t Task1; #endif -WiFiManager wifiManager(Serial1); +WiFiManager wifiManager(DebugSer); WiFiManagerParameter param_pwm_value("pwm_value", "PWM value", "", 6); WiFiServer wifiServer(3333); @@ -65,7 +64,13 @@ void on_connected(WiFiEvent_t event, WiFiEventInfo_t info){ void wdt_start() { #ifdef ESP32 - esp_task_wdt_init(6, true); +//?? + esp_task_wdt_config_t wdt_config = { + .timeout_ms = 6000, + .idle_core_mask = (1 << portNUM_PROCESSORS) - 1, + .trigger_panic = 1, + }; + esp_task_wdt_init(&wdt_config); #elif defined(ESP8266) ESP.wdtDisable(); #endif @@ -96,13 +101,13 @@ inline void enableTX() { void set_pwm(uint8_t value){ #ifdef PWM_PIN - ledcWrite(PWM_CHANNEL, value); + ledcWrite(PWM_PIN, value); #endif } uint32_t get_pwm(){ #ifdef PWM_PIN - return ledcRead(PWM_CHANNEL); + return ledcRead(PWM_PIN); #endif return 0; } @@ -191,7 +196,7 @@ void saveParamsCallback () { set_pwm(new_pwm_value); preferences.putUInt("pwm_value", new_pwm_value); } - Serial1.printf("pwm_value set: %s %d\n", param_pwm_value.getValue(), new_pwm_value); + DebugSer.printf("pwm_value set: %s %d\n", param_pwm_value.getValue(), new_pwm_value); } void setup() { @@ -200,21 +205,19 @@ void setup() { check_reset(); #ifdef ESP32 - Serial1.begin(115200, SERIAL_8N1, 10, 9); last_reset_code = rtc_get_reset_reason(0); #elif defined(ESP8266) - Serial1.begin(115200); last_reset_code = (int) ESP.getResetInfoPtr(); #endif Bus.begin(); - Serial1.setDebugOutput(true); + DebugSer.begin(115200); + DebugSer.setDebugOutput(true); disableTX(); #ifdef PWM_PIN - ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION); - ledcAttachPin(PWM_PIN, PWM_CHANNEL); + ledcAttach(PWM_PIN, PWM_FREQ, PWM_RESOLUTION); #endif set_pwm(preferences.getUInt("pwm_value", 130)); @@ -267,7 +270,7 @@ bool handleStatusServerRequests() { if (!statusServer.hasClient()) return false; - WiFiClient client = statusServer.available(); + WiFiClient client = statusServer.accept(); if (client.availableForWrite() >= AVAILABLE_THRESHOLD) { client.printf("async mode: %s\n", USE_ASYNCHRONOUS ? "true" : "false"); From 7fbbd0f2f0404f26d871afd8f6774ae5df101cfa Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Thu, 1 Feb 2024 15:12:16 +0100 Subject: [PATCH 02/10] fix to arduino2 --- platformio.ini | 4 ++-- src/main.cpp | 16 ++++++---------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/platformio.ini b/platformio.ini index eed54d4..3ca07a0 100644 --- a/platformio.ini +++ b/platformio.ini @@ -47,7 +47,7 @@ upload_port = esp-ebus.local upload_protocol = espota [env:esp32-c3] -platform = espressif32 +platform = espressif32@6.5.0 board = esp32-c3-devkitm-1 build_flags = -DRESET_PIN=20 @@ -55,7 +55,7 @@ build_flags = -DARDUINO_USB_MODE=1 -DARDUINO_USB_CDC_ON_BOOT=1 -DBusSer=Serial1 - -DDebugSer=HWCDCSerial + -DDebugSer=Serial lib_deps = ${env.lib_deps} diff --git a/src/main.cpp b/src/main.cpp index bbdea91..f80db2e 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -18,6 +18,7 @@ Preferences preferences; #define ALPHA 0.3 +#define PWM_CHANNEL 0 #define PWM_FREQ 10000 #define PWM_RESOLUTION 8 @@ -64,13 +65,7 @@ void on_connected(WiFiEvent_t event, WiFiEventInfo_t info){ void wdt_start() { #ifdef ESP32 -//?? - esp_task_wdt_config_t wdt_config = { - .timeout_ms = 6000, - .idle_core_mask = (1 << portNUM_PROCESSORS) - 1, - .trigger_panic = 1, - }; - esp_task_wdt_init(&wdt_config); + esp_task_wdt_init(6, true); #elif defined(ESP8266) ESP.wdtDisable(); #endif @@ -101,13 +96,13 @@ inline void enableTX() { void set_pwm(uint8_t value){ #ifdef PWM_PIN - ledcWrite(PWM_PIN, value); + ledcWrite(PWM_CHANNEL, value); #endif } uint32_t get_pwm(){ #ifdef PWM_PIN - return ledcRead(PWM_PIN); + return ledcRead(PWM_CHANNEL); #endif return 0; } @@ -217,7 +212,8 @@ void setup() { disableTX(); #ifdef PWM_PIN - ledcAttach(PWM_PIN, PWM_FREQ, PWM_RESOLUTION); + ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION); + ledcAttachPin(PWM_PIN, PWM_CHANNEL); #endif set_pwm(preferences.getUInt("pwm_value", 130)); From dbf1ebedcb106aeeac5373663e89a8a4c8aaa184 Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Thu, 1 Feb 2024 15:17:30 +0100 Subject: [PATCH 03/10] add missing build flags --- platformio.ini | 3 +++ 1 file changed, 3 insertions(+) diff --git a/platformio.ini b/platformio.ini index 3ca07a0..99b4ab4 100644 --- a/platformio.ini +++ b/platformio.ini @@ -25,6 +25,7 @@ build_flags = -DTX_DISABLE_PIN=5 -DBusSer=Serial -DDebugSer=Serial1 + lib_deps = ${env.lib_deps} https://github.com/marvinroger/ESP8266TrueRandom @@ -40,6 +41,8 @@ extends = env:esp12e build_flags = -DRESET_PIN=5 -DTX_DISABLE_PIN=2 + -DBusSer=Serial + -DDebugSer=Serial1 [env:esp12e-v3-ota] extends = env:esp12e-v3 From 7fef8634bc253da57629579452de7ca23247eaea Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Fri, 2 Feb 2024 04:57:18 +0100 Subject: [PATCH 04/10] make channel random for esp32 --- src/main.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main.cpp b/src/main.cpp index f80db2e..614b44b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -50,7 +50,7 @@ int reconnectCount = 0; int random_ch(){ #ifdef ESP32 - return 6; + return esp_random() % 13 + 1 ; #elif defined(ESP8266) return ESP8266TrueRandom.random(1, 13); #endif From 2cdfcf05e4c8eb603a8a80b1a5f817a0c88a6e0e Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Fri, 2 Feb 2024 08:37:11 +0100 Subject: [PATCH 05/10] switch to iotwebconf --- platformio.ini | 5 +++- src/main.cpp | 79 ++++++++++++++++++++++++++++++++++++++++++-------- 2 files changed, 71 insertions(+), 13 deletions(-) diff --git a/platformio.ini b/platformio.ini index 99b4ab4..4d79c84 100644 --- a/platformio.ini +++ b/platformio.ini @@ -11,9 +11,10 @@ [env] framework = arduino lib_deps = - https://github.com/tzapu/WiFiManager#0d84861 + https://github.com/prampec/IotWebConf#v3.2.1 extra_scripts = pre:auto_firmware_version.py +monitor_filters = esp32_exception_decoder [env:esp12e] platform = espressif8266 @@ -60,6 +61,8 @@ build_flags = -DBusSer=Serial1 -DDebugSer=Serial +monitor_filters = esp32_exception_decoder + lib_deps = ${env.lib_deps} https://github.com/guido4096/espsoftwareserial.git#add-startbit-timestamp diff --git a/src/main.cpp b/src/main.cpp index 614b44b..aa519da 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,6 @@ #include -#include //https://github.com/tzapu/WiFiManager WiFi Configuration Magic +#include +#include #include "main.hpp" #include "enhanced.hpp" #include "bus.hpp" @@ -29,8 +30,12 @@ Preferences preferences; TaskHandle_t Task1; #endif -WiFiManager wifiManager(DebugSer); -WiFiManagerParameter param_pwm_value("pwm_value", "PWM value", "", 6); +#define CONFIG_VERSION "eea" +DNSServer dnsServer; +WebServer configServer(80); +char pwm_value_string[8]; +IotWebConf iotWebConf(HOSTNAME, &dnsServer, &configServer, "", CONFIG_VERSION); +IotWebConfNumberParameter pwm_value_param = IotWebConfNumberParameter("PWM value", "pwm_value", pwm_value_string, 8, "130", "1..255", "min='1' max='255' step='1'"); WiFiServer wifiServer(3333); WiFiServer wifiServerRO(3334); @@ -185,13 +190,29 @@ void data_loop(void * pvParameters){ } } + void saveParamsCallback () { - uint8_t new_pwm_value = atoi(param_pwm_value.getValue()); + + uint8_t new_pwm_value = atoi(pwm_value_string); if (new_pwm_value > 0){ set_pwm(new_pwm_value); preferences.putUInt("pwm_value", new_pwm_value); } - DebugSer.printf("pwm_value set: %s %d\n", param_pwm_value.getValue(), new_pwm_value); + DebugSer.printf("pwm_value set: %s %d\n", pwm_value_string, new_pwm_value); + +} + +void handleRoot() +{ + // -- Let IotWebConf test and handle captive portal requests. + if (iotWebConf.handleCaptivePortal()) + { + // -- Captive portal request were already served. + return; + } + String s = "Hello World"; + + configServer.send(200, "text/html", s); } void setup() { @@ -220,25 +241,57 @@ void setup() { if (preferences.getBool("firstboot", true)){ preferences.putBool("firstboot", false); - WiFi.begin(DEFAULT_AP, DEFAULT_PASS); + iotWebConf.init(); + strncpy(iotWebConf.getApPasswordParameter()->valueBuffer, "ebusebus", IOTWEBCONF_WORD_LEN); + strncpy(iotWebConf.getWifiSsidParameter()->valueBuffer, "ebus-test", IOTWEBCONF_WORD_LEN); + strncpy(iotWebConf.getWifiPasswordParameter()->valueBuffer, "lectronz", IOTWEBCONF_WORD_LEN); + iotWebConf.saveConfig(); + } else { + iotWebConf.skipApStartup(); } - - WiFi.enableAP(false); +/* + WiFi.enableAP(true); WiFi.begin(); - +*/ #ifdef ESP32 WiFi.onEvent(on_connected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED); #endif + int wifi_ch = random_ch(); + DebugSer.printf("Channel for AP mode: %d\n", wifi_ch); + + iotWebConf.addSystemParameter(&pwm_value_param); + iotWebConf.setConfigSavedCallback(&saveParamsCallback); + iotWebConf.getApTimeoutParameter()->visible = true; + iotWebConf.setWifiConnectionTimeoutMs(7000); + + // -- Initializing the configuration. + iotWebConf.init(); + + // -- Set up required URL handlers on the web server. + configServer.on("/", []{ iotWebConf.handleConfig(); }); + configServer.on("/config", []{ iotWebConf.handleConfig(); }); + configServer.onNotFound([](){ iotWebConf.handleNotFound(); }); + while (iotWebConf.getState() != iotwebconf::NetworkState::OnLine){ + iotWebConf.doLoop(); + } + +/* wifiManager.setSaveParamsCallback(saveParamsCallback); wifiManager.addParameter(¶m_pwm_value); wifiManager.setHostname(HOSTNAME); wifiManager.setConfigPortalTimeout(120); - wifiManager.setWiFiAPChannel(random_ch()); + wifiManager.setWiFiAPChannel(wifi_ch); wifiManager.autoConnect(HOSTNAME); wifiManager.startWebPortal(); - + */ +/* + WiFi.channel(wifi_ch); + if (WiFi.status() != WL_CONNECTED) { + delay(1000); + } + */ wifiServer.begin(); wifiServerRO.begin(); wifiServerEnh.begin(); @@ -298,6 +351,8 @@ bool handleStatusServerRequests() { } void loop() { + iotWebConf.doLoop(); + ArduinoOTA.handle(); #ifdef ESP8266 @@ -309,7 +364,7 @@ void loop() { wdt_feed(); #ifdef ESP32 - wifiManager.process(); + //wifiManager.process(); #endif if (WiFi.status() != WL_CONNECTED) { From 128c1852f0b3fe3e673a1c765d76317b98aebde4 Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Fri, 2 Feb 2024 14:15:20 +0100 Subject: [PATCH 06/10] cleanup plus status led --- platformio.ini | 1 + src/main.cpp | 31 ++++++++----------------------- 2 files changed, 9 insertions(+), 23 deletions(-) diff --git a/platformio.ini b/platformio.ini index 4d79c84..519fb8d 100644 --- a/platformio.ini +++ b/platformio.ini @@ -60,6 +60,7 @@ build_flags = -DARDUINO_USB_CDC_ON_BOOT=1 -DBusSer=Serial1 -DDebugSer=Serial + -DSTATUS_LED_PIN=3 monitor_filters = esp32_exception_decoder diff --git a/src/main.cpp b/src/main.cpp index aa519da..db1f65a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -249,22 +249,24 @@ void setup() { } else { iotWebConf.skipApStartup(); } -/* - WiFi.enableAP(true); - WiFi.begin(); -*/ + #ifdef ESP32 WiFi.onEvent(on_connected, WiFiEvent_t::ARDUINO_EVENT_WIFI_STA_CONNECTED); #endif int wifi_ch = random_ch(); DebugSer.printf("Channel for AP mode: %d\n", wifi_ch); + WiFi.channel(wifi_ch); // doesn't work, https://github.com/prampec/IotWebConf/issues/286 iotWebConf.addSystemParameter(&pwm_value_param); iotWebConf.setConfigSavedCallback(&saveParamsCallback); iotWebConf.getApTimeoutParameter()->visible = true; iotWebConf.setWifiConnectionTimeoutMs(7000); +#ifdef STATUS_LED_PIN + iotWebConf.setStatusPin(STATUS_LED_PIN); +#endif + // -- Initializing the configuration. iotWebConf.init(); @@ -272,26 +274,11 @@ void setup() { configServer.on("/", []{ iotWebConf.handleConfig(); }); configServer.on("/config", []{ iotWebConf.handleConfig(); }); configServer.onNotFound([](){ iotWebConf.handleNotFound(); }); + while (iotWebConf.getState() != iotwebconf::NetworkState::OnLine){ iotWebConf.doLoop(); } -/* - wifiManager.setSaveParamsCallback(saveParamsCallback); - wifiManager.addParameter(¶m_pwm_value); - - wifiManager.setHostname(HOSTNAME); - wifiManager.setConfigPortalTimeout(120); - wifiManager.setWiFiAPChannel(wifi_ch); - wifiManager.autoConnect(HOSTNAME); - wifiManager.startWebPortal(); - */ -/* - WiFi.channel(wifi_ch); - if (WiFi.status() != WL_CONNECTED) { - delay(1000); - } - */ wifiServer.begin(); wifiServerRO.begin(); wifiServerEnh.begin(); @@ -351,8 +338,6 @@ bool handleStatusServerRequests() { } void loop() { - iotWebConf.doLoop(); - ArduinoOTA.handle(); #ifdef ESP8266 @@ -364,7 +349,7 @@ void loop() { wdt_feed(); #ifdef ESP32 - //wifiManager.process(); + iotWebConf.doLoop(); #endif if (WiFi.status() != WL_CONNECTED) { From eea25dcfbfff4eeab77f8845cda51c0aa617f49c Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Sat, 3 Feb 2024 20:33:19 +0100 Subject: [PATCH 07/10] add status via http --- src/main.cpp | 61 +++++++++++++++++++++++++++++++++------------------- 1 file changed, 39 insertions(+), 22 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index db1f65a..6901709 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -202,6 +202,42 @@ void saveParamsCallback () { } +char* status_string(){ + static char status[1024]; + + int pos = 0; + + pos += sprintf(status + pos, "async mode: %s\n", USE_ASYNCHRONOUS ? "true" : "false"); + pos += sprintf(status + pos, "software serial mode: %s\n", USE_SOFTWARE_SERIAL ? "true" : "false"); + pos += sprintf(status + pos, "uptime: %ld ms\n", millis()); + pos += sprintf(status + pos, "last_connect_time: %ld ms\n", lastConnectTime); + pos += sprintf(status + pos, "reconnect_count: %d \n", reconnectCount); + pos += sprintf(status + pos, "rssi: %d dBm\n", WiFi.RSSI()); + pos += sprintf(status + pos, "free_heap: %d B\n", ESP.getFreeHeap()); + pos += sprintf(status + pos, "reset_code: %d\n", last_reset_code); + pos += sprintf(status + pos, "loop_duration: %ld us\r\n", loopDuration); + pos += sprintf(status + pos, "max_loop_duration: %ld us\r\n", maxLoopDuration); + pos += sprintf(status + pos, "version: %s\r\n", AUTO_VERSION); + pos += sprintf(status + pos, "nbr arbitrations: %i\r\n", (int)Bus._nbrArbitrations); + pos += sprintf(status + pos, "nbr restarts1: %i\r\n", (int)Bus._nbrRestarts1); + pos += sprintf(status + pos, "nbr restarts2: %i\r\n", (int)Bus._nbrRestarts2); + pos += sprintf(status + pos, "nbr lost1: %i\r\n", (int)Bus._nbrLost1); + pos += sprintf(status + pos, "nbr lost2: %i\r\n", (int)Bus._nbrLost2); + pos += sprintf(status + pos, "nbr won1: %i\r\n", (int)Bus._nbrWon1); + pos += sprintf(status + pos, "nbr won2: %i\r\n", (int)Bus._nbrWon2); + pos += sprintf(status + pos, "nbr late: %i\r\n", (int)Bus._nbrLate); + pos += sprintf(status + pos, "nbr errors: %i\r\n", (int)Bus._nbrErrors); + pos += sprintf(status + pos, "pwm_value: %i\r\n", get_pwm()); + + return status; +} + +void handleStatus() +{ + configServer.send(200, "text/plain", status_string()); +} + + void handleRoot() { // -- Let IotWebConf test and handle captive portal requests. @@ -273,6 +309,8 @@ void setup() { // -- Set up required URL handlers on the web server. configServer.on("/", []{ iotWebConf.handleConfig(); }); configServer.on("/config", []{ iotWebConf.handleConfig(); }); + configServer.on("/param", []{ iotWebConf.handleConfig(); }); + configServer.on("/status", []{ handleStatus(); }); configServer.onNotFound([](){ iotWebConf.handleNotFound(); }); while (iotWebConf.getState() != iotwebconf::NetworkState::OnLine){ @@ -309,28 +347,7 @@ bool handleStatusServerRequests() { WiFiClient client = statusServer.accept(); if (client.availableForWrite() >= AVAILABLE_THRESHOLD) { - client.printf("async mode: %s\n", USE_ASYNCHRONOUS ? "true" : "false"); - client.printf("software serial mode: %s\n", USE_SOFTWARE_SERIAL ? "true" : "false"); - client.printf("uptime: %ld ms\n", millis()); - client.printf("last_connect_time: %ld ms\n", lastConnectTime); - client.printf("reconnect_count: %d \n", reconnectCount); - client.printf("rssi: %d dBm\n", WiFi.RSSI()); - client.printf("free_heap: %d B\n", ESP.getFreeHeap()); - client.printf("reset_code: %d\n", last_reset_code); - client.printf("loop_duration: %ld us\r\n", loopDuration); - client.printf("max_loop_duration: %ld us\r\n", maxLoopDuration); - client.printf("version: %s\r\n", AUTO_VERSION); - client.printf("nbr arbitrations: %i\r\n", (int)Bus._nbrArbitrations); - client.printf("nbr restarts1: %i\r\n", (int)Bus._nbrRestarts1); - client.printf("nbr restarts2: %i\r\n", (int)Bus._nbrRestarts2); - client.printf("nbr lost1: %i\r\n", (int)Bus._nbrLost1); - client.printf("nbr lost2: %i\r\n", (int)Bus._nbrLost2); - client.printf("nbr won1: %i\r\n", (int)Bus._nbrWon1); - client.printf("nbr won2: %i\r\n", (int)Bus._nbrWon2); - client.printf("nbr late: %i\r\n", (int)Bus._nbrLate); - client.printf("nbr errors: %i\r\n", (int)Bus._nbrErrors); - client.printf("pwm_value: %i\r\n", get_pwm()); - + client.print(status_string()); client.flush(); client.stop(); } From abb91b832b01b1c17a82b499c649d806f983def0 Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Sat, 3 Feb 2024 20:52:00 +0100 Subject: [PATCH 08/10] add homepage --- src/main.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 6901709..f9baf44 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,6 +25,7 @@ Preferences preferences; #define DEFAULT_AP "ebus-test" #define DEFAULT_PASS "lectronz" +#define DEFAULT_APMODE_PASS "ebusebus" #ifdef ESP32 TaskHandle_t Task1; @@ -246,7 +247,16 @@ void handleRoot() // -- Captive portal request were already served. return; } - String s = "Hello World"; + String s = "esp-eBus adapter"; + s += ""; + s += ""; + s += "Adapter status
"; + s += "Configuration - user: admin password: your configured AP mode password or default: "; + s += DEFAULT_APMODE_PASS; + s += "
"; + s += "
"; + s += "For more info see project page: https://github.com/danielkucera/esp-arduino-ebus"; + s += ""; configServer.send(200, "text/html", s); } @@ -278,7 +288,7 @@ void setup() { if (preferences.getBool("firstboot", true)){ preferences.putBool("firstboot", false); iotWebConf.init(); - strncpy(iotWebConf.getApPasswordParameter()->valueBuffer, "ebusebus", IOTWEBCONF_WORD_LEN); + strncpy(iotWebConf.getApPasswordParameter()->valueBuffer, DEFAULT_APMODE_PASS, IOTWEBCONF_WORD_LEN); strncpy(iotWebConf.getWifiSsidParameter()->valueBuffer, "ebus-test", IOTWEBCONF_WORD_LEN); strncpy(iotWebConf.getWifiPasswordParameter()->valueBuffer, "lectronz", IOTWEBCONF_WORD_LEN); iotWebConf.saveConfig(); @@ -307,7 +317,7 @@ void setup() { iotWebConf.init(); // -- Set up required URL handlers on the web server. - configServer.on("/", []{ iotWebConf.handleConfig(); }); + configServer.on("/", []{ handleRoot(); }); configServer.on("/config", []{ iotWebConf.handleConfig(); }); configServer.on("/param", []{ iotWebConf.handleConfig(); }); configServer.on("/status", []{ handleStatus(); }); From 954a96580844ff09af377679e1546260cfb214f0 Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Sat, 3 Feb 2024 21:26:11 +0100 Subject: [PATCH 09/10] add firmware update --- src/main.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/main.cpp b/src/main.cpp index f9baf44..8daaf3c 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -12,9 +12,15 @@ Preferences preferences; #include #include #include "esp32c3/rom/rtc.h" + #include + + HTTPUpdateServer httpUpdater; #else #include #include + #include + + ESP8266HTTPUpdateServer httpUpdater; #endif #define ALPHA 0.3 @@ -254,6 +260,7 @@ void handleRoot() s += "Configuration - user: admin password: your configured AP mode password or default: "; s += DEFAULT_APMODE_PASS; s += "
"; + s += "Firmware update
"; s += "
"; s += "For more info see project page: https://github.com/danielkucera/esp-arduino-ebus"; s += ""; @@ -323,6 +330,10 @@ void setup() { configServer.on("/status", []{ handleStatus(); }); configServer.onNotFound([](){ iotWebConf.handleNotFound(); }); + iotWebConf.setupUpdateServer( + [](const char* updatePath) { httpUpdater.setup(&configServer, updatePath); }, + [](const char* userName, char* password) { httpUpdater.updateCredentials(userName, password); }); + while (iotWebConf.getState() != iotwebconf::NetworkState::OnLine){ iotWebConf.doLoop(); } From 92a70c0f9a367695852b35263f85f35d001b9014 Mon Sep 17 00:00:00 2001 From: Daniel Kucera Date: Sat, 3 Feb 2024 22:41:41 +0100 Subject: [PATCH 10/10] updated readme --- README.md | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 3ea97b9..ac6003d 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ - count the turns between these positions and set the trimmer in the middle position with D1 blinking - if you have adjusted the trimmer, disconnect and connect the adapter to bus again - search for WiFi networks, you should see network with name "esp-eBus" -- connect to the network - a configuration page should open automatically +- connect to the network - a configuration page should open automatically, default password is: ebusebus - configure your WiFi network settings (SSID, password) - after reboot, you should be able to run `ping esp-ebus.local` successfully from a computer in your network (if your network is correctly configured for mDNS) - to verify there are bytes being received by the adapter, you can connect to `esp-ebus.local` port `3334` using telnet - you should see unreadable binary data @@ -138,15 +138,15 @@ There are following options: - heavier option - it will compile the firmware from source code and upload using internall tooling - [using espota.py](#espotapy) - lightweight - just needs OTA script and precompiled firmware file -- physically using a USB-TTL adaptor +- physically using a USB-TTL adaptor or device USB port (HW v5.0+) ### web interface -- [reset device](#config-reset) to access config portal -- connect to the esp-eBus WiFi network -- click blue `Update` button +- open web interface of the device by IP or on: http://esp-ebus.local +- find the update link - upload correct firmware file (see hardware revisions) -- click red `Update` button -- wait for restart, reconnect to adapter and configure WiFi +- click `Update` button +- wait for restart, reconnect to adapter and configure WiFi if not connected automatically +- in case you cannot open web interface, [reset device](#config-reset) to access it ### platform.io - clone this repository using git @@ -179,7 +179,19 @@ Uploading: [============================================================] 100% D 16:33:31 [INFO]: Result: OK ``` -### upgrading using USB-TTL adaptor +### upgrading over USB (HW v5.0+) + - this version has built-in USB serial interface + - download `firmware-fullflash-*` firmware from https://github.com/danielkucera/esp8266-arduino-ebus/releases + - connect `PROG` and `GND` + - connect adapter to a PC using USB-A - USB-C cable + - you should see a new serial port + - flash the firmware to address 0x0 using either one of tools: + - Web based: https://adafruit.github.io/Adafruit_WebSerial_ESPTool/ + - Windows: using Flash Download Tools from https://www.espressif.com/en/support/download/other-tools + - Linux esptool.py: `esptool.py write_flash 0x0 firmware-fullflash-*` + + +### upgrading over USB-TTL adaptor (before HW v5.0) You will need an USB-TTL adaptor (dongle) which suports 3V3 voltage levels and has 3V3 voltage output pin - download firmware bin file from https://github.com/danielkucera/esp8266-arduino-ebus/releases - download NodeMCU PyFlasher from https://github.com/marcelstoer/nodemcu-pyflasher/releases