From 4c3852cde41e3cd43110808390d5e40158763c3b Mon Sep 17 00:00:00 2001 From: lumapu Date: Tue, 3 May 2022 23:29:33 +0200 Subject: [PATCH] * fixed erase settings * fixed behavior if no MQTT IP is set (the system was nearly unusable because of delayed responses) * fixed Station / AP WiFi on startup -> more information will be printed to the serial console * added new ticker for serial value dump --- tools/esp8266/app.cpp | 54 ++++++++++++++++++++----------- tools/esp8266/app.h | 3 ++ tools/esp8266/defines.h | 2 +- tools/esp8266/html/h/index_html.h | 2 +- tools/esp8266/html/h/setup_html.h | 2 +- tools/esp8266/html/index.html | 2 +- tools/esp8266/html/setup.html | 10 +++--- tools/esp8266/main.cpp | 33 ++++++++++++++----- tools/esp8266/main.h | 12 ++++--- 9 files changed, 81 insertions(+), 39 deletions(-) diff --git a/tools/esp8266/app.cpp b/tools/esp8266/app.cpp index 53b7a93f..b3fb0e2e 100644 --- a/tools/esp8266/app.cpp +++ b/tools/esp8266/app.cpp @@ -7,10 +7,13 @@ //----------------------------------------------------------------------------- app::app() : Main() { - mSendTicker = 0xffffffff; - mSendInterval = 0; - mMqttTicker = 0xffffffff; - mMqttInterval = 0; + mSendTicker = 0xffffffff; + mSendInterval = 0; + mMqttTicker = 0xffffffff; + mMqttInterval = 0; + mSerialTicker = 0xffffffff; + mSerialInterval = 0; + mMqttActive = false; mShowRebootRequest = false; @@ -86,12 +89,17 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) { char addr[16] = {0}; sprintf(addr, "%d.%d.%d.%d", mqttAddr[0], mqttAddr[1], mqttAddr[2], mqttAddr[3]); + mMqttActive = (mqttAddr[0] > 0); + if(mMqttInterval < 1000) mMqttInterval = 1000; mMqtt.setup(addr, mqttTopic, mqttUser, mqttPwd, mqttPort); mMqttTicker = 0; + mSerialTicker = 0; + mSerialInterval = mMqttInterval; // TODO: add extra setting for this! + mMqtt.sendMsg("version", mVersion); } @@ -166,25 +174,31 @@ void app::loop(void) { // mqtt - mMqtt.loop(); - if(checkTicker(&mMqttTicker, mMqttInterval)) { - mMqtt.isConnected(true); - char topic[30], val[10]; - for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { - Inverter<> *iv = mSys->getInverterByPos(id); - if(NULL != iv) { - for(uint8_t i = 0; i < iv->listLen; i++) { - if(0.0f != iv->getValue(i)) { - snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, fields[iv->assign[i].fieldId]); - snprintf(val, 10, "%.3f", iv->getValue(i)); - mMqtt.sendMsg(topic, val); - yield(); + if(mMqttActive) { + mMqtt.loop(); + if(checkTicker(&mMqttTicker, mMqttInterval)) { + mMqtt.isConnected(true); + char topic[30], val[10]; + for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { + Inverter<> *iv = mSys->getInverterByPos(id); + if(NULL != iv) { + for(uint8_t i = 0; i < iv->listLen; i++) { + if(0.0f != iv->getValue(i)) { + snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, fields[iv->assign[i].fieldId]); + snprintf(val, 10, "%.3f", iv->getValue(i)); + mMqtt.sendMsg(topic, val); + yield(); + } } } } } + } - // Serial debug + + // Serial debug + if(checkTicker(&mSerialTicker, mSerialInterval)) { + char topic[30], val[10]; for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { Inverter<> *iv = mSys->getInverterByPos(id); if(NULL != iv) { @@ -230,6 +244,10 @@ void app::showSetup(void) { html.replace("{DEVICE}", String(mDeviceName)); html.replace("{VERSION}", String(mVersion)); + if(mApActive) + html.replace("{IP}", String("http://192.168.1.1")); + else + html.replace("{IP}", ("http://" + String(WiFi.localIP().toString()))); String inv; uint64_t invSerial; diff --git a/tools/esp8266/app.h b/tools/esp8266/app.h index 64207d3d..df5074ab 100644 --- a/tools/esp8266/app.h +++ b/tools/esp8266/app.h @@ -81,6 +81,9 @@ class app : public Main { mqtt mMqtt; uint32_t mMqttTicker; uint16_t mMqttInterval; + bool mMqttActive; + uint32_t mSerialTicker; + uint16_t mSerialInterval; }; #endif /*__APP_H__*/ diff --git a/tools/esp8266/defines.h b/tools/esp8266/defines.h index 98ba2539..131e7742 100644 --- a/tools/esp8266/defines.h +++ b/tools/esp8266/defines.h @@ -16,7 +16,7 @@ //------------------------------------- #define VERSION_MAJOR 0 #define VERSION_MINOR 3 -#define VERSION_PATCH 4 +#define VERSION_PATCH 5 //------------------------------------- diff --git a/tools/esp8266/html/h/index_html.h b/tools/esp8266/html/h/index_html.h index 67dc9144..6705332a 100644 --- a/tools/esp8266/html/h/index_html.h +++ b/tools/esp8266/html/h/index_html.h @@ -1,4 +1,4 @@ #ifndef __INDEX_H__ #define __INDEX_H__ -const char index_html[] PROGMEM = "Index - {DEVICE}

