diff --git a/src/devices/boiler.cpp b/src/devices/boiler.cpp index 680ffd317..56e999aaa 100644 --- a/src/devices/boiler.cpp +++ b/src/devices/boiler.cpp @@ -351,7 +351,7 @@ void Boiler::show_values(uuid::console::Shell & shell) { buffer[1] = '\0'; strlcpy(s, buffer, 7); strlcat(s, "x3min", 7); - print_value(shell, 2, F("Warm Water circulation pump freq"), FPSTR(s)); // TODO check + print_value(shell, 2, F("Warm Water circulation pump freq"), s); } print_value(shell, 2, F("Warm Water circulation active"), wWCirc_, nullptr, EMS_VALUE_BOOL); @@ -395,7 +395,7 @@ void Boiler::show_values(uuid::console::Shell & shell) { if (Helpers::hasValue(serviceCode_)) { shell.printfln(F(" System service code: %s (%d)"), serviceCodeChar_, serviceCode_); } else if (serviceCodeChar_[0] != '\0') { - print_value(shell, 2, F("System service code"), FPSTR(serviceCodeChar_)); // TODO check + print_value(shell, 2, F("System service code"), serviceCodeChar_); } // UBAParameters diff --git a/src/devices/thermostat.cpp b/src/devices/thermostat.cpp index b9ab96d8e..be31f29f5 100644 --- a/src/devices/thermostat.cpp +++ b/src/devices/thermostat.cpp @@ -678,11 +678,13 @@ std::shared_ptr Thermostat::heating_circuit(std::sha // create a new heating circuit object heating_circuits_.emplace_back(new HeatingCircuit(hc_num, monitor_typeids[hc_num - 1], set_typeids[hc_num - 1])); + std::sort(heating_circuits_.begin(), heating_circuits_.end()); // sort based on hc number + // set the flag saying we want its data during the next auto fetch toggle_fetch(monitor_typeids[hc_num - 1], true); toggle_fetch(set_typeids[hc_num - 1], true); - return heating_circuits_.back(); + return heating_circuits_.back(); // even after sorting, this should still point back to the newly created HC } // decodes the thermostat mode for the heating circuit based on the thermostat type @@ -881,6 +883,8 @@ void Thermostat::show_values(uuid::console::Shell & shell) { } } + // std::sort(heating_circuits_.begin(), heating_circuits_.end()); // sort based on hc number. This has moved to the heating_circuit() function + for (const auto & hc : heating_circuits_) { shell.printfln(F(" Heating Circuit %d:"), hc->hc_num()); @@ -1033,17 +1037,35 @@ void Thermostat::process_JunkersMonitor(std::shared_ptr telegram // type 0x02A5 - data from the Nefit RC1010/3000 thermostat (0x18) and RC300/310s on 0x10 void Thermostat::process_RC300Monitor(std::shared_ptr telegram) { + // can't remember why this is here? if (telegram->message_data[2] == 0x00) { return; } + std::shared_ptr hc = heating_circuit(telegram); - telegram->read_value(hc->curr_roomTemp, 0); // is * 10 + + // if current room temp starts with 0x90 it's usually trouble + if (telegram->message_data[0] != 0x90) { + telegram->read_value(hc->curr_roomTemp, 0); // is * 10 + } + telegram->read_value(hc->mode_type, 10, 1); telegram->read_value(hc->mode, 10, 0); // bit 1, mode (auto=1 or manual=0) - // setpoint is in offset 3 and also 7. We're sticking to 3 for now. - // also ignore if its 0 - see https://github.com/proddy/EMS-ESP/issues/256#issuecomment-585171426 - telegram->read_value8(hc->setpoint_roomTemp, 3); // is * 2, force as single byte + // if manual, take the current setpoint temp at pos 6 + // if auto, take the next setpoint temp at pos 7 + // pos 3 is the current target temp and sometimes can be 0 + // see https://github.com/proddy/EMS-ESP/issues/256#issuecomment-585171426 + uint8_t pos; + if (hc->mode == 0) { // manual + pos = 6; + } else if (hc->mode == 1) { // auto + pos = 7; + } else { + pos = 3; + } + + telegram->read_value8(hc->setpoint_roomTemp, pos); // is * 2, force as single byte } // type 0x02B9 EMS+ for reading from RC300/RC310 thermostat diff --git a/src/devices/thermostat.h b/src/devices/thermostat.h index 732632684..1fac23fbd 100644 --- a/src/devices/thermostat.h +++ b/src/devices/thermostat.h @@ -62,7 +62,6 @@ class Thermostat : public EMSdevice { uint8_t designtemp = EMS_VALUE_UINT_NOTSET; // heatingcurve design temp at MinExtTemp int8_t offsettemp = EMS_VALUE_INT_NOTSET; // heatingcurve offest temp at roomtemp signed! - uint8_t hc_num() const { return hc_num_; } @@ -80,13 +79,17 @@ class Thermostat : public EMSdevice { enum Mode : uint8_t { UNKNOWN, OFF, MANUAL, AUTO, DAY, NIGHT, HEAT, NOFROST, ECO, HOLIDAY, COMFORT, OFFSET, DESIGN, SUMMER }; + // for sorting + friend inline bool operator<(const std::shared_ptr & lhs, const std::shared_ptr & rhs) { + return (lhs->hc_num_ < rhs->hc_num_); + } + private: uint8_t hc_num_; uint16_t monitor_typeid_; uint16_t set_typeid_; }; - std::string mode_tostring(uint8_t mode) const; virtual void show_values(uuid::console::Shell & shell); diff --git a/src/emsdevice.h b/src/emsdevice.h index 527d856c2..beb15adab 100644 --- a/src/emsdevice.h +++ b/src/emsdevice.h @@ -170,8 +170,11 @@ class EMSdevice { }; - static constexpr uint16_t EMS_DEVICE_ID_BOILER = 0x08; // fixed device_id for Master Boiler + // device IDs + static constexpr uint16_t EMS_DEVICE_ID_BOILER = 0x08; // fixed device_id for Master Boiler/UBA + static constexpr uint16_t EMS_DEVICE_ID_MODEM = 0x48; // gateways like the KM200 + // type IDs static constexpr uint16_t EMS_TYPE_VERSION = 0x02; // type ID for Version information. Generic across all EMS devices. static constexpr uint16_t EMS_TYPE_UBADevices = 0x07; // EMS connected devices diff --git a/src/emsesp.cpp b/src/emsesp.cpp index 30a67f0a0..9d0814a27 100644 --- a/src/emsesp.cpp +++ b/src/emsesp.cpp @@ -287,7 +287,6 @@ std::string EMSESP::pretty_telegram(std::shared_ptr telegram) { if (offset) { snprintf_P(&str[0], str.capacity() + 1, - // PSTR("%s(0x%02X) -> %s(0x%02X), %s(0x%02X), data: %s (#data=%d)"), PSTR("%s(0x%02X) -> %s(0x%02X), %s(0x%02X), data: %s @offset %d"), src_name.c_str(), src, @@ -300,7 +299,6 @@ std::string EMSESP::pretty_telegram(std::shared_ptr telegram) { } else { snprintf_P(&str[0], str.capacity() + 1, - // PSTR("%s(0x%02X) -> %s(0x%02X), %s(0x%02X), data: %s (#data=%d)"), PSTR("%s(0x%02X) -> %s(0x%02X), %s(0x%02X), data: %s"), src_name.c_str(), src, @@ -394,7 +392,6 @@ void EMSESP::process_version(std::shared_ptr telegram) { // We also check for common telgram types, like the Version(0x02) // returns false if there are none found bool EMSESP::process_telegram(std::shared_ptr telegram) { - // if watching... if (watch() == 1) { if ((watch_id_ == WATCH_NONE) || (telegram->src == watch_id_) || (telegram->dest == watch_id_) || (telegram->type_id == watch_id_)) { @@ -402,8 +399,8 @@ bool EMSESP::process_telegram(std::shared_ptr telegram) { } } - // only process broadcast telegrams or ones sent to us on request - if ((telegram->dest != 0x00) && (telegram->dest != rxservice_.ems_bus_id())) { + // only process broadcast telegrams or ones sent to us on request or ones sent to a modem device (like the KM200) + if ((telegram->dest != 0x00) && (telegram->dest != rxservice_.ems_bus_id()) && (telegram->dest != EMSdevice::EMS_DEVICE_ID_MODEM)) { return false; } @@ -575,7 +572,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { // if we ask ourself at roomcontrol for version e.g. 0B 98 02 00 20 Roomctrl::check((data[1] ^ 0x80 ^ rxservice_.ems_mask()), data); #ifdef EMSESP_DEBUG - // get_uptime is only updated once per loop, does not give the right time + // get_uptime is only updated once per loop, does not give the right time LOG_DEBUG(F("[DEBUG] Echo after %d ms: %s"), ::millis() - tx_time_, Helpers::data_to_hex(data, length).c_str()); #endif return; // it's an echo @@ -639,7 +636,7 @@ void EMSESP::incoming_telegram(uint8_t * data, const uint8_t length) { // if ht3 poll must be ems_bus_id else if Buderus poll must be (ems_bus_id | 0x80) if ((first_value ^ 0x80 ^ rxservice_.ems_mask()) == txservice_.ems_bus_id()) { EMSbus::last_bus_activity(uuid::get_uptime()); // set the flag indication the EMS bus is active - tx_time_ = ::millis(); // get_uptime is only updated once per loop, does not give the right time + tx_time_ = ::millis(); // get_uptime is only updated once per loop, does not give the right time txservice_.send(); } // send remote room temperature if active diff --git a/src/telegram.cpp b/src/telegram.cpp index d3f4a853d..d6d91cb34 100644 --- a/src/telegram.cpp +++ b/src/telegram.cpp @@ -261,40 +261,34 @@ void RxService::add(uint8_t * data, uint8_t length) { } // src, dest and offset are always in fixed positions - uint8_t src = data[0] & 0x7F; // strip MSB, don't care if its read or write for processing + uint8_t src = data[0] & 0x7F; // strip MSB (HT3 adds it) uint8_t dest = data[1] & 0x7F; // strip MSB, don't care if its read or write for processing uint8_t offset = data[3]; // offset is always 4th byte - // set default values, which will be adjusted depending on the EMS1.0/2.0 logic below - uint16_t type_id = 0; - uint8_t * message_data = data; - uint8_t message_length = length; + uint16_t type_id; + uint8_t * message_data; // where the message block starts + uint8_t message_length; // length of the message block, excluding CRC - // work out depending on the type where the data message block starts - if (data[2] < 0xF0 || length < 6) { + // work out depending on the type, where the data message block starts and the message length + if (data[2] < 0xF0) { // EMS 1.0 - type_id = data[2]; - message_data += 4; // message block starts at 5th byte - message_length -= 5; // remove 4 bytes header plus CRC + type_id = data[2]; + message_data = data + 4; + message_length = length - 5; } else { // EMS 2.0 / EMS+ - if (data[2] == 0xFF) { - message_length -= 7; // remove 6 byte header plus CRC - message_data += 6; // message block starts at 7th position - type_id = (data[4] << 8) + data[5] + 256; // set type_id if there is one - } else { - // its F9 or F7 - uint8_t shift = (data[4] != 0xFF) ? 1 : 0; // true (1) if 5th byte is not 0xFF, then telegram is 1 byte longer - type_id = (data[5 + shift] << 8) + data[6 + shift] + 256; - message_data += 6 + shift; // there is a special byte after the typeID which we ignore for now - if (length > (9 + shift)) { - message_length -= (9 + shift); - } + uint8_t shift = 0; // default when data[2] is 0xFF + if (data[2] != 0xFF) { + // its F9 or F7, re-calculate shift. If the 5th byte is not 0xFF then telegram is 1 byte longer + shift = (data[4] != 0xFF) ? 2 : 1; } + type_id = (data[4 + shift] << 8) + data[5 + shift] + 256; + message_data = data + 6 + shift; + message_length = length - 6 - shift; } - // if we don't have a type_id, exit - if (type_id == 0) { + // if we don't have a type_id or empty data block, exit + if ((type_id == 0) || (message_length == 0)) { return; } @@ -456,20 +450,20 @@ void TxService::send_telegram(const QueuedTxTelegram & tx_telegram) { length++; // add one since we want to now include the CRC #if defined(ESP8266) - Settings settings; + Settings settings; if (settings.ems_tx_mode() <= 4) { #endif - // This logging causes errors with timer based tx-modes on esp8266! - LOG_DEBUG(F("Sending %s Tx [#%d], telegram: %s"), - (telegram->operation == Telegram::Operation::TX_WRITE) ? F("write") : F("read"), - tx_telegram.id_, - telegram->to_string(telegram_raw, length).c_str()); + // This logging causes errors with timer based tx-modes on esp8266! + LOG_DEBUG(F("Sending %s Tx [#%d], telegram: %s"), + (telegram->operation == Telegram::Operation::TX_WRITE) ? F("write") : F("read"), + tx_telegram.id_, + telegram->to_string(telegram_raw, length).c_str()); #ifdef EMSESP_DEBUG - // if watching in 'raw' mode - if (EMSESP::watch() == 2) { - LOG_NOTICE(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); - } + // if watching in 'raw' mode + if (EMSESP::watch() == 2) { + LOG_NOTICE(F("[DEBUG] Tx: %s"), Helpers::data_to_hex(telegram_raw, length).c_str()); + } #endif #if defined(ESP8266) } @@ -574,10 +568,11 @@ void TxService::read_request(const uint16_t type_id, const uint8_t dest, const u // Send a raw telegram to the bus, telegram is a text string of hex values void TxService::send_raw(const char * telegram_data) { // since the telegram data is a const, make a copy. add 1 to grab the \0 EOS - char telegram[EMS_MAX_TELEGRAM_LENGTH]; - for (uint8_t i = 0; i < strlen(telegram_data) + 1; i++) { + char telegram[EMS_MAX_TELEGRAM_LENGTH * 3]; + for (uint8_t i = 0; i < strlen(telegram_data); i++) { telegram[i] = telegram_data[i]; } + telegram[strlen(telegram_data)] = '\0'; // make sure its terminated uint8_t count = 0; char * p; @@ -590,6 +585,7 @@ void TxService::send_raw(const char * telegram_data) { strlcpy(value, p, 10); data[0] = (uint8_t)strtol(value, 0, 16); } + // and iterate until end while (p != 0) { if ((p = strtok(nullptr, " ,"))) { diff --git a/src/test/test.cpp b/src/test/test.cpp index eea81961b..c0286eeb9 100644 --- a/src/test/test.cpp +++ b/src/test/test.cpp @@ -170,6 +170,51 @@ static constexpr uint32_t EMS_VALUE_ULONG_INVALID = 0x80000000; EMSESP::show_values(shell); } + if (command == "km") { + shell.printfln(F("Testing KM200 Gateway")); + + EMSESP::rxservice_.ems_mask(EMSbus::EMS_MASK_BUDERUS); + + std::string version("1.2.3"); + EMSESP::add_device(0x10, 158, version, EMSdevice::Brand::BUDERUS); // RC300 + EMSESP::add_device(0x48, 189, version, EMSdevice::Brand::BUDERUS); // KM200 + EMSESP::rxservice_.loop(); + + // see https://github.com/proddy/EMS-ESP/issues/390 + uart_telegram("90 48 FF 00 01 A6 4C"); + uart_telegram("90 48 F9 00 FF 01 B0 08 0B 00 00 00 14 00 00 00 19 00 00 00 4B 00 00"); + uart_telegram("90 48 FF 08 01 A7 6D"); + uart_telegram("90 48 F9 00 FF 01 9C 08 03 00 00 00 1E 00 00 00 4B 00 00 00 55 00 00"); + uart_telegram("90 48 F9 00 FF 01 9C 07 03 00 00 00 1E 00 00 00 30 00 00 00 3C 00 00"); + uart_telegram("90 48 F9 00 FF 01 9D 00 43 00 00 00 01 00 00 00 02 00 03 00 06 00 03 00 02 05"); + uart_telegram("90 48 F9 00 FF 01 9D 07 03 00 00 00 1E 00 00 00 30 00 00 00 3C 00 00 00 30 C4"); + uart_telegram("90 48 F9 00 FF 01 9D 08 03 00 00 00 1E 00 00 00 4B 00 00 00 55 00 00 00 4B C8"); + uart_telegram("90 48 F9 00 FF 01 B1 08 0B 00 00 00 14 00 00 00 19 00 00 00 4B 00 00 00 19 A2"); + uart_telegram("90 48 FF 07 01 A7 51"); + uart_telegram("90 48 FF 08 01 A7 6D"); + uart_telegram("90 48 FF 00 01 A7 4D"); + uart_telegram("90 48 FF 25 01 A6 D8"); + uart_telegram("90 48 FF 07 01 A7 51"); + uart_telegram("90 0B 06 00 14 06 17 08 03 22 00 01 10 FF 00 18"); // time + uart_telegram("90 0B FF 00 01 A5 80 00 01 28 17 00 28 2A 05 A0 02 03 03 05 A0 05 A0 00 00 11 01 02 FF FF 00"); + uart_telegram("90 0B FF 00 01 B9 00 2E 26 26 1B 03 00 FF FF 05 28 01 E1 20 01 0F 05 2A"); + uart_telegram("90 0B FF 00 01 A6 90 0B FF 00 01 A6 18"); + uart_telegram("90 0B FF 00 01 B9 00 2E 26 26 1B 03 00 FF FF 05 28 01 E1 20 01 0F 05 2A"); + uart_telegram("90 0B FF 00 01 A6 90 0B FF 00 01 A6 18"); + uart_telegram("90 0B FF 00 01 BA 00 2E 2A 26 1E 03 00 FF FF 05 2A 01 E1 20 01 0F 05 2A"); + uart_telegram("90 0B FF 00 01 A7 90 0B FF 00 01 A7 19"); + uart_telegram("90 0B FF 00 01 BB 00 2E 2A 26 1E 03 00 FF FF 05 2A 01 E1 20 01 0F 05 2A"); + uart_telegram("90 0B FF 00 01 A8 90 0B FF 00 01 A8 16"); + uart_telegram("90 0B FF 00 01 BC 00 2E 2A 26 1E 03 00 FF FF 05 2A 01 E1 20 01 0F 05 2A"); + uart_telegram("90 0B FF 00 01 A5 80 00 01 28 17 00 28 2A 05 A0 02 03 03 05 A0 05 A0 00 00 11 01 02 FF FF 00"); + + // uart_telegram("10 00 FF 00 01 A5 00 D7 21 00 00 00 00 30 01 84 01 01 03 01 84 01 F1 00 00 11 01 00 08 63 00"); + + EMSESP::show_emsbus(shell); + EMSESP::rxservice_.loop(); + EMSESP::show_values(shell); + } + if (command == "cr100") { shell.printfln(F("Testing CR100")); @@ -386,14 +431,12 @@ static constexpr uint32_t EMS_VALUE_ULONG_INVALID = 0x80000000; } if (command == "rx2") { - // incoming Rx uart_telegram({0x1B, 0x5B, 0xFD, 0x2D, 0x9E, 0x3A, 0xB6, 0xE5, 0x02, 0x20, 0x33, 0x30, 0x32, 0x3A, 0x20, 0x5B, 0x73, 0xFF, 0xFF, 0xCB, 0xDF, 0xB7, 0xA7, 0xB5, 0x67, 0x77, 0x77, 0xE4, 0xFF, 0xFD, 0x77, 0xFF}); } // https://github.com/proddy/EMS-ESP/issues/380#issuecomment-633663007 if (command == "rx3") { - // incoming Rx uart_telegram({0x21, 0x0B, 0xFF, 0x00}); } @@ -460,6 +503,44 @@ void Test::uart_telegram(const std::vector & rx_data) { std::copy(rx_data.begin(), rx_data.end(), data); data[len] = EMSESP::rxservice_.calculate_crc(rx_data.data(), len); EMSESP::incoming_telegram(data, len + 1); + EMSESP::rxservice_.loop(); +} + +// takes raw string +void Test::uart_telegram(const char * rx_data) { + // since the telegram data is a const, make a copy. add 1 to grab the \0 EOS + char telegram[EMS_MAX_TELEGRAM_LENGTH * 3]; + for (uint8_t i = 0; i < strlen(rx_data); i++) { + telegram[i] = rx_data[i]; + } + telegram[strlen(rx_data)] = '\0'; // make sure its terminated + + uint8_t count = 0; + char * p; + char value[10] = {0}; + + uint8_t data[EMS_MAX_TELEGRAM_LENGTH]; + + // get first value, which should be the src + if ((p = strtok(telegram, " ,"))) { // delimiter + strlcpy(value, p, 10); + data[0] = (uint8_t)strtol(value, 0, 16); + } + + // and iterate until end + while (p != 0) { + if ((p = strtok(nullptr, " ,"))) { + strlcpy(value, p, 10); + uint8_t val = (uint8_t)strtol(value, 0, 16); + data[++count] = val; + } + } + + if (count == 0) { + return; // nothing to send + } + + EMSESP::incoming_telegram(data, count + 1); } #pragma GCC diagnostic push diff --git a/src/test/test.h b/src/test/test.h index 86a28f88b..e37cc6229 100644 --- a/src/test/test.h +++ b/src/test/test.h @@ -43,6 +43,7 @@ class Test { static void dummy_mqtt_commands(const char * message); static void rx_telegram(const std::vector & data); static void uart_telegram(const std::vector & rx_data); + static void uart_telegram(const char *rx_data); }; } // namespace emsesp diff --git a/src/version.h b/src/version.h index 8f4a11059..d463861d7 100644 --- a/src/version.h +++ b/src/version.h @@ -1 +1 @@ -#define EMSESP_APP_VERSION "2.0.0a21" +#define EMSESP_APP_VERSION "2.0.0a22"