Browse Source

* converted to "poor-man-ticker" using millis() for uptime, send and mqtt

* added inverter overview
* added send count to statistics
pull/22/head
lumapu 3 years ago
parent
commit
2abd388726
  1. 66
      tools/esp8266/app.cpp
  2. 11
      tools/esp8266/app.h
  3. 2
      tools/esp8266/defines.h
  4. 2
      tools/esp8266/hmRadio.h
  5. 2
      tools/esp8266/html/h/style_css.h
  6. 32
      tools/esp8266/html/style.css
  7. 18
      tools/esp8266/main.cpp
  8. 20
      tools/esp8266/main.h

66
tools/esp8266/app.cpp

@ -7,14 +7,13 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
app::app() : Main() { app::app() : Main() {
mSendTicker = new Ticker(); mSendTicker = 0xffffffff;
mFlagSend = false; mSendInterval = 0;
mMqttTicker = 0xffffffff;
mMqttInterval = 0;
mShowRebootRequest = false; mShowRebootRequest = false;
mMqttTicker = NULL;
mMqttEvt = false;
memset(mCmds, 0, sizeof(uint32_t)*DBG_CMD_LIST_LEN); memset(mCmds, 0, sizeof(uint32_t)*DBG_CMD_LIST_LEN);
//memset(mChannelStat, 0, sizeof(uint32_t) * 4); //memset(mChannelStat, 0, sizeof(uint32_t) * 4);
@ -43,7 +42,6 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) {
mWeb->on("/mqttstate", std::bind(&app::showMqtt, this)); mWeb->on("/mqttstate", std::bind(&app::showMqtt, this));
if(mSettingsValid) { if(mSettingsValid) {
uint16_t interval;
uint64_t invSerial; uint64_t invSerial;
char invName[MAX_NAME_LENGTH + 1] = {0}; char invName[MAX_NAME_LENGTH + 1] = {0};
uint8_t invType; uint8_t invType;
@ -59,10 +57,10 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) {
} }
} }
mEep->read(ADDR_INV_INTERVAL, &interval); mEep->read(ADDR_INV_INTERVAL, &mSendInterval);
if(interval < 1000) if(mSendInterval < 1000)
interval = 1000; mSendInterval = 1000;
mSendTicker->attach_ms(interval, std::bind(&app::sendTicker, this)); mSendTicker = 0;
// pinout // pinout
@ -84,17 +82,16 @@ void app::setup(const char *ssid, const char *pwd, uint32_t timeout) {
mEep->read(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN); mEep->read(ADDR_MQTT_USER, mqttUser, MQTT_USER_LEN);
mEep->read(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN); mEep->read(ADDR_MQTT_PWD, mqttPwd, MQTT_PWD_LEN);
mEep->read(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN); mEep->read(ADDR_MQTT_TOPIC, mqttTopic, MQTT_TOPIC_LEN);
mEep->read(ADDR_MQTT_INTERVAL, &interval); mEep->read(ADDR_MQTT_INTERVAL, &mMqttInterval);
mEep->read(ADDR_MQTT_PORT, &mqttPort); mEep->read(ADDR_MQTT_PORT, &mqttPort);
char addr[16] = {0}; char addr[16] = {0};
sprintf(addr, "%d.%d.%d.%d", mqttAddr[0], mqttAddr[1], mqttAddr[2], mqttAddr[3]); sprintf(addr, "%d.%d.%d.%d", mqttAddr[0], mqttAddr[1], mqttAddr[2], mqttAddr[3]);
if(interval < 1000) if(mMqttInterval < 1000)
interval = 1000; mMqttInterval = 1000;
mMqtt.setup(addr, mqttTopic, mqttUser, mqttPwd, mqttPort); mMqtt.setup(addr, mqttTopic, mqttUser, mqttPwd, mqttPort);
mMqttTicker = new Ticker(); mMqttTicker = 0;
mMqttTicker->attach_ms(interval, std::bind(&app::mqttTicker, this));
mMqtt.sendMsg("version", mVersion); mMqtt.sendMsg("version", mVersion);
} }
@ -157,15 +154,13 @@ void app::loop(void) {
mSys->BufCtrl.popBack(); mSys->BufCtrl.popBack();
} }
if(mFlagSend) { if(checkTicker(&mSendTicker, &mSendInterval)) {
mFlagSend = false;
Inverter<> *inv; Inverter<> *inv;
for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) { for(uint8_t i = 0; i < MAX_NUM_INVERTERS; i ++) {
inv = mSys->getInverterByPos(i); inv = mSys->getInverterByPos(i);
if(NULL != inv) { if(NULL != inv) {
mSys->Radio.sendTimePacket(inv->radioId.u64, mTimestamp); mSys->Radio.sendTimePacket(inv->radioId.u64, mTimestamp);
yield(); yield();
//delay(100);
} }
} }
} }
@ -173,8 +168,7 @@ void app::loop(void) {
// mqtt // mqtt
mMqtt.loop(); mMqtt.loop();
if(mMqttEvt) { if(checkTicker(&mMqttTicker, &mMqttInterval)) {
mMqttEvt = false;
mMqtt.isConnected(true); mMqtt.isConnected(true);
char topic[30], val[10]; char topic[30], val[10];
for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { for(uint8_t id = 0; id < mSys->getNumInverters(); id++) {
@ -185,7 +179,6 @@ void app::loop(void) {
snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, fields[iv->assign[i].fieldId]); snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, fields[iv->assign[i].fieldId]);
snprintf(val, 10, "%.3f", iv->getValue(i)); snprintf(val, 10, "%.3f", iv->getValue(i));
mMqtt.sendMsg(topic, val); mMqtt.sendMsg(topic, val);
//delay(20);
yield(); yield();
} }
} }
@ -193,7 +186,6 @@ void app::loop(void) {
} }
// Serial debug // Serial debug
//char topic[30], val[10];
for(uint8_t id = 0; id < mSys->getNumInverters(); id++) { for(uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id); Inverter<> *iv = mSys->getInverterByPos(id);
if(NULL != iv) { if(NULL != iv) {
@ -217,18 +209,6 @@ void app::handleIntr(void) {
} }
//-----------------------------------------------------------------------------
void app::sendTicker(void) {
mFlagSend = true;
}
//-----------------------------------------------------------------------------
void app::mqttTicker(void) {
mMqttEvt = true;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showIndex(void) { void app::showIndex(void) {
String html = FPSTR(index_html); String html = FPSTR(index_html);
@ -371,6 +351,8 @@ void app::showStatistics(void) {
} }
content += String("other: ") + String(mCmds[DBG_CMD_LIST_LEN]) + String("\n\n"); content += String("other: ") + String(mCmds[DBG_CMD_LIST_LEN]) + String("\n\n");
content += "Send Cnt: " + String(mSys->Radio.mSendCnt) + String("\n\n");
/*content += "\nCHANNELs:\n"; /*content += "\nCHANNELs:\n";
content += String("23: ") + String(mChannelStat[0]) + String("\n"); content += String("23: ") + String(mChannelStat[0]) + String("\n");
content += String("40: ") + String(mChannelStat[1]) + String("\n"); content += String("40: ") + String(mChannelStat[1]) + String("\n");
@ -410,7 +392,21 @@ void app::showLiveData(void) {
case INV_TYPE_HM1200: modNum = 4; break; case INV_TYPE_HM1200: modNum = 4; break;
} }
modHtml += "<div class=\"ch-group\"><h3>" + String(iv->name) + "</h3>"; modHtml += "<div class=\"iv\">";
modHtml += "<div class=\"ch-iv\"><span class=\"head\">" + String(iv->name) + "</span>";
uint8_t list[8] = {FLD_UAC, FLD_IAC, FLD_PAC, FLD_F, FLD_PCT, FLD_T, FLD_YT, FLD_YD};
for(uint8_t fld = 0; fld < 8; fld++) {
pos = (iv->getPosByChFld(CH0, list[fld]));
if(0xff != pos) {
modHtml += "<div class=\"subgrp\">";
modHtml += "<span class=\"value\">" + String(iv->getValue(pos));
modHtml += "<span class=\"unit\">" + String(iv->getUnit(pos)) + "</span></span>";
modHtml += "<span class=\"info\">" + String(iv->getFieldName(pos)) + "</span>";
modHtml += "</div>";
}
}
modHtml += "</div>";
for(uint8_t ch = 1; ch <= modNum; ch ++) { for(uint8_t ch = 1; ch <= modNum; ch ++) {
modHtml += "<div class=\"ch\"><span class=\"head\">CHANNEL " + String(ch) + "</span>"; modHtml += "<div class=\"ch\"><span class=\"head\">CHANNEL " + String(ch) + "</span>";

11
tools/esp8266/app.h

@ -40,9 +40,6 @@ class app : public Main {
} }
private: private:
void sendTicker(void);
void mqttTicker(void);
void showIndex(void); void showIndex(void);
void showSetup(void); void showSetup(void);
void showSave(void); void showSave(void);
@ -74,8 +71,8 @@ class app : public Main {
HmSystemType *mSys; HmSystemType *mSys;
Ticker *mSendTicker; uint32_t mSendTicker;
bool mFlagSend; uint16_t mSendInterval;
uint32_t mCmds[DBG_CMD_LIST_LEN+1]; uint32_t mCmds[DBG_CMD_LIST_LEN+1];
//uint32_t mChannelStat[4]; //uint32_t mChannelStat[4];
@ -83,8 +80,8 @@ class app : public Main {
// mqtt // mqtt
mqtt mMqtt; mqtt mMqtt;
Ticker *mMqttTicker; uint32_t mMqttTicker;
bool mMqttEvt; uint16_t mMqttInterval;
}; };
#endif /*__APP_H__*/ #endif /*__APP_H__*/

2
tools/esp8266/defines.h

@ -25,7 +25,7 @@
//------------------------------------- //-------------------------------------
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 3 #define VERSION_MINOR 3
#define VERSION_PATCH 2 #define VERSION_PATCH 3
//------------------------------------- //-------------------------------------

2
tools/esp8266/hmRadio.h

@ -206,6 +206,7 @@ class HmRadio {
uint8_t pinIrq; uint8_t pinIrq;
uint8_t AmplifierPower; uint8_t AmplifierPower;
uint32_t mSendCnt;
private: private:
void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len) { void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len) {
@ -265,7 +266,6 @@ class HmRadio {
RF24 mNrf24; RF24 mNrf24;
uint8_t mSendChannel; uint8_t mSendChannel;
BUFFER *mBufCtrl; BUFFER *mBufCtrl;
uint32_t mSendCnt;
uint8_t mSendBuf[MAX_RF_PAYLOAD_SIZE]; uint8_t mSendBuf[MAX_RF_PAYLOAD_SIZE];
}; };

2
tools/esp8266/html/h/style_css.h

@ -1,4 +1,4 @@
#ifndef __STYLE_H__ #ifndef __STYLE_H__
#define __STYLE_H__ #define __STYLE_H__
const char style_css[] PROGMEM = "h1 {margin:0;padding:20pt;font-size:22pt;color:#fff;background-color:#006ec0;display:block;text-transform:uppercase;}html, body {font-family:Arial;margin:0;padding:0;}p {text-align:justify;font-size:13pt;}.des {margin-top:35px;font-size:14pt;color:#006ec0;}.subdes {font-size:13pt;color:#006ec0;margin-left:7px;}.fw {width:60px;display:block;float:left;}.color {width:50px;height:50px;border:1px solid #ccc;}.range {width:300px;}a:link, a:visited {text-decoration:none;font-size:13pt;color:#006ec0;}a:hover, a:focus {color:#f00;}a.erase {background-color:#006ec0;color:#fff;padding:7px;display:inline-block;margin-top:30px;float:right;}#content {padding:15px 15px 60px 15px;}#footer {position:fixed;bottom:0px;height:45px;background-color:#006ec0;width:100%;}#footer p {color:#fff;padding-left:20px;padding-right:20px;font-size:10pt !important;}#footer a {color:#fff;}div.content {background-color:#fff;padding-bottom:65px;overflow:hidden;}input, select {padding:7px;font-size:13pt;}input.text, select {width:70%;box-sizing:border-box;margin-bottom:10px;border:1px solid #ccc;}input.btn {background-color:#006ec0;color:#fff;border:0px;float:right;margin:10px 0 30px;text-transform:uppercase;}input.cb {margin-bottom:20px;}label {width:20%;display:inline-block;font-size:12pt;padding-right:10px;margin-left:10px;}.left {float:left;}.right {float:right;}div.ch-group {display:inline-block;}div.ch {width:250px;height:410px;background-color:#006ec0;display:inline-block;margin-right:20px;margin-bottom:20px;}div.ch .value, div.ch .info, div.ch .head {color:#fff;display:block;width:100%;text-align:center;}div.ch .unit {font-size:19px;margin-left:10px;}div.ch .value {margin-top:20px;font-size:30px;}div.ch .info {margin-top:3px;font-size:10px;}div.ch .head {background-color:#003c80;padding:10px 0 10px 0;}"; const char style_css[] PROGMEM = "h1 {margin:0;padding:20pt;font-size:22pt;color:#fff;background-color:#006ec0;display:block;text-transform:uppercase;}html, body {font-family:Arial;margin:0;padding:0;}p {text-align:justify;font-size:13pt;}.des {margin-top:35px;font-size:14pt;color:#006ec0;}.subdes {font-size:13pt;color:#006ec0;margin-left:7px;}.fw {width:60px;display:block;float:left;}.color {width:50px;height:50px;border:1px solid #ccc;}.range {width:300px;}a:link, a:visited {text-decoration:none;font-size:13pt;color:#006ec0;}a:hover, a:focus {color:#f00;}a.erase {background-color:#006ec0;color:#fff;padding:7px;display:inline-block;margin-top:30px;float:right;}#content {padding:15px 15px 60px 15px;}#footer {position:fixed;bottom:0px;height:45px;background-color:#006ec0;width:100%;}#footer p {color:#fff;padding-left:20px;padding-right:20px;font-size:10pt !important;}#footer a {color:#fff;}div.content {background-color:#fff;padding-bottom:65px;overflow:hidden;}input, select {padding:7px;font-size:13pt;}input.text, select {width:70%;box-sizing:border-box;margin-bottom:10px;border:1px solid #ccc;}input.btn {background-color:#006ec0;color:#fff;border:0px;float:right;margin:10px 0 30px;text-transform:uppercase;}input.cb {margin-bottom:20px;}label {width:20%;display:inline-block;font-size:12pt;padding-right:10px;margin-left:10px;}.left {float:left;}.right {float:right;}div.ch-iv {width:100%;background-color:#32b004;display:inline-block;margin-bottom:20px;padding-bottom:20px;}div.ch {width:250px;height:410px;background-color:#006ec0;display:inline-block;margin-right:20px;margin-bottom:20px;}div.ch .value, div.ch .info, div.ch .head, div.ch-iv .value, div.ch-iv .info, div.ch-iv .head {color:#fff;display:block;width:100%;text-align:center;}.subgrp {float:left;width:250px;}div.ch .unit, div.ch-iv .unit {font-size:19px;margin-left:10px;}div.ch .value, div.ch-iv .value {margin-top:20px;font-size:30px;}div.ch .info, div.ch-iv .info {margin-top:3px;font-size:10px;}div.ch .head {background-color:#003c80;padding:10px 0 10px 0;}div.ch-iv .head {background-color:#1c6800;padding:10px 0 10px 0;}div.iv {max-width:1060px;}div.ch:last-child {margin-right:0px !important;}";
#endif /*__STYLE_H__*/ #endif /*__STYLE_H__*/

32
tools/esp8266/html/style.css

@ -136,8 +136,12 @@ label {
float: right; float: right;
} }
div.ch-group { div.ch-iv {
width: 100%;
background-color: #32b004;
display: inline-block; display: inline-block;
margin-bottom: 20px;
padding-bottom: 20px;
} }
div.ch { div.ch {
@ -148,24 +152,29 @@ div.ch {
margin-right: 20px; margin-right: 20px;
margin-bottom: 20px; margin-bottom: 20px;
} }
div.ch .value, div.ch .info, div.ch .head { div.ch .value, div.ch .info, div.ch .head, div.ch-iv .value, div.ch-iv .info, div.ch-iv .head {
color: #fff; color: #fff;
display: block; display: block;
width: 100%; width: 100%;
text-align: center; text-align: center;
} }
div.ch .unit { .subgrp {
float: left;
width: 250px;
}
div.ch .unit, div.ch-iv .unit {
font-size: 19px; font-size: 19px;
margin-left: 10px; margin-left: 10px;
} }
div.ch .value { div.ch .value, div.ch-iv .value {
margin-top: 20px; margin-top: 20px;
font-size: 30px; font-size: 30px;
} }
div.ch .info { div.ch .info, div.ch-iv .info {
margin-top: 3px; margin-top: 3px;
font-size: 10px; font-size: 10px;
} }
@ -174,3 +183,16 @@ div.ch .head {
background-color: #003c80; background-color: #003c80;
padding: 10px 0 10px 0; padding: 10px 0 10px 0;
} }
div.ch-iv .head {
background-color: #1c6800;
padding: 10px 0 10px 0;
}
div.iv {
max-width: 1060px;
}
div.ch:last-child {
margin-right: 0px !important;
}

18
tools/esp8266/main.cpp

@ -24,9 +24,8 @@ Main::Main(void) {
Serial.begin(115200); Serial.begin(115200);
mUptimeSecs = 0; mUptimeSecs = 0;
mUptimeTicker = new Ticker(); mUptimeTicker = 0xffffffff;
mUptimeTicker->attach(1, std::bind(&Main::uptimeTicker, this)); mUptimeInterval = 1000;
} }
@ -67,6 +66,11 @@ void Main::loop(void) {
if(mApActive) if(mApActive)
mDns->processNextRequest(); mDns->processNextRequest();
mWeb->handleClient(); mWeb->handleClient();
if(checkTicker(&mUptimeTicker, &mUptimeInterval)) {
mUptimeSecs++;
mTimestamp++;
}
} }
@ -271,14 +275,6 @@ void Main::showReboot(void) {
} }
//-----------------------------------------------------------------------------
void Main::uptimeTicker(void) {
mUptimeSecs++;
mTimestamp++;
}
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
time_t Main::getNtpTime(void) { time_t Main::getNtpTime(void) {
time_t date = 0; time_t date = 0;

20
tools/esp8266/main.h

@ -6,7 +6,6 @@
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <DNSServer.h> #include <DNSServer.h>
#include <ESP8266WebServer.h> #include <ESP8266WebServer.h>
#include <Ticker.h>
#include <ESP8266HTTPUpdateServer.h> #include <ESP8266HTTPUpdateServer.h>
@ -64,6 +63,20 @@ class Main {
} while(addr < ADDR_START_SETTINGS); } while(addr < ADDR_START_SETTINGS);
} }
inline bool checkTicker(uint32_t *ticker, uint16_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;
}
char mStationSsid[SSID_LEN]; char mStationSsid[SSID_LEN];
char mStationPwd[PWD_LEN]; char mStationPwd[PWD_LEN];
bool mWifiSettingsValid; bool mWifiSettingsValid;
@ -87,14 +100,13 @@ class Main {
void showUptime(void); void showUptime(void);
void showTime(void); void showTime(void);
void showCss(void); void showCss(void);
void uptimeTicker(void);
time_t getNtpTime(void); time_t getNtpTime(void);
void sendNTPpacket(IPAddress& address); void sendNTPpacket(IPAddress& address);
time_t offsetDayLightSaving (uint32_t local_t); time_t offsetDayLightSaving (uint32_t local_t);
Ticker *mUptimeTicker; uint32_t mUptimeTicker;
uint16_t mUptimeInterval;
uint32_t mUptimeSecs; uint32_t mUptimeSecs;
DNSServer *mDns; DNSServer *mDns;

Loading…
Cancel
Save