Browse Source

* PR #76 updated debug messages: now 5 different levels are available

* fixed CRC loop issue
pull/80/head
lumapu 3 years ago
parent
commit
0347a3df44
  1. 91
      tools/esp8266/app.cpp
  2. 5
      tools/esp8266/app.h
  3. 132
      tools/esp8266/debug.h
  4. 4
      tools/esp8266/defines.h
  5. 44
      tools/esp8266/hmInverter.h
  6. 46
      tools/esp8266/hmRadio.h
  7. 27
      tools/esp8266/hmSystem.h
  8. 58
      tools/esp8266/main.cpp
  9. 20
      tools/esp8266/main.h
  10. 14
      tools/esp8266/mqtt.h

91
tools/esp8266/app.cpp

@ -12,7 +12,7 @@
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
app::app() : Main() { app::app() : Main() {
DBGAPP(F("app::app():Main")); DPRINTLN(DBG_VERBOSE, F("app::app():Main"));
mSendTicker = 0xffff; mSendTicker = 0xffff;
mSendInterval = 0; mSendInterval = 0;
mMqttTicker = 0xffff; mMqttTicker = 0xffff;
@ -49,7 +49,7 @@ app::~app(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::setup(uint32_t timeout) { void app::setup(uint32_t timeout) {
DBGAPP(F("app::setup")); DPRINTLN(DBG_VERBOSE, F("app::setup"));
Main::setup(timeout); Main::setup(timeout);
mWeb->on("/", std::bind(&app::showIndex, this)); mWeb->on("/", std::bind(&app::showIndex, this));
@ -78,7 +78,7 @@ void app::setup(uint32_t timeout) {
if(0ULL != invSerial) { if(0ULL != invSerial) {
iv = mSys->addInverter(name, invSerial, modPwr); iv = mSys->addInverter(name, invSerial, modPwr);
if(NULL != iv) { if(NULL != iv) {
DPRINTLN(F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX)); DPRINTLN(DBG_INFO, F("add inverter: ") + String(name) + ", SN: " + String(invSerial, HEX));
for(uint8_t j = 0; j < 4; j++) { 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); mEep->read(ADDR_INV_CH_NAME + (i * 4 * MAX_NAME_LENGTH) + j * MAX_NAME_LENGTH, name, MAX_NAME_LENGTH);
@ -167,11 +167,13 @@ void app::setup(uint32_t timeout) {
} }
} }
else { else {
DPRINTLN(F("Settings not valid, erasing ...")); 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(); eraseSettings();
saveValues(false); saveValues(false);
delay(100); delay(100);
DPRINTLN(F("... restarting ...")); DPRINTLN(DBG_INFO, F("... restarting ..."));
delay(100); delay(100);
ESP.restart(); ESP.restart();
} }
@ -179,21 +181,21 @@ void app::setup(uint32_t timeout) {
mSys->setup(); mSys->setup();
if(!mWifiSettingsValid) if(!mWifiSettingsValid)
DPRINTLN(F("Warn: your settings are not valid! check [IP]/setup")); DPRINTLN(DBG_WARN, F("your settings are not valid! check [IP]/setup"));
else { else {
DPRINTLN(F("\n\n----------------------------------------")); DPRINTLN(DBG_INFO, F("\n\n----------------------------------------"));
DPRINTLN(F("Welcome to AHOY!")); DPRINTLN(DBG_INFO, F("Welcome to AHOY!"));
DPRINT(F("\npoint your browser to http://")); DPRINT(DBG_INFO, F("\npoint your browser to http://"));
DPRINTLN(WiFi.localIP()); DBGPRINTLN(WiFi.localIP());
DPRINTLN(F("to configure your device")); DPRINTLN(DBG_INFO, F("to configure your device"));
DPRINTLN(F("----------------------------------------\n")); DPRINTLN(DBG_INFO, F("----------------------------------------\n"));
} }
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::loop(void) { void app::loop(void) {
//DBGAPP(F("app::loop")) DPRINTLN(DBG_VERBOSE, F("app::loop"));
Main::loop(); Main::loop();
mSys->Radio.loop(); mSys->Radio.loop();
@ -201,11 +203,10 @@ void app::loop(void) {
yield(); yield();
if(checkTicker(&mRxTicker, 5)) { if(checkTicker(&mRxTicker, 5)) {
#ifdef DEBUG_APP DPRINTLN(DBG_DEBUG, F("app_loops =") + String(app_loops));
//DPRINTLN(F("app_loops =") + String(app_loops)); app_loops=0;
//app_loops=0; DPRINT(DBG_DEBUG, F("a"));
//DPRINT(F("a"));
#endif
bool rxRdy = mSys->Radio.switchRxCh(); bool rxRdy = mSys->Radio.switchRxCh();
if(!mSys->BufCtrl.empty()) { if(!mSys->BufCtrl.empty()) {
@ -215,7 +216,7 @@ void app::loop(void) {
if(mSys->Radio.checkPaketCrc(p->packet, &len, p->rxCh)) { if(mSys->Radio.checkPaketCrc(p->packet, &len, p->rxCh)) {
// process buffer only on first occurrence // process buffer only on first occurrence
if(mSerialDebug) { if(mSerialDebug) {
DPRINT("Received " + String(len) + " bytes channel " + String(p->rxCh) + ": "); DPRINT(DBG_DEBUG, "Received " + String(len) + " bytes channel " + String(p->rxCh) + ": ");
mSys->Radio.dumpBuf(NULL, p->packet, len); mSys->Radio.dumpBuf(NULL, p->packet, len);
} }
mFrameCnt++; mFrameCnt++;
@ -287,7 +288,7 @@ void app::loop(void) {
if(0.0f != iv->getValue(i)) { if(0.0f != iv->getValue(i)) {
snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i)); snprintf(topic, 30, "%s/ch%d/%s", iv->name, iv->assign[i].ch, iv->getFieldName(i));
snprintf(val, 10, "%.3f %s", iv->getValue(i), iv->getUnit(i)); snprintf(val, 10, "%.3f %s", iv->getValue(i), iv->getUnit(i));
DPRINTLN(String(topic) + ": " + String(val)); DPRINTLN(DBG_INFO, String(topic) + ": " + String(val));
} }
yield(); yield();
} }
@ -302,11 +303,11 @@ void app::loop(void) {
if(0 != mTimestamp) { if(0 != mTimestamp) {
if(mSerialDebug) if(mSerialDebug)
DPRINTLN(F("Free heap: 0x") + String(ESP.getFreeHeap(), HEX)); DPRINTLN(DBG_DEBUG, F("Free heap: 0x") + String(ESP.getFreeHeap(), HEX));
if(!mSys->BufCtrl.empty()) { if(!mSys->BufCtrl.empty()) {
if(mSerialDebug) if(mSerialDebug)
DPRINTLN(F("recbuf not empty! #") + String(mSys->BufCtrl.getFill())); DPRINTLN(DBG_DEBUG, F("recbuf not empty! #") + String(mSys->BufCtrl.getFill()));
} }
int8_t maxLoop = MAX_NUM_INVERTERS; int8_t maxLoop = MAX_NUM_INVERTERS;
@ -325,8 +326,8 @@ void app::loop(void) {
if(!mPayload[iv->id].complete) { if(!mPayload[iv->id].complete) {
mRxFailed++; mRxFailed++;
if(mSerialDebug) { if(mSerialDebug) {
DPRINT(F("Inverter #") + String(iv->id) + " "); DPRINT(DBG_INFO, F("Inverter #") + String(iv->id) + " ");
DPRINTLN(F("no Payload received! (retransmits: ") + String(mPayload[iv->id].retransmits) + ")"); DPRINTLN(DBG_INFO, F("no Payload received! (retransmits: ") + String(mPayload[iv->id].retransmits) + ")");
} }
} }
@ -340,13 +341,13 @@ void app::loop(void) {
yield(); yield();
if(mSerialDebug) if(mSerialDebug)
DPRINTLN(F("Requesting Inverter SN ") + String(iv->serial.u64, HEX)); DPRINTLN(DBG_INFO, F("Requesting Inverter SN ") + String(iv->serial.u64, HEX));
mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts); mSys->Radio.sendTimePacket(iv->radioId.u64, mPayload[iv->id].ts);
mRxTicker = 0; mRxTicker = 0;
} }
} }
else if(mSerialDebug) else if(mSerialDebug)
DPRINTLN(F("time not set, can't request inverter!")); DPRINTLN(DBG_WARN, F("time not set, can't request inverter!"));
yield(); yield();
} }
} }
@ -355,14 +356,14 @@ void app::loop(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::handleIntr(void) { void app::handleIntr(void) {
DBGAPP(F("app::handleIntr")); DPRINTLN(DBG_VERBOSE, F("app::handleIntr"));
mSys->Radio.handleIntr(); mSys->Radio.handleIntr();
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool app::buildPayload(uint8_t id) { bool app::buildPayload(uint8_t id) {
//DBGAPP(F("app::buildPayload")); DPRINTLN(DBG_VERBOSE, F("app::buildPayload"));
uint16_t crc = 0xffff, crcRcv; uint16_t crc = 0xffff, crcRcv;
if(mPayload[id].maxPackId > MAX_PAYLOAD_ENTRIES) if(mPayload[id].maxPackId > MAX_PAYLOAD_ENTRIES)
mPayload[id].maxPackId = MAX_PAYLOAD_ENTRIES; mPayload[id].maxPackId = MAX_PAYLOAD_ENTRIES;
@ -387,7 +388,7 @@ bool app::buildPayload(uint8_t id) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::processPayload(bool retransmit) { void app::processPayload(bool retransmit) {
//DBGAPP(F("app::processPayload")); DPRINTLN(DBG_VERBOSE, F("app::processPayload"));
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) {
@ -401,7 +402,7 @@ void app::processPayload(bool retransmit) {
for(uint8_t i = 0; i < (mPayload[iv->id].maxPackId-1); i ++) { for(uint8_t i = 0; i < (mPayload[iv->id].maxPackId-1); i ++) {
if(mPayload[iv->id].len[i] == 0) { if(mPayload[iv->id].len[i] == 0) {
if(mSerialDebug) if(mSerialDebug)
DPRINTLN(F("Error while retrieving data: Frame ") + String(i+1) + F(" missing: Request Retransmit")); DPRINTLN(DBG_ERROR, F("while retrieving data: Frame ") + String(i+1) + F(" missing: Request Retransmit"));
mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x15, (0x81+i), true); mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x15, (0x81+i), true);
break; // only retransmit one frame per loop break; // only retransmit one frame per loop
} }
@ -410,7 +411,7 @@ void app::processPayload(bool retransmit) {
} }
else { else {
if(mSerialDebug) if(mSerialDebug)
DPRINTLN(F("Error while retrieving data: last frame missing: Request Retransmit")); DPRINTLN(DBG_ERROR, F("while retrieving data: last frame missing: Request Retransmit"));
if(0x00 != mLastPacketId) if(0x00 != mLastPacketId)
mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x15, mLastPacketId, true); mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x15, mLastPacketId, true);
else else
@ -433,7 +434,7 @@ void app::processPayload(bool retransmit) {
} }
offs-=2; offs-=2;
if(mSerialDebug) { if(mSerialDebug) {
DPRINT(F("Payload (") + String(offs) + "): "); DPRINT(DBG_INFO, F("Payload (") + String(offs) + "): ");
mSys->Radio.dumpBuf(NULL, payload, offs); mSys->Radio.dumpBuf(NULL, payload, offs);
} }
mRxSuccess++; mRxSuccess++;
@ -453,7 +454,7 @@ void app::processPayload(bool retransmit) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showIndex(void) { void app::showIndex(void) {
//DBGAPP(F("app::showIndex")); DPRINTLN(DBG_VERBOSE, F("app::showIndex"));
String html = FPSTR(index_html); String html = FPSTR(index_html);
html.replace(F("{DEVICE}"), mDeviceName); html.replace(F("{DEVICE}"), mDeviceName);
html.replace(F("{VERSION}"), mVersion); html.replace(F("{VERSION}"), mVersion);
@ -465,7 +466,7 @@ void app::showIndex(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showSetup(void) { void app::showSetup(void) {
DBGAPP(F("app::showSetup")); DPRINTLN(DBG_VERBOSE, F("app::showSetup"));
// overrides same method in main.cpp // overrides same method in main.cpp
uint16_t interval; uint16_t interval;
@ -585,14 +586,14 @@ void app::showSetup(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showSave(void) { void app::showSave(void) {
DBGAPP(F("app::showSave")); DPRINTLN(DBG_VERBOSE, F("app::showSave"));
saveValues(true); saveValues(true);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showErase() { void app::showErase() {
DBGAPP(F("app::showErase")); DPRINTLN(DBG_VERBOSE, F("app::showErase"));
eraseSettings(); eraseSettings();
showReboot(); showReboot();
} }
@ -600,7 +601,7 @@ void app::showErase() {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showStatistics(void) { void app::showStatistics(void) {
//DBGAPP(F("app::showStatistics")); DPRINTLN(DBG_VERBOSE, F("app::showStatistics"));
String content = F("Receive success: ") + String(mRxSuccess) + "\n"; String content = F("Receive success: ") + String(mRxSuccess) + "\n";
content += F("Receive fail: ") + String(mRxFailed) + "\n"; content += F("Receive fail: ") + String(mRxFailed) + "\n";
content += F("Frames received: ") + String(mFrameCnt) + "\n"; content += F("Frames received: ") + String(mFrameCnt) + "\n";
@ -648,7 +649,7 @@ void app::showStatistics(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showHoymiles(void) { void app::showHoymiles(void) {
DBGAPP(F("app::showHoymiles")); DPRINTLN(DBG_VERBOSE, F("app::showHoymiles"));
String html = FPSTR(hoymiles_html); String html = FPSTR(hoymiles_html);
html.replace(F("{DEVICE}"), mDeviceName); html.replace(F("{DEVICE}"), mDeviceName);
html.replace(F("{VERSION}"), mVersion); html.replace(F("{VERSION}"), mVersion);
@ -660,7 +661,7 @@ void app::showHoymiles(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::showLiveData(void) { void app::showLiveData(void) {
//DBGAPP(F("app::showLiveData")); DPRINTLN(DBG_VERBOSE, F("app::showLiveData"));
String modHtml; String modHtml;
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);
@ -738,7 +739,7 @@ void app::showLiveData(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::saveValues(bool webSend = true) { void app::saveValues(bool webSend = true) {
DBGAPP(F("app::saveValues")); DPRINTLN(DBG_VERBOSE, F("app::saveValues"));
Main::saveValues(false); // general configuration Main::saveValues(false); // general configuration
if(mWeb->args() > 0) { if(mWeb->args() > 0) {
@ -822,8 +823,8 @@ void app::saveValues(bool webSend = true) {
mEep->write(ADDR_SER_ENABLE, (uint8_t)((tmp) ? 0x01 : 0x00)); mEep->write(ADDR_SER_ENABLE, (uint8_t)((tmp) ? 0x01 : 0x00));
mSerialDebug = (mWeb->arg("serDbg") == "on"); mSerialDebug = (mWeb->arg("serDbg") == "on");
mEep->write(ADDR_SER_DEBUG, (uint8_t)((mSerialDebug) ? 0x01 : 0x00)); mEep->write(ADDR_SER_DEBUG, (uint8_t)((mSerialDebug) ? 0x01 : 0x00));
DPRINT("Info: Serial debug is "); DPRINT(DBG_INFO, "Serial debug is ");
if(mSerialDebug) DPRINTLN("on"); else DPRINTLN("off"); if(mSerialDebug) DPRINTLN(DBG_INFO, "on"); else DPRINTLN(DBG_INFO, "off");
mSys->Radio.mSerialDebug = mSerialDebug; mSys->Radio.mSerialDebug = mSerialDebug;
updateCrc(); updateCrc();
@ -847,11 +848,11 @@ void app::saveValues(bool webSend = true) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void app::updateCrc(void) { void app::updateCrc(void) {
DBGAPP(F("app::updateCrc")); DPRINTLN(DBG_VERBOSE, F("app::updateCrc"));
Main::updateCrc(); Main::updateCrc();
uint16_t crc; uint16_t crc;
crc = buildEEpCrc(ADDR_START_SETTINGS, (ADDR_NEXT - ADDR_START_SETTINGS)); crc = buildEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT) - (ADDR_START_SETTINGS)));
//DPRINTLN("new CRC: " + String(crc, HEX)); DPRINTLN(DBG_DEBUG, F("new CRC: ") + String(crc, HEX));
mEep->write(ADDR_SETTINGS_CRC, crc); mEep->write(ADDR_SETTINGS_CRC, crc);
} }

5
tools/esp8266/app.h

@ -16,11 +16,6 @@
#include "hmSystem.h" #include "hmSystem.h"
#include "mqtt.h" #include "mqtt.h"
#ifdef DEBUG_APP
#define DBGAPP(f) (DPRINTLN(f))
#else
#define DBGAPP(f)
#endif
typedef CircularBuffer<packet_t, PACKET_BUFFER_SIZE> BufferType; typedef CircularBuffer<packet_t, PACKET_BUFFER_SIZE> BufferType;
typedef HmRadio<RF24_CE_PIN, RF24_CS_PIN, RF24_IRQ_PIN, BufferType> RadioType; typedef HmRadio<RF24_CE_PIN, RF24_CS_PIN, RF24_IRQ_PIN, BufferType> RadioType;

132
tools/esp8266/debug.h

@ -6,39 +6,115 @@
#ifndef __DEBUG_H__ #ifndef __DEBUG_H__
#define __DEBUG_H__ #define __DEBUG_H__
//-----------------------------------------------------------------------------
// available levels
#define DBG_ERROR 1
#define DBG_WARN 2
#define DBG_INFO 3
#define DBG_DEBUG 4
#define DBG_VERBOSE 5
//-----------------------------------------------------------------------------
// globally used level
#define DEBUG_LEVEL DBG_INFO
#ifdef NDEBUG #ifdef NDEBUG
#define DPRINT(str) #define DBGPRINT(str)
#define DPRINTLN(str) #define DBGPRINTLN(str)
#else
#ifndef DSERIAL
#define DSERIAL Serial
#endif
template <class T>
inline void DBGPRINT(T str) { DSERIAL.print(str); }
template <class T>
inline void DBGPRINTLN(T str) { DBGPRINT(str); DBGPRINT(F("\r\n")); }
inline void DHEX(uint8_t b) {
if( b<0x10 ) DSERIAL.print('0');
DSERIAL.print(b,HEX);
}
inline void DHEX(uint16_t b) {
if( b<0x10 ) DSERIAL.print(F("000"));
else if( b<0x100 ) DSERIAL.print(F("00"));
else if( b<0x1000 ) DSERIAL.print(F("0"));
DSERIAL.print(b,HEX);
}
inline void DHEX(uint32_t b) {
if( b<0x10 ) DSERIAL.print(F("0000000"));
else if( b<0x100 ) DSERIAL.print(F("000000"));
else if( b<0x1000 ) DSERIAL.print(F("00000"));
else if( b<0x10000 ) DSERIAL.print(F("0000"));
else if( b<0x100000 ) DSERIAL.print(F("000"));
else if( b<0x1000000 ) DSERIAL.print(F("00"));
else if( b<0x10000000 ) DSERIAL.print(F("0"));
DSERIAL.print(b,HEX);
}
#endif
#if DEBUG_LEVEL >= DBG_ERROR
#define PERR(str) DBGPRINT(F("E: ")); DBGPRINT(str);
#define PERRLN(str) DBGPRINT(F("E: ")); DBGPRINTLN(str);
#else
#define PERR(str)
#define PERRLN(str)
#endif
#if DEBUG_LEVEL >= DBG_WARN
#define PWARN(str) DBGPRINT(F("W: ")); DBGPRINT(str);
#define PWARNLN(str) DBGPRINT(F("W: ")); DBGPRINTLN(str);
#else
#define PWARN(str)
#define PWARNLN(str)
#endif
#if DEBUG_LEVEL >= DBG_INFO
#define PINFO(str) DBGPRINT(F("I: ")); DBGPRINT(str);
#define PINFOLN(str) DBGPRINT(F("I: ")); DBGPRINTLN(str);
#else #else
#define PINFO(str)
#define PINFOLN(str)
#endif
#ifndef DSERIAL #if DEBUG_LEVEL >= DBG_DEBUG
#define DSERIAL Serial #define PDBG(str) DBGPRINT(F("D: ")); DBGPRINT(str);
#define PDBGLN(str) DBGPRINT(F("D: ")); DBGPRINTLN(str);
#else
#define PDBG(str)
#define PDBGLN(str)
#endif #endif
template <class T> #if DEBUG_LEVEL >= DBG_VERBOSE
inline void DPRINT(T str) { DSERIAL.print(str); } #define PVERB(str) DBGPRINT(F("V: ")); DBGPRINT(str);
template <class T> #define PVERBLN(str) DBGPRINT(F("V: ")); DBGPRINTLN(str);
inline void DPRINTLN(T str) { DPRINT(str); DPRINT(F("\r\n")); } #else
inline void DHEX(uint8_t b) { #define PVERB(str)
if( b<0x10 ) DSERIAL.print('0'); #define PVERBLN(str)
DSERIAL.print(b,HEX);
}
inline void DHEX(uint16_t b) {
if( b<0x10 ) DSERIAL.print(F("000"));
else if( b<0x100 ) DSERIAL.print(F("00"));
else if( b<0x1000 ) DSERIAL.print(F("0"));
DSERIAL.print(b,HEX);
}
inline void DHEX(uint32_t b) {
if( b<0x10 ) DSERIAL.print(F("0000000"));
else if( b<0x100 ) DSERIAL.print(F("000000"));
else if( b<0x1000 ) DSERIAL.print(F("00000"));
else if( b<0x10000 ) DSERIAL.print(F("0000"));
else if( b<0x100000 ) DSERIAL.print(F("000"));
else if( b<0x1000000 ) DSERIAL.print(F("00"));
else if( b<0x10000000 ) DSERIAL.print(F("0"));
DSERIAL.print(b,HEX);
}
#endif #endif
#define DPRINT(level, str) ({\
switch(level) {\
case DBG_ERROR: PERR(str); break; \
case DBG_WARN: PWARN(str); break; \
case DBG_INFO: PINFO(str); break; \
case DBG_DEBUG: PDBG(str); break; \
default: PVERB(str); break; \
}\
})
#define DPRINTLN(level, str) ({\
switch(level) {\
case DBG_ERROR: PERRLN(str); break; \
case DBG_WARN: PWARNLN(str); break; \
case DBG_INFO: PINFOLN(str); break; \
case DBG_DEBUG: PDBGLN(str); break; \
default: PVERBLN(str); break; \
}\
})
#endif /*__DEBUG_H__*/ #endif /*__DEBUG_H__*/

4
tools/esp8266/defines.h

@ -21,7 +21,7 @@
//------------------------------------- //-------------------------------------
#define VERSION_MAJOR 0 #define VERSION_MAJOR 0
#define VERSION_MINOR 4 #define VERSION_MINOR 4
#define VERSION_PATCH 18 #define VERSION_PATCH 19
//------------------------------------- //-------------------------------------
@ -93,7 +93,7 @@ typedef struct {
#define ADDR_NEXT ADDR_SER_INTERVAL + SER_INTERVAL_LEN #define ADDR_NEXT ADDR_SER_INTERVAL + SER_INTERVAL_LEN
// #define ADDR_SETTINGS_CRC 950 // #define ADDR_SETTINGS_CRC 950
#define ADDR_SETTINGS_CRC ADDR_NEXT + CRC_LEN #define ADDR_SETTINGS_CRC ADDR_NEXT + 2
#if(ADDR_SETTINGS_CRC <= ADDR_NEXT) #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 +")"

44
tools/esp8266/hmInverter.h

@ -8,12 +8,6 @@
#include "hmDefines.h" #include "hmDefines.h"
#ifdef DEBUG_HMINVERTER
#define DBGINV(f) (DPRINTLN(f))
#else
#define DBGINV(f)
#endif
/** /**
* For values which are of interest and not transmitted by the inverter can be * For values which are of interest and not transmitted by the inverter can be
* calculated automatically. * calculated automatically.
@ -92,7 +86,7 @@ class Inverter {
} }
void init(void) { void init(void) {
DBGINV(F("hmInverter.h:init")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:init"));
getAssignment(); getAssignment();
toRadioId(); toRadioId();
record = new RECORDTYPE[listLen]; record = new RECORDTYPE[listLen];
@ -102,7 +96,7 @@ class Inverter {
} }
uint8_t getPosByChFld(uint8_t channel, uint8_t fieldId) { uint8_t getPosByChFld(uint8_t channel, uint8_t fieldId) {
//DBGINV(F("hmInverter.h:getPosByChFld")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getPosByChFld"));
uint8_t pos = 0; uint8_t pos = 0;
for(; pos < listLen; pos++) { for(; pos < listLen; pos++) {
if((assign[pos].ch == channel) && (assign[pos].fieldId == fieldId)) if((assign[pos].ch == channel) && (assign[pos].fieldId == fieldId))
@ -112,22 +106,22 @@ class Inverter {
} }
const char *getFieldName(uint8_t pos) { const char *getFieldName(uint8_t pos) {
//DBGINV(F("hmInverter.h:getFieldName")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getFieldName"));
return fields[assign[pos].fieldId]; return fields[assign[pos].fieldId];
} }
const char *getUnit(uint8_t pos) { const char *getUnit(uint8_t pos) {
//DBGINV(F("hmInverter.h:getUnit")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getUnit"));
return units[assign[pos].unitId]; return units[assign[pos].unitId];
} }
uint8_t getChannel(uint8_t pos) { uint8_t getChannel(uint8_t pos) {
//DBGINV(F("hmInverter.h:getChannel")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getChannel"));
return assign[pos].ch; return assign[pos].ch;
} }
void addValue(uint8_t pos, uint8_t buf[]) { void addValue(uint8_t pos, uint8_t buf[]) {
//DBGINV(F("hmInverter.h:addValue")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:addValue"));
uint8_t ptr = assign[pos].start; uint8_t ptr = assign[pos].start;
uint8_t end = ptr + assign[pos].num; uint8_t end = ptr + assign[pos].num;
uint16_t div = assign[pos].div; uint16_t div = assign[pos].div;
@ -144,12 +138,12 @@ class Inverter {
} }
RECORDTYPE getValue(uint8_t pos) { RECORDTYPE getValue(uint8_t pos) {
//DBGINV(F("hmInverter.h:getValue")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getValue"));
return record[pos]; return record[pos];
} }
void doCalculations(void) { void doCalculations(void) {
//DBGINV(F("hmInverter.h:doCalculations")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:doCalculations"));
for(uint8_t i = 0; i < listLen; i++) { for(uint8_t i = 0; i < listLen; i++) {
if(CMD_CALC == assign[i].div) { if(CMD_CALC == assign[i].div) {
record[i] = calcFunctions<RECORDTYPE>[assign[i].start].func(this, assign[i].num); record[i] = calcFunctions<RECORDTYPE>[assign[i].start].func(this, assign[i].num);
@ -159,12 +153,12 @@ class Inverter {
} }
bool isAvailable(uint32_t timestamp) { bool isAvailable(uint32_t timestamp) {
//DBGINV(F("hmInverter.h:isAvailable")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:isAvailable"));
return ((timestamp - ts) < INACT_THRES_SEC); return ((timestamp - ts) < INACT_THRES_SEC);
} }
bool isProducing(uint32_t timestamp) { bool isProducing(uint32_t timestamp) {
//DBGINV(F("hmInverter.h:isProducing")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:isProducing"));
if(isAvailable(timestamp)) { if(isAvailable(timestamp)) {
uint8_t pos = getPosByChFld(CH0, FLD_PAC); uint8_t pos = getPosByChFld(CH0, FLD_PAC);
return (getValue(pos) > INACT_PWR_THRESH); return (getValue(pos) > INACT_PWR_THRESH);
@ -173,13 +167,13 @@ class Inverter {
} }
uint32_t getLastTs(void) { uint32_t getLastTs(void) {
//DBGINV(F("hmInverter.h:getLastTs")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getLastTs"));
return ts; return ts;
} }
private: private:
void toRadioId(void) { void toRadioId(void) {
//DBGINV(F("hmInverter.h:toRadioId")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:toRadioId"));
radioId.u64 = 0ULL; radioId.u64 = 0ULL;
radioId.b[4] = serial.b[0]; radioId.b[4] = serial.b[0];
radioId.b[3] = serial.b[1]; radioId.b[3] = serial.b[1];
@ -189,7 +183,7 @@ class Inverter {
} }
void getAssignment(void) { void getAssignment(void) {
//DBGINV(F("hmInverter.h:getAssignment")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getAssignment"));
if(INV_TYPE_1CH == type) { if(INV_TYPE_1CH == type) {
listLen = (uint8_t)(HM1CH_LIST_LEN); listLen = (uint8_t)(HM1CH_LIST_LEN);
assign = (byteAssign_t*)hm1chAssignment; assign = (byteAssign_t*)hm1chAssignment;
@ -222,7 +216,7 @@ class Inverter {
template<class T=float> template<class T=float>
static T calcYieldTotalCh0(Inverter<> *iv, uint8_t arg0) { static T calcYieldTotalCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcYieldTotalCh0")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcYieldTotalCh0"));
if(NULL != iv) { if(NULL != iv) {
T yield = 0; T yield = 0;
for(uint8_t i = 1; i <= iv->channels; i++) { for(uint8_t i = 1; i <= iv->channels; i++) {
@ -236,7 +230,7 @@ static T calcYieldTotalCh0(Inverter<> *iv, uint8_t arg0) {
template<class T=float> template<class T=float>
static T calcYieldDayCh0(Inverter<> *iv, uint8_t arg0) { static T calcYieldDayCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcYieldDayCh0")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcYieldDayCh0"));
if(NULL != iv) { if(NULL != iv) {
T yield = 0; T yield = 0;
for(uint8_t i = 1; i <= iv->channels; i++) { for(uint8_t i = 1; i <= iv->channels; i++) {
@ -250,7 +244,7 @@ static T calcYieldDayCh0(Inverter<> *iv, uint8_t arg0) {
template<class T=float> template<class T=float>
static T calcUdcCh(Inverter<> *iv, uint8_t arg0) { static T calcUdcCh(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcUdcCh")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcUdcCh"));
// arg0 = channel of source // arg0 = channel of source
for(uint8_t i = 0; i < iv->listLen; i++) { for(uint8_t i = 0; i < iv->listLen; i++) {
if((FLD_UDC == iv->assign[i].fieldId) && (arg0 == iv->assign[i].ch)) { if((FLD_UDC == iv->assign[i].fieldId) && (arg0 == iv->assign[i].ch)) {
@ -263,7 +257,7 @@ static T calcUdcCh(Inverter<> *iv, uint8_t arg0) {
template<class T=float> template<class T=float>
static T calcPowerDcCh0(Inverter<> *iv, uint8_t arg0) { static T calcPowerDcCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcPowerDcCh0")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcPowerDcCh0"));
if(NULL != iv) { if(NULL != iv) {
T dcPower = 0; T dcPower = 0;
for(uint8_t i = 1; i <= iv->channels; i++) { for(uint8_t i = 1; i <= iv->channels; i++) {
@ -277,7 +271,7 @@ static T calcPowerDcCh0(Inverter<> *iv, uint8_t arg0) {
template<class T=float> template<class T=float>
static T calcEffiencyCh0(Inverter<> *iv, uint8_t arg0) { static T calcEffiencyCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcEfficiencyCh0")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcEfficiencyCh0"));
if(NULL != iv) { if(NULL != iv) {
uint8_t pos = iv->getPosByChFld(CH0, FLD_PAC); uint8_t pos = iv->getPosByChFld(CH0, FLD_PAC);
T acPower = iv->getValue(pos); T acPower = iv->getValue(pos);
@ -294,7 +288,7 @@ static T calcEffiencyCh0(Inverter<> *iv, uint8_t arg0) {
template<class T=float> template<class T=float>
static T calcIrradiation(Inverter<> *iv, uint8_t arg0) { static T calcIrradiation(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcIrradiation")); DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcIrradiation"));
// arg0 = channel // arg0 = channel
if(NULL != iv) { if(NULL != iv) {
uint8_t pos = iv->getPosByChFld(arg0, FLD_PDC); uint8_t pos = iv->getPosByChFld(arg0, FLD_PDC);

46
tools/esp8266/hmRadio.h

@ -22,12 +22,6 @@
#define RX_LOOP_CNT 300 #define RX_LOOP_CNT 300
#ifdef DEBUG_HMRADIO
#define DBGHMR(f) (DPRINTLN(f))
#else
#define DBGHMR(f)
#endif
const char* const rf24AmpPower[] = {"MIN", "LOW", "HIGH", "MAX"}; const char* const rf24AmpPower[] = {"MIN", "LOW", "HIGH", "MAX"};
@ -60,7 +54,7 @@ template <uint8_t CE_PIN, uint8_t CS_PIN, uint8_t IRQ_PIN, class BUFFER, uint64_
class HmRadio { class HmRadio {
public: public:
HmRadio() : mNrf24(CE_PIN, CS_PIN, SPI_SPEED) { HmRadio() : mNrf24(CE_PIN, CS_PIN, SPI_SPEED) {
DBGHMR(F("hmRadio.h : HmRadio():mNrf24(CE_PIN: ") + String(CE_PIN) + F(", CS_PIN: ") + String(CS_PIN) + F(", SPI_SPEED: ") + String(SPI_SPEED) + ")"); DPRINTLN(DBG_VERBOSE, F("hmRadio.h : HmRadio():mNrf24(CE_PIN: ") + String(CE_PIN) + F(", CS_PIN: ") + String(CS_PIN) + F(", SPI_SPEED: ") + String(SPI_SPEED) + ")");
mTxChLst[0] = 40; mTxChLst[0] = 40;
//mTxChIdx = 1; //mTxChIdx = 1;
@ -87,7 +81,7 @@ class HmRadio {
~HmRadio() {} ~HmRadio() {}
void setup(BUFFER *ctrl) { void setup(BUFFER *ctrl) {
DBGHMR(F("hmRadio.h:setup")); DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setup"));
pinMode(pinIrq, INPUT_PULLUP); pinMode(pinIrq, INPUT_PULLUP);
mBufCtrl = ctrl; mBufCtrl = ctrl;
@ -107,17 +101,17 @@ class HmRadio {
// enable only receiving interrupts // enable only receiving interrupts
mNrf24.maskIRQ(true, true, false); mNrf24.maskIRQ(true, true, false);
DPRINTLN(F("RF24 Amp Pwr: RF24_PA_") + String(rf24AmpPower[AmplifierPower])); DPRINTLN(DBG_INFO, F("RF24 Amp Pwr: RF24_PA_") + String(rf24AmpPower[AmplifierPower]));
mNrf24.setPALevel(AmplifierPower & 0x03); mNrf24.setPALevel(AmplifierPower & 0x03);
mNrf24.startListening(); mNrf24.startListening();
DPRINTLN(F("Radio Config:")); DPRINTLN(DBG_INFO, F("Radio Config:"));
mNrf24.printPrettyDetails(); mNrf24.printPrettyDetails();
mTxCh = getDefaultChannel(); mTxCh = getDefaultChannel();
if(!mNrf24.isChipConnected()) { if(!mNrf24.isChipConnected()) {
DPRINTLN(F("WARNING! your NRF24 module can't be reached, check the wiring")); DPRINTLN(DBG_WARN, F("WARNING! your NRF24 module can't be reached, check the wiring"));
} }
} }
@ -153,12 +147,12 @@ class HmRadio {
} }
void handleIntr(void) { void handleIntr(void) {
//DBGHMR(F("hmRadio.h:handleIntr")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:handleIntr"));
mIrqRcvd = true; mIrqRcvd = true;
} }
uint8_t getDefaultChannel(void) { uint8_t getDefaultChannel(void) {
//DBGHMR(F("hmRadio.h:getDefaultChannel")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:getDefaultChannel"));
return mTxChLst[0]; return mTxChLst[0];
} }
/*uint8_t getLastChannel(void) { /*uint8_t getLastChannel(void) {
@ -172,7 +166,7 @@ class HmRadio {
}*/ }*/
void sendTimePacket(uint64_t invId, uint32_t ts) { void sendTimePacket(uint64_t invId, uint32_t ts) {
//DBGHMR(F("hmRadio.h:sendTimePacket")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendTimePacket"));
sendCmdPacket(invId, 0x15, 0x80, false); sendCmdPacket(invId, 0x15, 0x80, false);
mTxBuf[10] = 0x0b; // cid mTxBuf[10] = 0x0b; // cid
mTxBuf[11] = 0x00; mTxBuf[11] = 0x00;
@ -188,7 +182,7 @@ class HmRadio {
} }
void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool calcCrc = true) { void sendCmdPacket(uint64_t invId, uint8_t mid, uint8_t pid, bool calcCrc = true) {
//DBGHMR(F("hmRadio.h:sendCmdPacket")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendCmdPacket"));
memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE); memset(mTxBuf, 0, MAX_RF_PAYLOAD_SIZE);
mTxBuf[0] = mid; // message id mTxBuf[0] = mid; // message id
CP_U32_BigEndian(&mTxBuf[1], (invId >> 8)); CP_U32_BigEndian(&mTxBuf[1], (invId >> 8));
@ -201,7 +195,7 @@ class HmRadio {
} }
bool checkPaketCrc(uint8_t buf[], uint8_t *len, uint8_t rxCh) { bool checkPaketCrc(uint8_t buf[], uint8_t *len, uint8_t rxCh) {
//DBGHMR(F("hmRadio.h:checkPaketCrc")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:checkPaketCrc"));
*len = (buf[0] >> 2); *len = (buf[0] >> 2);
if(*len > (MAX_RF_PAYLOAD_SIZE - 2)) if(*len > (MAX_RF_PAYLOAD_SIZE - 2))
*len = MAX_RF_PAYLOAD_SIZE - 2; *len = MAX_RF_PAYLOAD_SIZE - 2;
@ -216,8 +210,8 @@ class HmRadio {
} }
bool switchRxCh(uint16_t addLoop = 0) { bool switchRxCh(uint16_t addLoop = 0) {
//DBGHMR(F("hmRadio.h:switchRxCh")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:switchRxCh"));
//DBGHMR(F("R")); //DPRINTLN(DBG_VERBOSE, F("R"));
mRxLoopCnt += addLoop; mRxLoopCnt += addLoop;
if(mRxLoopCnt != 0) { if(mRxLoopCnt != 0) {
@ -232,18 +226,18 @@ class HmRadio {
} }
void dumpBuf(const char *info, uint8_t buf[], uint8_t len) { void dumpBuf(const char *info, uint8_t buf[], uint8_t len) {
//DBGHMR(F("hmRadio.h:dumpBuf")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:dumpBuf"));
if(NULL != info) if(NULL != info)
DPRINT(String(info)); DBGPRINT(String(info));
for(uint8_t i = 0; i < len; i++) { for(uint8_t i = 0; i < len; i++) {
DHEX(buf[i]); DHEX(buf[i]);
DPRINT(" "); DBGPRINT(" ");
} }
DPRINTLN(""); DBGPRINTLN("");
} }
bool isChipConnected(void) { bool isChipConnected(void) {
//DBGHMR(F("hmRadio.h:isChipConnected")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected"));
return mNrf24.isChipConnected(); return mNrf24.isChipConnected();
} }
@ -258,11 +252,11 @@ class HmRadio {
private: private:
void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len, bool clear=false) { void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len, bool clear=false) {
//DBGHMR(F("hmRadio.h:sendPacket")); //DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket"));
//DBGHMR("sent packet: #" + String(mSendCnt)); //DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(mSendCnt));
//dumpBuf("SEN ", buf, len); //dumpBuf("SEN ", buf, len);
if(mSerialDebug) { if(mSerialDebug) {
DPRINT("Transmit " + String(len) + " | "); DPRINT(DBG_INFO, "Transmit " + String(len) + " | ");
dumpBuf(NULL, buf, len); dumpBuf(NULL, buf, len);
} }

27
tools/esp8266/hmSystem.h

@ -11,11 +11,6 @@
#include "hmRadio.h" #include "hmRadio.h"
#endif #endif
#ifdef DEBUG_HMSYSTEM
#define DBGHMS(f) (DPRINTLN(f))
#else
#define DBGHMS(f)
#endif
template <class RADIO, class BUFFER, uint8_t MAX_INVERTER=3, class INVERTERTYPE=Inverter<float>> template <class RADIO, class BUFFER, uint8_t MAX_INVERTER=3, class INVERTERTYPE=Inverter<float>>
class HmSystem { class HmSystem {
@ -33,39 +28,39 @@ class HmSystem {
} }
void setup() { void setup() {
DBGHMS(F("hmSystem.h:setup")); DPRINTLN(DBG_VERBOSE, F("hmSystem.h:setup"));
Radio.setup(&BufCtrl); Radio.setup(&BufCtrl);
} }
INVERTERTYPE *addInverter(const char *name, uint64_t serial, uint16_t chMaxPwr[]) { INVERTERTYPE *addInverter(const char *name, uint64_t serial, uint16_t chMaxPwr[]) {
DBGHMS(F("hmSystem.h:addInverter")); DPRINTLN(DBG_VERBOSE, F("hmSystem.h:addInverter"));
if(MAX_INVERTER <= mNumInv) { if(MAX_INVERTER <= mNumInv) {
DPRINT(F("max number of inverters reached!")); DPRINT(DBG_WARN, F("max number of inverters reached!"));
return NULL; return NULL;
} }
INVERTERTYPE *p = &mInverter[mNumInv]; INVERTERTYPE *p = &mInverter[mNumInv];
p->id = mNumInv; p->id = mNumInv;
p->serial.u64 = serial; p->serial.u64 = serial;
memcpy(p->chMaxPwr, chMaxPwr, (4*2)); memcpy(p->chMaxPwr, chMaxPwr, (4*2));
//DPRINT("SERIAL: " + String(p->serial.b[5], HEX)); DPRINT(DBG_VERBOSE, "SERIAL: " + String(p->serial.b[5], HEX));
//DPRINTLN(" " + String(p->serial.b[4], HEX)); DPRINTLN(DBG_VERBOSE, " " + String(p->serial.b[4], HEX));
if(p->serial.b[5] == 0x11) { if(p->serial.b[5] == 0x11) {
switch(p->serial.b[4]) { switch(p->serial.b[4]) {
case 0x21: p->type = INV_TYPE_1CH; break; case 0x21: p->type = INV_TYPE_1CH; break;
case 0x41: p->type = INV_TYPE_2CH; break; case 0x41: p->type = INV_TYPE_2CH; break;
case 0x61: p->type = INV_TYPE_4CH; break; case 0x61: p->type = INV_TYPE_4CH; break;
default: DPRINTLN(F("unknown inverter type: 11") + String(p->serial.b[4], HEX)); break; default: DPRINTLN(DBG_ERROR, F("unknown inverter type: 11") + String(p->serial.b[4], HEX)); break;
} }
} }
else else
DPRINTLN(F("inverter type can't be detected!")); DPRINTLN(DBG_ERROR, F("inverter type can't be detected!"));
p->init(); p->init();
uint8_t len = (uint8_t)strlen(name); uint8_t len = (uint8_t)strlen(name);
strncpy(p->name, name, (len > MAX_NAME_LENGTH) ? MAX_NAME_LENGTH : len); strncpy(p->name, name, (len > MAX_NAME_LENGTH) ? MAX_NAME_LENGTH : len);
if(NULL == p->assign) { if(NULL == p->assign) {
DPRINT(F("no assignment for type found!")); DPRINT(DBG_ERROR, F("no assignment for type found!"));
return NULL; return NULL;
} }
else { else {
@ -75,7 +70,7 @@ class HmSystem {
} }
INVERTERTYPE *findInverter(uint8_t buf[]) { INVERTERTYPE *findInverter(uint8_t buf[]) {
//DBGHMS(F("hmSystem.h:findInverter")); DPRINTLN(DBG_VERBOSE, F("hmSystem.h:findInverter"));
INVERTERTYPE *p; INVERTERTYPE *p;
for(uint8_t i = 0; i < mNumInv; i++) { for(uint8_t i = 0; i < mNumInv; i++) {
p = &mInverter[i]; p = &mInverter[i];
@ -89,7 +84,7 @@ class HmSystem {
} }
INVERTERTYPE *getInverterByPos(uint8_t pos) { INVERTERTYPE *getInverterByPos(uint8_t pos) {
//DBGHMS(F("hmSystem.h:getInverterByPos")); DPRINTLN(DBG_VERBOSE, F("hmSystem.h:getInverterByPos"));
if(mInverter[pos].serial.u64 != 0ULL) if(mInverter[pos].serial.u64 != 0ULL)
return &mInverter[pos]; return &mInverter[pos];
else else
@ -97,7 +92,7 @@ class HmSystem {
} }
uint8_t getNumInverters(void) { uint8_t getNumInverters(void) {
//DBGHMS(F("hmSystem.h:getNumInverters")); DPRINTLN(DBG_VERBOSE, F("hmSystem.h:getNumInverters"));
return mNumInv; return mNumInv;
} }

58
tools/esp8266/main.cpp

@ -31,7 +31,7 @@ Main::Main(void) {
mEep = new eep(); mEep = new eep();
Serial.begin(115200); Serial.begin(115200);
DBGMAIN(F("Main::Main")); DPRINTLN(DBG_VERBOSE, F("Main::Main"));
mUptimeSecs = 0; mUptimeSecs = 0;
mUptimeTicker = 0xffffffff; mUptimeTicker = 0xffffffff;
@ -45,7 +45,7 @@ Main::Main(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::setup(uint32_t timeout) { void Main::setup(uint32_t timeout) {
DBGMAIN(F("Main::setup")); DPRINTLN(DBG_VERBOSE, F("Main::setup"));
bool startAp = mApActive; bool startAp = mApActive;
mLimit = timeout; mLimit = timeout;
@ -74,7 +74,7 @@ void Main::setup(uint32_t timeout) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::loop(void) { void Main::loop(void) {
//DBGMAIN(F("M")); //DPRINTLN(DBG_VERBOSE, F("M"));
if(mApActive) { if(mApActive) {
mDns->processNextRequest(); mDns->processNextRequest();
#ifndef AP_ONLY #ifndef AP_ONLY
@ -83,7 +83,7 @@ void Main::loop(void) {
mApActive = setupStation(mLimit); mApActive = setupStation(mLimit);
if(mApActive) { if(mApActive) {
if(strlen(WIFI_AP_PWD) < 8) if(strlen(WIFI_AP_PWD) < 8)
DPRINTLN(F("ERROR: password must be at least 8 characters long")); DPRINTLN(DBG_ERROR, F("password must be at least 8 characters long"));
setupAp(WIFI_AP_SSID, WIFI_AP_PWD); setupAp(WIFI_AP_SSID, WIFI_AP_PWD);
} }
} }
@ -91,11 +91,11 @@ void Main::loop(void) {
if(millis() - mApLastTick > 10000) { if(millis() - mApLastTick > 10000) {
uint8_t cnt = WiFi.softAPgetStationNum(); uint8_t cnt = WiFi.softAPgetStationNum();
if(cnt > 0) { if(cnt > 0) {
DPRINTLN(String(cnt) + F(" clients connected, resetting AP timeout")); DPRINTLN(DBG_INFO, String(cnt) + F(" clients connected, resetting AP timeout"));
mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000)); mNextTryTs = (millis() + (WIFI_AP_ACTIVE_TIME * 1000));
} }
mApLastTick = millis(); mApLastTick = millis();
DPRINTLN(F("AP will be closed in ") + String((mNextTryTs - mApLastTick) / 1000) + F(" seconds")); DPRINTLN(DBG_INFO, F("AP will be closed in ") + String((mNextTryTs - mApLastTick) / 1000) + F(" seconds"));
} }
} }
#endif #endif
@ -109,7 +109,7 @@ void Main::loop(void) {
else { else {
if(!mApActive) { if(!mApActive) {
mTimestamp = getNtpTime(); mTimestamp = getNtpTime();
DPRINTLN("[NTP]: " + getDateTimeStr(mTimestamp)); DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp));
} }
} }
@ -123,11 +123,11 @@ void Main::loop(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool Main::getConfig(void) { bool Main::getConfig(void) {
DBGMAIN(F("Main::getConfig")); DPRINTLN(DBG_VERBOSE, F("Main::getConfig"));
bool mApActive = false; bool mApActive = false;
mWifiSettingsValid = checkEEpCrc(ADDR_START, ADDR_WIFI_CRC, ADDR_WIFI_CRC); mWifiSettingsValid = checkEEpCrc(ADDR_START, ADDR_WIFI_CRC, ADDR_WIFI_CRC);
mSettingsValid = checkEEpCrc(ADDR_START_SETTINGS, (ADDR_NEXT-ADDR_START_SETTINGS), ADDR_SETTINGS_CRC); mSettingsValid = checkEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT)-(ADDR_START_SETTINGS)), ADDR_SETTINGS_CRC);
if(mWifiSettingsValid) { if(mWifiSettingsValid) {
mEep->read(ADDR_SSID, mStationSsid, SSID_LEN); mEep->read(ADDR_SSID, mStationSsid, SSID_LEN);
@ -146,15 +146,15 @@ bool Main::getConfig(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::setupAp(const char *ssid, const char *pwd) { void Main::setupAp(const char *ssid, const char *pwd) {
DBGMAIN(F("Main::setupAp")); DPRINTLN(DBG_VERBOSE, F("Main::setupAp"));
IPAddress apIp(192, 168, 1, 1); IPAddress apIp(192, 168, 1, 1);
DPRINTLN(F("\n---------\nAP MODE\nSSDI: ") DPRINTLN(DBG_INFO, F("\n---------\nAP MODE\nSSID: ")
+ String(ssid) + F("\nPWD: ") + String(ssid) + F("\nPWD: ")
+ String(pwd) + F("\nActive for: ") + String(pwd) + F("\nActive for: ")
+ String(WIFI_AP_ACTIVE_TIME) + F(" seconds") + String(WIFI_AP_ACTIVE_TIME) + F(" seconds")
+ F("\n---------\n")); + F("\n---------\n"));
DBGMAIN("DBG: " + String(mNextTryTs)); DPRINTLN(DBG_DEBUG, String(mNextTryTs));
WiFi.mode(WIFI_AP); WiFi.mode(WIFI_AP);
WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0)); WiFi.softAPConfig(apIp, apIp, IPAddress(255, 255, 255, 0));
@ -173,7 +173,7 @@ void Main::setupAp(const char *ssid, const char *pwd) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
bool Main::setupStation(uint32_t timeout) { bool Main::setupStation(uint32_t timeout) {
DBGMAIN(F("Main::setupStation")); DPRINTLN(DBG_VERBOSE, F("Main::setupStation"));
int32_t cnt; int32_t cnt;
bool startAp = false; bool startAp = false;
@ -190,7 +190,7 @@ bool Main::setupStation(uint32_t timeout) {
WiFi.hostname(mDeviceName); WiFi.hostname(mDeviceName);
delay(2000); delay(2000);
DPRINTLN(F("connect to network '") + String(mStationSsid) + F("' ...")); DPRINTLN(DBG_INFO, F("connect to network '") + String(mStationSsid) + F("' ..."));
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(100); delay(100);
if(cnt % 100 == 0) if(cnt % 100 == 0)
@ -223,7 +223,7 @@ bool Main::setupStation(uint32_t timeout) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showSetup(void) { void Main::showSetup(void) {
DBGMAIN(F("Main::showSetup")); DPRINTLN(DBG_VERBOSE, F("Main::showSetup"));
String html = FPSTR(setup_html); String html = FPSTR(setup_html);
html.replace(F("{SSID}"), mStationSsid); html.replace(F("{SSID}"), mStationSsid);
// PWD will be left at the default value (for protection) // PWD will be left at the default value (for protection)
@ -241,23 +241,21 @@ void Main::showSetup(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showCss(void) { void Main::showCss(void) {
DBGMAIN(F("Main::showCss")); DPRINTLN(DBG_VERBOSE, F("Main::showCss"));
mWeb->send(200, "text/css", FPSTR(style_css)); mWeb->send(200, "text/css", FPSTR(style_css));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showSave(void) { void Main::showSave(void) {
#ifdef DEBUG_HMMAIN DPRINTLN(DBG_VERBOSE, F("Main::showSave"));
DPRINTLN(F("Main::showSave"));
#endif
saveValues(true); saveValues(true);
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::saveValues(bool webSend = true) { void Main::saveValues(bool webSend = true) {
DBGMAIN(F("Main::saveValues")); DPRINTLN(DBG_VERBOSE, F("Main::saveValues"));
if(mWeb->args() > 0) { if(mWeb->args() > 0) {
if(mWeb->arg("ssid") != "") { if(mWeb->arg("ssid") != "") {
memset(mStationSsid, 0, SSID_LEN); memset(mStationSsid, 0, SSID_LEN);
@ -291,7 +289,7 @@ void Main::saveValues(bool webSend = true) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::updateCrc(void) { void Main::updateCrc(void) {
DBGMAIN(F("Main::updateCrc")); DPRINTLN(DBG_VERBOSE, F("Main::updateCrc"));
uint16_t crc; uint16_t crc;
crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC); crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
//Serial.println("new CRC: " + String(crc, HEX)); //Serial.println("new CRC: " + String(crc, HEX));
@ -302,7 +300,7 @@ void Main::updateCrc(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showUptime(void) { void Main::showUptime(void) {
//DBGMAIN(F("Main::showUptime")); //DPRINTLN(DBG_VERBOSE, F("Main::showUptime"));
char time[20] = {0}; char time[20] = {0};
int upTimeSc = uint32_t((mUptimeSecs) % 60); int upTimeSc = uint32_t((mUptimeSecs) % 60);
@ -318,14 +316,14 @@ void Main::showUptime(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showTime(void) { void Main::showTime(void) {
//DBGMAIN(F("Main::showTime")); //DPRINTLN(DBG_VERBOSE, F("Main::showTime"));
mWeb->send(200, "text/plain", getDateTimeStr(mTimestamp)); mWeb->send(200, "text/plain", getDateTimeStr(mTimestamp));
} }
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showNotFound(void) { void Main::showNotFound(void) {
DBGMAIN(F("Main::showNotFound - ") + mWeb->uri()); DPRINTLN(DBG_VERBOSE, F("Main::showNotFound - ") + mWeb->uri());
String msg = F("File Not Found\n\nURI: "); String msg = F("File Not Found\n\nURI: ");
msg += mWeb->uri(); msg += mWeb->uri();
msg += F("\nMethod: "); msg += F("\nMethod: ");
@ -344,7 +342,7 @@ void Main::showNotFound(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showReboot(void) { void Main::showReboot(void) {
DBGMAIN(F("Main::showReboot")); 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>")); 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); delay(1000);
ESP.restart(); ESP.restart();
@ -354,7 +352,7 @@ void Main::showReboot(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::showFactoryRst(void) { void Main::showFactoryRst(void) {
DBGMAIN(F("Main::showFactoryRst")); DPRINTLN(DBG_VERBOSE, F("Main::showFactoryRst"));
String content = ""; String content = "";
int refresh = 3; int refresh = 3;
if(mWeb->args() > 0) { if(mWeb->args() > 0) {
@ -383,7 +381,7 @@ void Main::showFactoryRst(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
time_t Main::getNtpTime(void) { time_t Main::getNtpTime(void) {
//DBGMAIN(F("Main::getNtpTime")); //DPRINTLN(DBG_VERBOSE, F("Main::getNtpTime"));
time_t date = 0; time_t date = 0;
IPAddress timeServer; IPAddress timeServer;
uint8_t buf[NTP_PACKET_SIZE]; uint8_t buf[NTP_PACKET_SIZE];
@ -421,7 +419,7 @@ time_t Main::getNtpTime(void) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
void Main::sendNTPpacket(IPAddress& address) { void Main::sendNTPpacket(IPAddress& address) {
//DBGMAIN(F("Main::sendNTPpacket")); //DPRINTLN(DBG_VERBOSE, F("Main::sendNTPpacket"));
uint8_t buf[NTP_PACKET_SIZE] = {0}; uint8_t buf[NTP_PACKET_SIZE] = {0};
buf[0] = B11100011; // LI, Version, Mode buf[0] = B11100011; // LI, Version, Mode
@ -442,7 +440,7 @@ void Main::sendNTPpacket(IPAddress& address) {
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------
String Main::getDateTimeStr(time_t t) { String Main::getDateTimeStr(time_t t) {
//DBGMAIN(F("Main::getDateTimeStr")); //DPRINTLN(DBG_VERBOSE, F("Main::getDateTimeStr"));
char str[20] = {0}; char str[20] = {0};
if(0 == t) if(0 == t)
sprintf(str, "n/a"); sprintf(str, "n/a");
@ -456,7 +454,7 @@ String Main::getDateTimeStr(time_t t) {
// calculates the daylight saving time for middle Europe. Input: Unixtime in UTC // calculates the daylight saving time for middle Europe. Input: Unixtime in UTC
// from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536 // from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536
time_t Main::offsetDayLightSaving (uint32_t local_t) { time_t Main::offsetDayLightSaving (uint32_t local_t) {
//DBGMAIN(F("Main::offsetDayLightSaving")); //DPRINTLN(DBG_VERBOSE, F("Main::offsetDayLightSaving"));
int m = month (local_t); 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 0; // no DSL in Jan, Feb, Nov, Dez
if(m > 3 && m < 10) return 1; // DSL in Apr, May, Jun, Jul, Aug, Sep if(m > 3 && m < 10) return 1; // DSL in Apr, May, Jun, Jul, Aug, Sep

20
tools/esp8266/main.h

@ -23,11 +23,6 @@
#include "crc.h" #include "crc.h"
#include "debug.h" #include "debug.h"
#ifdef DEBUG_MAIN
#define DBGMAIN(f) (DPRINTLN(f))
#else
#define DBGMAIN(f)
#endif
const byte mDnsPort = 53; const byte mDnsPort = 53;
@ -51,7 +46,7 @@ class Main {
virtual void updateCrc(void); virtual void updateCrc(void);
inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) { inline uint16_t buildEEpCrc(uint32_t start, uint32_t length) {
DBGMAIN(F("main.h:buildEEpCrc")); DPRINTLN(DBG_VERBOSE, F("main.h:buildEEpCrc"));
uint8_t buf[32]; uint8_t buf[32];
uint16_t crc = 0xffff; uint16_t crc = 0xffff;
uint8_t len; uint8_t len;
@ -67,16 +62,17 @@ class Main {
} }
bool checkEEpCrc(uint32_t start, uint32_t length, uint32_t crcPos) { bool checkEEpCrc(uint32_t start, uint32_t length, uint32_t crcPos) {
//DBGMAIN(F("main.h:checkEEpCrc")); DPRINTLN(DBG_VERBOSE, F("main.h:checkEEpCrc"));
DPRINTLN(DBG_DEBUG, F("start: ") + String(start) + F(", length: ") + String(length));
uint16_t crcRd, crcCheck; uint16_t crcRd, crcCheck;
crcCheck = buildEEpCrc(start, length); crcCheck = buildEEpCrc(start, length);
mEep->read(crcPos, &crcRd); mEep->read(crcPos, &crcRd);
//DBGMAIN("CRC RD: " + String(crcRd, HEX) + " CRC CALC: " + String(crcCheck, HEX)); DPRINTLN(DBG_DEBUG, "CRC RD: " + String(crcRd, HEX) + " CRC CALC: " + String(crcCheck, HEX));
return (crcCheck == crcRd); return (crcCheck == crcRd);
} }
void eraseSettings(bool all = false) { void eraseSettings(bool all = false) {
//DBGMAIN(F("main.h:eraseSettings")); //DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings"));
uint8_t buf[64] = {0}; uint8_t buf[64] = {0};
uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS; uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS;
uint16_t end; uint16_t end;
@ -84,7 +80,7 @@ class Main {
end = addr + 64; end = addr + 64;
if(end > (ADDR_SETTINGS_CRC + 2)) if(end > (ADDR_SETTINGS_CRC + 2))
end = (ADDR_SETTINGS_CRC + 2); end = (ADDR_SETTINGS_CRC + 2);
DPRINTLN(F("erase: 0x") + String(addr, HEX) + " - 0x" + String(end, HEX)); DPRINTLN(DBG_DEBUG, F("erase: 0x") + String(addr, HEX) + " - 0x" + String(end, HEX));
mEep->write(addr, buf, (end-addr)); mEep->write(addr, buf, (end-addr));
addr = end; addr = end;
} while(addr < (ADDR_SETTINGS_CRC + 2)); } while(addr < (ADDR_SETTINGS_CRC + 2));
@ -92,7 +88,7 @@ class Main {
} }
inline bool checkTicker(uint32_t *ticker, uint32_t interval) { inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
//DBGMAIN(F("c")); //DPRINTLN(DBG_VERBOSE, F("c"));
uint32_t mil = millis(); uint32_t mil = millis();
if(mil >= *ticker) { if(mil >= *ticker) {
*ticker = mil + interval; *ticker = mil + interval;
@ -107,7 +103,7 @@ class Main {
} }
void stats(void) { void stats(void) {
DBGMAIN(F("main.h:stats")); DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
uint32_t free; uint32_t free;
uint16_t max; uint16_t max;
uint8_t frag; uint8_t frag;

14
tools/esp8266/mqtt.h

@ -26,7 +26,7 @@ class mqtt {
} }
void setup(const char *broker, const char *topic, const char *user, const char *pwd, uint16_t port) { void setup(const char *broker, const char *topic, const char *user, const char *pwd, uint16_t port) {
DPRINTLN(F("mqtt.h:setup")); DPRINTLN(DBG_VERBOSE, F("mqtt.h:setup"));
mAddressSet = true; mAddressSet = true;
mClient->setServer(broker, port); mClient->setServer(broker, port);
@ -37,7 +37,7 @@ class mqtt {
} }
void sendMsg(const char *topic, const char *msg) { void sendMsg(const char *topic, const char *msg) {
//DPRINTLN(F("mqtt.h:sendMsg")); //DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
if(mAddressSet) { if(mAddressSet) {
char top[64]; char top[64];
snprintf(top, 64, "%s/%s", mTopic, topic); snprintf(top, 64, "%s/%s", mTopic, topic);
@ -50,24 +50,24 @@ class mqtt {
} }
bool isConnected(bool doRecon = false) { bool isConnected(bool doRecon = false) {
//DPRINTLN(F("mqtt.h:isConnected")); //DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected"));
if(doRecon) if(doRecon)
reconnect(); reconnect();
return mClient->connected(); return mClient->connected();
} }
char *getUser(void) { char *getUser(void) {
//DPRINTLN(F("mqtt.h:getUser")); //DPRINTLN(DBG_VERBOSE, F("mqtt.h:getUser"));
return mUser; return mUser;
} }
char *getPwd(void) { char *getPwd(void) {
//DPRINTLN(F("mqtt.h:getPwd")); //DPRINTLN(DBG_VERBOSE, F("mqtt.h:getPwd"));
return mPwd; return mPwd;
} }
char *getTopic(void) { char *getTopic(void) {
//DPRINTLN(F("mqtt.h:getTopic")); //DPRINTLN(DBG_VERBOSE, F("mqtt.h:getTopic"));
return mTopic; return mTopic;
} }
@ -84,7 +84,7 @@ class mqtt {
private: private:
void reconnect(void) { void reconnect(void) {
//DPRINTLN(F("mqtt.h:reconnect")); //DPRINTLN(DBG_VERBOSE, F("mqtt.h:reconnect"));
if(!mClient->connected()) { if(!mClient->connected()) {
if((strlen(mUser) > 0) && (strlen(mPwd) > 0)) if((strlen(mUser) > 0) && (strlen(mPwd) > 0))
mClient->connect(DEF_DEVICE_NAME, mUser, mPwd); mClient->connect(DEF_DEVICE_NAME, mUser, mPwd);

Loading…
Cancel
Save