Browse Source

* 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
pull/25/head
lumapu 2 years ago
parent
commit
4c3852cde4
  1. 54
      tools/esp8266/app.cpp
  2. 3
      tools/esp8266/app.h
  3. 2
      tools/esp8266/defines.h
  4. 2
      tools/esp8266/html/h/index_html.h
  5. 2
      tools/esp8266/html/h/setup_html.h
  6. 2
      tools/esp8266/html/index.html
  7. 10
      tools/esp8266/html/setup.html
  8. 33
      tools/esp8266/main.cpp
  9. 12
      tools/esp8266/main.h

54
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;

3
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__*/

2
tools/esp8266/defines.h

@ -16,7 +16,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 3
#define VERSION_PATCH 4
#define VERSION_PATCH 5
//-------------------------------------

2
tools/esp8266/html/h/index_html.h

@ -1,4 +1,4 @@
#ifndef __INDEX_H__
#define __INDEX_H__
const char index_html[] PROGMEM = "<!doctype html><html><head><title>Index - {DEVICE}</title><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><script type=\"text/javascript\">window.setInterval(\"getAjax('/uptime', 'uptime')\", 1000);window.setInterval(\"getAjax('/time', 'time')\", 1000);window.setInterval(\"getAjax('/cmdstat', 'cmds')\", 2000);function getAjax(url, resid) {var http = null;http = new XMLHttpRequest();if(http != null) {http.open(\"GET\", url, true);http.onreadystatechange = print;http.send(null);}function print() {if(http.readyState == 4) {document.getElementById(resid).innerHTML = http.responseText;}}}</script></head><body><h1>AHOY - {DEVICE}</h1><div id=\"content\" class=\"content\"><p><a href=\"/hoymiles\">Visualization</a><br/><br/><a href=\"/setup\">Setup</a><br/></p><p><span class=\"des\">Uptime: </span><span id=\"uptime\"></span></p><p><span class=\"des\">Time: </span><span id=\"time\"></span></p><p><span class=\"des\">Statistics: </span><pre id=\"cmds\"></pre></p><div id=\"note\">This project was started from <a href=\"https://www.mikrocontroller.net/topic/525778\" target=\"_blank\">this (Mikrocontroller.net)</a>discussion.<br/>New updates can be found on Github: <a href=\"https://github.com/grindylow/ahoy\" target=\"_blank\">https://github.com/grindylow/ahoy</a><br/><br/>Please report issues using the feature provided by Github. </div></div><div id=\"footer\"><p class=\"left\">&copy 2022</p><p class=\"left\"><a href=\"/update\">Update Firmware</a></p><p class=\"right\">AHOY :: {VERSION}</p><p class=\"right\"><a href=\"/reboot\">Reboot</a></p></div></body></html>";
const char index_html[] PROGMEM = "<!doctype html><html><head><title>Index - {DEVICE}</title><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"><script type=\"text/javascript\">window.setInterval(\"getAjax('/uptime', 'uptime')\", 1000);window.setInterval(\"getAjax('/time', 'time')\", 1000);window.setInterval(\"getAjax('/cmdstat', 'cmds')\", 2000);function getAjax(url, resid) {var http = null;http = new XMLHttpRequest();if(http != null) {http.open(\"GET\", url, true);http.onreadystatechange = print;http.send(null);}function print() {if(http.readyState == 4) {document.getElementById(resid).innerHTML = http.responseText;}}}</script></head><body><h1>AHOY - {DEVICE}</h1><div id=\"content\" class=\"content\"><p><a href=\"/hoymiles\">Visualization</a><br/><br/><a href=\"/setup\">Setup</a><br/></p><p><span class=\"des\">Uptime: </span><span id=\"uptime\"></span></p><p><span class=\"des\">Time: </span><span id=\"time\"></span></p><p><span class=\"des\">Statistics: </span><pre id=\"cmds\"></pre></p><div id=\"note\">This project was started from <a href=\"https://www.mikrocontroller.net/topic/525778\" target=\"_blank\">this discussion. (Mikrocontroller.net)</a><br/>New updates can be found on Github: <a href=\"https://github.com/grindylow/ahoy\" target=\"_blank\">https://github.com/grindylow/ahoy</a><br/><br/>Please report issues using the feature provided by Github. </div></div><div id=\"footer\"><p class=\"left\">&copy 2022</p><p class=\"left\"><a href=\"/update\">Update Firmware</a></p><p class=\"right\">AHOY :: {VERSION}</p><p class=\"right\"><a href=\"/reboot\">Reboot</a></p></div></body></html>";
#endif /*__INDEX_H__*/