AHOY - {DEVICE}

Visualization

Setup

Uptime:

Time:

Statistics:

This project was started from this (Mikrocontroller.net)discussion.
New updates can be found on Github: https://github.com/grindylow/ahoy

Please report issues using the feature provided by Github.

© 2022

Update Firmware

AHOY :: {VERSION}

Reboot

"; +const char index_html[] PROGMEM = "Index - {DEVICE}

AHOY - {DEVICE}

Visualization

Setup

Uptime:

Time:

Statistics:

This project was started from this discussion. (Mikrocontroller.net)
New updates can be found on Github: https://github.com/grindylow/ahoy

Please report issues using the feature provided by Github.

© 2022

Update Firmware

AHOY :: {VERSION}

Reboot

"; #endif /*__INDEX_H__*/ diff --git a/tools/esp8266/html/h/setup_html.h b/tools/esp8266/html/h/setup_html.h index 1b01ff97..49ef4896 100644 --- a/tools/esp8266/html/h/setup_html.h +++ b/tools/esp8266/html/h/setup_html.h @@ -1,4 +1,4 @@ #ifndef __SETUP_H__ #define __SETUP_H__ -const char setup_html[] PROGMEM = "Setup - {DEVICE}

Setup

Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.

WiFi

Device Host Name

ERASE SETTINGS (not WiFi)

Inverter

{INVERTERS}

General

Pinout (Wemos)

{PINOUT}

Radio (NRF24L01+)

MQTT

 

Home

Update Firmware

AHOY - {VERSION}

Factory Reset

Reboot

"; +const char setup_html[] PROGMEM = "Setup - {DEVICE}

Setup

Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.

WiFi

Device Host Name

ERASE SETTINGS (not WiFi)

Inverter

{INVERTERS}

General

Pinout (Wemos)

{PINOUT}

Radio (NRF24L01+)

MQTT

 

Home

Update Firmware

AHOY - {VERSION}

Factory Reset

Reboot

"; #endif /*__SETUP_H__*/ diff --git a/tools/esp8266/html/index.html b/tools/esp8266/html/index.html index ce2f177b..541caeb1 100644 --- a/tools/esp8266/html/index.html +++ b/tools/esp8266/html/index.html @@ -39,7 +39,7 @@

Statistics:

- This project was started from this (Mikrocontroller.net) discussion.
+ This project was started from this discussion. (Mikrocontroller.net)
New updates can be found on Github: https://github.com/grindylow/ahoy

Please report issues using the feature provided by Github. diff --git a/tools/esp8266/html/setup.html b/tools/esp8266/html/setup.html index fa0af415..baa84dab 100644 --- a/tools/esp8266/html/setup.html +++ b/tools/esp8266/html/setup.html @@ -12,7 +12,7 @@

Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.

-
+

WiFi

