Browse Source

Merge branch 'main'

pull/157/head
Andreas Schiffler 2 years ago
parent
commit
304e947656
  1. 1
      .gitignore
  2. 772
      tools/esp8266/app.cpp
  3. 186
      tools/esp8266/app.h
  4. 39
      tools/esp8266/config.h
  5. 91
      tools/esp8266/defines.h
  6. 2
      tools/esp8266/eep.h
  7. 30
      tools/esp8266/esp8266.cpp
  8. 2
      tools/esp8266/hmInverter.h
  9. 24
      tools/esp8266/hmRadio.h
  10. 8
      tools/esp8266/hmSystem.h
  11. 2
      tools/esp8266/html/convert.py
  12. 4
      tools/esp8266/html/h/hoymiles_html.h
  13. 2
      tools/esp8266/html/h/index_html.h
  14. 4
      tools/esp8266/html/h/visualization_html.h
  15. 5
      tools/esp8266/html/index.html
  16. 0
      tools/esp8266/html/visualization.html
  17. 471
      tools/esp8266/main.cpp
  18. 159
      tools/esp8266/main.h
  19. 66
      tools/esp8266/mqtt.h
  20. 401
      tools/esp8266/web.cpp
  21. 52
      tools/esp8266/web.h
  22. 248
      tools/esp8266/wifi.cpp
  23. 55
      tools/esp8266/wifi.h

1
.gitignore

@ -19,3 +19,4 @@ tools/esp8266/binaries
*.ipch
tools/esp8266/.vscode/extensions.json
.DS_Store
.vscode

772
tools/esp8266/app.cpp