2
tools/esp8266/html/h/setup_html.h

@ -1,4 +1,4 @@
#ifndef __SETUP_H__
#define __SETUP_H__
const char setup_html[] PROGMEM = "<!doctype html><html><head><title>Setup - {DEVICE}</title><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body><h1>Setup</h1><div id=\"setup\" class=\"content\"><div id=\"content\"><p>Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information. </p><form method=\"post\" action=\"/save\"><p class=\"des\">WiFi</p><label for=\"ssid\">SSID</label><input type=\"text\" class=\"text\" name=\"ssid\" value=\"{SSID}\"/><label for=\"pwd\">Password</label><input type=\"password\" class=\"text\" name=\"pwd\" value=\"{PWD}\"/><p class=\"des\">Device Host Name</p><label for=\"device\">Device Name</label><input type=\"text\" class=\"text\" name=\"device\" value=\"{DEVICE}\"/><a class=\"erase\" href=\"/erase\">ERASE SETTINGS (not WiFi)</a><p class=\"des\">Inverter</p>{INVERTERS}<br/><p class=\"subdes\">General</p><label for=\"invInterval\">Interval (ms)</label><input type=\"text\" class=\"text\" name=\"invInterval\" value=\"{INV_INTERVAL}\"/><p class=\"des\">Pinout (Wemos)</p>{PINOUT}<p class=\"des\">Radio (NRF24L01+)</p><label for=\"rf24Power\">Amplifier Power Level</label><select name=\"rf24Power\">{RF24}</select><p class=\"des\">MQTT</p><label for=\"mqttAddr\">Broker / Server IP</label><input type=\"text\" class=\"text\" name=\"mqttAddr\" value=\"{MQTT_ADDR}\"/><label for=\"mqttPort\">Port</label><input type=\"text\" class=\"text\" name=\"mqttPort\" value=\"{MQTT_PORT}\"/><label for=\"mqttUser\">Username (optional)</label><input type=\"text\" class=\"text\" name=\"mqttUser\" value=\"{MQTT_USER}\"/><label for=\"mqttPwd\">Password (optional)</label><input type=\"text\" class=\"text\" name=\"mqttPwd\" value=\"{MQTT_PWD}\"/><label for=\"mqttTopic\">Topic</label><input type=\"text\" class=\"text\" name=\"mqttTopic\" value=\"{MQTT_TOPIC}\"/><label for=\"mqttInterval\">Interval (ms)</label><input type=\"text\" class=\"text\" name=\"mqttInterval\" value=\"{MQTT_INTERVAL}\"/><p class=\"des\">&nbsp;</p><input type=\"checkbox\" class=\"cb\" name=\"reboot\"/><label for=\"reboot\">Reboot device after successful save</label><input type=\"submit\" value=\"save\" class=\"btn\" /></form></div></div><div id=\"footer\"><p class=\"left\"><a href=\"/\">Home</a></p><p class=\"left\"><a href=\"/update\">Update Firmware</a></p><p class=\"right\">AHOY - {VERSION}</p><p class=\"right\"><a href=\"/factory\">Factory Reset</a></p><p class=\"right\"><a href=\"/reboot\">Reboot</a></p></div></body></html>";
const char setup_html[] PROGMEM = "<!doctype html><html><head><title>Setup - {DEVICE}</title><link rel=\"stylesheet\" type=\"text/css\" href=\"style.css\"/><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\"></head><body><h1>Setup</h1><div id=\"setup\" class=\"content\"><div id=\"content\"><p>Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information. </p><form method=\"post\" action=\"{IP}/save\"><p class=\"des\">WiFi</p><label for=\"ssid\">SSID</label><input type=\"text\" class=\"text\" name=\"ssid\" value=\"{SSID}\"/><label for=\"pwd\">Password</label><input type=\"password\" class=\"text\" name=\"pwd\" value=\"{PWD}\"/><p class=\"des\">Device Host Name</p><label for=\"device\">Device Name</label><input type=\"text\" class=\"text\" name=\"device\" value=\"{DEVICE}\"/><a class=\"erase\" href=\"/erase\">ERASE SETTINGS (not WiFi)</a><p class=\"des\">Inverter</p>{INVERTERS}<br/><p class=\"subdes\">General</p><label for=\"invInterval\">Interval (ms)</label><input type=\"text\" class=\"text\" name=\"invInterval\" value=\"{INV_INTERVAL}\"/><p class=\"des\">Pinout (Wemos)</p>{PINOUT}<p class=\"des\">Radio (NRF24L01+)</p><label for=\"rf24Power\">Amplifier Power Level</label><select name=\"rf24Power\">{RF24}</select><p class=\"des\">MQTT</p><label for=\"mqttAddr\">Broker / Server IP</label><input type=\"text\" class=\"text\" name=\"mqttAddr\" value=\"{MQTT_ADDR}\"/><label for=\"mqttPort\">Port</label><input type=\"text\" class=\"text\" name=\"mqttPort\" value=\"{MQTT_PORT}\"/><label for=\"mqttUser\">Username (optional)</label><input type=\"text\" class=\"text\" name=\"mqttUser\" value=\"{MQTT_USER}\"/><label for=\"mqttPwd\">Password (optional)</label><input type=\"text\" class=\"text\" name=\"mqttPwd\" value=\"{MQTT_PWD}\"/><label for=\"mqttTopic\">Topic</label><input type=\"text\" class=\"text\" name=\"mqttTopic\" value=\"{MQTT_TOPIC}\"/><label for=\"mqttInterval\">Interval (ms)</label><input type=\"text\" class=\"text\" name=\"mqttInterval\" value=\"{MQTT_INTERVAL}\"/><p class=\"des\">&nbsp;</p><input type=\"checkbox\" class=\"cb\" name=\"reboot\"/><label for=\"reboot\">Reboot device after successful save</label><input type=\"submit\" value=\"save\" class=\"btn\" /></form></div></div><div id=\"footer\"><p class=\"left\"><a href=\"{IP}/\">Home</a></p><p class=\"left\"><a href=\"{IP}/update\">Update Firmware</a></p><p class=\"right\">AHOY - {VERSION}</p><p class=\"right\"><a href=\"{IP}/factory\">Factory Reset</a></p><p class=\"right\"><a href=\"{IP}/reboot\">Reboot</a></p></div></body></html>";
#endif /*__SETUP_H__*/