@@ -61,11 +61,11 @@
diff --git a/tools/esp8266/main.cpp b/tools/esp8266/main.cpp index 24938704..6ef21964 100644 --- a/tools/esp8266/main.cpp +++ b/tools/esp8266/main.cpp @@ -18,6 +18,7 @@ Main::Main(void) { mLimit = 10; mNextTryTs = 0; + mApLastTick = 0; snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); @@ -53,16 +54,16 @@ void Main::setup(const char *ssid, const char *pwd, uint32_t timeout) { if(true == startAp) { if(strlen(pwd) < 8) - Serial.println("password must be at least 8 characters long"); - setupAp(ssid, pwd); + DPRINTLN("ERROR: password must be at least 8 characters long"); + } + else { + mTimestamp = getNtpTime(); + DPRINTLN("[NTP]: " + getDateTimeStr(getNtpTime())); } mUpdater->setup(mWeb); - mApActive = startAp; - mTimestamp = getNtpTime(); - //Serial.println("[NTP]: " + getDateTimeStr(getNtpTime())); } @@ -71,10 +72,17 @@ void Main::loop(void) { if(mApActive) { mDns->processNextRequest(); if(checkTicker(&mNextTryTs, (WIFI_AP_ACTIVE_TIME * 1000))) { + mApLastTick = millis(); mApActive = setupStation(mLimit); if(mApActive) setupAp(WIFI_AP_SSID, WIFI_AP_PWD); } + else { + if(millis() - mApLastTick > 10000) { + mApLastTick = millis(); + DPRINTLN("AP will be closed in " + String((mNextTryTs - mApLastTick) / 1000) + " seconds"); + } + } } mWeb->handleClient(); @@ -118,7 +126,12 @@ bool Main::getConfig(void) { void Main::setupAp(const char *ssid, const char *pwd) { IPAddress apIp(192, 168, 1, 1); - Serial.println("\n---------\nAP MODE\nSSDI: "+ String(ssid) + "\nPWD: " + String(pwd) + "\n---------\n"); + DPRINTLN("\n---------\nAP MODE\nSSDI: " + + String(ssid) + "\nPWD: " + + String(pwd) + "\nActive for: " + + String(WIFI_AP_ACTIVE_TIME) + " seconds" + + "\n---------\n"); + DPRINTLN("DBG: " + String(mNextTryTs)); WiFi.mode(WIFI_AP); WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0)); @@ -153,7 +166,7 @@ bool Main::setupStation(uint32_t timeout) { WiFi.hostname(mDeviceName); delay(2000); - Serial.println("wait for network"); + DPRINTLN("connect to network '" + String(mStationSsid) + "' ..."); while (WiFi.status() != WL_CONNECTED) { delay(100); if(cnt % 100 == 0) @@ -192,6 +205,10 @@ void Main::showSetup(void) { // -> the PWD will only be changed if it does not match the default "{PWD}" html.replace("{DEVICE}", String(mDeviceName)); html.replace("{VERSION}", String(mVersion)); + if(mApActive) + html.replace("{IP}", String("http://192.168.1.1")); + else + html.replace("{IP}", ("http://" + String(WiFi.localIP().toString()))); mWeb->send(200, "text/html", html); } @@ -233,7 +250,7 @@ void Main::saveValues(bool webSend = true) { if(webSend) { if(mWeb->arg("reboot") == "on") showReboot(); - else + else // TODO: add device name as redirect in AP-mode mWeb->send(200, "text/html", "Setup saved" "

saved

"); } diff --git a/tools/esp8266/main.h b/tools/esp8266/main.h index 43962670..be98060a 100644 --- a/tools/esp8266/main.h +++ b/tools/esp8266/main.h @@ -16,6 +16,7 @@ #include "eep.h" #include "defines.h" #include "crc.h" +#include "debug.h" const byte mDnsPort = 53; @@ -57,11 +58,13 @@ class Main { uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS; uint16_t end; do { - end = addr += 64; + end = addr + 64; if(end > (ADDR_SETTINGS_CRC + 2)) - end = (ADDR_SETTINGS_CRC + 2 - addr); - mEep->write(ADDR_START_SETTINGS, buf, (ADDR_NEXT-ADDR_START_SETTINGS)); - } while(addr < ADDR_START_SETTINGS); + end = (ADDR_SETTINGS_CRC + 2); + DPRINTLN("erase: 0x" + String(addr, HEX) + " - 0x" + String(end, HEX)); + mEep->write(addr, buf, (end-addr)); + addr = end; + } while(addr < (ADDR_SETTINGS_CRC + 2)); } inline bool checkTicker(uint32_t *ticker, uint32_t interval) { @@ -90,6 +93,7 @@ class Main { uint32_t mTimestamp; uint32_t mLimit; uint32_t mNextTryTs; + uint32_t mApLastTick; private: bool getConfig(void);