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() {
DBGAPP(F("app::app():Main"));
DPRINTLN(DBG_VERBOSE, F("app::app():Main"));
mSendTicker = 0xffff;
mSendInterval = 0;
mMqttTicker = 0xffff;
@ -49,7 +49,7 @@ app::~app(void) {
//-----------------------------------------------------------------------------
void app::setup(uint32_t timeout) {
DBGAPP(F("app::setup"));
DPRINTLN(DBG_VERBOSE, F("app::setup"));
Main::setup(timeout);
mWeb->on("/", std::bind(&app::showIndex, this));
@ -78,7 +78,7 @@ void app::setup(uint32_t timeout) {
if(0ULL != invSerial) {
iv = mSys->addInverter(name, invSerial, modPwr);
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++) {
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 {
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();
saveValues(false);
delay(100);
DPRINTLN(F("... restarting ..."));
DPRINTLN(DBG_INFO, F("... restarting ..."));
delay(100);
ESP.restart();
}
@ -179,21 +181,21 @@ void app::setup(uint32_t timeout) {
mSys->setup();
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 {
DPRINTLN(F("\n\n----------------------------------------"));
DPRINTLN(F("Welcome to AHOY!"));
DPRINT(F("\npoint your browser to http://"));
DPRINTLN(WiFi.localIP());
DPRINTLN(F("to configure your device"));
DPRINTLN(F("----------------------------------------\n"));
DPRINTLN(DBG_INFO, F("\n\n----------------------------------------"));
DPRINTLN(DBG_INFO, F("Welcome to AHOY!"));
DPRINT(DBG_INFO, F("\npoint your browser to http://"));
DBGPRINTLN(WiFi.localIP());
DPRINTLN(DBG_INFO, F("to configure your device"));
DPRINTLN(DBG_INFO, F("----------------------------------------\n"));
}
}
//-----------------------------------------------------------------------------
void app::loop(void) {
//DBGAPP(F("app::loop"))
DPRINTLN(DBG_VERBOSE, F("app::loop"));
Main::loop();
mSys->Radio.loop();
@ -201,11 +203,10 @@ void app::loop(void) {
yield();
if(checkTicker(&mRxTicker, 5)) {
#ifdef DEBUG_APP
//DPRINTLN(F("app_loops =") + String(app_loops));
//app_loops=0;
//DPRINT(F("a"));
#endif
DPRINTLN(DBG_DEBUG, F("app_loops =") + String(app_loops));
app_loops=0;
DPRINT(DBG_DEBUG, F("a"));
bool rxRdy = mSys->Radio.switchRxCh();
if(!mSys->BufCtrl.empty()) {
@ -215,7 +216,7 @@ void app::loop(void) {
if(mSys->Radio.checkPaketCrc(p->packet, &len, p->rxCh)) {
// process buffer only on first occurrence
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);
}
mFrameCnt++;
@ -287,7 +288,7 @@ void app::loop(void) {
if(0.0f != iv->getValue(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));
DPRINTLN(String(topic) + ": " + String(val));
DPRINTLN(DBG_INFO, String(topic) + ": " + String(val));
}
yield();
}
@ -302,11 +303,11 @@ void app::loop(void) {
if(0 != mTimestamp) {
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(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;
@ -325,8 +326,8 @@ void app::loop(void) {
if(!mPayload[iv->id].complete) {
mRxFailed++;
if(mSerialDebug) {
DPRINT(F("Inverter #") + String(iv->id) + " ");
DPRINTLN(F("no Payload received! (retransmits: ") + String(mPayload[iv->id].retransmits) + ")");
DPRINT(DBG_INFO, F("Inverter #") + String(iv->id) + " ");
DPRINTLN(DBG_INFO, F("no Payload received! (retransmits: ") + String(mPayload[iv->id].retransmits) + ")");
}
}
@ -340,13 +341,13 @@ void app::loop(void) {
yield();
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);
mRxTicker = 0;
}
}
else if(mSerialDebug)
DPRINTLN(F("time not set, can't request inverter!"));
DPRINTLN(DBG_WARN, F("time not set, can't request inverter!"));
yield();
}
}
@ -355,14 +356,14 @@ void app::loop(void) {
//-----------------------------------------------------------------------------
void app::handleIntr(void) {
DBGAPP(F("app::handleIntr"));
DPRINTLN(DBG_VERBOSE, F("app::handleIntr"));
mSys->Radio.handleIntr();
}
//-----------------------------------------------------------------------------
bool app::buildPayload(uint8_t id) {
//DBGAPP(F("app::buildPayload"));
DPRINTLN(DBG_VERBOSE, F("app::buildPayload"));
uint16_t crc = 0xffff, crcRcv;
if(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) {
//DBGAPP(F("app::processPayload"));
DPRINTLN(DBG_VERBOSE, F("app::processPayload"));
for(uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
if(NULL != iv) {
@ -401,7 +402,7 @@ void app::processPayload(bool retransmit) {
for(uint8_t i = 0; i < (mPayload[iv->id].maxPackId-1); i ++) {
if(mPayload[iv->id].len[i] == 0) {
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);
break; // only retransmit one frame per loop
}
@ -410,7 +411,7 @@ void app::processPayload(bool retransmit) {
}
else {
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)
mSys->Radio.sendCmdPacket(iv->radioId.u64, 0x15, mLastPacketId, true);
else
@ -433,7 +434,7 @@ void app::processPayload(bool retransmit) {
}
offs-=2;
if(mSerialDebug) {
DPRINT(F("Payload (") + String(offs) + "): ");
DPRINT(DBG_INFO, F("Payload (") + String(offs) + "): ");
mSys->Radio.dumpBuf(NULL, payload, offs);
}
mRxSuccess++;
@ -453,7 +454,7 @@ void app::processPayload(bool retransmit) {
//-----------------------------------------------------------------------------
void app::showIndex(void) {
//DBGAPP(F("app::showIndex"));
DPRINTLN(DBG_VERBOSE, F("app::showIndex"));
String html = FPSTR(index_html);
html.replace(F("{DEVICE}"), mDeviceName);
html.replace(F("{VERSION}"), mVersion);
@ -465,7 +466,7 @@ void app::showIndex(void) {
//-----------------------------------------------------------------------------
void app::showSetup(void) {
DBGAPP(F("app::showSetup"));
DPRINTLN(DBG_VERBOSE, F("app::showSetup"));
// overrides same method in main.cpp
uint16_t interval;
@ -585,14 +586,14 @@ void app::showSetup(void) {
//-----------------------------------------------------------------------------
void app::showSave(void) {
DBGAPP(F("app::showSave"));
DPRINTLN(DBG_VERBOSE, F("app::showSave"));
saveValues(true);
}
//-----------------------------------------------------------------------------
void app::showErase() {
DBGAPP(F("app::showErase"));
DPRINTLN(DBG_VERBOSE, F("app::showErase"));
eraseSettings();
showReboot();
}
@ -600,7 +601,7 @@ void app::showErase() {
//-----------------------------------------------------------------------------
void app::showStatistics(void) {
//DBGAPP(F("app::showStatistics"));
DPRINTLN(DBG_VERBOSE, F("app::showStatistics"));
String content = F("Receive success: ") + String(mRxSuccess) + "\n";
content += F("Receive fail: ") + String(mRxFailed) + "\n";
content += F("Frames received: ") + String(mFrameCnt) + "\n";
@ -648,7 +649,7 @@ void app::showStatistics(void) {
//-----------------------------------------------------------------------------
void app::showHoymiles(void) {
DBGAPP(F("app::showHoymiles"));
DPRINTLN(DBG_VERBOSE, F("app::showHoymiles"));
String html = FPSTR(hoymiles_html);
html.replace(F("{DEVICE}"), mDeviceName);
html.replace(F("{VERSION}"), mVersion);
@ -660,7 +661,7 @@ void app::showHoymiles(void) {
//-----------------------------------------------------------------------------
void app::showLiveData(void) {
//DBGAPP(F("app::showLiveData"));
DPRINTLN(DBG_VERBOSE, F("app::showLiveData"));
String modHtml;
for(uint8_t id = 0; id < mSys->getNumInverters(); id++) {
Inverter<> *iv = mSys->getInverterByPos(id);
@ -738,7 +739,7 @@ void app::showLiveData(void) {
//-----------------------------------------------------------------------------
void app::saveValues(bool webSend = true) {
DBGAPP(F("app::saveValues"));
DPRINTLN(DBG_VERBOSE, F("app::saveValues"));
Main::saveValues(false); // general configuration
if(mWeb->args() > 0) {
@ -822,8 +823,8 @@ void app::saveValues(bool webSend = true) {
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("Info: Serial debug is ");
if(mSerialDebug) DPRINTLN("on"); else DPRINTLN("off");
DPRINT(DBG_INFO, "Serial debug is ");
if(mSerialDebug) DPRINTLN(DBG_INFO, "on"); else DPRINTLN(DBG_INFO, "off");
mSys->Radio.mSerialDebug = mSerialDebug;
updateCrc();
@ -847,11 +848,11 @@ void app::saveValues(bool webSend = true) {
//-----------------------------------------------------------------------------
void app::updateCrc(void) {
DBGAPP(F("app::updateCrc"));
DPRINTLN(DBG_VERBOSE, F("app::updateCrc"));
Main::updateCrc();
uint16_t crc;
crc = buildEEpCrc(ADDR_START_SETTINGS, (ADDR_NEXT - ADDR_START_SETTINGS));
//DPRINTLN("new CRC: " + String(crc, HEX));
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);
}

5
tools/esp8266/app.h

@ -16,11 +16,6 @@
#include "hmSystem.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 HmRadio<RF24_CE_PIN, RF24_CS_PIN, RF24_IRQ_PIN, BufferType> RadioType;

132
tools/esp8266/debug.h

@ -6,39 +6,115 @@
#ifndef __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
#define DPRINT(str)
#define DPRINTLN(str)
#define DBGPRINT(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
#define PINFO(str)
#define PINFOLN(str)
#endif
#ifndef DSERIAL
#define DSERIAL Serial
#if DEBUG_LEVEL >= DBG_DEBUG
#define PDBG(str) DBGPRINT(F("D: ")); DBGPRINT(str);
#define PDBGLN(str) DBGPRINT(F("D: ")); DBGPRINTLN(str);
#else
#define PDBG(str)
#define PDBGLN(str)
#endif
template <class T>
inline void DPRINT(T str) { DSERIAL.print(str); }
template <class T>
inline void DPRINTLN(T str) { DPRINT(str); DPRINT(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);
}
#if DEBUG_LEVEL >= DBG_VERBOSE
#define PVERB(str) DBGPRINT(F("V: ")); DBGPRINT(str);
#define PVERBLN(str) DBGPRINT(F("V: ")); DBGPRINTLN(str);
#else
#define PVERB(str)
#define PVERBLN(str)
#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__*/

4
tools/esp8266/defines.h

@ -21,7 +21,7 @@
//-------------------------------------
#define VERSION_MAJOR 0
#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_SETTINGS_CRC 950
#define ADDR_SETTINGS_CRC ADDR_NEXT + CRC_LEN
#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 +")"

44
tools/esp8266/hmInverter.h

@ -8,12 +8,6 @@
#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
* calculated automatically.
@ -92,7 +86,7 @@ class Inverter {
}
void init(void) {
DBGINV(F("hmInverter.h:init"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:init"));
getAssignment();
toRadioId();
record = new RECORDTYPE[listLen];
@ -102,7 +96,7 @@ class Inverter {
}
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;
for(; pos < listLen; pos++) {
if((assign[pos].ch == channel) && (assign[pos].fieldId == fieldId))
@ -112,22 +106,22 @@ class Inverter {
}
const char *getFieldName(uint8_t pos) {
//DBGINV(F("hmInverter.h:getFieldName"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getFieldName"));
return fields[assign[pos].fieldId];
}
const char *getUnit(uint8_t pos) {
//DBGINV(F("hmInverter.h:getUnit"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getUnit"));
return units[assign[pos].unitId];
}
uint8_t getChannel(uint8_t pos) {
//DBGINV(F("hmInverter.h:getChannel"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getChannel"));
return assign[pos].ch;
}
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 end = ptr + assign[pos].num;
uint16_t div = assign[pos].div;
@ -144,12 +138,12 @@ class Inverter {
}
RECORDTYPE getValue(uint8_t pos) {
//DBGINV(F("hmInverter.h:getValue"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getValue"));
return record[pos];
}
void doCalculations(void) {
//DBGINV(F("hmInverter.h:doCalculations"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:doCalculations"));
for(uint8_t i = 0; i < listLen; i++) {
if(CMD_CALC == assign[i].div) {
record[i] = calcFunctions<RECORDTYPE>[assign[i].start].func(this, assign[i].num);
@ -159,12 +153,12 @@ class Inverter {
}
bool isAvailable(uint32_t timestamp) {
//DBGINV(F("hmInverter.h:isAvailable"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:isAvailable"));
return ((timestamp - ts) < INACT_THRES_SEC);
}
bool isProducing(uint32_t timestamp) {
//DBGINV(F("hmInverter.h:isProducing"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:isProducing"));
if(isAvailable(timestamp)) {
uint8_t pos = getPosByChFld(CH0, FLD_PAC);
return (getValue(pos) > INACT_PWR_THRESH);
@ -173,13 +167,13 @@ class Inverter {
}
uint32_t getLastTs(void) {
//DBGINV(F("hmInverter.h:getLastTs"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getLastTs"));
return ts;
}
private:
void toRadioId(void) {
//DBGINV(F("hmInverter.h:toRadioId"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:toRadioId"));
radioId.u64 = 0ULL;
radioId.b[4] = serial.b[0];
radioId.b[3] = serial.b[1];
@ -189,7 +183,7 @@ class Inverter {
}
void getAssignment(void) {
//DBGINV(F("hmInverter.h:getAssignment"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:getAssignment"));
if(INV_TYPE_1CH == type) {
listLen = (uint8_t)(HM1CH_LIST_LEN);
assign = (byteAssign_t*)hm1chAssignment;
@ -222,7 +216,7 @@ class Inverter {
template<class T=float>
static T calcYieldTotalCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcYieldTotalCh0"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcYieldTotalCh0"));
if(NULL != iv) {
T yield = 0;
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>
static T calcYieldDayCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcYieldDayCh0"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcYieldDayCh0"));
if(NULL != iv) {
T yield = 0;
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>
static T calcUdcCh(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcUdcCh"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcUdcCh"));
// arg0 = channel of source
for(uint8_t i = 0; i < iv->listLen; i++) {
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>
static T calcPowerDcCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcPowerDcCh0"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcPowerDcCh0"));
if(NULL != iv) {
T dcPower = 0;
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>
static T calcEffiencyCh0(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcEfficiencyCh0"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcEfficiencyCh0"));
if(NULL != iv) {
uint8_t pos = iv->getPosByChFld(CH0, FLD_PAC);
T acPower = iv->getValue(pos);
@ -294,7 +288,7 @@ static T calcEffiencyCh0(Inverter<> *iv, uint8_t arg0) {
template<class T=float>
static T calcIrradiation(Inverter<> *iv, uint8_t arg0) {
//DBGINV(F("hmInverter.h:calcIrradiation"));
DPRINTLN(DBG_VERBOSE, F("hmInverter.h:calcIrradiation"));
// arg0 = channel
if(NULL != iv) {
uint8_t pos = iv->getPosByChFld(arg0, FLD_PDC);

46
tools/esp8266/hmRadio.h

@ -22,12 +22,6 @@
#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"};
@ -60,7 +54,7 @@ template <uint8_t CE_PIN, uint8_t CS_PIN, uint8_t IRQ_PIN, class BUFFER, uint64_
class HmRadio {
public:
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;
//mTxChIdx = 1;
@ -87,7 +81,7 @@ class HmRadio {
~HmRadio() {}
void setup(BUFFER *ctrl) {
DBGHMR(F("hmRadio.h:setup"));
DPRINTLN(DBG_VERBOSE, F("hmRadio.h:setup"));
pinMode(pinIrq, INPUT_PULLUP);
mBufCtrl = ctrl;
@ -107,17 +101,17 @@ class HmRadio {
// enable only receiving interrupts
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.startListening();
DPRINTLN(F("Radio Config:"));
DPRINTLN(DBG_INFO, F("Radio Config:"));
mNrf24.printPrettyDetails();
mTxCh = getDefaultChannel();
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) {
//DBGHMR(F("hmRadio.h:handleIntr"));
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:handleIntr"));
mIrqRcvd = true;
}
uint8_t getDefaultChannel(void) {
//DBGHMR(F("hmRadio.h:getDefaultChannel"));
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:getDefaultChannel"));
return mTxChLst[0];
}
/*uint8_t getLastChannel(void) {
@ -172,7 +166,7 @@ class HmRadio {
}*/
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);
mTxBuf[10] = 0x0b; // cid
mTxBuf[11] = 0x00;
@ -188,7 +182,7 @@ class HmRadio {
}
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);
mTxBuf[0] = mid; // message id
CP_U32_BigEndian(&mTxBuf[1], (invId >> 8));
@ -201,7 +195,7 @@ class HmRadio {
}
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);
if(*len > (MAX_RF_PAYLOAD_SIZE - 2))
*len = MAX_RF_PAYLOAD_SIZE - 2;
@ -216,8 +210,8 @@ class HmRadio {
}
bool switchRxCh(uint16_t addLoop = 0) {
//DBGHMR(F("hmRadio.h:switchRxCh"));
//DBGHMR(F("R"));
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:switchRxCh"));
//DPRINTLN(DBG_VERBOSE, F("R"));
mRxLoopCnt += addLoop;
if(mRxLoopCnt != 0) {
@ -232,18 +226,18 @@ class HmRadio {
}
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)
DPRINT(String(info));
DBGPRINT(String(info));
for(uint8_t i = 0; i < len; i++) {
DHEX(buf[i]);
DPRINT(" ");
DBGPRINT(" ");
}
DPRINTLN("");
DBGPRINTLN("");
}
bool isChipConnected(void) {
//DBGHMR(F("hmRadio.h:isChipConnected"));
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:isChipConnected"));
return mNrf24.isChipConnected();
}
@ -258,11 +252,11 @@ class HmRadio {
private:
void sendPacket(uint64_t invId, uint8_t buf[], uint8_t len, bool clear=false) {
//DBGHMR(F("hmRadio.h:sendPacket"));
//DBGHMR("sent packet: #" + String(mSendCnt));
//DPRINTLN(DBG_VERBOSE, F("hmRadio.h:sendPacket"));
//DPRINTLN(DBG_VERBOSE, "sent packet: #" + String(mSendCnt));
//dumpBuf("SEN ", buf, len);
if(mSerialDebug) {
DPRINT("Transmit " + String(len) + " | ");
DPRINT(DBG_INFO, "Transmit " + String(len) + " | ");
dumpBuf(NULL, buf, len);
}

27
tools/esp8266/hmSystem.h

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

58
tools/esp8266/main.cpp

@ -31,7 +31,7 @@ Main::Main(void) {
mEep = new eep();
Serial.begin(115200);
DBGMAIN(F("Main::Main"));
DPRINTLN(DBG_VERBOSE, F("Main::Main"));
mUptimeSecs = 0;
mUptimeTicker = 0xffffffff;
@ -45,7 +45,7 @@ Main::Main(void) {
//-----------------------------------------------------------------------------
void Main::setup(uint32_t timeout) {
DBGMAIN(F("Main::setup"));
DPRINTLN(DBG_VERBOSE, F("Main::setup"));
bool startAp = mApActive;
mLimit = timeout;
@ -74,7 +74,7 @@ void Main::setup(uint32_t timeout) {
//-----------------------------------------------------------------------------
void Main::loop(void) {
//DBGMAIN(F("M"));
//DPRINTLN(DBG_VERBOSE, F("M"));
if(mApActive) {
mDns->processNextRequest();
#ifndef AP_ONLY
@ -83,7 +83,7 @@ void Main::loop(void) {
mApActive = setupStation(mLimit);
if(mApActive) {
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);
}
}
@ -91,11 +91,11 @@ void Main::loop(void) {
if(millis() - mApLastTick > 10000) {
uint8_t cnt = WiFi.softAPgetStationNum();
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));
}
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
@ -109,7 +109,7 @@ void Main::loop(void) {
else {
if(!mApActive) {
mTimestamp = getNtpTime();
DPRINTLN("[NTP]: " + getDateTimeStr(mTimestamp));
DPRINTLN(DBG_INFO, "[NTP]: " + getDateTimeStr(mTimestamp));
}
}
@ -123,11 +123,11 @@ void Main::loop(void) {
//-----------------------------------------------------------------------------
bool Main::getConfig(void) {
DBGMAIN(F("Main::getConfig"));
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);
mSettingsValid = checkEEpCrc(ADDR_START_SETTINGS, ((ADDR_NEXT)-(ADDR_START_SETTINGS)), ADDR_SETTINGS_CRC);
if(mWifiSettingsValid) {
mEep->read(ADDR_SSID, mStationSsid, SSID_LEN);
@ -146,15 +146,15 @@ bool Main::getConfig(void) {
//-----------------------------------------------------------------------------
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);
DPRINTLN(F("\n---------\nAP MODE\nSSDI: ")
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"));
DBGMAIN("DBG: " + String(mNextTryTs));
DPRINTLN(DBG_DEBUG, String(mNextTryTs));
WiFi.mode(WIFI_AP);
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) {
DBGMAIN(F("Main::setupStation"));
DPRINTLN(DBG_VERBOSE, F("Main::setupStation"));
int32_t cnt;
bool startAp = false;
@ -190,7 +190,7 @@ bool Main::setupStation(uint32_t timeout) {
WiFi.hostname(mDeviceName);
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) {
delay(100);
if(cnt % 100 == 0)
@ -223,7 +223,7 @@ bool Main::setupStation(uint32_t timeout) {
//-----------------------------------------------------------------------------
void Main::showSetup(void) {
DBGMAIN(F("Main::showSetup"));
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)
@ -241,23 +241,21 @@ void Main::showSetup(void) {
//-----------------------------------------------------------------------------
void Main::showCss(void) {
DBGMAIN(F("Main::showCss"));
DPRINTLN(DBG_VERBOSE, F("Main::showCss"));
mWeb->send(200, "text/css", FPSTR(style_css));
}
//-----------------------------------------------------------------------------
void Main::showSave(void) {
#ifdef DEBUG_HMMAIN
DPRINTLN(F("Main::showSave"));
#endif
DPRINTLN(DBG_VERBOSE, F("Main::showSave"));
saveValues(true);
}
//-----------------------------------------------------------------------------
void Main::saveValues(bool webSend = true) {
DBGMAIN(F("Main::saveValues"));
DPRINTLN(DBG_VERBOSE, F("Main::saveValues"));
if(mWeb->args() > 0) {
if(mWeb->arg("ssid") != "") {
memset(mStationSsid, 0, SSID_LEN);
@ -291,7 +289,7 @@ void Main::saveValues(bool webSend = true) {
//-----------------------------------------------------------------------------
void Main::updateCrc(void) {
DBGMAIN(F("Main::updateCrc"));
DPRINTLN(DBG_VERBOSE, F("Main::updateCrc"));
uint16_t crc;
crc = buildEEpCrc(ADDR_START, ADDR_WIFI_CRC);
//Serial.println("new CRC: " + String(crc, HEX));
@ -302,7 +300,7 @@ void Main::updateCrc(void) {
//-----------------------------------------------------------------------------
void Main::showUptime(void) {
//DBGMAIN(F("Main::showUptime"));
//DPRINTLN(DBG_VERBOSE, F("Main::showUptime"));
char time[20] = {0};
int upTimeSc = uint32_t((mUptimeSecs) % 60);
@ -318,14 +316,14 @@ void Main::showUptime(void) {
//-----------------------------------------------------------------------------
void Main::showTime(void) {
//DBGMAIN(F("Main::showTime"));
//DPRINTLN(DBG_VERBOSE, F("Main::showTime"));
mWeb->send(200, "text/plain", getDateTimeStr(mTimestamp));
}
//-----------------------------------------------------------------------------
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: ");
msg += mWeb->uri();
msg += F("\nMethod: ");
@ -344,7 +342,7 @@ void Main::showNotFound(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>"));
delay(1000);
ESP.restart();
@ -354,7 +352,7 @@ void Main::showReboot(void) {
//-----------------------------------------------------------------------------
void Main::showFactoryRst(void) {
DBGMAIN(F("Main::showFactoryRst"));
DPRINTLN(DBG_VERBOSE, F("Main::showFactoryRst"));
String content = "";
int refresh = 3;
if(mWeb->args() > 0) {
@ -383,7 +381,7 @@ void Main::showFactoryRst(void) {
//-----------------------------------------------------------------------------
time_t Main::getNtpTime(void) {
//DBGMAIN(F("Main::getNtpTime"));
//DPRINTLN(DBG_VERBOSE, F("Main::getNtpTime"));
time_t date = 0;
IPAddress timeServer;
uint8_t buf[NTP_PACKET_SIZE];
@ -421,7 +419,7 @@ time_t Main::getNtpTime(void) {
//-----------------------------------------------------------------------------
void Main::sendNTPpacket(IPAddress& address) {
//DBGMAIN(F("Main::sendNTPpacket"));
//DPRINTLN(DBG_VERBOSE, F("Main::sendNTPpacket"));
uint8_t buf[NTP_PACKET_SIZE] = {0};
buf[0] = B11100011; // LI, Version, Mode
@ -442,7 +440,7 @@ void Main::sendNTPpacket(IPAddress& address) {
//-----------------------------------------------------------------------------
String Main::getDateTimeStr(time_t t) {
//DBGMAIN(F("Main::getDateTimeStr"));
//DPRINTLN(DBG_VERBOSE, F("Main::getDateTimeStr"));
char str[20] = {0};
if(0 == t)
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
// from: https://forum.arduino.cc/index.php?topic=172044.msg1278536#msg1278536
time_t Main::offsetDayLightSaving (uint32_t local_t) {
//DBGMAIN(F("Main::offsetDayLightSaving"));
//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

20
tools/esp8266/main.h

@ -23,11 +23,6 @@
#include "crc.h"
#include "debug.h"
#ifdef DEBUG_MAIN
#define DBGMAIN(f) (DPRINTLN(f))
#else
#define DBGMAIN(f)
#endif
const byte mDnsPort = 53;
@ -51,7 +46,7 @@ class Main {
virtual void updateCrc(void);
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];
uint16_t crc = 0xffff;
uint8_t len;
@ -67,16 +62,17 @@ class Main {
}
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;
crcCheck = buildEEpCrc(start, length);
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);
}
void eraseSettings(bool all = false) {
//DBGMAIN(F("main.h:eraseSettings"));
//DPRINTLN(DBG_VERBOSE, F("main.h:eraseSettings"));
uint8_t buf[64] = {0};
uint16_t addr = (all) ? ADDR_START : ADDR_START_SETTINGS;
uint16_t end;
@ -84,7 +80,7 @@ class Main {
end = addr + 64;
if(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));
addr = end;
} while(addr < (ADDR_SETTINGS_CRC + 2));
@ -92,7 +88,7 @@ class Main {
}
inline bool checkTicker(uint32_t *ticker, uint32_t interval) {
//DBGMAIN(F("c"));
//DPRINTLN(DBG_VERBOSE, F("c"));
uint32_t mil = millis();
if(mil >= *ticker) {
*ticker = mil + interval;
@ -107,7 +103,7 @@ class Main {
}
void stats(void) {
DBGMAIN(F("main.h:stats"));
DPRINTLN(DBG_VERBOSE, F("main.h:stats"));
uint32_t free;
uint16_t max;
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) {
DPRINTLN(F("mqtt.h:setup"));
DPRINTLN(DBG_VERBOSE, F("mqtt.h:setup"));
mAddressSet = true;
mClient->setServer(broker, port);
@ -37,7 +37,7 @@ class mqtt {
}
void sendMsg(const char *topic, const char *msg) {
//DPRINTLN(F("mqtt.h:sendMsg"));
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:sendMsg"));
if(mAddressSet) {
char top[64];
snprintf(top, 64, "%s/%s", mTopic, topic);
@ -50,24 +50,24 @@ class mqtt {
}
bool isConnected(bool doRecon = false) {
//DPRINTLN(F("mqtt.h:isConnected"));
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:isConnected"));
if(doRecon)
reconnect();
return mClient->connected();
}
char *getUser(void) {
//DPRINTLN(F("mqtt.h:getUser"));
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getUser"));
return mUser;
}
char *getPwd(void) {
//DPRINTLN(F("mqtt.h:getPwd"));
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getPwd"));
return mPwd;
}
char *getTopic(void) {
//DPRINTLN(F("mqtt.h:getTopic"));
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:getTopic"));
return mTopic;
}
@ -84,7 +84,7 @@ class mqtt {
private:
void reconnect(void) {
//DPRINTLN(F("mqtt.h:reconnect"));
//DPRINTLN(DBG_VERBOSE, F("mqtt.h:reconnect"));
if(!mClient->connected()) {
if((strlen(mUser) > 0) && (strlen(mPwd) > 0))
mClient->connect(DEF_DEVICE_NAME, mUser, mPwd);

Loading…
Cancel
Save