2
tools/esp8266/html/index.html

@ -39,7 +39,7 @@
<p><span class="des">Statistics: </span><pre id="cmds"></pre></p>
<div id="note">
This project was started from <a href="https://www.mikrocontroller.net/topic/525778" target="_blank">this (Mikrocontroller.net)</a> discussion.<br/>
This project was started from <a href="https://www.mikrocontroller.net/topic/525778" target="_blank">this discussion. (Mikrocontroller.net)</a><br/>
New updates can be found on Github: <a href="https://github.com/grindylow/ahoy" target="_blank">https://github.com/grindylow/ahoy</a><br/>
<br/>
Please report issues using the feature provided by Github.

10
tools/esp8266/html/setup.html

@ -12,7 +12,7 @@
<p>
Enter the credentials to your prefered WiFi station. After rebooting the device tries to connect with this information.
</p>
<form method="post" action="/save">
<form method="post" action="{IP}/save">
<p class="des">WiFi</p>
<label for="ssid">SSID</label>
<input type="text" class="text" name="ssid" value="{SSID}"/>
@ -61,11 +61,11 @@
</div>
<div id="footer">
<p class="left"><a href="/">Home</a></p>
<p class="left"><a href="/update">Update Firmware</a></p>
<p class="left"><a href="{IP}/">Home</a></p>
<p class="left"><a href="{IP}/update">Update Firmware</a></p>
<p class="right">AHOY - {VERSION}</p>
<p class="right"><a href="/factory">Factory Reset</a></p>
<p class="right"><a href="/reboot">Reboot</a></p>
<p class="right"><a href="{IP}/factory">Factory Reset</a></p>
<p class="right"><a href="{IP}/reboot">Reboot</a></p>
</div>
</body>
</html>

33
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", "<!doctype html><html><head><title>Setup saved</title><meta http-equiv=\"refresh\" content=\"0; URL=/setup\"></head><body>"
"<p>saved</p></body></html>");
}

12
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);

Loading…
Cancel
Save