From 2fa34c369fa157a8ff962f4f9123953537ab4692 Mon Sep 17 00:00:00 2001 From: Paul Date: Tue, 18 Feb 2020 16:06:24 +0100 Subject: [PATCH] external sensor MQTT format - #327 --- CHANGELOG.md | 2 ++ src/MyESP.cpp | 48 +++++++++++++++++++++++------------------------- src/MyESP.h | 7 ++++--- src/my_config.h | 5 ++++- 4 files changed, 33 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f9d9b82fa..3467fd981 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - First implementation of writing to Junker Thermostats (thanks @Neonox31) - Added model type (Buderus, Sieger, Junkers, Nefit, Bosch, Worcester) to device names - `boiler wwonetime` command from Telnet +- `set bus_id ` to support multiple EMS-ESP circuits. Default is 0x0B to mimic a service key. ### Fixed - set boiler warm water temp on Junkers/Bosch HT3 @@ -23,6 +24,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - improved MQTT publishing to stop flooding. `publish_time` must be at least 1 (second) +- External sensors (like dallas) are sent as a nested MQTT topic including their unqiue identifier ### Removed - `autodetect scan` diff --git a/src/MyESP.cpp b/src/MyESP.cpp index 8f6ace613..6d560abaf 100644 --- a/src/MyESP.cpp +++ b/src/MyESP.cpp @@ -410,37 +410,30 @@ void MyESP::mqttUnsubscribe(const char * topic) { // Publish using the user's custom retain flag bool MyESP::mqttPublish(const char * topic, const char * payload) { - // use the custom MQTT retain flag return mqttPublish(topic, payload, _mqtt_retain); } // MQTT Publish // returns true if all good bool MyESP::mqttPublish(const char * topic, const char * payload, bool retain) { - if (mqttClient.connected() && (strlen(topic) > 0)) { -#ifdef MYESP_DEBUG - myDebug_P(PSTR("[MQTT] Sending publish to %s with payload %s"), _mqttTopic(topic), payload); -#endif - uint16_t packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, retain, payload); - - if (packet_id) { - _addMQTTLog(topic, payload, MYESP_MQTTLOGTYPE_PUBLISH); // add to the log - return true; - } - - // it failed, try again https://github.com/proddy/EMS-ESP/issues/264 - delay(100); // this is blocking and probably not a good idea - packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, retain, payload); - if (packet_id) { - _addMQTTLog(topic, payload, MYESP_MQTTLOGTYPE_PUBLISH); // add to the log - return true; // ok this time - } - - // it didn't work again, will return false - myDebug_P(PSTR("[MQTT] Error publishing to %s with payload %s [error %d]"), _mqttTopic(topic), payload, packet_id); - _mqtt_publish_fails++; // increment failure counter + if (!mqttClient.connected() || !_hasValue(topic) || !_hasValue(payload)) { + return false; } +#ifdef MYESP_DEBUG + myDebug_P(PSTR("[MQTT] Sending publish to %s with payload %s"), _mqttTopic(topic), payload); +#endif + uint16_t packet_id = mqttClient.publish(_mqttTopic(topic), _mqtt_qos, retain, payload); + + if (packet_id) { + _addMQTTLog(topic, payload, MYESP_MQTTLOGTYPE_PUBLISH); // add to the log + return true; + } + + // it failed, we should try again https://github.com/proddy/EMS-ESP/issues/264 + myDebug_P(PSTR("[MQTT] Error publishing to %s with payload %s [error %d]"), _mqttTopic(topic), payload, packet_id); + _mqtt_publish_fails++; // increment failure counter + return false; // failed } @@ -1065,8 +1058,7 @@ void MyESP::_telnetCommand(char * commandLine) { // save everything if ((strcmp(ptrToCommandName, "save") == 0) && (wc == 1)) { - _fs_writeConfig(); - _fs_createCustomConfig(); + saveSettings(); return; } @@ -1098,6 +1090,12 @@ void MyESP::_telnetCommand(char * commandLine) { } } +// public function so clients can save config +void MyESP::saveSettings() { + _fs_writeConfig(); + _fs_createCustomConfig(); +} + // returns WiFi hostname as a String object String MyESP::_getESPhostname() { String hostname; diff --git a/src/MyESP.h b/src/MyESP.h index 9ed10806d..ce2621d76 100644 --- a/src/MyESP.h +++ b/src/MyESP.h @@ -9,7 +9,7 @@ #ifndef MyESP_h #define MyESP_h -#define MYESP_VERSION "1.2.26" +#define MYESP_VERSION "1.2.27" #include #include @@ -215,9 +215,9 @@ static_assert(sizeof(RtcmemData) <= (RTCMEM_BLOCKS * 4u), "RTCMEM struct is too #define MYESP_HEARTBEAT_INTERVAL 120000 // in milliseconds, how often the MQTT heartbeat is sent (2 mins) typedef struct { - bool set; // is it a set command + bool set; // is it a set command? char key[50]; - char description[100]; + char description[110]; } command_t; typedef enum { MYESP_FSACTION_SET, MYESP_FSACTION_LIST, MYESP_FSACTION_SAVE, MYESP_FSACTION_LOAD } MYESP_FSACTION_t; @@ -302,6 +302,7 @@ class MyESP { // FS void setSettings(fs_loadsave_callback_f loadsave, fs_setlist_callback_f setlist, bool useSerial = true); + void saveSettings(); bool fs_saveConfig(JsonObject root); bool fs_saveCustomConfig(JsonObject root); bool fs_setSettingValue(char ** setting, const char * value, const char * value_default); diff --git a/src/my_config.h b/src/my_config.h index c55414c16..27117b416 100644 --- a/src/my_config.h +++ b/src/my_config.h @@ -82,4 +82,7 @@ #define TOPIC_SHOWER_DURATION "duration" // duration of the last shower // MQTT for External Sensors -#define TOPIC_EXTERNAL_SENSORS "sensors" // for sending sensor values to MQTT +#define TOPIC_EXTERNAL_SENSORS "sensors" // topic for sending sensor values to MQTT +#define PAYLOAD_EXTERNAL_SENSOR_NUM "sensor" // which sensor # +#define PAYLOAD_EXTERNAL_SENSOR_ID "id" +#define PAYLOAD_EXTERNAL_SENSOR_TEMP "temp"