Skip to content

Commit

Permalink
* PR #76 updated debug messages: now 5 different levels are available
Browse files Browse the repository at this point in the history
* fixed CRC loop issue
  • Loading branch information
lumapu committed Jun 17, 2022
1 parent 7e2df35 commit 0347a3d
Show file tree
Hide file tree
Showing 10 changed files with 245 additions and 196 deletions.
91 changes: 46 additions & 45 deletions tools/esp8266/app.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -167,45 +167,46 @@ 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();
}

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

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()) {
Expand All @@ -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++;
Expand Down Expand Up @@ -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();
}
Expand All @@ -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;
Expand All @@ -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) + ")");
}
}

Expand All @@ -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();
}
}
Expand All @@ -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;
Expand All @@ -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) {
Expand All @@ -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
}
Expand All @@ -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
Expand All @@ -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++;
Expand All @@ -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);
Expand All @@ -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;
Expand Down Expand Up @@ -585,22 +586,22 @@ 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();
}


//-----------------------------------------------------------------------------
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";
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand All @@ -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 changes: 0 additions & 5 deletions tools/esp8266/app.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Loading

0 comments on commit 0347a3d

Please sign in to comment.