@ -4,236 +4,68 @@
//-----------------------------------------------------------------------------
#include "app.h"
#include "favicon.h"
#include "html/h/index_html.h"
#include "html/h/setup_html.h"
#include "html/h/hoymiles_html.h"
#include <ArduinoJson.h>
//-----------------------------------------------------------------------------
app::app() : Main() {
DPRINTLN(DBG_VERBOSE, F("app::app():Main"));
mSendTicker = 0xffff;
mSendInterval = SEND_INTERVAL;
mMqttTicker = 0xffff;
mMqttInterval = MQTT_INTERVAL;
mSerialTicker = 0xffff;
mSerialInterval = SERIAL_INTERVAL;
mMqttActive = false;
mTicker = 0;
mRxTicker = 0;
app::app() {
DPRINTLN(DBG_VERBOSE, F("app::app"));
mEep = new eep();
Serial.begin(115200);
mSendLastIvId = 0;
mWifi = new wifi(this, &mSysConfig, &mConfig);
mShowRebootRequest = false;
mWebInst = new web(this, &mSysConfig, &mConfig, mVersion);
mWebInst->setup();
mSerialValues = true;
mSerialDebug = false;
memset(mPayload, 0, (MAX_NUM_INVERTERS * sizeof(invPayload_t)));
mRxFailed = 0;
mRxSuccess = 0;
mFrameCnt = 0;
mLastPacketId = 0x00;
resetSystem();
loadDefaultConfig();
mSys = new HmSystemType();
}
//-----------------------------------------------------------------------------
app::~app(void) {
}
//-----------------------------------------------------------------------------
void app::setup(uint32_t timeout) {
DPRINTLN(DBG_VERBOSE, F("app::setup"));
Main::setup(timeout);
mWeb->on("/", std::bind(&app::showIndex, this));
mWeb->on("/favicon.ico", std::bind(&app::showFavicon, this));
mWeb->on("/setup", std::bind(&app::showSetup, this));
mWeb->on("/save", std::bind(&app::showSave, this));
mWeb->on("/erase", std::bind(&app::showErase, this));
mWeb->on("/cmdstat", std::bind(&app::showStatistics, this));
mWeb->on("/hoymiles", std::bind(&app::showHoymiles, this));
mWeb->on("/livedata", std::bind(&app::showLiveData, this));
mWeb->on("/json", std::bind(&app::showJSON, this));
mWeb->on("/api",HTTP_POST, std::bind(&app::webapi, this));
if(mSettingsValid) {
mEep->read(ADDR_INV_INTERVAL, &mSendInterval);
if(mSendInterval < MIN_SEND_INTERVAL)
mSendInterval = MIN_SEND_INTERVAL;
mSendTicker = mSendInterval;
// inverter
uint64_t invSerial;
char name[MAX_NAME_LENGTH + 1] = {0};
uint16_t modPwr[4];
Inverter<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
mEep->read(ADDR_INV_ADDR + (i * 8), &invSerial);
mEep->read(ADDR_INV_NAME + (i * MAX_NAME_LENGTH), name, MAX_NAME_LENGTH);
mEep->read(ADDR_INV_CH_PWR + (i * 2 * 4), modPwr, 4);
if(0ULL != invSerial) {
iv = mSys->addInverter(name, invSerial, modPwr);
if(NULL != iv) {
mEep->read(ADDR_INV_PWR_LIM + (i * 2),(uint16_t *)&(iv->powerLimit[0]));
if (iv->powerLimit[0] != 0xffff) { // only set it, if it is changed by user. Default value in the html setup page is -1 = 0xffff
iv->powerLimit[1] = 0x0001; // set the limit as persistent
iv->devControlCmd = ActivePowerContr; // set active power limit
iv->devControlRequest = true; // set to true to update the active power limit from setup html page
DPRINTLN(DBG_INFO, F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX) + ", Power Limit: " + String(iv->powerLimit[0]));
}
for(uint8_t j = 0; j < 4; j++) {
mEep->read(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, iv->chName[j], MAX_NAME_LENGTH);
}
}
mWifiSettingsValid = checkEEpCrc(ADDR_START, ADDR_WIFI_CRC, ADDR_WIFI_CRC);
mSettingsValid = checkEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT)-(ADDR_START_SETTINGS)), ADDR_SETTINGS_CRC);
loadEEpconfig();
mMqttInterval += mSendInterval;
}
}
mEep->read(ADDR_INV_MAX_RTRY, &mMaxRetransPerPyld);
if(0 == mMaxRetransPerPyld)
mMaxRetransPerPyld = DEF_MAX_RETRANS_PER_PYLD;
// pinout
mEep->read(ADDR_PINOUT, &mSys->Radio.pinCs);
mEep->read(ADDR_PINOUT+1, &mSys->Radio.pinCe);
mEep->read(ADDR_PINOUT+2, &mSys->Radio.pinIrq);
if(mSys->Radio.pinCs == mSys->Radio.pinCe) {
mSys->Radio.pinCs = RF24_CS_PIN;
mSys->Radio.pinCe = RF24_CE_PIN;
mSys->Radio.pinIrq = RF24_IRQ_PIN;
}
mWifi->setup(timeout, mWifiSettingsValid);
// nrf24 amplifier power
mEep->read(ADDR_RF24_AMP_PWR, &mSys->Radio.AmplifierPower);
// serial console
uint8_t tmp;
mEep->read(ADDR_SER_INTERVAL, &mSerialInterval);
if(mSerialInterval < MIN_SERIAL_INTERVAL)
mSerialInterval = MIN_SERIAL_INTERVAL;
mEep->read(ADDR_SER_ENABLE, &tmp);
mSerialValues = (tmp == 0x01);
mEep->read(ADDR_SER_DEBUG, &tmp);
mSerialDebug = (tmp == 0x01);
mSys->Radio.mSerialDebug = mSerialDebug;
// ntp
char ntpAddr[NTP_ADDR_LEN];
uint16_t ntpPort;
mEep->read(ADDR_NTP_ADDR, ntpAddr, NTP_ADDR_LEN);
mEep->read(ADDR_NTP_PORT, &ntpPort);
// TODO set ntpAddr & ntpPort in main
// mqtt
uint16_t mqttPort;
char mqttAddr[MQTT_ADDR_LEN];
char mqttUser[MQTT_USER_LEN];
char mqttPwd[MQTT_PWD_LEN];
char mqttTopic[MQTT_TOPIC_LEN];
char mqttDevName[DEVNAME_LEN];
mEep->read(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN);
mEep->read(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN);
mEep->read(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN);
mEep->read(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN);
mEep->read(ADDR_DEVNAME, mqttDevName, DEVNAME_LEN);
//mEep->read(ADDR_MQTT_INTERVAL, &mMqttInterval);
mEep->read(ADDR_MQTT_PORT, &mqttPort);
if(mqttAddr[0] > 0) {
mMqttActive = true;
if(mMqttInterval < MIN_MQTT_INTERVAL)
mMqttInterval = MIN_MQTT_INTERVAL;
}
else
mMqttInterval = 0xffff;
if(0 == mqttPort)
mqttPort = 1883;
#ifndef AP_ONLY
setupMqtt();
#endif
mSys->setup(&mConfig);
}
mMqtt.setup(mqttAddr, mqttTopic, mqttUser, mqttPwd, mqttDevName, mqttPort);
mMqtt.mClient->setCallback(std::bind(&app::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
mMqttTicker = 0;
//-----------------------------------------------------------------------------
void app::loop(void) {
DPRINTLN(DBG_VERBOSE, F("app::loop"));
#ifdef __MQTT_TEST__
// für mqtt test
mMqttTicker = mMqttInterval -10;
#endif
mSerialTicker = 0;
bool apActive = mWifi->loop();
mWebInst->loop();
if(mqttAddr[0] > 0) {
char topic[30];
mMqtt.sendMsg("device", mqttDevName);
mMqtt.sendMsg("version", mVersion);
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mSys->getInverterByPos(i);
if(NULL != iv) {
for(uint8_t i = 0; i < 4; i++) {
if(0 != iv->chName[i][0]) {
snprintf(topic, 30, "%s/ch%d/%s", iv->name, i+1, "name");
mMqtt.sendMsg(topic, iv->chName[i]);
yield();
}
}
}
if(checkTicker(&mUptimeTicker, mUptimeInterval)) {
mUptimeSecs++;
if(0 != mTimestamp)
mTimestamp++;
else {
if(!apActive) {
mTimestamp = mWifi->getNtpTime();
DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp));
}
}
}
else {
DPRINTLN(DBG_DEBUG, F("CRC pos: ") + String(ADDR_SETTINGS_CRC));
DPRINTLN(DBG_DEBUG, F("NXT pos: ") + String(ADDR_NEXT));
DPRINTLN(DBG_INFO, F("Settings not valid, erasing ..."));
eraseSettings();
saveValues(false);
delay(100);
DPRINTLN(DBG_INFO, F("... restarting ..."));
delay(100);
ESP.restart();
}
mSys->setup();
if(!mWifiSettingsValid)
DPRINTLN(DBG_WARN, F("your settings are not valid! check [IP]/setup"));
else {
DPRINTLN(DBG_INFO, F("\n\n----------------------------------------"));
DPRINTLN(DBG_INFO, F("Welcome to AHOY!"));
DPRINT(DBG_INFO, F("\npoint your browser to http://"));
if(mApActive)
DBGPRINTLN(F("192.168.1.1"));
else
DBGPRINTLN(WiFi.localIP());
DPRINTLN(DBG_INFO, F("to configure your device"));
DPRINTLN(DBG_INFO, F("----------------------------------------\n"));
}
}
//-----------------------------------------------------------------------------
void app::loop(void) {
DPRINTLN(DBG_VERBOSE, F("app::loop"));
Main::loop();
mSys->Radio.loop();
yield();
if(checkTicker(&mRxTicker, 5)) {
//DPRINTLN(DBG_VERBOSE, F("app_loops =") + String(app_loops));
app_loops=0;
DPRINT(DBG_VERBOSE, F("a"));
bool rxRdy = mSys->Radio.switchRxCh();
if(!mSys->BufCtrl.empty()) {
@ -242,7 +74,7 @@ void app::loop(void) {
if(mSys->Radio.checkPaketCrc(p->packet, &len, p->rxCh)) {
// process buffer only on first occurrence
if(mSerialDebug) {
if(mConfig.serialDebug) {
DPRINT(DBG_INFO, "RX " + String(len) + "B Ch" + String(p->rxCh) + " | ");
mSys->Radio.dumpBuf(NULL, p->packet, len);
}
@ -349,7 +181,7 @@ void app::loop(void) {
mMqtt.loop();
if(checkTicker(&mTicker, 1000)) {
if((++mMqttTicker >= mMqttInterval) && (mMqttInterval != 0xffff)) {
if((++mMqttTicker >= mMqttInterval) && (mMqttInterval != 0xffff) && mMqttActive) {
mMqttTicker = 0;
mMqtt.isConnected(true);
char topic[30], val[10];
@ -367,11 +199,11 @@ void app::loop(void) {
}
}
snprintf(val, 10, "%ld", millis()/1000);
#ifndef __MQTT_NO_DISCOVERCONFIG__
// MQTTDiscoveryConfig nur wenn nicht abgeschaltet.
sendMqttDiscoveryConfig();
#endif
#endif
mMqtt.sendMsg("uptime", val);
#ifdef __MQTT_TEST__
@ -380,8 +212,8 @@ void app::loop(void) {
#endif
}
if(mSerialValues) {
if(++mSerialTicker >= mSerialInterval) {
if(mConfig.serialShowIv) {
if(++mSerialTicker >= mConfig.serialInterval) {
mSerialTicker = 0;
char topic[30], val[10];
for(uint8_t id = 0; id < mSys->getNumInverters(); id++) {
@ -404,15 +236,15 @@ void app::loop(void) {
}
}
if(++mSendTicker >= mSendInterval) {
if(++mSendTicker >= mConfig.sendInterval) {
mSendTicker = 0;
if(0 != mTimestamp) {
if(mSerialDebug)
if(mConfig.serialDebug)
DPRINTLN(DBG_DEBUG, F("Free heap: 0x") + String(ESP.getFreeHeap(), HEX));
if(!mSys->BufCtrl.empty()) {
if(mSerialDebug)
if(mConfig.serialDebug)
DPRINTLN(DBG_DEBUG, F("recbuf not empty! #") + String(mSys->BufCtrl.getFill()));
}
@ -431,7 +263,7 @@ void app::loop(void) {
if(!mPayload[iv->id].complete) {
mRxFailed++;
if(mSerialDebug) {
if(mConfig.serialDebug) {
DPRINT(DBG_INFO, F("Inverter #") + String(iv->id) + " ");
DPRINTLN(DBG_INFO, F("no Payload received! (retransmits: ") + String(mPayload[iv->id].retransmits) + ")");
}
@ -446,11 +278,11 @@ void app::loop(void) {
mPayload[iv->id].ts = mTimestamp;
yield();
if(mSerialDebug)
if(mConfig.serialDebug)
DPRINTLN(DBG_DEBUG, F("app:loop WiFi WiFi.status ") + String(WiFi.status()) );
DPRINTLN(DBG_INFO, F("Requesting Inverter SN ") + String(iv->serial.u64, HEX));
if(iv->devControlRequest && iv->powerLimit[0] > 0){ // prevent to "switch off"
if(mSerialDebug)
if(mConfig.serialDebug)
DPRINTLN(DBG_INFO, F("Devcontrol request ") + String(iv->devControlCmd) + F(" power limit ") + String(iv->powerLimit[0]));
mSys->Radio.sendControlPacket(iv->radioId.u64,iv->devControlCmd ,iv->powerLimit);
} else {
@ -459,7 +291,7 @@ void app::loop(void) {
}
}
}
else if(mSerialDebug)
else if(mConfig.serialDebug)
DPRINTLN(DBG_WARN, F("time not set, can't request inverter!"));
yield();
}
@ -514,12 +346,12 @@ void app::processPayload(bool retransmit) {
if(!buildPayload(iv->id)) {
if(mPayload[iv->id].requested) {
if(retransmit) {
if(mPayload[iv->id].retransmits < mMaxRetransPerPyld) {
if(mPayload[iv->id].retransmits < mConfig.maxRetransPerPyld) {
mPayload[iv->id].retransmits++;
if(mPayload[iv->id].maxPackId != 0) {
for(uint8_t i = 0; i < (mPayload[iv->id].maxPackId-1); i ++) {
if(mPayload[iv->id].len[i] == 0) {
if(mSerialDebug)
if(mConfig.serialDebug)
DPRINTLN(DBG_ERROR, F("while retrieving data: Frame ") + String(i+1) + F(" missing: Request Retransmit"));
mSys->Radio.sendCmdPacket(iv->radioId.u64, TX_REQ_INFO, (SINGLE_FRAME+i), true);
break; // only retransmit one frame per loop
@ -528,7 +360,7 @@ void app::processPayload(bool retransmit) {
}
}
else {
if(mSerialDebug)
if(mConfig.serialDebug)
DPRINTLN(DBG_ERROR, F("while retrieving data: last frame missing: Request Retransmit"));
if(0x00 != mLastPacketId)
mSys->Radio.sendCmdPacket(iv->radioId.u64, TX_REQ_INFO, mLastPacketId, true);
@ -551,7 +383,7 @@ void app::processPayload(bool retransmit) {
yield();
}
offs-=2;
if(mSerialDebug) {
if(mConfig.serialDebug) {
DPRINT(DBG_INFO, F("Payload (") + String(offs) + "): ");
mSys->Radio.dumpBuf(NULL, payload, offs);
}
@ -586,168 +418,6 @@ void app::processPayload(bool retransmit) {
}
//-----------------------------------------------------------------------------
void app::showIndex(void) {
DPRINTLN(DBG_VERBOSE, F("app::showIndex"));
String html = FPSTR(index_html);
html.replace(F("{DEVICE}"), mDeviceName);
html.replace(F("{VERSION}"), mVersion);
html.replace(F("{TS}"), String(mSendInterval) + " ");
html.replace(F("{JS_TS}"), String(mSendInterval * 1000));
html.replace(F("{BUILD}"), String(AUTO_GIT_HASH));
mWeb->send(200, "text/html", html);
}
//-----------------------------------------------------------------------------
void app::showSetup(void) {
DPRINTLN(DBG_VERBOSE, F("app::showSetup"));
// overrides same method in main.cpp
String html = FPSTR(setup_html);
html.replace(F("{SSID}"), mStationSsid);
// PWD will be left at the default value (for protection)
// -> the PWD will only be changed if it does not match the placeholder "{PWD}"
html.replace(F("{DEVICE}"), String(mDeviceName));
html.replace(F("{VERSION}"), String(mVersion));
if(mApActive)
html.replace(F("{IP}"), String(F("http://192.168.1.1")));
else
html.replace(F("{IP}"), ("http://" + String(WiFi.localIP().toString())));
String inv;
uint64_t invSerial;
char name[MAX_NAME_LENGTH + 1] = {0};
uint16_t modPwr[4];
uint16_t invActivePowerLimit = -1;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
mEep->read(ADDR_INV_ADDR + (i * 8), &invSerial);
mEep->read(ADDR_INV_NAME + (i * MAX_NAME_LENGTH), name, MAX_NAME_LENGTH);
mEep->read(ADDR_INV_CH_PWR + (i * 2 * 4), modPwr, 4);
mEep->read(ADDR_INV_PWR_LIM + (i * 2),(uint16_t *) &invActivePowerLimit);
inv += F("<p class=\"subdes\">Inverter ") + String(i) + "</p>";
inv += F("<label for=\"inv") + String(i) + F("Addr\">Address</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("Addr\" value=\"");
if(0ULL != invSerial)
inv += String(invSerial, HEX);
inv += F("\"/ maxlength=\"12\" onkeyup=\"checkSerial()\">");
inv += F("<label for=\"inv") + String(i) + F("Name\">Name</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("Name\" value=\"");
inv += String(name);
inv += F("\"/ maxlength=\"") + String(MAX_NAME_LENGTH) + "\">";
inv += F("<label for=\"inv") + String(i) + F("ActivePowerLimit\">Active Power Limit (W)</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("ActivePowerLimit\" value=\"");
if (name[0] == 0){
// If this value will be "saved" on next reboot the command to set the power limit will not be executed.
inv += String(65535);
} else {
inv += String(invActivePowerLimit);
}
inv += F("\"/ maxlength=\"") + String(6) + "\">";
inv += F("<label for=\"inv") + String(i) + F("ModPwr0\" name=\"lbl") + String(i);
inv += F("ModPwr\">Max Module Power (Wp)</label>");
for(uint8_t j = 0; j < 4; j++) {
inv += F("<input type=\"text\" class=\"text sh\" name=\"inv") + String(i) + F("ModPwr") + String(j) + F("\" value=\"");
inv += String(modPwr[j]);
inv += F("\"/ maxlength=\"4\">");
}
inv += F("<br/><label for=\"inv") + String(i) + F("ModName0\" name=\"lbl") + String(i);
inv += F("ModName\">Module Name</label>");
for(uint8_t j = 0; j < 4; j++) {
mEep->read(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, name, MAX_NAME_LENGTH);
inv += F("<input type=\"text\" class=\"text sh\" name=\"inv") + String(i) + F("ModName") + String(j) + F("\" value=\"");
inv += String(name);
inv += F("\"/ maxlength=\"") + String(MAX_NAME_LENGTH) + "\">";
}
}
html.replace(F("{INVERTERS}"), String(inv));
// pinout
String pinout;
for(uint8_t i = 0; i < 3; i++) {
pinout += F("<label for=\"") + String(pinArgNames[i]) + "\">" + String(pinNames[i]) + F("</label>");
pinout += F("<select name=\"") + String(pinArgNames[i]) + "\">";
for(uint8_t j = 0; j <= 16; j++) {
pinout += F("<option value=\"") + String(j) + "\"";
switch(i) {
default: if(j == mSys->Radio.pinCs) pinout += F(" selected"); break;
case 1: if(j == mSys->Radio.pinCe) pinout += F(" selected"); break;
case 2: if(j == mSys->Radio.pinIrq) pinout += F(" selected"); break;
}
pinout += ">" + String(wemosPins[j]) + F("</option>");
}
pinout += F("</select>");
}
html.replace(F("{PINOUT}"), String(pinout));
// nrf24l01+
String rf24;
for(uint8_t i = 0; i <= 3; i++) {
rf24 += F("<option value=\"") + String(i) + "\"";
if(i == mSys->Radio.AmplifierPower)
rf24 += F(" selected");
rf24 += ">" + String(rf24AmpPower[i]) + F("</option>");
}
html.replace(F("{RF24}"), String(rf24));
if(mSettingsValid) {
html.replace(F("{INV_INTVL}"), String(mSendInterval));
html.replace(F("{INV_RETRIES}"), String(mMaxRetransPerPyld));
uint8_t tmp;
mEep->read(ADDR_SER_ENABLE, &tmp);
html.replace(F("{SER_INTVL}"), String(mSerialInterval));
html.replace(F("{SER_VAL_CB}"), (tmp == 0x01) ? "checked" : "");
mEep->read(ADDR_SER_DEBUG, &tmp);
html.replace(F("{SER_DBG_CB}"), (tmp == 0x01) ? "checked" : "");
char ntpAddr[NTP_ADDR_LEN] = {0};
uint16_t ntpPort;
mEep->read(ADDR_NTP_ADDR, ntpAddr, NTP_ADDR_LEN);
mEep->read(ADDR_NTP_PORT, &ntpPort);
html.replace(F("{NTP_ADDR}"), String(ntpAddr));
html.replace(F("{NTP_PORT}"), String(ntpPort));
char mqttAddr[MQTT_ADDR_LEN] = {0};
uint16_t mqttPort;
mEep->read(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN);
mEep->read(ADDR_MQTT_PORT, &mqttPort);
html.replace(F("{MQTT_ADDR}"), String(mqttAddr));
html.replace(F("{MQTT_PORT}"), String(mMqtt.getPort()));
html.replace(F("{MQTT_USER}"), String(mMqtt.getUser()));
html.replace(F("{MQTT_PWD}"), String(mMqtt.getPwd()));
html.replace(F("{MQTT_TOPIC}"), String(mMqtt.getTopic()));
html.replace(F("{MQTT_INTVL}"), String(mMqttInterval));
}
mWeb->send(200, F("text/html"), html);
}
//-----------------------------------------------------------------------------
void app::showSave(void) {
DPRINTLN(DBG_VERBOSE, F("app::showSave"));
saveValues(true);
}
//-----------------------------------------------------------------------------
void app::showErase() {
DPRINTLN(DBG_VERBOSE, F("app::showErase"));
eraseSettings();
showReboot();
}
//-----------------------------------------------------------------------------
void app::cbMqtt(char* topic, byte* payload, unsigned int length) {
// callback handling on subscribed devcontrol topic
@ -820,10 +490,8 @@ void app::cbMqtt(char* topic, byte* payload, unsigned int length) {
}
//-----------------------------------------------------------------------------
void app::showStatistics(void) {
DPRINTLN(DBG_VERBOSE, F("app::showStatistics"));
String app::getStatistics(void) {
String content = F("Receive success: ") + String(mRxSuccess) + "\n";
content += F("Receive fail: ") + String(mRxFailed) + "\n";
content += F("Frames received: ") + String(mFrameCnt) + "\n";
@ -868,7 +536,7 @@ void app::showStatistics(void) {
content += F("not ");
content += F("connected\n");
mWeb->send(200, F("text/plain"), content);
return content;
}
//-----------------------------------------------------------------------------
@ -885,22 +553,6 @@ void app::webapi(void) { // ToDo
mSys->InfoCmd = payload["cmd"];
DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(mSys->InfoCmd));
}
if (payload["tx_request"] == (uint8_t)TX_REQ_DEVCONTROL){
if(payload["cmd"] == (uint8_t)ActivePowerContr){
uint8_t iv_id = payload["inverter"];
if (iv_id >= 0 && iv_id <= MAX_NUM_INVERTERS){
Inverter<> *iv = this->mSys->getInverterByPos(iv_id);
uint16_t webapiPayload = payload["payload"];
if (webapiPayload > 0 && webapiPayload < 10000){
iv->devControlCmd = ActivePowerContr;
iv->powerLimit[0] = webapiPayload;
iv->powerLimit[1] = 0x0000; // if power limit is set via external interface --> set it temporay
DPRINTLN(DBG_INFO, F("Power limit for inverter ") + String(iv->id) + F(" set to ") + String(iv->powerLimit[0]) + F("W via REST API") );
iv->devControlRequest = true;
}
}
}
}
mWeb->send ( 200, "text/json", "{success:true}" );
}
@ -998,13 +650,13 @@ void app::showLiveData(void) {
#endif
}
}
mWeb->send(200, F("text/html"), modHtml);
return modHtml;
}
//-----------------------------------------------------------------------------
void app::showJSON(void) {
DPRINTLN(DBG_VERBOSE, F("app::showJSON"));
String app::getJson(void) {
DPRINTLN(DBG_VERBOSE, F("app::showJson"));
String modJson;
modJson = F("{\n");
@ -1024,139 +676,17 @@ void app::showJSON(void) {
}
modJson += F("\"json_ts\": \"") + String(getDateTimeStr(mTimestamp)) + F("\"\n}\n");
// mWeb->send(200, F("text/json"), modJson);
mWeb->send(200, F("application/json"), modJson); // the preferred content-type (https://stackoverflow.com/questions/22406077/what-is-the-exact-difference-between-content-type-text-json-and-application-jso)
return modJson;
}
//-----------------------------------------------------------------------------
void app::saveValues(bool webSend = true) {
DPRINTLN(DBG_VERBOSE, F("app::saveValues"));
Main::saveValues(false); // general configuration
if(mWeb->args() > 0) {
char buf[20] = {0};
uint8_t i = 0;
uint16_t interval;
uint16_t activepowerlimit=-1;
// inverter
serial_u addr;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
// address
mWeb->arg("inv" + String(i) + "Addr").toCharArray(buf, 20);
if(strlen(buf) == 0)
memset(buf, 0, 20);
addr.u64 = Serial2u64(buf);
mEep->write(ADDR_INV_ADDR + (i * 8), addr.u64);
// active power limit
activepowerlimit = mWeb->arg("inv" + String(i) + "ActivePowerLimit").toInt();
if (activepowerlimit != 0xffff && activepowerlimit > 0) {
mEep->write(ADDR_INV_PWR_LIM + i * 2,activepowerlimit);
}
// name
mWeb->arg("inv" + String(i) + "Name").toCharArray(buf, 20);
mEep->write(ADDR_INV_NAME + (i * MAX_NAME_LENGTH), buf, MAX_NAME_LENGTH);
// max channel power / name
for(uint8_t j = 0; j < 4; j++) {
uint16_t pwr = mWeb->arg("inv" + String(i) + "ModPwr" + String(j)).toInt();
mEep->write(ADDR_INV_CH_PWR + (i * 2 * 4) + (j*2), pwr);
memset(buf, 0, 20);
mWeb->arg("inv" + String(i) + "ModName" + String(j)).toCharArray(buf, 20);
mEep->write(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, buf, MAX_NAME_LENGTH);
}
}
interval = mWeb->arg("invInterval").toInt();
mEep->write(ADDR_INV_INTERVAL, interval);
i = mWeb->arg("invRetry").toInt();
mEep->write(ADDR_INV_MAX_RTRY, i);
// pinout
for(uint8_t i = 0; i < 3; i ++) {
uint8_t pin = mWeb->arg(String(pinArgNames[i])).toInt();
mEep->write(ADDR_PINOUT + i, pin);
}
// nrf24 amplifier power
mSys->Radio.AmplifierPower = mWeb->arg("rf24Power").toInt() & 0x03;
mEep->write(ADDR_RF24_AMP_PWR, mSys->Radio.AmplifierPower);
// ntp
char ntpAddr[NTP_ADDR_LEN] = {0};
uint16_t ntpPort;
mWeb->arg("ntpAddr").toCharArray(ntpAddr, NTP_ADDR_LEN);
ntpPort = mWeb->arg("ntpPort").toInt();
mEep->write(ADDR_NTP_ADDR, ntpAddr, NTP_ADDR_LEN);
mEep->write(ADDR_NTP_PORT, ntpPort);
// mqtt
char mqttAddr[MQTT_ADDR_LEN] = {0};
uint16_t mqttPort;
char mqttUser[MQTT_USER_LEN];
char mqttPwd[MQTT_PWD_LEN];
char mqttTopic[MQTT_TOPIC_LEN];
mWeb->arg("mqttAddr").toCharArray(mqttAddr, MQTT_ADDR_LEN);
mWeb->arg("mqttUser").toCharArray(mqttUser, MQTT_USER_LEN);
mWeb->arg("mqttPwd").toCharArray(mqttPwd, MQTT_PWD_LEN);
mWeb->arg("mqttTopic").toCharArray(mqttTopic, MQTT_TOPIC_LEN);
//interval = mWeb->arg("mqttIntvl").toInt();
mqttPort = mWeb->arg("mqttPort").toInt();
mEep->write(ADDR_MQTT_ADDR, mqttAddr, MQTT_ADDR_LEN);
mEep->write(ADDR_MQTT_PORT, mqttPort);
mEep->write(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN);
mEep->write(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN);
mEep->write(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN);
//mEep->write(ADDR_MQTT_INTERVAL, interval);
// serial console
bool tmp;
interval = mWeb->arg("serIntvl").toInt();
mEep->write(ADDR_SER_INTERVAL, interval);
tmp = (mWeb->arg("serEn") == "on");
mEep->write(ADDR_SER_ENABLE, (uint8_t)((tmp) ? 0x01 : 0x00));
mSerialDebug = (mWeb->arg("serDbg") == "on");
mEep->write(ADDR_SER_DEBUG, (uint8_t)((mSerialDebug) ? 0x01 : 0x00));
DPRINT(DBG_INFO, "Serial debug is ");
if(mSerialDebug) DPRINTLN(DBG_INFO, "on"); else DPRINTLN(DBG_INFO, "off");
mSys->Radio.mSerialDebug = mSerialDebug;
updateCrc();
mEep->commit();
if((mWeb->arg("reboot") == "on"))
showReboot();
else {
mShowRebootRequest = true;
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Setup saved</title><meta http-equiv=\"refresh\" content=\"1; URL=/setup\"></head><body>"
"<p>saved</p></body></html>"));
}
}
else {
updateCrc();
mEep->commit();
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Error</title><meta http-equiv=\"refresh\" content=\"3; URL=/setup\"></head><body>"
"<p>Error while saving</p></body></html>"));
}
bool app::getWifiApActive(void) {
return mWifi->getApActive();
}
//-----------------------------------------------------------------------------
void app::updateCrc(void) {
DPRINTLN(DBG_VERBOSE, F("app::updateCrc"));
Main::updateCrc();
uint16_t crc;
crc = buildEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT) - (ADDR_START_SETTINGS)));
DPRINTLN(DBG_DEBUG, F("new CRC: ") + String(crc, HEX));
mEep->write(ADDR_SETTINGS_CRC, crc);
}
void app::sendMqttDiscoveryConfig(void) {
DPRINTLN(DBG_VERBOSE, F("app::sendMqttDiscoveryConfig"));
@ -1178,7 +708,7 @@ void app::sendMqttDiscoveryConfig(void) {
} else {
snprintf(name, 32, "%s CH%d %s", iv->name, iv->assign[i].ch, iv->getFieldName(i));
}
snprintf(stateTopic, 64, "%s/%s/ch%d/%s", mMqtt.getTopic(), iv->name, iv->assign[i].ch, iv->getFieldName(i));
snprintf(stateTopic, 64, "%s/%s/ch%d/%s", mConfig.mqtt.topic, iv->name, iv->assign[i].ch, iv->getFieldName(i));
snprintf(discoveryTopic, 64, "%s/sensor/%s/ch%d_%s/config", MQTT_DISCOVERY_PREFIX, iv->name, iv->assign[i].ch, iv->getFieldName(i));
snprintf(uniq_id, 32, "ch%d_%s", iv->assign[i].ch, iv->getFieldName(i));
const char* devCls = getFieldDeviceClass(iv->assign[i].fieldId);
@ -1210,6 +740,8 @@ void app::sendMqttDiscoveryConfig(void) {
}
}
//-----------------------------------------------------------------------------
const char* app::getFieldDeviceClass(uint8_t fieldId) {
uint8_t pos = 0;
for(; pos < DEVICE_CLS_ASSIGN_LIST_LEN; pos++) {
@ -1219,6 +751,8 @@ const char* app::getFieldDeviceClass(uint8_t fieldId) {
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : deviceClasses[deviceFieldAssignment[pos].deviceClsId];
}
//-----------------------------------------------------------------------------
const char* app::getFieldStateClass(uint8_t fieldId) {
uint8_t pos = 0;
for(; pos < DEVICE_CLS_ASSIGN_LIST_LEN; pos++) {
@ -1227,3 +761,187 @@ const char* app::getFieldStateClass(uint8_t fieldId) {
}
return (pos >= DEVICE_CLS_ASSIGN_LIST_LEN) ? NULL : stateClasses[deviceFieldAssignment[pos].stateClsId];
}
//-----------------------------------------------------------------------------
void app::resetSystem(void) {
mUptimeSecs = 0;
mUptimeTicker = 0xffffffff;
mUptimeInterval = 1000;
#ifdef AP_ONLY
mTimestamp = 1;
#else
mTimestamp = 0;
#endif
mHeapStatCnt = 0;
mSendTicker = 0xffff;
mMqttTicker = 0xffff;
mMqttInterval = MQTT_INTERVAL;
mSerialTicker = 0xffff;
mMqttActive = false;
mTicker = 0;
mRxTicker = 0;
mSendLastIvId = 0;
mShowRebootRequest = false;
memset(mPayload, 0, (MAX_NUM_INVERTERS * sizeof(invPayload_t)));
mRxFailed = 0;
mRxSuccess = 0;
mFrameCnt = 0;
mLastPacketId = 0x00;
}
//-----------------------------------------------------------------------------
void app::loadDefaultConfig(void) {
memset(&mSysConfig, 0, sizeof(sysConfig_t));
memset(&mConfig, 0, sizeof(config_t));
snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
snprintf(mSysConfig.deviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME);
// wifi
snprintf(mSysConfig.stationSsid, SSID_LEN, "%s", FB_WIFI_SSID);
snprintf(mSysConfig.stationPwd, PWD_LEN, "%s", FB_WIFI_PWD);
// nrf24
mConfig.sendInterval = SEND_INTERVAL;
mConfig.maxRetransPerPyld = DEF_MAX_RETRANS_PER_PYLD;
mConfig.pinCs = DEF_RF24_CS_PIN;
mConfig.pinCe = DEF_RF24_CE_PIN;
mConfig.pinIrq = DEF_RF24_IRQ_PIN;
mConfig.amplifierPower = DEF_AMPLIFIERPOWER & 0x03;
// ntp
snprintf(mConfig.ntpAddr, NTP_ADDR_LEN, "%s", DEF_NTP_SERVER_NAME);
mConfig.ntpPort = DEF_NTP_PORT;
// mqtt
snprintf(mConfig.mqtt.broker, MQTT_ADDR_LEN, "%s", DEF_MQTT_BROKER);
mConfig.mqtt.port = DEF_MQTT_PORT;
snprintf(mConfig.mqtt.user, MQTT_USER_LEN, "%s", DEF_MQTT_USER);
snprintf(mConfig.mqtt.pwd, MQTT_PWD_LEN, "%s", DEF_MQTT_PWD);
snprintf(mConfig.mqtt.topic, MQTT_TOPIC_LEN, "%s", DEF_MQTT_TOPIC);
// serial
mConfig.serialInterval = SERIAL_INTERVAL;
mConfig.serialShowIv = false;
mConfig.serialDebug = false;
}
//-----------------------------------------------------------------------------
void app::loadEEpconfig(void) {
DPRINTLN(DBG_VERBOSE, F("app::loadEEpconfig"));
if(mWifiSettingsValid)
mEep->read(ADDR_CFG_SYS, (uint8_t*) &mSysConfig, CFG_SYS_LEN);
if(mSettingsValid) {
mEep->read(ADDR_CFG, (uint8_t*) &mConfig, CFG_LEN);
mSendTicker = mConfig.sendInterval;
mSerialTicker = 0;
// inverter
uint64_t invSerial;
char name[MAX_NAME_LENGTH + 1] = {0};
uint16_t modPwr[4];
Inverter<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
mEep->read(ADDR_INV_ADDR + (i * 8), &invSerial);
mEep->read(ADDR_INV_NAME + (i * MAX_NAME_LENGTH), name, MAX_NAME_LENGTH);
mEep->read(ADDR_INV_CH_PWR + (i * 2 * 4), modPwr, 4);
if(0ULL != invSerial) {
iv = mSys->addInverter(name, invSerial, modPwr);
if(NULL != iv) {
mEep->read(ADDR_INV_PWR_LIM + (i * 2),(uint16_t *)&(iv->powerLimit[0]));
if (iv->powerLimit[0] != 0xffff) { // only set it, if it is changed by user. Default value in the html setup page is -1 = 0xffff
iv->powerLimit[1] = 0x0001; // set the limit as persistent
iv->devControlCmd = ActivePowerContr; // set active power limit
iv->devControlRequest = true; // set to true to update the active power limit from setup html page
DPRINTLN(DBG_INFO, F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX) + ", Power Limit: " + String(iv->powerLimit[0]));
}
for(uint8_t j = 0; j < 4; j++) {
mEep->read(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, iv->chName[j], MAX_NAME_LENGTH);
}
}
// TODO: the original mqttinterval value is not needed any more
mMqttInterval += mConfig.sendInterval;
}
}
}
}
//-----------------------------------------------------------------------------
void app::saveValues(void) {
DPRINTLN(DBG_VERBOSE, F("app::saveValues"));
mEep->write(ADDR_CFG_SYS, (uint8_t*)&mSysConfig, CFG_SYS_LEN);
mEep->write(ADDR_CFG, (uint8_t*)&mConfig, CFG_LEN);
Inverter<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mSys->getInverterByPos(i);
if(NULL != iv) {
mEep->write(ADDR_INV_ADDR + (i * 8), iv->serial.u64);
mEep->write(ADDR_INV_PWR_LIM + i * 2, iv->powerLimit[0]);
mEep->write(ADDR_INV_NAME + (i * MAX_NAME_LENGTH), iv->name, MAX_NAME_LENGTH);
// max channel power / name
for(uint8_t j = 0; j < 4; j++) {
mEep->write(ADDR_INV_CH_PWR + (i * 2 * 4) + (j*2), iv->chMaxPwr[j]);
mEep->write(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, iv->chName[j], MAX_NAME_LENGTH);
}
}
}
updateCrc();
mEep->commit();
}
//-----------------------------------------------------------------------------
void app::setupMqtt(void) {
if(mSettingsValid) {
if(mConfig.mqtt.broker[0] > 0) {
mMqttActive = true;
if(mMqttInterval < MIN_MQTT_INTERVAL)
mMqttInterval = MIN_MQTT_INTERVAL;
}
else
mMqttInterval = 0xffff;
mMqttTicker = 0;
mMqtt.setup(&mConfig.mqtt, mSysConfig.deviceName);
mMqtt.setCallback(std::bind(&app::cbMqtt, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
if(mMqttActive) {
mMqtt.sendMsg("version", mVersion);
if(mMqtt.isConnected())
mMqtt.sendMsg("device", mSysConfig.deviceName);
/*char topic[30];
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mSys->getInverterByPos(i);
if(NULL != iv) {
for(uint8_t i = 0; i < 4; i++) {
if(0 != iv->chName[i][0]) {
snprintf(topic, 30, "%s/ch%d/%s", iv->name, i+1, "name");
mMqtt.sendMsg(topic, iv->chName[i]);
yield();
}
}
}
}*/
}
}
}

186
tools/esp8266/app.h

@ -6,16 +6,23 @@
#ifndef __APP_H__
#define __APP_H__
#include "main.h"
#include "dbg.h"
#include "Arduino.h"
#include <RF24.h>
#include <RF24_config.h>
#include <ArduinoJson.h>
#include "eep.h"
#include "defines.h"
#include "crc.h"
#include "CircularBuffer.h"
#include "hmSystem.h"
#include "mqtt.h"
#include "wifi.h"
#include "web.h"
// hier läst sich das Verhalten der app in Bezug auf MQTT
// durch PER-Conpiler defines anpassen
@ -25,7 +32,7 @@
#define __MQTT_NO_DISCOVERCONFIG__ // das versenden der MQTTDiscoveryConfig abschalten ( gehört eigentlich ins Setup )
typedef CircularBuffer<packet_t, PACKET_BUFFER_SIZE> BufferType;
typedef HmRadio<RF24_CE_PIN, RF24_CS_PIN, RF24_IRQ_PIN, BufferType> RadioType;
typedef HmRadio<DEF_RF24_CE_PIN, DEF_RF24_CS_PIN, BufferType> RadioType;
typedef Inverter<float> InverterType;
typedef HmSystem<RadioType, BufferType, MAX_NUM_INVERTERS, InverterType> HmSystemType;
@ -49,43 +56,28 @@ typedef struct {
} invPayload_t;
class app : public Main {
class wifi;
class web;
class app {
public:
app();
~app();
~app() {}
void setup(uint32_t timeout);
void loop(void);
void handleIntr(void);
void cbMqtt(char* topic, byte* payload, unsigned int length);
void saveValues(void);
String getStatistics(void);
String getLiveData(void);
String getJson(void);
bool getWifiApActive(void);
uint8_t app_loops;
uint8_t getIrqPin(void) {
return mSys->Radio.pinIrq;
return mConfig.pinIrq;
}
private:
bool buildPayload(uint8_t id);
void processPayload(bool retransmit);
void showFavicon(void);
void showIndex(void);
void showSetup(void);
void showSave(void);
void showErase(void);
void showStatistics(void);
void showHoymiles(void);
void showLiveData(void);
void showJSON(void);
void webapi(void);
void saveValues(bool webSend);
void updateCrc(void);
void sendMqttDiscoveryConfig(void);
const char* getFieldDeviceClass(uint8_t fieldId);
const char* getFieldStateClass(uint8_t fieldId);
uint64_t Serial2u64(const char *val) {
char tmp[3] = {0};
uint64_t ret = 0ULL;
@ -101,12 +93,142 @@ class app : public Main {
return ret;
}
bool mShowRebootRequest;
String getDateTimeStr(time_t t) {
char str[20] = {0};
if(0 == t)
sprintf(str, "n/a");
else
sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
return String(str);
}
inline uint32_t getUptime(void) {
return mUptimeSecs;
}
inline uint32_t getTimestamp(void) {
return mTimestamp;
}
void eraseSettings(bool all = false) {
//DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings"));
uint8_t buf[64] = {0};
uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS;
uint16_t end;
do {
end = addr + 64;
if(end > (ADDR_SETTINGS_CRC + 2))
end = (ADDR_SETTINGS_CRC + 2);
DPRINTLN(DBG_DEBUG, F("erase: 0x") + String(addr, HEX) + " - 0x" + String(end, HEX));
mEep->write(addr, buf, (end-addr));
addr = end;
} while(addr < (ADDR_SETTINGS_CRC + 2));
mEep->commit();
}
inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
uint32_t mil = millis();
if(mil >= *ticker) {
*ticker = mil + interval;
return true;
}
else if(mil < (*ticker - interval)) {
*ticker = mil + interval;
return true;
}
return false;
}
HmSystemType *mSys;
private:
void resetSystem(void);
void loadDefaultConfig(void);
void loadEEpconfig(void);
void setupMqtt(void);
bool buildPayload(uint8_t id);
void processPayload(bool retransmit);
void sendMqttDiscoveryConfig(void);
const char* getFieldDeviceClass(uint8_t fieldId);
const char* getFieldStateClass(uint8_t fieldId);
inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
uint8_t buf[32];
uint16_t crc = 0xffff;
uint8_t len;
while(length > 0) {
len = (length < 32) ? length : 32;
mEep->read(start, buf, len);
crc = crc16(buf, len, crc);
start += len;
length -= len;
}
return crc;
}
void updateCrc(void) {
DPRINTLN(DBG_VERBOSE, F("app::updateCrc"));
uint16_t crc;
crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
DPRINTLN(DBG_DEBUG, F("new Wifi CRC: ") + String(crc, HEX));
mEep->write(ADDR_WIFI_CRC, crc);
crc = buildEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT) - (ADDR_START_SETTINGS)));
DPRINTLN(DBG_DEBUG, F("new Settings CRC: ") + String(crc, HEX));
mEep->write(ADDR_SETTINGS_CRC, crc);
mEep->commit();
}
bool checkEEpCrc(uint32_t start, uint32_t length, uint32_t crcPos) {
DPRINTLN(DBG_VERBOSE, F("main.h:checkEEpCrc"));
DPRINTLN(DBG_DEBUG, F("start: ") + String(start) + F(", length: ") + String(length));
uint16_t crcRd, crcCheck;
crcCheck = buildEEpCrc(start, length);
mEep->read(crcPos, &crcRd);
DPRINTLN(DBG_DEBUG, "CRC RD: " + String(crcRd, HEX) + " CRC CALC: " + String(crcCheck, HEX));
return (crcCheck == crcRd);
}
void stats(void) {
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
uint32_t free;
uint16_t max;
uint8_t frag;
ESP.getHeapStats(&free, &max, &frag);
DPRINT(DBG_VERBOSE, F("free: ") + String(free));
DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%");
DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag));
}
uint32_t mUptimeTicker;
uint16_t mUptimeInterval;
uint32_t mUptimeSecs;
uint8_t mHeapStatCnt;
bool mWifiSettingsValid;
bool mSettingsValid;
eep *mEep;
uint32_t mTimestamp;
bool mShowRebootRequest;
wifi *mWifi;
web *mWebInst;
sysConfig_t mSysConfig;
config_t mConfig;
char mVersion[12];
uint16_t mSendTicker;
uint16_t mSendInterval;
uint8_t mSendLastIvId;
invPayload_t mPayload[MAX_NUM_INVERTERS];
@ -114,12 +236,9 @@ class app : public Main {
uint32_t mRxSuccess;
uint32_t mFrameCnt;
uint8_t mLastPacketId;
uint8_t mMaxRetransPerPyld;
// timer
uint32_t mTicker;
bool mSerialValues;
bool mSerialDebug;
uint32_t mRxTicker;
@ -132,7 +251,6 @@ class app : public Main {
// serial
uint16_t mSerialTicker;
uint16_t mSerialInterval;
};
#endif /*__APP_H__*/

39
tools/esp8266/config.h

@ -31,6 +31,14 @@
// default device name
#define DEF_DEVICE_NAME "AHOY-DTU"
// default pinout
#define DEF_RF24_CS_PIN 15
#define DEF_RF24_CE_PIN 2
#define DEF_RF24_IRQ_PIN 0
// default NRF24 power, possible values (0 - 3)
#define DEF_AMPLIFIERPOWER 2
// number of packets hold in buffer
#define PACKET_BUFFER_SIZE 30
@ -38,13 +46,10 @@
#define MAX_NUM_INVERTERS 3
// default serial interval
#define SERIAL_INTERVAL 5
#define SERIAL_INTERVAL 5
// default send interval
#define SEND_INTERVAL 30
// default mqtt interval
#define MQTT_INTERVAL 60
#define SEND_INTERVAL 30
// maximum human readable inverter name length
#define MAX_NAME_LENGTH 16
@ -64,6 +69,30 @@
// threshold of minimum power on which the inverter is marked as inactive
#define INACT_PWR_THRESH 3
// default ntp server uri
#define DEF_NTP_SERVER_NAME "pool.ntp.org"
// default ntp server port
#define DEF_NTP_PORT 8888
// default mqtt interval
#define MQTT_INTERVAL 60
// default MQTT broker uri
#define DEF_MQTT_BROKER "\0"
// default MQTT port
#define DEF_MQTT_PORT 1883
// default MQTT user
#define DEF_MQTT_USER "\0"
// default MQTT pwd
#define DEF_MQTT_PWD "\0"
// default MQTT topic
#define DEF_MQTT_TOPIC "inverter"
// changes the style of "/setup" page, visualized = nicer
#define LIVEDATA_VISUALIZED

91
tools/esp8266/defines.h

@ -8,20 +8,12 @@
#include "config.h"
//-------------------------------------
// PINOUT (Default, can be changed in setup)
//-------------------------------------
#define RF24_CS_PIN 15
#define RF24_CE_PIN 2
#define RF24_IRQ_PIN 0
//-------------------------------------
// VERSION
//-------------------------------------
#define VERSION_MAJOR 0
#define VERSION_MINOR 5
#define VERSION_PATCH 9
#define VERSION_PATCH 11
//-------------------------------------
@ -111,18 +103,58 @@ typedef enum {
#define SER_INTERVAL_LEN 2 // uint16_t
#define ADDR_START 0
#define ADDR_SSID ADDR_START
#define ADDR_PWD ADDR_SSID + SSID_LEN
#define ADDR_DEVNAME ADDR_PWD + PWD_LEN
#define ADDR_WIFI_CRC ADDR_DEVNAME + DEVNAME_LEN
#define ADDR_START_SETTINGS ADDR_WIFI_CRC + CRC_LEN
typedef struct {
char broker[MQTT_ADDR_LEN];
uint16_t port;
char user[MQTT_USER_LEN];
char pwd[MQTT_PWD_LEN];
char topic[MQTT_TOPIC_LEN];
} mqttConfig_t;
typedef struct {
char deviceName[DEVNAME_LEN];
// wifi
char stationSsid[SSID_LEN];
char stationPwd[PWD_LEN];
} sysConfig_t;
typedef struct {
// nrf24
uint16_t sendInterval;
uint8_t maxRetransPerPyld;
uint8_t pinCs;
uint8_t pinCe;
uint8_t pinIrq;
uint8_t amplifierPower;
// ntp
char ntpAddr[NTP_ADDR_LEN];
uint16_t ntpPort;
// mqtt
mqttConfig_t mqtt;
// serial
uint16_t serialInterval;
bool serialShowIv;
bool serialDebug;
} config_t;
#define ADDR_PINOUT ADDR_START_SETTINGS
#define CFG_MQTT_LEN MQTT_ADDR_LEN + 2 + MQTT_USER_LEN + MQTT_PWD_LEN +MQTT_TOPIC_LEN
#define CFG_SYS_LEN DEVNAME_LEN + SSID_LEN + PWD_LEN + 1
#define CFG_LEN 7 + NTP_ADDR_LEN + 2 + CFG_MQTT_LEN + 4
#define ADDR_RF24_AMP_PWR ADDR_PINOUT + PINOUT_LEN
#define ADDR_START 0
#define ADDR_CFG_SYS ADDR_START
#define ADDR_WIFI_CRC ADDR_CFG_SYS + CFG_SYS_LEN
#define ADDR_START_SETTINGS ADDR_WIFI_CRC + CRC_LEN
#define ADDR_CFG ADDR_START_SETTINGS
#define ADDR_CFG_INVERTER ADDR_CFG + CFG_LEN
#define ADDR_INV_ADDR ADDR_RF24_AMP_PWR + RF24_AMP_PWR_LEN
#define ADDR_INV_ADDR ADDR_CFG_INVERTER
#define ADDR_INV_NAME ADDR_INV_ADDR + INV_ADDR_LEN
#define ADDR_INV_CH_PWR ADDR_INV_NAME + INV_NAME_LEN
#define ADDR_INV_CH_NAME ADDR_INV_CH_PWR + INV_CH_CH_PWR_LEN
@ -130,31 +162,20 @@ typedef enum {
#define ADDR_INV_MAX_RTRY ADDR_INV_INTERVAL + INV_INTERVAL_LEN
#define ADDR_INV_PWR_LIM ADDR_INV_MAX_RTRY + INV_MAX_RTRY_LEN
#define ADDR_NTP_ADDR ADDR_INV_PWR_LIM + INV_PWR_LIM_LEN //Bugfix #125
#define ADDR_NTP_PORT ADDR_NTP_ADDR + NTP_ADDR_LEN
#define ADDR_MQTT_ADDR ADDR_NTP_PORT + NTP_PORT_LEN
#define ADDR_MQTT_USER ADDR_MQTT_ADDR + MQTT_ADDR_LEN
#define ADDR_MQTT_PWD ADDR_MQTT_USER + MQTT_USER_LEN
#define ADDR_MQTT_TOPIC ADDR_MQTT_PWD + MQTT_PWD_LEN
#define ADDR_MQTT_INTERVAL ADDR_MQTT_TOPIC + MQTT_TOPIC_LEN
#define ADDR_MQTT_PORT ADDR_MQTT_INTERVAL + MQTT_INTERVAL_LEN
#define ADDR_NEXT ADDR_INV_PWR_LIM + INV_PWR_LIM_LEN
#define ADDR_SER_ENABLE ADDR_MQTT_PORT + MQTT_PORT_LEN
#define ADDR_SER_DEBUG ADDR_SER_ENABLE + SER_ENABLE_LEN
#define ADDR_SER_INTERVAL ADDR_SER_DEBUG + SER_DEBUG_LEN
#define ADDR_NEXT ADDR_SER_INTERVAL + SER_INTERVAL_LEN
// #define ADDR_SETTINGS_CRC 950
#define ADDR_SETTINGS_CRC ADDR_NEXT + 2
#if(ADDR_SETTINGS_CRC <= ADDR_NEXT)
#pragma error "address overlap! (ADDR_SETTINGS_CRC="+ ADDR_SETTINGS_CRC +", ADDR_NEXT="+ ADDR_NEXT +")"
#pragma error "address overlap! (ADDR_SETTINGS_CRC="+ ADDR_SETTINGS_CRC +", ADDR_NEXT="+ ADDR_NEXT +")"
#endif
#if(ADDR_SETTINGS_CRC >= 4096 - CRC_LEN)
#pragma error "EEPROM size exceeded! (ADDR_SETTINGS_CRC="+ ADDR_SETTINGS_CRC +", CRC_LEN="+ CRC_LEN +")"
#pragma error "Configure less inverters? (MAX_NUM_INVERTERS=" + MAX_NUM_INVERTERS +")"
#pragma error "EEPROM size exceeded! (ADDR_SETTINGS_CRC="+ ADDR_SETTINGS_CRC +", CRC_LEN="+ CRC_LEN +")"
#pragma error "Configure less inverters? (MAX_NUM_INVERTERS=" + MAX_NUM_INVERTERS +")"
#endif
#endif /*__DEFINES_H__*/

2
tools/esp8266/eep.h

@ -130,7 +130,7 @@ class eep {
EEPROM.write(addr++, (value ) & 0xff);
}
void write(uint64_t addr, uint64_t value) {
void write(uint32_t addr, uint64_t value) {
EEPROM.write(addr++, (value >> 56) & 0xff);
EEPROM.write(addr++, (value >> 48) & 0xff);
EEPROM.write(addr++, (value >> 40) & 0xff);

30
tools/esp8266/esp8266.cpp

@ -1,30 +0,0 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#include "dbg.h"
#include "app.h"
#include "config.h"
app myApp;
//-----------------------------------------------------------------------------
IRAM_ATTR void handleIntr(void) {
myApp.handleIntr();
}
//-----------------------------------------------------------------------------
void setup() {
myApp.setup(WIFI_TRY_CONNECT_TIME);
// TODO: move to HmRadio
attachInterrupt(digitalPinToInterrupt(myApp.getIrqPin()), handleIntr, FALLING);
}
//-----------------------------------------------------------------------------
void loop() {
myApp.loop();
}

2
tools/esp8266/hmInverter.h

@ -83,7 +83,7 @@ class Inverter {
Inverter() {
ts = 0;
powerLimit[0] = -1; // 65535 W Limit -> unlimited
powerLimit[0] = 0xffff; // 65535 W Limit -> unlimited
powerLimit[1] = 0x0001; // 0x0000 --> set temporary , 0x0001 --> set persistent
devControlRequest = false;
devControlCmd = 0xff;

24
tools/esp8266/hmRadio.h

@ -27,7 +27,7 @@
#define ALL_FRAMES 0x80
#define SINGLE_FRAME 0x81
const char* const rf24AmpPower[] = {"MIN", "LOW", "HIGH", "MAX"};
const char* const rf24AmpPowerNames[] = {"MIN", "LOW", "HIGH", "MAX"};
//-----------------------------------------------------------------------------
@ -55,7 +55,7 @@ const char* const rf24AmpPower[] = {"MIN", "LOW", "HIGH", "MAX"};
//-----------------------------------------------------------------------------
// HM Radio class
//-----------------------------------------------------------------------------
template <uint8_t CE_PIN, uint8_t CS_PIN, uint8_t IRQ_PIN, class BUFFER, uint64_t DTU_ID=DTU_RADIO_ID>
template <uint8_t CE_PIN, uint8_t CS_PIN, class BUFFER, uint64_t DTU_ID=DTU_RADIO_ID>
class HmRadio {
public:
HmRadio() : mNrf24(CE_PIN, CS_PIN, SPI_SPEED) {
@ -73,11 +73,6 @@ class HmRadio {
mRxChIdx = 0; // Start RX with 03
mRxLoopCnt = RF_LOOP_CNT;
pinCs = CS_PIN;
pinCe = CE_PIN;
pinIrq = IRQ_PIN;
AmplifierPower = 1;
mSendCnt = 0;
mSerialDebug = false;
@ -85,13 +80,13 @@ class HmRadio {
}
~HmRadio() {}
void setup(BUFFER *ctrl) {
void setup(config_t *config, BUFFER *ctrl) {
DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setup"));
pinMode(pinIrq, INPUT_PULLUP);
pinMode(config->pinIrq, INPUT_PULLUP);
mBufCtrl = ctrl;
mNrf24.begin(pinCe, pinCs);
mNrf24.begin(config->pinCe, config->pinCs);
mNrf24.setRetries(0, 0);
mNrf24.setChannel(DEFAULT_RECV_CHANNEL);
@ -106,8 +101,8 @@ class HmRadio {
// enable only receiving interrupts
mNrf24.maskIRQ(true, true, false);
DPRINTLN(DBG_INFO, F("RF24 Amp Pwr: RF24_PA_") + String(rf24AmpPower[AmplifierPower]));
mNrf24.setPALevel(AmplifierPower & 0x03);
DPRINTLN(DBG_INFO, F("RF24 Amp Pwr: RF24_PA_") + String(rf24AmpPowerNames[config->amplifierPower]));
mNrf24.setPALevel(config->amplifierPower & 0x03);
mNrf24.startListening();
DPRINTLN(DBG_INFO, F("Radio Config:"));
@ -277,11 +272,8 @@ class HmRadio {
return mNrf24.isChipConnected();
}
uint8_t pinCs;
uint8_t pinCe;
uint8_t pinIrq;
uint8_t AmplifierPower;
uint32_t mSendCnt;
bool mSerialDebug;

8
tools/esp8266/hmSystem.h

@ -30,9 +30,9 @@ class HmSystem {
// TODO: cleanup
}
void setup() {
void setup(config_t *config) {
DPRINTLN(DBG_VERBOSE, F("hmSystem.h:setup"));
Radio.setup(&BufCtrl);
Radio.setup(config, &BufCtrl);
}
INVERTERTYPE *addInverter(const char *name, uint64_t serial, uint16_t chMaxPwr[]) {
@ -86,9 +86,9 @@ class HmSystem {
return NULL;
}
INVERTERTYPE *getInverterByPos(uint8_t pos) {
INVERTERTYPE *getInverterByPos(uint8_t pos, bool check = true) {
DPRINTLN(DBG_VERBOSE, F("hmSystem.h:getInverterByPos"));
if(mInverter[pos].serial.u64 != 0ULL)
if((mInverter[pos].serial.u64 != 0ULL) || false == check)
return &mInverter[pos];
else
return NULL;

2
tools/esp8266/html/convert.py

@ -34,5 +34,5 @@ def convert2Header(inFile):
convert2Header("index.html")
convert2Header("setup.html")
convert2Header("hoymiles.html")
convert2Header("visualization.html")
convert2Header("style.css")

4
tools/esp8266/html/h/hoymiles_html.h

@ -1,4 +0,0 @@
#ifndef __HOYMILES_HTML_H__
#define __HOYMILES_HTML_H__
const char hoymiles_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\">getAjax('/livedata', 'livedata');window.setInterval(\"getAjax('/livedata', 'livedata')\", {JS_TS});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><style type=\"text/css\"></style></head><body><h1>AHOY - {DEVICE}</h1><div id=\"content\" class=\"content\"><div id=\"livedata\"></div><p>Every {TS}seconds the values are updated</p></div><div id=\"footer\"><p class=\"left\">&copy 2022</p><p class=\"left\"><a href=\"/\">Home</a></p><p class=\"right\">AHOY :: {VERSION}</p></div></body></html>";
#endif /*__HOYMILES_HTML_H__*/

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

@ -1,4 +1,4 @@
#ifndef __INDEX_HTML_H__
#define __INDEX_HTML_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\">getAjax('/uptime', 'uptime');getAjax('/time', 'time');getAjax('/cmdstat', 'cmds');window.setInterval(\"getAjax('/uptime', 'uptime')\", {JS_TS});window.setInterval(\"getAjax('/time', 'time')\", {JS_TS});window.setInterval(\"getAjax('/cmdstat', 'cmds')\", {JS_TS});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><p>Every {TS}seconds the values are updated</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 <a href=\"https://github.com/grindylow/ahoy/issues\">Github</a><br/><br/>Discuss with us on <a href=\"https://discord.gg/WzhxEY62mB\">Discord</a><br/><p class=\"lic\"><a href=\"https://creativecommons.org/licenses/by-nc-sa/3.0/de\">Creative Commons - https://creativecommons.org/licenses/by-nc-sa/3.0/de/</a><br/>Check the licenses which are published on <a href=\"https://github.com/grindylow/ahoy\">https://github.com/grindylow/ahoy</a>as well</p></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><p class=\"right\">Git SHA: {BUILD}</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\">getAjax('/uptime', 'uptime');getAjax('/cmdstat', 'cmds');window.setInterval(\"getAjax('/uptime', 'uptime')\", {JS_TS});window.setInterval(\"getAjax('/cmdstat', 'cmds')\", {JS_TS});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=\"/visualization\">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\">Statistics: </span><pre id=\"cmds\"></pre></p><p>Every {TS}seconds the values are updated</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 <a href=\"https://github.com/grindylow/ahoy/issues\">Github</a><br/><br/>Discuss with us on <a href=\"https://discord.gg/WzhxEY62mB\">Discord</a><br/><p class=\"lic\"><a href=\"https://creativecommons.org/licenses/by-nc-sa/3.0/de\">Creative Commons - https://creativecommons.org/licenses/by-nc-sa/3.0/de/</a><br/>Check the licenses which are published on <a href=\"https://github.com/grindylow/ahoy\">https://github.com/grindylow/ahoy</a>as well</p></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><p class=\"right\">Git SHA: {BUILD}</p></div></body></html>";
#endif /*__INDEX_HTML_H__*/

4
tools/esp8266/html/h/visualization_html.h

@ -0,0 +1,4 @@
#ifndef __VISUALIZATION_HTML_H__
#define __VISUALIZATION_HTML_H__
const char visualization_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\">getAjax('/livedata', 'livedata');window.setInterval(\"getAjax('/livedata', 'livedata')\", {JS_TS});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><style type=\"text/css\"></style></head><body><h1>AHOY - {DEVICE}</h1><div id=\"content\" class=\"content\"><div id=\"livedata\"></div><p>Every {TS}seconds the values are updated</p></div><div id=\"footer\"><p class=\"left\">&copy 2022</p><p class=\"left\"><a href=\"/\">Home</a></p><p class=\"right\">AHOY :: {VERSION}</p></div></body></html>";
#endif /*__VISUALIZATION_HTML_H__*/

5
tools/esp8266/html/index.html

@ -6,10 +6,8 @@
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript">
getAjax('/uptime', 'uptime');
getAjax('/time', 'time');
getAjax('/cmdstat', 'cmds');
window.setInterval("getAjax('/uptime', 'uptime')", {JS_TS});
window.setInterval("getAjax('/time', 'time')", {JS_TS});
window.setInterval("getAjax('/cmdstat', 'cmds')", {JS_TS});
function getAjax(url, resid) {
@ -33,12 +31,11 @@
<h1>AHOY - {DEVICE}</h1>
<div id="content" class="content">
<p>
<a href="/hoymiles">Visualization</a><br/>
<a href="/visualization">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>
<p>Every {TS}seconds the values are updated</p>

0
tools/esp8266/html/hoymiles.html → tools/esp8266/html/visualization.html

471
tools/esp8266/main.cpp

@ -3,476 +3,29 @@
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#include "main.h"
#include "version.h"
#include "dbg.h"
#include "app.h"
#include "config.h"
#include "html/h/style_css.h"
#include "html/h/setup_html.h"
app myApp;
//-----------------------------------------------------------------------------
Main::Main(void) {
mDns = new DNSServer();
mWeb = new ESP8266WebServer(80);
mUpdater = new ESP8266HTTPUpdateServer();
mUdp = new WiFiUDP();
mApActive = true;
mWifiSettingsValid = false;
mSettingsValid = false;
mLimit = 10;
mNextTryTs = 0;
mApLastTick = 0;
snprintf(mVersion, 12, "%d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
memset(&mDeviceName, 0, DEVNAME_LEN);
mEep = new eep();
Serial.begin(115200);
DPRINTLN(DBG_VERBOSE, F("Main::Main"));
mUptimeSecs = 0;
mUptimeTicker = 0xffffffff;
mUptimeInterval = 1000;
#ifdef AP_ONLY
mTimestamp = 1;
#else
mTimestamp = 0;
#endif
mHeapStatCnt = 0;
}
//-----------------------------------------------------------------------------
void Main::setup(uint32_t timeout) {
DPRINTLN(DBG_VERBOSE, F("Main::setup"));
bool startAp = mApActive;
mLimit = timeout;
mWeb->on("/setup", std::bind(&Main::showSetup, this));
mWeb->on("/save", std::bind(&Main::showSave, this));
mWeb->on("/uptime", std::bind(&Main::showUptime, this));
mWeb->on("/time", std::bind(&Main::showTime, this));
mWeb->on("/style.css", std::bind(&Main::showCss, this));
mWeb->on("/reboot", std::bind(&Main::showReboot, this));
mWeb->on("/factory", std::bind(&Main::showFactoryRst, this));
mWeb->onNotFound ( std::bind(&Main::showNotFound, this));
startAp = getConfig();
#ifndef AP_ONLY
if(false == startAp)
startAp = setupStation(timeout);
#endif
mUpdater->setup(mWeb);
mApActive = startAp;
mStActive = !startAp;
}
//-----------------------------------------------------------------------------
void Main::loop(void) {
//DPRINTLN(DBG_VERBOSE, F("M"));
if(mApActive) {
mDns->processNextRequest();
#ifndef AP_ONLY
if(checkTicker(&mNextTryTs, (WIFI_AP_ACTIVE_TIME * 1000))) {
mApActive = setupStation(mLimit);
if(mApActive) {
if(strlen(WIFI_AP_PWD) < 8)
DPRINTLN(DBG_ERROR, F("password must be at least 8 characters long"));
mApLastTick = millis();
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
setupAp(WIFI_AP_SSID, WIFI_AP_PWD);
}
}
else {
if(millis() - mApLastTick > 10000) {
uint8_t cnt = WiFi.softAPgetStationNum();
if(cnt > 0) {
DPRINTLN(DBG_INFO, String(cnt) + F(" clients connected, resetting AP timeout"));
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
}
mApLastTick = millis();
DPRINTLN(DBG_INFO, F("AP will be closed in ") + String((mNextTryTs - mApLastTick) / 1000) + F(" seconds"));
}
}
#endif
}
mWeb->handleClient();
if(checkTicker(&mUptimeTicker, mUptimeInterval)) {
mUptimeSecs++;
if(0 != mTimestamp)
mTimestamp++;
else {
if(!mApActive) {
mTimestamp = getNtpTime();
DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp));
}
}
/*if(++mHeapStatCnt >= 10) {
mHeapStatCnt = 0;
stats();
}*/
}
if (WiFi.status() != WL_CONNECTED) {
DPRINTLN(DBG_INFO, "[WiFi]: Connection Lost");
mStActive = false;
}
}
//-----------------------------------------------------------------------------
bool Main::getConfig(void) {
DPRINTLN(DBG_VERBOSE, F("Main::getConfig"));
bool mApActive = false;
mWifiSettingsValid = checkEEpCrc(ADDR_START, ADDR_WIFI_CRC, ADDR_WIFI_CRC);
mSettingsValid = checkEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT)-(ADDR_START_SETTINGS)), ADDR_SETTINGS_CRC);
if(mWifiSettingsValid) {
mEep->read(ADDR_SSID, mStationSsid, SSID_LEN);
mEep->read(ADDR_PWD, mStationPwd, PWD_LEN);
mEep->read(ADDR_DEVNAME, mDeviceName, DEVNAME_LEN);
}
if((!mWifiSettingsValid) || (mStationSsid[0] == 0xff)) {
snprintf(mStationSsid, SSID_LEN, "%s", FB_WIFI_SSID);
snprintf(mStationPwd, PWD_LEN, "%s", FB_WIFI_PWD);
snprintf(mDeviceName, DEVNAME_LEN, "%s", DEF_DEVICE_NAME);
}
return mApActive;
}
//-----------------------------------------------------------------------------
void Main::setupAp(const char *ssid, const char *pwd) {
DPRINTLN(DBG_VERBOSE, F("Main::setupAp"));
IPAddress apIp(192, 168, 1, 1);
DPRINTLN(DBG_INFO, F("\n---------\nAP MODE\nSSID: ")
+ String(ssid) + F("\nPWD: ")
+ String(pwd) + F("\nActive for: ")
+ String(WIFI_AP_ACTIVE_TIME) + F(" seconds")
+ F("\n---------\n"));
DPRINTLN(DBG_DEBUG, String(mNextTryTs));
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
WiFi.softAP(ssid, pwd);
mDns->start(mDnsPort, "*", apIp);
mWeb->onNotFound([&]() {
showSetup();
});
mWeb->on("/", std::bind(&Main::showSetup, this));
mWeb->begin();
}
//-----------------------------------------------------------------------------
bool Main::setupStation(uint32_t timeout) {
DPRINTLN(DBG_VERBOSE, F("Main::setupStation"));
int32_t cnt;
bool startAp = false;
if(timeout >= 3)
cnt = (timeout - 3) / 2 * 10;
else {
timeout = 1;
cnt = 1;
}
WiFi.mode(WIFI_STA);
WiFi.begin(mStationSsid, mStationPwd);
if(String(mDeviceName) != "")
WiFi.hostname(mDeviceName);
delay(2000);
DPRINTLN(DBG_INFO, F("connect to network '") + String(mStationSsid) + F("' ..."));
while (WiFi.status() != WL_CONNECTED) {
delay(100);
if(cnt % 100 == 0)
Serial.println(".");
else
Serial.print(".");
if(timeout > 0) { // limit == 0 -> no limit
if(--cnt <= 0) {
if(WiFi.status() != WL_CONNECTED) {
startAp = true;
WiFi.disconnect();
}
delay(100);
break;
}
}
}
Serial.println(".");
if(false == startAp) {
mWeb->begin();
}
delay(1000);
return startAp;
}
//-----------------------------------------------------------------------------
void Main::showSetup(void) {
DPRINTLN(DBG_VERBOSE, F("Main::showSetup"));
String html = FPSTR(setup_html);
html.replace(F("{SSID}"), mStationSsid);
// PWD will be left at the default value (for protection)
// -> the PWD will only be changed if it does not match the default "{PWD}"
html.replace(F("{DEVICE}"), String(mDeviceName));
html.replace(F("{VERSION}"), String(mVersion));
if(mApActive)
html.replace("{IP}", String(F("http://192.168.1.1")));
else
html.replace("{IP}", (F("http://") + String(WiFi.localIP().toString())));
mWeb->send(200, F("text/html"), html);
}
//-----------------------------------------------------------------------------
void Main::showCss(void) {
DPRINTLN(DBG_VERBOSE, F("Main::showCss"));
mWeb->send(200, "text/css", FPSTR(style_css));
}
//-----------------------------------------------------------------------------
void Main::showSave(void) {
DPRINTLN(DBG_VERBOSE, F("Main::showSave"));
saveValues(true);
}
//-----------------------------------------------------------------------------
void Main::saveValues(bool webSend = true) {
DPRINTLN(DBG_VERBOSE, F("Main::saveValues"));
if(mWeb->args() > 0) {
if(mWeb->arg("ssid") != "") {
memset(mStationSsid, 0, SSID_LEN);
mWeb->arg("ssid").toCharArray(mStationSsid, SSID_LEN);
mEep->write(ADDR_SSID, mStationSsid, SSID_LEN);
if(mWeb->arg("pwd") != "{PWD}") {
memset(mStationPwd, 0, PWD_LEN);
mWeb->arg("pwd").toCharArray(mStationPwd, PWD_LEN);
mEep->write(ADDR_PWD, mStationPwd, PWD_LEN);
}
}
memset(mDeviceName, 0, DEVNAME_LEN);
mWeb->arg("device").toCharArray(mDeviceName, DEVNAME_LEN);
mEep->write(ADDR_DEVNAME, mDeviceName, DEVNAME_LEN);
mEep->commit();
updateCrc();
if(webSend) {
if(mWeb->arg("reboot") == "on")
showReboot();
else // TODO: add device name as redirect in AP-mode
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Setup saved</title><meta http-equiv=\"refresh\" content=\"0; URL=/setup\"></head><body>"
"<p>saved</p></body></html>"));
}
}
}
//-----------------------------------------------------------------------------
void Main::updateCrc(void) {
DPRINTLN(DBG_VERBOSE, F("Main::updateCrc"));
uint16_t crc;
crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
//Serial.println("new CRC: " + String(crc, HEX));
mEep->write(ADDR_WIFI_CRC, crc);
mEep->commit();
}
//-----------------------------------------------------------------------------
void Main::showUptime(void) {
//DPRINTLN(DBG_VERBOSE, F("Main::showUptime"));
char time[20] = {0};
int upTimeSc = uint32_t((mUptimeSecs) % 60);
int upTimeMn = uint32_t((mUptimeSecs / (60)) % 60);
int upTimeHr = uint32_t((mUptimeSecs / (60 * 60)) % 24);
int upTimeDy = uint32_t((mUptimeSecs / (60 * 60 * 24)) % 365);
snprintf(time, 20, "%d Tage, %02d:%02d:%02d", upTimeDy, upTimeHr, upTimeMn, upTimeSc);
mWeb->send(200, "text/plain", String(time));
}
//-----------------------------------------------------------------------------
void Main::showTime(void) {
//DPRINTLN(DBG_VERBOSE, F("Main::showTime"));
mWeb->send(200, "text/plain", getDateTimeStr(mTimestamp));
}
//-----------------------------------------------------------------------------
void Main::showNotFound(void) {
DPRINTLN(DBG_VERBOSE, F("Main::showNotFound - ") + mWeb->uri());
String msg = F("File Not Found\n\nURI: ");
msg += mWeb->uri();
msg += F("\nMethod: ");
msg += ( mWeb->method() == HTTP_GET ) ? "GET" : "POST";
msg += F("\nArguments: ");
msg += mWeb->args();
msg += "\n";
for(uint8_t i = 0; i < mWeb->args(); i++ ) {
msg += " " + mWeb->argName(i) + ": " + mWeb->arg(i) + "\n";
}
mWeb->send(404, F("text/plain"), msg);
IRAM_ATTR void handleIntr(void) {
myApp.handleIntr();
}
//-----------------------------------------------------------------------------
void Main::showReboot(void) {
DPRINTLN(DBG_VERBOSE, F("Main::showReboot"));
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Rebooting ...</title><meta http-equiv=\"refresh\" content=\"10; URL=/\"></head><body>rebooting ... auto reload after 10s</body></html>"));
delay(1000);
ESP.restart();
}
//-----------------------------------------------------------------------------
void Main::showFactoryRst(void) {
DPRINTLN(DBG_VERBOSE, F("Main::showFactoryRst"));
String content = "";
int refresh = 3;
if(mWeb->args() > 0) {
if(mWeb->arg("reset").toInt() == 1) {
eraseSettings(true);
content = F("factory reset: success\n\nrebooting ... ");
refresh = 10;
}
else {
content = F("factory reset: aborted");
refresh = 3;
}
}
else {
content = F("<h1>Factory Reset</h1>"
"<p><a href=\"/factory?reset=1\">RESET</a><br/><br/><a href=\"/factory?reset=0\">CANCEL</a><br/></p>");
refresh = 120;
}
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Factory Reset</title><meta http-equiv=\"refresh\" content=\"") + String(refresh) + F("; URL=/\"></head><body>") + content + F("</body></html>"));
if(refresh == 10) {
delay(1000);
ESP.restart();
}
}
//-----------------------------------------------------------------------------
time_t Main::getNtpTime(void) {
//DPRINTLN(DBG_VERBOSE, F("Main::getNtpTime"));
time_t date = 0;
IPAddress timeServer;
uint8_t buf[NTP_PACKET_SIZE];
uint8_t retry = 0;
WiFi.hostByName(NTP_SERVER_NAME, timeServer);
mUdp->begin(NTP_LOCAL_PORT);
void setup() {
myApp.setup(WIFI_TRY_CONNECT_TIME);
sendNTPpacket(timeServer);
while(retry++ < 5) {
int wait = 150;
while(--wait) {
if(NTP_PACKET_SIZE <= mUdp->parsePacket()) {
uint64_t secsSince1900;
mUdp->read(buf, NTP_PACKET_SIZE);
secsSince1900 = (buf[40] << 24);
secsSince1900 |= (buf[41] << 16);
secsSince1900 |= (buf[42] << 8);
secsSince1900 |= (buf[43] );
date = secsSince1900 - 2208988800UL; // UTC time
date += (TIMEZONE + offsetDayLightSaving(date)) * 3600;
break;
}
else
delay(10);
}
}
return date;
}
//-----------------------------------------------------------------------------
void Main::sendNTPpacket(IPAddress& address) {
//DPRINTLN(DBG_VERBOSE, F("Main::sendNTPpacket"));
uint8_t buf[NTP_PACKET_SIZE] = {0};
buf[0] = B11100011; // LI, Version, Mode
buf[1] = 0; // Stratum
buf[2] = 6; // Max Interval between messages in seconds
buf[3] = 0xEC; // Clock Precision
// bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
buf[12] = 49; // four-byte reference ID identifying
buf[13] = 0x4E;
buf[14] = 49;
buf[15] = 52;
mUdp->beginPacket(address, 123); // NTP request, port 123
mUdp->write(buf, NTP_PACKET_SIZE);
mUdp->endPacket();
}
//-----------------------------------------------------------------------------
String Main::getDateTimeStr(time_t t) {
//DPRINTLN(DBG_VERBOSE, F("Main::getDateTimeStr"));
char str[20] = {0};
if(0 == t)
sprintf(str, "n/a");
else
sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", year(t), month(t), day(t), hour(t), minute(t), second(t));
return String(str);
// TODO: move to HmRadio
attachInterrupt(digitalPinToInterrupt(myApp.getIrqPin()), handleIntr, FALLING);
}
//-----------------------------------------------------------------------------
// calculates the daylight saving time for middle Europe. Input: Unixtime in UTC
// from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536
time_t Main::offsetDayLightSaving (uint32_t local_t) {
//DPRINTLN(DBG_VERBOSE, F("Main::offsetDayLightSaving"));
int m = month (local_t);
if(m < 3 || m > 10) return 0; // no DSL in Jan, Feb, Nov, Dez
if(m > 3 && m < 10) return 1; // DSL in Apr, May, Jun, Jul, Aug, Sep
int y = year (local_t);
int h = hour (local_t);
int hToday = (h + 24 * day(local_t));
if((m == 3 && hToday >= (1 + TIMEZONE + 24 * (31 - (5 * y /4 + 4) % 7)))
|| (m == 10 && hToday < (1 + TIMEZONE + 24 * (31 - (5 * y /4 + 1) % 7))) )
return 1;
else
return 0;
void loop() {
myApp.loop();
}

159
tools/esp8266/main.h

@ -1,159 +0,0 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#ifndef __MAIN_H__
#define __MAIN_H__
#include "dbg.h"
#include "Arduino.h"
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
// NTP
#include <WiFiUdp.h>
#include <TimeLib.h>
#include "eep.h"
#include "defines.h"
#include "crc.h"
const byte mDnsPort = 53;
/* NTP TIMESERVER CONFIG */
#define NTP_SERVER_NAME "pool.ntp.org"
#define NTP_LOCAL_PORT 8888
#define NTP_PACKET_SIZE 48
#define TIMEZONE 1 // Central European time +1
class Main {
public:
Main(void);
virtual void setup(uint32_t timeout);
virtual void loop();
String getDateTimeStr (time_t t);
protected:
void showReboot(void);
virtual void saveValues(bool webSend);
virtual void updateCrc(void);
inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
uint8_t buf[32];
uint16_t crc = 0xffff;
uint8_t len;
while(length > 0) {
len = (length < 32) ? length : 32;
mEep->read(start, buf, len);
crc = crc16(buf, len, crc);
start += len;
length -= len;
}
return crc;
}
bool checkEEpCrc(uint32_t start, uint32_t length, uint32_t crcPos) {
DPRINTLN(DBG_VERBOSE, F("main.h:checkEEpCrc"));
DPRINTLN(DBG_DEBUG, F("start: ") + String(start) + F(", length: ") + String(length));
uint16_t crcRd, crcCheck;
crcCheck = buildEEpCrc(start, length);
mEep->read(crcPos, &crcRd);
DPRINTLN(DBG_DEBUG, "CRC RD: " + String(crcRd, HEX) + " CRC CALC: " + String(crcCheck, HEX));
return (crcCheck == crcRd);
}
void eraseSettings(bool all = false) {
//DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings"));
uint8_t buf[64] = {0};
uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS;
uint16_t end;
do {
end = addr + 64;
if(end > (ADDR_SETTINGS_CRC + 2))
end = (ADDR_SETTINGS_CRC + 2);
DPRINTLN(DBG_DEBUG, F("erase: 0x") + String(addr, HEX) + " - 0x" + String(end, HEX));
mEep->write(addr, buf, (end-addr));
addr = end;
} while(addr < (ADDR_SETTINGS_CRC + 2));
mEep->commit();
}
inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
//DPRINTLN(DBG_VERBOSE, F("c"));
uint32_t mil = millis();
if(mil >= *ticker) {
*ticker = mil + interval;
return true;
}
else if(mil < (*ticker - interval)) {
*ticker = mil + interval;
return true;
}
return false;
}
void stats(void) {
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
uint32_t free;
uint16_t max;
uint8_t frag;
ESP.getHeapStats(&free, &max, &frag);
DPRINT(DBG_VERBOSE, F("free: ") + String(free));
DPRINT(DBG_VERBOSE, F(" - max: ") + String(max) + "%");
DPRINTLN(DBG_VERBOSE, F(" - frag: ") + String(frag));
}
char mStationSsid[SSID_LEN];
char mStationPwd[PWD_LEN];
bool mWifiSettingsValid;
bool mSettingsValid;
bool mApActive;
bool mStActive;
ESP8266WebServer *mWeb;
char mVersion[9];
char mDeviceName[DEVNAME_LEN];
eep *mEep;
uint32_t mTimestamp;
uint32_t mLimit;
uint32_t mNextTryTs;
uint32_t mApLastTick;
private:
bool getConfig(void);
void setupAp(const char *ssid, const char *pwd);
bool setupStation(uint32_t timeout);
void showNotFound(void);
virtual void showSetup(void);
virtual void showSave(void);
void showUptime(void);
void showTime(void);
void showCss(void);
void showFactoryRst(void);
time_t getNtpTime(void);
void sendNTPpacket(IPAddress& address);
time_t offsetDayLightSaving (uint32_t local_t);
uint32_t mUptimeTicker;
uint16_t mUptimeInterval;
uint32_t mUptimeSecs;
uint8_t mHeapStatCnt;
DNSServer *mDns;
ESP8266HTTPUpdateServer *mUpdater;
WiFiUDP *mUdp; // for time server
};
#endif /*__MAIN_H__*/

66
tools/esp8266/mqtt.h

@ -12,44 +12,34 @@
class mqtt {
public:
PubSubClient *mClient;
mqtt() {
mClient = new PubSubClient(mEspClient);
mAddressSet = false;
memset(mBroker, 0, MQTT_ADDR_LEN);
memset(mUser, 0, MQTT_USER_LEN);
memset(mPwd, 0, MQTT_PWD_LEN);
memset(mTopic, 0, MQTT_TOPIC_LEN);
memset(mDevName, 0, DEVNAME_LEN);
}
~mqtt() { }
void setup(const char *broker, const char *topic, const char *user, const char *pwd, const char *devname, uint16_t port) {
void setup(mqttConfig_t *cfg, const char *devname) {
DPRINTLN(DBG_VERBOSE, F("mqtt.h:setup"));
mAddressSet = true;
mPort = port;
snprintf(mBroker, MQTT_ADDR_LEN, "%s", broker);
snprintf(mUser, MQTT_USER_LEN, "%s", user);
snprintf(mPwd, MQTT_PWD_LEN, "%s", pwd);
snprintf(mTopic, MQTT_TOPIC_LEN, "%s", topic);
mCfg = cfg;
snprintf(mDevName, DEVNAME_LEN, "%s", devname);
mClient->setServer(mBroker, mPort);
mClient->setServer(mCfg->broker, mCfg->port);
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE);
}
void setCallback(void (*func)(const char* topic, byte* payload, unsigned int length)){
mClient->setCallback(func);
void setCallback(MQTT_CALLBACK_SIGNATURE){
mClient->setCallback(callback);
}
void sendMsg(const char *topic, const char *msg) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
char top[64];
snprintf(top, 64, "%s/%s", mTopic, topic);
snprintf(top, 64, "%s/%s", mCfg->topic, topic);
sendMsg2(top, msg, false);
}
@ -69,35 +59,6 @@ class mqtt {
return mClient->connected();
}
char *getBroker(void) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getBroker"));
return mBroker;
}
char *getUser(void) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getUser"));
return mUser;
}
char *getPwd(void) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getPwd"));
return mPwd;
}
char *getTopic(void) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getTopic"));
return mTopic;
}
char *getDevName(void) {
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getDevName"));
return mDevName;
}
uint16_t getPort(void) {
return mPort;
}
void loop() {
//DPRINT(F("m"));
if(!mClient->connected())
@ -114,30 +75,27 @@ class mqtt {
if(strlen(mDevName) > 0) {
// der Server und der Port müssen neu gesetzt werden,
// da ein MQTT_CONNECTION_LOST -3 die Werte zerstört hat.
mClient->setServer(mBroker, mPort);
mClient->setServer(mCfg->broker, mCfg->port);
mClient->setBufferSize(MQTT_MAX_PACKET_SIZE);
if((strlen(mUser) > 0) && (strlen(mPwd) > 0))
mClient->connect(mDevName, mUser, mPwd);
if((strlen(mCfg->user) > 0) && (strlen(mCfg->pwd) > 0))
mClient->connect(mDevName, mCfg->user, mCfg->pwd);
else
mClient->connect(mDevName);
}
// ein Subscribe ist nur nach einem connect notwendig
char topic[MQTT_TOPIC_LEN + 13 ]; // "/devcontrol/#" --> + 6 byte
// ToDo: "/devcontrol/#" is hardcoded
snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mTopic);
snprintf(topic, MQTT_TOPIC_LEN + 13, "%s/devcontrol/#", mCfg->topic);
DPRINTLN(DBG_INFO, F("subscribe to ") + String(topic));
mClient->subscribe(topic); // subscribe to mTopic + "/devcontrol/#"
}
}
WiFiClient mEspClient;
PubSubClient *mClient;
bool mAddressSet;
uint16_t mPort;
char mBroker[MQTT_ADDR_LEN];
char mUser[MQTT_USER_LEN];
char mPwd[MQTT_PWD_LEN];
char mTopic[MQTT_TOPIC_LEN];
mqttConfig_t *mCfg;
char mDevName[DEVNAME_LEN];
};

401
tools/esp8266/web.cpp

@ -0,0 +1,401 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#include "web.h"
#include "html/h/index_html.h"
#include "html/h/style_css.h"
#include "favicon.h"
#include "html/h/setup_html.h"
#include "html/h/visualization_html.h"
//-----------------------------------------------------------------------------
web::web(app *main, sysConfig_t *sysCfg, config_t *config, char version[]) {
mMain = main;
mSysCfg = sysCfg;
mConfig = config;
mVersion = version;
mWeb = new ESP8266WebServer(80);
mUpdater = new ESP8266HTTPUpdateServer();
mUpdater->setup(mWeb);
}
//-----------------------------------------------------------------------------
void web::setup(void) {
mWeb->begin();
mWeb->on("/", std::bind(&web::showIndex, this));
mWeb->on("/style.css", std::bind(&web::showCss, this));
mWeb->on("/favicon.ico", std::bind(&web::showFavicon, this));
mWeb->onNotFound ( std::bind(&web::showNotFound, this));
mWeb->on("/uptime", std::bind(&web::showUptime, this));
mWeb->on("/reboot", std::bind(&web::showReboot, this));
mWeb->on("/erase", std::bind(&web::showErase, this));
mWeb->on("/factory", std::bind(&web::showFactoryRst, this));
mWeb->on("/setup", std::bind(&web::showSetup, this));
mWeb->on("/save", std::bind(&web::showSave, this));
mWeb->on("/cmdstat", std::bind(&web::showStatistics, this));
mWeb->on("/visualization", std::bind(&web::showVisualization, this));
mWeb->on("/livedata", std::bind(&web::showLiveData, this));
mWeb->on("/json", std::bind(&web::showJson, this));
mWeb->on("/api", HTTP_POST, std::bind(&web::showWebApi, this));
}
//-----------------------------------------------------------------------------
void web::loop(void) {
mWeb->handleClient();
}
//-----------------------------------------------------------------------------
void web::showIndex(void) {
DPRINTLN(DBG_VERBOSE, F("showIndex"));
String html = FPSTR(index_html);
html.replace(F("{DEVICE}"), mSysCfg->deviceName);
html.replace(F("{VERSION}"), mVersion);
html.replace(F("{TS}"), String(mConfig->sendInterval) + " ");
html.replace(F("{JS_TS}"), String(mConfig->sendInterval * 1000));
html.replace(F("{BUILD}"), String(AUTO_GIT_HASH));
mWeb->send(200, "text/html", html);
}
//-----------------------------------------------------------------------------
void web::showCss(void) {
mWeb->send(200, "text/css", FPSTR(style_css));
}
//-----------------------------------------------------------------------------
void web::showFavicon(void) {
static const char favicon_type[] PROGMEM = "image/x-icon";
static const char favicon_content[] PROGMEM = FAVICON_PANEL_16;
mWeb->send_P(200, favicon_type, favicon_content, sizeof(favicon_content));
}
//-----------------------------------------------------------------------------
void web::showNotFound(void) {
DPRINTLN(DBG_VERBOSE, F("showNotFound - ") + mWeb->uri());
String msg = F("File Not Found\n\nURI: ");
msg += mWeb->uri();
mWeb->send(404, F("text/plain"), msg);
}
//-----------------------------------------------------------------------------
void web::showUptime(void) {
char time[21] = {0};
uint32_t uptime = mMain->getUptime();
uint32_t upTimeSc = uint32_t((uptime) % 60);
uint32_t upTimeMn = uint32_t((uptime / (60)) % 60);
uint32_t upTimeHr = uint32_t((uptime / (60 * 60)) % 24);
uint32_t upTimeDy = uint32_t((uptime / (60 * 60 * 24)) % 365);
snprintf(time, 20, "%d Days, %02d:%02d:%02d", upTimeDy, upTimeHr, upTimeMn, upTimeSc);
mWeb->send(200, "text/plain", String(time) + "; now: " + mMain->getDateTimeStr(mMain->getTimestamp()));
}
//-----------------------------------------------------------------------------
void web::showReboot(void) {
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Rebooting ...</title><meta http-equiv=\"refresh\" content=\"10; URL=/\"></head><body>rebooting ... auto reload after 10s</body></html>"));
delay(1000);
ESP.restart();
}
//-----------------------------------------------------------------------------
void web::showErase() {
DPRINTLN(DBG_VERBOSE, F("showErase"));
mMain->eraseSettings();
showReboot();
}
//-----------------------------------------------------------------------------
void web::showFactoryRst(void) {
DPRINTLN(DBG_VERBOSE, F("showFactoryRst"));
String content = "";
int refresh = 3;
if(mWeb->args() > 0) {
if(mWeb->arg("reset").toInt() == 1) {
mMain->eraseSettings(true);
content = F("factory reset: success\n\nrebooting ... ");
refresh = 10;
}
else {
content = F("factory reset: aborted");
refresh = 3;
}
}
else {
content = F("<h1>Factory Reset</h1>"
"<p><a href=\"/factory?reset=1\">RESET</a><br/><br/><a href=\"/factory?reset=0\">CANCEL</a><br/></p>");
refresh = 120;
}
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Factory Reset</title><meta http-equiv=\"refresh\" content=\"") + String(refresh) + F("; URL=/\"></head><body>") + content + F("</body></html>"));
if(refresh == 10) {
delay(1000);
ESP.restart();
}
}
//-----------------------------------------------------------------------------
void web::showSetup(void) {
DPRINTLN(DBG_VERBOSE, F("showSetup"));
String html = FPSTR(setup_html);
html.replace(F("{SSID}"), mSysCfg->stationSsid);
// PWD will be left at the default value (for protection)
// -> the PWD will only be changed if it does not match the default "{PWD}"
html.replace(F("{DEVICE}"), String(mSysCfg->deviceName));
html.replace(F("{VERSION}"), String(mVersion));
if(mMain->getWifiApActive())
html.replace("{IP}", String(F("http://192.168.1.1")));
else
html.replace("{IP}", (F("http://") + String(WiFi.localIP().toString())));
String inv = "";
Inverter<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mMain->mSys->getInverterByPos(i);
inv += F("<p class=\"subdes\">Inverter ") + String(i) + "</p>";
inv += F("<label for=\"inv") + String(i) + F("Addr\">Address</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("Addr\" value=\"");
if(NULL != iv)
inv += String(iv->serial.u64, HEX);
inv += F("\"/ maxlength=\"12\" onkeyup=\"checkSerial()\">");
inv += F("<label for=\"inv") + String(i) + F("Name\">Name</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("Name\" value=\"");
if(NULL != iv)
inv += String(iv->name);
inv += F("\"/ maxlength=\"") + String(MAX_NAME_LENGTH) + "\">";
inv += F("<label for=\"inv") + String(i) + F("ActivePowerLimit\">Active Power Limit (W)</label>");
inv += F("<input type=\"text\" class=\"text\" name=\"inv") + String(i) + F("ActivePowerLimit\" value=\"");
if(NULL != iv)
inv += String(iv->powerLimit[0]);
inv += F("\"/ maxlength=\"") + String(6) + "\">";
inv += F("<label for=\"inv") + String(i) + F("ModPwr0\" name=\"lbl") + String(i);
inv += F("ModPwr\">Max Module Power (Wp)</label>");
for(uint8_t j = 0; j < 4; j++) {
inv += F("<input type=\"text\" class=\"text sh\" name=\"inv") + String(i) + F("ModPwr") + String(j) + F("\" value=\"");
if(NULL != iv)
inv += String(iv->chMaxPwr[j]);
inv += F("\"/ maxlength=\"4\">");
}
inv += F("<br/><label for=\"inv") + String(i) + F("ModName0\" name=\"lbl") + String(i);
inv += F("ModName\">Module Name</label>");
for(uint8_t j = 0; j < 4; j++) {
inv += F("<input type=\"text\" class=\"text sh\" name=\"inv") + String(i) + F("ModName") + String(j) + F("\" value=\"");
if(NULL != iv)
inv += String(iv->chName[j]);
inv += F("\"/ maxlength=\"") + String(MAX_NAME_LENGTH) + "\">";
}
}
html.replace(F("{INVERTERS}"), String(inv));
// pinout
String pinout;
for(uint8_t i = 0; i < 3; i++) {
pinout += F("<label for=\"") + String(pinArgNames[i]) + "\">" + String(pinNames[i]) + F("</label>");
pinout += F("<select name=\"") + String(pinArgNames[i]) + "\">";
for(uint8_t j = 0; j <= 16; j++) {
pinout += F("<option value=\"") + String(j) + "\"";
switch(i) {
default: if(j == mConfig->pinCs) pinout += F(" selected"); break;
case 1: if(j == mConfig->pinCe) pinout += F(" selected"); break;
case 2: if(j == mConfig->pinIrq) pinout += F(" selected"); break;
}
pinout += ">" + String(wemosPins[j]) + F("</option>");
}
pinout += F("</select>");
}
html.replace(F("{PINOUT}"), String(pinout));
// nrf24l01+
String rf24;
for(uint8_t i = 0; i <= 3; i++) {
rf24 += F("<option value=\"") + String(i) + "\"";
if(i == mConfig->amplifierPower)
rf24 += F(" selected");
rf24 += ">" + String(rf24AmpPowerNames[i]) + F("</option>");
}
html.replace(F("{RF24}"), String(rf24));
html.replace(F("{INV_INTVL}"), String(mConfig->sendInterval));
html.replace(F("{INV_RETRIES}"), String(mConfig->maxRetransPerPyld));
html.replace(F("{SER_INTVL}"), String(mConfig->serialInterval));
html.replace(F("{SER_VAL_CB}"), (mConfig->serialShowIv) ? "checked" : "");
html.replace(F("{SER_DBG_CB}"), (mConfig->serialDebug) ? "checked" : "");
html.replace(F("{NTP_ADDR}"), String(mConfig->ntpAddr));
html.replace(F("{NTP_PORT}"), String(mConfig->ntpPort));
html.replace(F("{MQTT_ADDR}"), String(mConfig->mqtt.broker));
html.replace(F("{MQTT_PORT}"), String(mConfig->mqtt.port));
html.replace(F("{MQTT_USER}"), String(mConfig->mqtt.user));
html.replace(F("{MQTT_PWD}"), String(mConfig->mqtt.pwd));
html.replace(F("{MQTT_TOPIC}"), String(mConfig->mqtt.topic));
html.replace(F("{MQTT_INTVL}"), String("0"));
mWeb->send(200, F("text/html"), html);
}
//-----------------------------------------------------------------------------
void web::showSave(void) {
DPRINTLN(DBG_VERBOSE, F("showSave"));
if(mWeb->args() > 0) {
char buf[20] = {0};
// general
if(mWeb->arg("ssid") != "")
mWeb->arg("ssid").toCharArray(mSysCfg->stationSsid, SSID_LEN);
if(mWeb->arg("pwd") != "{PWD}")
mWeb->arg("pwd").toCharArray(mSysCfg->stationPwd, PWD_LEN);
if(mWeb->arg("device") != "")
mWeb->arg("device").toCharArray(mSysCfg->deviceName, DEVNAME_LEN);
// inverter
Inverter<> *iv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
iv = mMain->mSys->getInverterByPos(i, false);
// address
mWeb->arg("inv" + String(i) + "Addr").toCharArray(buf, 20);
if(strlen(buf) == 0)
memset(buf, 0, 20);
iv->serial.u64 = mMain->Serial2u64(buf);
// active power limit
uint16_t actPwrLimit = mWeb->arg("inv" + String(i) + "ActivePowerLimit").toInt();
if (actPwrLimit != 0xffff && actPwrLimit > 0)
iv->powerLimit[0] = actPwrLimit;
// name
mWeb->arg("inv" + String(i) + "Name").toCharArray(iv->name, MAX_NAME_LENGTH);
// max channel power / name
for(uint8_t j = 0; j < 4; j++) {
iv->chMaxPwr[j] = mWeb->arg("inv" + String(i) + "ModPwr" + String(j)).toInt() & 0xffff;
mWeb->arg("inv" + String(i) + "ModName" + String(j)).toCharArray(iv->chName[j], MAX_NAME_LENGTH);
}
}
if(mWeb->arg("invInterval") != "")
mConfig->sendInterval = mWeb->arg("invInterval").toInt();
if(mWeb->arg("invRetry") != "")
mConfig->sendInterval = mWeb->arg("invRetry").toInt();
// pinout
uint8_t pin;
for(uint8_t i = 0; i < 3; i ++) {
pin = mWeb->arg(String(pinArgNames[i])).toInt();
switch(i) {
default: mConfig->pinCs = pin; break;
case 1: mConfig->pinCe = pin; break;
case 2: mConfig->pinIrq = pin; break;
}
}
// nrf24 amplifier power
mConfig->amplifierPower = mWeb->arg("rf24Power").toInt() & 0x03;
// ntp
if(mWeb->arg("ntpAddr") != "") {
mWeb->arg("ntpAddr").toCharArray(mConfig->ntpAddr, NTP_ADDR_LEN);
mConfig->ntpPort = mWeb->arg("ntpPort").toInt() & 0xffff;
}
// mqtt
if(mWeb->arg("mqttAddr") != "") {
mWeb->arg("mqttAddr").toCharArray(mConfig->mqtt.broker, MQTT_ADDR_LEN);
mWeb->arg("mqttUser").toCharArray(mConfig->mqtt.user, MQTT_USER_LEN);
mWeb->arg("mqttPwd").toCharArray(mConfig->mqtt.pwd, MQTT_PWD_LEN);
mWeb->arg("mqttTopic").toCharArray(mConfig->mqtt.topic, MQTT_TOPIC_LEN);
mConfig->mqtt.port = mWeb->arg("mqttPort").toInt();
}
// serial console
if(mWeb->arg("serIntvl") != "") {
mConfig->serialInterval = mWeb->arg("serIntvl").toInt() & 0xffff;
mConfig->serialDebug = (mWeb->arg("serEn") == "on");
mConfig->serialShowIv = (mWeb->arg("serDbg") == "on");
}
mMain->saveValues();
if(mWeb->arg("reboot") == "on")
showReboot();
else
mWeb->send(200, F("text/html"), F("<!doctype html><html><head><title>Setup saved</title><meta http-equiv=\"refresh\" content=\"0; URL=/setup\"></head><body>"
"<p>saved</p></body></html>"));
}
}
//-----------------------------------------------------------------------------
void web::showStatistics(void) {
DPRINTLN(DBG_VERBOSE, F("web::showStatistics"));
mWeb->send(200, F("text/plain"), mMain->getStatistics());
}
//-----------------------------------------------------------------------------
void web::showVisualization(void) {
DPRINTLN(DBG_VERBOSE, F("web::showVisualization"));
String html = FPSTR(visualization_html);
html.replace(F("{DEVICE}"), mSysCfg->deviceName);
html.replace(F("{VERSION}"), mVersion);
html.replace(F("{TS}"), String(mConfig->sendInterval) + " ");
html.replace(F("{JS_TS}"), String(mConfig->sendInterval * 1000));
mWeb->send(200, F("text/html"), html);
}
//-----------------------------------------------------------------------------
void web::showLiveData(void) {
DPRINTLN(DBG_VERBOSE, F("web::showLiveData"));
mWeb->send(200, F("text/html"), mMain->getLiveData());
}
//-----------------------------------------------------------------------------
void web::showJson(void) {
DPRINTLN(DBG_VERBOSE, F("web::showJson"));
mWeb->send(200, F("application/json"), mMain->getJson());
}
//-----------------------------------------------------------------------------
void web::showWebApi(void) {
DPRINTLN(DBG_VERBOSE, F("web::showWebApi"));
DPRINTLN(DBG_DEBUG, mWeb->arg("plain"));
const size_t capacity = 200; // Use arduinojson.org/assistant to compute the capacity.
DynamicJsonDocument payload(capacity);
// Parse JSON object
deserializeJson(payload, mWeb->arg("plain"));
// ToDo: error handling for payload
if (payload["tx_request"] == TX_REQ_INFO) {
mMain->mSys->InfoCmd = payload["cmd"];
DPRINTLN(DBG_INFO, F("Will make tx-request 0x15 with subcmd ") + String(mMain->mSys->InfoCmd));
}
mWeb->send ( 200, "text/json", "{success:true}" );
}

52
tools/esp8266/web.h

@ -0,0 +1,52 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#ifndef __WEB_H__
#define __WEB_H__
#include "dbg.h"
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include "app.h"
class app;
class web {
public:
web(app *main, sysConfig_t *sysCfg, config_t *config, char version[]);
~web() {}
void setup(void);
void loop(void);
void showIndex(void);
void showCss(void);
void showFavicon(void);
void showNotFound(void);
void showUptime(void);
void showReboot(void);
void showErase();
void showFactoryRst(void);
void showSetup(void);
void showSave(void);
void showStatistics(void);
void showVisualization(void);
void showLiveData(void);
void showJson(void);
void showWebApi(void);
private:
ESP8266WebServer *mWeb;
ESP8266HTTPUpdateServer *mUpdater;
config_t *mConfig;
sysConfig_t *mSysCfg;
char *mVersion;
app *mMain;
};
#endif /*__WEB_H__*/

248
tools/esp8266/wifi.cpp

@ -0,0 +1,248 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#include "wifi.h"
// NTP CONFIG
#define NTP_PACKET_SIZE 48
#define TIMEZONE 1 // Central European time +1
//-----------------------------------------------------------------------------
wifi::wifi(app *main, sysConfig_t *sysCfg, config_t *config) {
mMain = main;
mSysCfg = sysCfg;
mConfig = config;
mDns = new DNSServer();
mUdp = new WiFiUDP();
mWifiStationTimeout = 10;
wifiWasEstablished = false;
mNextTryTs = 0;
mApLastTick = 0;
mApActive = false;
}
//-----------------------------------------------------------------------------
void wifi::setup(uint32_t timeout, bool settingValid) {
mWifiStationTimeout = timeout;
#ifndef AP_ONLY
if(false == mApActive)
mApActive = setupStation(mWifiStationTimeout);
#endif
if(!settingValid) {
DPRINTLN(DBG_WARN, F("your settings are not valid! check [IP]/setup"));
mApActive = true;
mApLastTick = millis();
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
setupAp(WIFI_AP_SSID, WIFI_AP_PWD);
}
else {
DPRINTLN(DBG_INFO, F("\n\n----------------------------------------"));
DPRINTLN(DBG_INFO, F("Welcome to AHOY!"));
DPRINT(DBG_INFO, F("\npoint your browser to http://"));
if(mApActive)
DBGPRINTLN(F("192.168.1.1"));
else
DBGPRINTLN(WiFi.localIP());
DPRINTLN(DBG_INFO, F("to configure your device"));
DPRINTLN(DBG_INFO, F("----------------------------------------\n"));
}
}
//-----------------------------------------------------------------------------
bool wifi::loop(void) {
if(mApActive) {
mDns->processNextRequest();
#ifndef AP_ONLY
if(mMain->checkTicker(&mNextTryTs, (WIFI_AP_ACTIVE_TIME * 1000))) {
mApActive = setupStation(mWifiStationTimeout);
if(mApActive) {
if(strlen(WIFI_AP_PWD) < 8)
DPRINTLN(DBG_ERROR, F("password must be at least 8 characters long"));
mApLastTick = millis();
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
setupAp(WIFI_AP_SSID, WIFI_AP_PWD);
}
}
else {
if(millis() - mApLastTick > 10000) {
uint8_t cnt = WiFi.softAPgetStationNum();
if(cnt > 0) {
DPRINTLN(DBG_INFO, String(cnt) + F(" client connected, resetting AP timeout"));
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
}
mApLastTick = millis();
DPRINTLN(DBG_INFO, F("AP will be closed in ") + String((mNextTryTs - mApLastTick) / 1000) + F(" seconds"));
}
}
#endif
}
if((WiFi.status() != WL_CONNECTED) && wifiWasEstablished) {
if(!mApActive) {
DPRINTLN(DBG_INFO, "[WiFi]: Connection Lost");
mApActive = setupStation(mWifiStationTimeout);
}
}
return mApActive;
}
//-----------------------------------------------------------------------------
void wifi::setupAp(const char *ssid, const char *pwd) {
DPRINTLN(DBG_VERBOSE, F("app::setupAp"));
IPAddress apIp(192, 168, 1, 1);
DPRINTLN(DBG_INFO, F("\n---------\nAP MODE\nSSID: ")
+ String(ssid) + F("\nPWD: ")
+ String(pwd) + F("\nActive for: ")
+ String(WIFI_AP_ACTIVE_TIME) + F(" seconds")
+ F("\n---------\n"));
DPRINTLN(DBG_DEBUG, String(mNextTryTs));
WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
WiFi.softAP(ssid, pwd);
mDns->start(53, "*", apIp);
}
//-----------------------------------------------------------------------------
bool wifi::setupStation(uint32_t timeout) {
DPRINTLN(DBG_VERBOSE, F("app::setupStation"));
int32_t cnt;
bool startAp = false;
if(timeout >= 3)
cnt = (timeout - 3) / 2 * 10;
else {
timeout = 1;
cnt = 1;
}
WiFi.mode(WIFI_STA);
WiFi.begin(mSysCfg->stationSsid, mSysCfg->stationPwd);
if(String(mSysCfg->deviceName) != "")
WiFi.hostname(mSysCfg->deviceName);
delay(2000);
DPRINTLN(DBG_INFO, F("connect to network '") + String(mSysCfg->stationSsid) + F("' ..."));
while (WiFi.status() != WL_CONNECTED) {
delay(100);
if(cnt % 40 == 0)
Serial.println(".");
else
Serial.print(".");
if(timeout > 0) { // limit == 0 -> no limit
if(--cnt <= 0) {
if(WiFi.status() != WL_CONNECTED) {
startAp = true;
WiFi.disconnect();
}
delay(100);
break;
}
}
}
Serial.println(".");
if(false == startAp)
wifiWasEstablished = true;
delay(1000);
return startAp;
}
//-----------------------------------------------------------------------------
bool wifi::getApActive(void) {
return mApActive;
}
//-----------------------------------------------------------------------------
time_t wifi::getNtpTime(void) {
//DPRINTLN(DBG_VERBOSE, F("wifi::getNtpTime"));
time_t date = 0;
IPAddress timeServer;
uint8_t buf[NTP_PACKET_SIZE];
uint8_t retry = 0;
WiFi.hostByName(mConfig->ntpAddr, timeServer);
mUdp->begin(mConfig->ntpPort);
sendNTPpacket(timeServer);
while(retry++ < 5) {
int wait = 150;
while(--wait) {
if(NTP_PACKET_SIZE <= mUdp->parsePacket()) {
uint64_t secsSince1900;
mUdp->read(buf, NTP_PACKET_SIZE);
secsSince1900 = (buf[40] << 24);
secsSince1900 |= (buf[41] << 16);
secsSince1900 |= (buf[42] << 8);
secsSince1900 |= (buf[43] );
date = secsSince1900 - 2208988800UL; // UTC time
date += (TIMEZONE + offsetDayLightSaving(date)) * 3600;
break;
}
else
delay(10);
}
}
return date;
}
//-----------------------------------------------------------------------------
void wifi::sendNTPpacket(IPAddress& address) {
//DPRINTLN(DBG_VERBOSE, F("wifi::sendNTPpacket"));
uint8_t buf[NTP_PACKET_SIZE] = {0};
buf[0] = B11100011; // LI, Version, Mode
buf[1] = 0; // Stratum
buf[2] = 6; // Max Interval between messages in seconds
buf[3] = 0xEC; // Clock Precision
// bytes 4 - 11 are for Root Delay and Dispersion and were set to 0 by memset
buf[12] = 49; // four-byte reference ID identifying
buf[13] = 0x4E;
buf[14] = 49;
buf[15] = 52;
mUdp->beginPacket(address, 123); // NTP request, port 123
mUdp->write(buf, NTP_PACKET_SIZE);
mUdp->endPacket();
}
//-----------------------------------------------------------------------------
// calculates the daylight saving time for middle Europe. Input: Unixtime in UTC
// from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536
time_t wifi::offsetDayLightSaving (uint32_t local_t) {
//DPRINTLN(DBG_VERBOSE, F("wifi::offsetDayLightSaving"));
int m = month (local_t);
if(m < 3 || m > 10) return 0; // no DSL in Jan, Feb, Nov, Dez
if(m > 3 && m < 10) return 1; // DSL in Apr, May, Jun, Jul, Aug, Sep
int y = year (local_t);
int h = hour (local_t);
int hToday = (h + 24 * day(local_t));
if((m == 3 && hToday >= (1 + TIMEZONE + 24 * (31 - (5 * y /4 + 4) % 7)))
|| (m == 10 && hToday < (1 + TIMEZONE + 24 * (31 - (5 * y /4 + 1) % 7))) )
return 1;
else
return 0;
}

55
tools/esp8266/wifi.h

@ -0,0 +1,55 @@
//-----------------------------------------------------------------------------
// 2022 Ahoy, https://www.mikrocontroller.net/topic/525778
// Creative Commons - http://creativecommons.org/licenses/by-nc-sa/3.0/de/
//-----------------------------------------------------------------------------
#ifndef __WIFI_H__
#define __WIFI_H__
#include "dbg.h"
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
// NTP
#include <WiFiUdp.h>
#include <TimeLib.h>
#include <DNSServer.h>
#include "defines.h"
#include "app.h"
class app;
class wifi {
public:
wifi(app *main, sysConfig_t *sysCfg, config_t *config);
~wifi() {}
void setup(uint32_t timeout, bool settingValid);
bool loop(void);
void setupAp(const char *ssid, const char *pwd);
bool setupStation(uint32_t timeout);
bool getApActive(void);
time_t getNtpTime(void);
private:
void sendNTPpacket(IPAddress& address);
time_t offsetDayLightSaving (uint32_t local_t);
config_t *mConfig;
sysConfig_t *mSysCfg;
app *mMain;
DNSServer *mDns;
WiFiUDP *mUdp; // for time server
uint32_t mWifiStationTimeout;
uint32_t mNextTryTs;
uint32_t mApLastTick;
bool mApActive;
bool wifiWasEstablished;
};
#endif /*__WIFI_H__*/
Loading…
Cancel
Save