support for ESPurna

This commit is contained in:
proddy
2018-06-06 17:56:01 +02:00
parent ee2bec847b
commit adce66e4bb

View File

@@ -1,4 +1,3 @@
/* /*
* ems.cpp * ems.cpp
* handles all the processing of the EMS messages * handles all the processing of the EMS messages
@@ -6,12 +5,23 @@
*/ */
#include "ems.h" #include "ems.h"
#include "ESPHelper.h"
#include "emsuart.h" #include "emsuart.h"
#include <Arduino.h> #include <Arduino.h>
#include <TimeLib.h> #include <TimeLib.h>
// Check for ESPurna vs ESPHelper (standalone)
#ifdef USE_CUSTOM_H
#include "debug.h"
extern void debugSend(const char * format, ...);
#define myDebug(...) debugSend(__VA_ARGS__)
// #define myDebug(x, ...) DEBUG_MSG(x, ##__VA_ARGS__)
#else
#include <ESPHelper.h>
extern ESPHelper myESP;
#define myDebug(x, ...) myESP.printf(x, ##__VA_ARGS__)
#endif
_EMS_Sys_Status EMS_Sys_Status; // EMS Status _EMS_Sys_Status EMS_Sys_Status; // EMS Status
_EMS_TxTelegram EMS_TxTelegram; // Empty buffer for sending telegrams _EMS_TxTelegram EMS_TxTelegram; // Empty buffer for sending telegrams
@@ -40,8 +50,7 @@ const _EMS_Types EMS_Types[MAX_TYPECALLBACK] =
{EMS_ID_THERMOSTAT, EMS_TYPE_RC20StatusMessage, "RC20StatusMessage", _process_RC20StatusMessage}, {EMS_ID_THERMOSTAT, EMS_TYPE_RC20StatusMessage, "RC20StatusMessage", _process_RC20StatusMessage},
{EMS_ID_THERMOSTAT, EMS_TYPE_RC20Time, "RC20Time", _process_RC20Time}, {EMS_ID_THERMOSTAT, EMS_TYPE_RC20Time, "RC20Time", _process_RC20Time},
{EMS_ID_THERMOSTAT, EMS_TYPE_RC20Temperature, "RC20Temperature", _process_RC20Temperature}, {EMS_ID_THERMOSTAT, EMS_TYPE_RC20Temperature, "RC20Temperature", _process_RC20Temperature},
{EMS_ID_THERMOSTAT, EMS_TYPE_Version, "Version", _process_Version} {EMS_ID_THERMOSTAT, EMS_TYPE_Version, "Version", _process_Version}};
};
// reserve space for the data we collect from the Boiler and Thermostat // reserve space for the data we collect from the Boiler and Thermostat
_EMS_Boiler EMS_Boiler; _EMS_Boiler EMS_Boiler;
@@ -62,11 +71,7 @@ const uint8_t ems_crc_table[] =
0x65, 0x67, 0x99, 0x9B, 0x9D, 0x9F, 0x91, 0x93, 0x95, 0x97, 0x89, 0x8B, 0x8D, 0x8F, 0x81, 0x83, 0x85, 0x87, 0xB9, 0x65, 0x67, 0x99, 0x9B, 0x9D, 0x9F, 0x91, 0x93, 0x95, 0x97, 0x89, 0x8B, 0x8D, 0x8F, 0x81, 0x83, 0x85, 0x87, 0xB9,
0xBB, 0xBD, 0xBF, 0xB1, 0xB3, 0xB5, 0xB7, 0xA9, 0xAB, 0xAD, 0xAF, 0xA1, 0xA3, 0xA5, 0xA7, 0xD9, 0xDB, 0xDD, 0xDF, 0xBB, 0xBD, 0xBF, 0xB1, 0xB3, 0xB5, 0xB7, 0xA9, 0xAB, 0xAD, 0xAF, 0xA1, 0xA3, 0xA5, 0xA7, 0xD9, 0xDB, 0xDD, 0xDF,
0xD1, 0xD3, 0xD5, 0xD7, 0xC9, 0xCB, 0xCD, 0xCF, 0xC1, 0xC3, 0xC5, 0xC7, 0xF9, 0xFB, 0xFD, 0xFF, 0xF1, 0xF3, 0xF5, 0xD1, 0xD3, 0xD5, 0xD7, 0xC9, 0xCB, 0xCD, 0xCF, 0xC1, 0xC3, 0xC5, 0xC7, 0xF9, 0xFB, 0xFD, 0xFF, 0xF1, 0xF3, 0xF5,
0xF7, 0xE9, 0xEB, 0xED, 0xEF, 0xE1, 0xE3, 0xE5, 0xE7 0xF7, 0xE9, 0xEB, 0xED, 0xEF, 0xE1, 0xE3, 0xE5, 0xE7};
};
extern ESPHelper myESP; // needed for the DEBUG statements below
#define myDebug(x, ...) myESP.printf(x, ##__VA_ARGS__);
// constants timers // constants timers
const uint64_t RX_READ_TIMEOUT = 5000; // in ms. 5 seconds timeout for read replies const uint64_t RX_READ_TIMEOUT = 5000; // in ms. 5 seconds timeout for read replies
@@ -90,7 +95,7 @@ void ems_init() {
EMS_Sys_Status.emsPollEnabled = false; // start up with Poll disabled EMS_Sys_Status.emsPollEnabled = false; // start up with Poll disabled
EMS_Sys_Status.emsThermostatEnabled = true; // there is a RCxx thermostat active as default EMS_Sys_Status.emsThermostatEnabled = true; // there is a RCxx thermostat active as default
EMS_Sys_Status.emsLogVerbose = false; // Verbose logging is off EMS_Sys_Status.emsLogging = EMS_SYS_LOGGING_NONE; // Verbose logging is off
EMS_Thermostat.hour = 0; EMS_Thermostat.hour = 0;
EMS_Thermostat.minute = 0; EMS_Thermostat.minute = 0;
@@ -155,7 +160,7 @@ void _initTxBuffer() {
// Getters and Setters for parameters // Getters and Setters for parameters
void ems_setPoll(bool b) { void ems_setPoll(bool b) {
EMS_Sys_Status.emsPollEnabled = b; EMS_Sys_Status.emsPollEnabled = b;
myDebug("EMS Bus Poll is %s\n", EMS_Sys_Status.emsPollEnabled ? "enabled" : "disabled"); myDebug("EMS Bus Poll is set to %s\n", EMS_Sys_Status.emsPollEnabled ? "enabled" : "disabled");
} }
bool ems_getPoll() { bool ems_getPoll() {
@@ -168,16 +173,18 @@ bool ems_getThermostatEnabled() {
void ems_setThermostatEnabled(bool b) { void ems_setThermostatEnabled(bool b) {
EMS_Sys_Status.emsThermostatEnabled = b; EMS_Sys_Status.emsThermostatEnabled = b;
myDebug("Thermostat is %s\n", EMS_Sys_Status.emsThermostatEnabled ? "enabled" : "disabled"); myDebug("Thermostat is set to %s\n", EMS_Sys_Status.emsThermostatEnabled ? "enabled" : "disabled");
} }
bool ems_getLogVerbose() { _EMS_SYS_LOGGING ems_getLogging() {
return EMS_Sys_Status.emsLogVerbose; return EMS_Sys_Status.emsLogging;
} }
void ems_setLogVerbose(bool b) { void ems_setLogging(_EMS_SYS_LOGGING loglevel) {
EMS_Sys_Status.emsLogVerbose = b; if (loglevel <= 2) {
myDebug("Verbose logging is %s.\n", EMS_Sys_Status.emsLogVerbose ? "on" : "off"); EMS_Sys_Status.emsLogging = loglevel;
myDebug("Logging is set to %d\n", EMS_Sys_Status.emsLogging);
}
} }
/* /*
@@ -199,12 +206,12 @@ uint8_t _crcCalculator(uint8_t * data, uint8_t len) {
// debug print a telegram to telnet console // debug print a telegram to telnet console
// len is length in bytes including the CRC // len is length in bytes including the CRC
void _debugPrintTelegram(const char * prefix, uint8_t * data, uint8_t len, const char * color) { void _debugPrintTelegram(const char * prefix, uint8_t * data, uint8_t len, const char * color) {
if (!EMS_Sys_Status.emsLogVerbose) if (EMS_Sys_Status.emsLogging != EMS_SYS_LOGGING_VERBOSE)
return; return;
bool crcok = (data[len - 1] == _crcCalculator(data, len)); bool crcok = (data[len - 1] == _crcCalculator(data, len));
if (crcok) { if (crcok) {
myDebug(color) myDebug(color);
} else { } else {
myDebug(COLOR_RED); myDebug(COLOR_RED);
} }
@@ -346,12 +353,6 @@ void _processType(uint8_t * telegram, uint8_t length) {
uint8_t type = telegram[2]; uint8_t type = telegram[2];
uint8_t * data = telegram + 4; // data block starts at position 5 uint8_t * data = telegram + 4; // data block starts at position 5
// for building a debug message
char s[100];
char color_s[20];
char src_s[20];
char dest_s[20];
// scan through known types we understand // scan through known types we understand
// set typeFound if we found a match // set typeFound if we found a match
int i = 0; int i = 0;
@@ -370,6 +371,29 @@ void _processType(uint8_t * telegram, uint8_t length) {
i++; i++;
} }
// did we actually ask for it from an earlier read/write request?
// note when we issue a read command the responder (dest) has to return a telegram back immediately
if (dest == EMS_ID_ME) {
// send Acknowledgement back to free the bus
emsaurt_tx_poll();
if ((EMS_TxTelegram.action == EMS_TX_READ) && (EMS_TxTelegram.type == type)) {
// yes we were expecting this one one
EMS_Sys_Status.emsRxPgks++; // increment rx counter
EMS_Sys_Status.emsLastRx = millis();
EMS_TxTelegram.action = EMS_TX_NONE;
emsLastRxCount = 0; // reset retry count
}
}
// print debug messages
if (EMS_Sys_Status.emsLogging != EMS_SYS_LOGGING_NONE) {
char color_s[20];
char src_s[20];
char dest_s[20];
char s[100];
// set source string
if (src == EMS_ID_BOILER) { if (src == EMS_ID_BOILER) {
strcpy(src_s, "Boiler"); strcpy(src_s, "Boiler");
} else if (src == EMS_ID_THERMOSTAT) { } else if (src == EMS_ID_THERMOSTAT) {
@@ -378,22 +402,10 @@ void _processType(uint8_t * telegram, uint8_t length) {
strcpy(src_s, "<unknown>"); strcpy(src_s, "<unknown>");
} }
// was it sent specifically to us? // set destination string
if (dest == EMS_ID_ME) { if (dest == EMS_ID_ME) {
strcpy(dest_s, "telegram for us"); strcpy(dest_s, "telegram for us");
strcpy(color_s, COLOR_YELLOW); strcpy(color_s, COLOR_YELLOW);
// did we actually ask for it from an earlier read/write request?
// note when we issue a read command the responder (dest) has to return a telegram back immediately
if ((EMS_TxTelegram.action == EMS_TX_READ) && (EMS_TxTelegram.type == type)) {
// yes we were expecting this one one
EMS_Sys_Status.emsRxPgks++; // increment rx counter
EMS_Sys_Status.emsLastRx = millis();
EMS_TxTelegram.action = EMS_TX_NONE;
emsLastRxCount = 0; // reset retry count
}
// send Acknowledgement back to free the bus
emsaurt_tx_poll();
} else if (dest == EMS_ID_NONE) { } else if (dest == EMS_ID_NONE) {
// it's probably just a broadcast // it's probably just a broadcast
strcpy(dest_s, "broadcast"); strcpy(dest_s, "broadcast");
@@ -404,12 +416,13 @@ void _processType(uint8_t * telegram, uint8_t length) {
strcpy(color_s, COLOR_MAGENTA); strcpy(color_s, COLOR_MAGENTA);
} }
// debug print // and print
sprintf(s, "%s %s, type 0x%02x", src_s, dest_s, type); sprintf(s, "%s %s, type 0x%02x", src_s, dest_s, type);
_debugPrintTelegram(s, telegram, length, color_s); _debugPrintTelegram(s, telegram, length, color_s);
if (typeFound) { if (typeFound) {
myDebug("--> %s(0x%02x) received.\n", EMS_Types[i].typeString, type); myDebug("--> %s(0x%02x) received.\n", EMS_Types[i].typeString, type);
} }
}
// check to see if we're waiting on a specific value, either from a recent read or by chance a broadcast // check to see if we're waiting on a specific value, either from a recent read or by chance a broadcast
// and then do the comparison // and then do the comparison
@@ -511,7 +524,7 @@ bool _process_UBAMonitorFast(uint8_t * data, uint8_t length) {
bool _process_UBAMonitorSlow(uint8_t * data, uint8_t length) { bool _process_UBAMonitorSlow(uint8_t * data, uint8_t length) {
EMS_Boiler.extTemp = _toFloat(0, data); // 0x8000 if not available EMS_Boiler.extTemp = _toFloat(0, data); // 0x8000 if not available
EMS_Boiler.boilTemp = _toFloat(2, data); // 0x8000 if not available EMS_Boiler.boilTemp = _toFloat(2, data); // 0x8000 if not available
EMS_Boiler.pumpMod = data[13]; EMS_Boiler.pumpMod = data[9];
EMS_Boiler.burnStarts = _toLong(10, data); EMS_Boiler.burnStarts = _toLong(10, data);
EMS_Boiler.burnWorkMin = _toLong(13, data); EMS_Boiler.burnWorkMin = _toLong(13, data);
EMS_Boiler.heatWorkMin = _toLong(19, data); EMS_Boiler.heatWorkMin = _toLong(19, data);
@@ -584,12 +597,15 @@ bool _process_RC20Time(uint8_t * data, uint8_t length) {
EMS_Thermostat.year = data[0]; EMS_Thermostat.year = data[0];
// we can optional set the time based on the thermostat's time if we want // we can optional set the time based on the thermostat's time if we want
// commented out because we use NTP to get time
/*
setTime(EMS_Thermostat.hour, setTime(EMS_Thermostat.hour,
EMS_Thermostat.minute, EMS_Thermostat.minute,
EMS_Thermostat.second, EMS_Thermostat.second,
EMS_Thermostat.day, EMS_Thermostat.day,
EMS_Thermostat.month, EMS_Thermostat.month,
EMS_Thermostat.year + 2000); EMS_Thermostat.year + 2000);
*/
return true; return true;
} }
@@ -615,7 +631,6 @@ void _buildTxTelegram(uint8_t data_value) {
EMS_Sys_Status.emsTxStatus = EMS_TX_PENDING; // armed and ready to send EMS_Sys_Status.emsTxStatus = EMS_TX_PENDING; // armed and ready to send
} }
/* /*
* Send a command to Tx to Read from another device * Send a command to Tx to Read from another device
* Read commands when sent must to responded too by the destination (target) immediately * Read commands when sent must to responded too by the destination (target) immediately
@@ -745,7 +760,6 @@ void ems_setWarmWaterActivated(bool activated) {
_buildTxTelegram(EMS_TxTelegram.checkValue); _buildTxTelegram(EMS_TxTelegram.checkValue);
} }
/* /*
* Helper functions for formatting and converting floats * Helper functions for formatting and converting floats
*/ */