1
mirror of https://github.com/rvdbreemen/OTGW-firmware synced 2024-09-26 16:19:56 +02:00

Improvement to serial handeling and profiling:

- removing delay from print status and removing profiling code
- change to debug so gmt + micros are used for now
- cleanup of handleotgw
- detecting buffer overrun on serial uart
- adding back the rxbuffer at 512 chars
- bugfix day of week in OpenTherm is different
- bugfix blinking led
- improving ot message logging
This commit is contained in:
Robert van den Breemen 2021-12-30 16:03:11 +01:00
parent 099738e370
commit 22d361e511
8 changed files with 412 additions and 346 deletions

43
Debug.h
View File

@ -33,26 +33,39 @@
// needs #include <TelnetStream.h> // Version 0.0.1 - https://github.com/jandrassy/TelnetStream
#include <time.h>
extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);
//#include <sys/time.h>
// #include <time.h>
// extern "C" int clock_gettime(clockid_t unused, struct timespec *tp);
char _bol[128];
void _debugBOL(const char *fn, int line)
{
//calculate fractional seconds to millis fraction
double fractional_seconds;
int microseconds;
struct timespec tp; //to enable clock_gettime()
clock_gettime(CLOCK_REALTIME, &tp);
fractional_seconds = (double) tp.tv_nsec;
fractional_seconds /= 1e3;
fractional_seconds = round(fractional_seconds);
microseconds = (int) fractional_seconds;
// This commented out code is using mix of system time and acetime to print, but it will not work on microsecond level correctly
// // //calculate fractional seconds to millis fraction
// double fractional_seconds;
// int microseconds;
// struct timespec tp; //to enable clock_gettime()
// clock_gettime(CLOCK_REALTIME, &tp);
// fractional_seconds = (double) tp.tv_nsec;
// fractional_seconds /= 1e3;
// fractional_seconds = round(fractional_seconds);
// microseconds = (int) fractional_seconds;
// snprintf(_bol, sizeof(_bol), "%02d:%02d:%02d.%06d (%7u|%6u) %-12.12s(%4d): ", \
// hour(), minute(), second(), microseconds, \
// ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(),\
// fn, line);
//Alternative based on localtime function
timeval now;
struct tm *tod;
gettimeofday(&now, nullptr);
tod = localtime(&now.tv_sec);
snprintf(_bol, sizeof(_bol), "%02d:%02d:%02d.%06d (%7u|%6u) %-12.12s(%4d): ", \
hour(), minute(), second(), microseconds, \
ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(),\
fn, line);
tod->tm_hour, tod->tm_min, tod->tm_sec, (int)now.tv_usec, \
ESP.getFreeHeap(), ESP.getMaxFreeBlockSize(),\
fn, line);
TelnetStream.print (_bol);
}

View File

@ -177,8 +177,8 @@ void handleMQTTcallback(char* topic, byte* payload, unsigned int length) {
void handleMQTT()
{
if (!settingMQTTenable) return;
DECLARE_TIMER_SEC(timerMQTTwaitforconnect, 42, CATCH_UP_MISSED_TICKS); // retry after 42 seconds
DECLARE_TIMER_SEC(timerMQTTwaitforretry, 3, CATCH_UP_MISSED_TICKS); // 3 seconds backoff
DECLARE_TIMER_SEC(timerMQTTwaitforconnect, 42, CATCH_UP_MISSED_TICKS); // wait before trying to connect again
DECLARE_TIMER_SEC(timerMQTTwaitforretry, 3, CATCH_UP_MISSED_TICKS); // wait for retry
//State debug timers
DECLARE_TIMER_SEC(timerMQTTdebugwaitforreconnect, 13);
@ -186,6 +186,8 @@ void handleMQTT()
DECLARE_TIMER_SEC(timerMQTTdebugwaitconnectionattempt, 1);
DECLARE_TIMER_SEC(timerMQTTdebugisconnected, 60);
if (MQTTclient.connected()) MQTTclient.loop(); //always do a MQTTclient.loop() first
switch(stateMQTT)
{
case MQTT_STATE_INIT:
@ -318,7 +320,7 @@ void handleMQTT()
case MQTT_STATE_ERROR:
if DUE(timerMQTTdebugerrorstate) MQTTDebugTln(F("MQTT State: MQTT ERROR, wait for 10 minutes, before trying again"));
//next retry in 10 minutes.
//wait for next retry
RESTART_TIMER(timerMQTTwaitforconnect);
stateMQTT = MQTT_STATE_WAIT_FOR_RECONNECT;
MQTTDebugTln(F("Next State: MQTT_STATE_WAIT_FOR_RECONNECT"));

View File

@ -436,11 +436,11 @@ enum OpenThermMessageID {
{ 121, OT_RW , ot_u16, "CHPumpOperationHours", "CH pump operation hours", "hrs" },
{ 122, OT_RW , ot_u16, "DHWPumpValveOperationHours", "DHW pump/valve operation hours", "hrs" },
{ 123, OT_RW , ot_u16, "DHWBurnerOperationHours", "DHW burner operation hours", "hrs" },
{ 124, OT_READ , ot_f88, "OpenThermVersionMaster", "Master Version OpenTherm Protocol Specification", "" },
{ 125, OT_READ , ot_f88, "OpenThermVersionSlave", "Slave Version OpenTherm Protocol Specification", "" },
{ 126, OT_READ , ot_u8u8, "MasterVersion", "Master product version number and type", "" },
{ 127, OT_READ , ot_u8u8, "SlaveVersion", "Slave product version number and type", "" },
{ 128, OT_UNDEF , ot_undef, "", "", "" },
{ 124, OT_READ , ot_f88, "OpenThermVersionMaster", "Master Version OpenTherm Protocol Specification", "" },
{ 125, OT_READ , ot_f88, "OpenThermVersionSlave", "Slave Version OpenTherm Protocol Specification", "" },
{ 126, OT_READ , ot_u8u8, "MasterVersion", "Master product version number and type", "" },
{ 127, OT_READ , ot_u8u8, "SlaveVersion", "Slave product version number and type", "" },
{ 128, OT_UNDEF , ot_undef, "", "", "" },
{ 129, OT_UNDEF , ot_undef, "", "", "" },
{ 130, OT_UNDEF , ot_undef, "", "", "" },
{ 131, OT_RW , ot_u8u8, "RemehadFdUcodes", "Remeha dF-/dU-codes", "" },

File diff suppressed because it is too large Load Diff

View File

@ -153,7 +153,8 @@ void sendtimecommand(){
//send time command to OTGW
//send time / weekday
char msg[15]={0};
sprintf(msg,"SC=%d:%02d/%d", hour(), minute(), dayOfWeek(now()));
#define calc_ot_dow(dow) ((dow+5)%7+1)
sprintf(msg,"SC=%d:%02d/%d", hour(), minute(), calc_ot_dow(dayOfWeek(now())));
addOTWGcmdtoqueue(msg, strlen(msg), true);
static int lastDay = 0;
@ -198,7 +199,10 @@ void blinkLED(uint8_t led, int nr, uint32_t waittime_ms){
void blinkLEDnow(uint8_t led = LED1){
pinMode(led, OUTPUT);
digitalWrite(led, !digitalRead(led));
if (settingLEDblink) {
digitalWrite(led, !digitalRead(led));
} else setLed(led, OFF);
}
//===[ no-blocking delay with running background tasks in ms ]===
@ -271,20 +275,20 @@ void doBackgroundTasks()
void loop()
{
DECLARE_TIMER_SEC(timer1s, 1, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer5s, 5, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer1s, 1, SKIP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer5s, 5, SKIP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer30s, 30, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer60s, 60, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_MIN(tmrcheckpic, 1440, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_MIN(timer5min, 5, CATCH_UP_MISSED_TICKS);
if (DUE(timer1s)) doTaskEvery1s();
if (DUE(timer5s)) doTaskEvery5s();
if (DUE(timer30s)) doTaskEvery30s();
if (DUE(timer60s)) doTaskEvery60s();
if (DUE(tmrcheckpic)) docheckforpic();
if (DUE(timer5min)) do5minevent();
if (DUE(timerpollsensor)) pollSensors(); // poll the temperature sensors connected to 2wire gpio pin
if (DUE(timer5min)) do5minevent();
if (DUE(timer60s)) doTaskEvery60s();
if (DUE(timer30s)) doTaskEvery30s();
if (DUE(timer5s)) doTaskEvery5s();
if (DUE(timer1s)) doTaskEvery1s();
if (DUE(tmrcheckpic)) docheckforpic();
evalOutputs(); // when the bits change, the output gpio bit will follow
doBackgroundTasks();
}

View File

@ -68,6 +68,7 @@ static const char banner[] = "OpenTherm Gateway ";
OTGWSerial::OTGWSerial(int resetPin, int progressLed)
: HardwareSerial(UART0), _reset(resetPin), _led(progressLed) {
HardwareSerial::setRxBufferSize(512);
HardwareSerial::begin(9600, SERIAL_8N1);
// The PIC may have been confused by garbage on the
// serial interface when the NodeMCU resets.
@ -77,6 +78,20 @@ OTGWSerial::OTGWSerial(int resetPin, int progressLed)
_version_pos = 0;
}
bool OTGWSerial::hasOverrun(void)
{
if (upgradeEvent()) return 0;
return HardwareSerial::hasOverrun();
}
bool OTGWSerial::hasRxError(void)
{
if (upgradeEvent()) return 0;
return HardwareSerial::hasRxError();
}
int OTGWSerial::available() {
if (upgradeEvent()) return 0;
return HardwareSerial::available();

View File

@ -69,6 +69,8 @@ class OTGWSerial: public HardwareSerial {
int available();
int read();
int availableForWrite();
bool hasOverrun();
bool hasRxError();
size_t write(uint8_t c);
size_t write(const uint8_t *buffer, size_t len);
size_t write(const char *buffer, size_t len) {

View File

@ -1,16 +1,16 @@
//The version number conforms to semver.org format
#define _VERSION_MAJOR 0
#define _VERSION_MINOR 9
#define _VERSION_PATCH 1
#define _VERSION_BUILD 1428
#define _VERSION_GITHASH "e8556c1"
//#define _VERSION_PRERELEASE beta //uncomment to define prerelease labels: alpha - beta - rc
#define _VERSION_DATE "23-12-2021"
#define _VERSION_TIME "20:20:37"
#define _SEMVER_CORE "0.9.1"
#define _SEMVER_BUILD "0.9.1+1428"
#define _SEMVER_GITHASH "0.9.1+e8556c1"
#define _SEMVER_FULL "0.9.1+e8556c1"
#define _SEMVER_NOBUILD "0.9.1 (23-12-2021)"
#define _VERSION "0.9.1+e8556c1 (23-12-2021)"
#define _VERSION_PATCH 2
#define _VERSION_BUILD 1530
#define _VERSION_GITHASH "05ec175"
#define _VERSION_PRERELEASE beta
#define _VERSION_DATE "30-12-2021"
#define _VERSION_TIME "15:56:37"
#define _SEMVER_CORE "0.9.2"
#define _SEMVER_BUILD "0.9.2+1530"
#define _SEMVER_GITHASH "0.9.2+05ec175"
#define _SEMVER_FULL "0.9.2-beta+05ec175"
#define _SEMVER_NOBUILD "0.9.2-beta (30-12-2021)"
#define _VERSION "0.9.2-beta+05ec175 (30-12-2021)"
//The version information is created automatically, more information here: https://github.com/rvdbreemen/autoinc-semver