From 6ab3c6fb5ed596d6816b12133eac082155e2ca03 Mon Sep 17 00:00:00 2001 From: DanielR92 Date: Sat, 30 Mar 2024 10:54:51 +0100 Subject: [PATCH] first steps MQTT --- patches/RF24_Hal.patch | 95 ++++++++++++----------------- src/config/settings.h | 2 - src/plugins/zeroExport/powermeter.h | 16 ++--- src/plugins/zeroExport/zeroExport.h | 32 +++++++--- src/publisher/pubMqtt.h | 1 + 5 files changed, 70 insertions(+), 76 deletions(-) diff --git a/patches/RF24_Hal.patch b/patches/RF24_Hal.patch index 88a53bf9..ed2c5bec 100644 --- a/patches/RF24_Hal.patch +++ b/patches/RF24_Hal.patch @@ -1,5 +1,5 @@ diff --git a/RF24.cpp b/RF24.cpp -index 9e5b4a8..af00758 100644 +index 2e500b6..af00758 100644 --- a/RF24.cpp +++ b/RF24.cpp @@ -12,228 +12,24 @@ @@ -605,8 +605,7 @@ index 9e5b4a8..af00758 100644 /****************************************************************************/ -bool RF24::begin(_SPI* spiBus, rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin) -+bool RF24::begin(RF24_hal* _hal) - { +-{ - ce_pin = _cepin; - csn_pin = _cspin; - return begin(spiBus); @@ -617,7 +616,8 @@ index 9e5b4a8..af00758 100644 -/****************************************************************************/ - -bool RF24::begin(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin) --{ ++bool RF24::begin(RF24_hal* _hal) + { - ce_pin = _cepin; - csn_pin = _cspin; + hal = _hal; @@ -670,17 +670,12 @@ index 9e5b4a8..af00758 100644 bool RF24::_init_pins() { if (!isValid()) { -@@ -1028,46 +527,7 @@ bool RF24::_init_pins() +@@ -1028,41 +527,7 @@ bool RF24::_init_pins() return false; } -#if defined(RF24_LINUX) - -- #if defined(MRAA) -- GPIO(); -- gpio.begin(ce_pin, csn_pin); -- #endif -- - pinMode(ce_pin, OUTPUT); - ce(LOW); - delay(100); @@ -718,7 +713,7 @@ index 9e5b4a8..af00758 100644 } /****************************************************************************/ -@@ -1151,7 +611,7 @@ bool RF24::isChipConnected() +@@ -1146,7 +611,7 @@ bool RF24::isChipConnected() bool RF24::isValid() { @@ -727,7 +722,7 @@ index 9e5b4a8..af00758 100644 } /****************************************************************************/ -@@ -1675,15 +1135,8 @@ void RF24::closeReadingPipe(uint8_t pipe) +@@ -1670,15 +1135,8 @@ void RF24::closeReadingPipe(uint8_t pipe) void RF24::toggle_features(void) { @@ -745,7 +740,7 @@ index 9e5b4a8..af00758 100644 } /****************************************************************************/ -@@ -1871,6 +1324,11 @@ uint8_t RF24::getARC(void) +@@ -1866,6 +1324,11 @@ uint8_t RF24::getARC(void) return read_register(OBSERVE_TX) & 0x0F; } @@ -758,7 +753,7 @@ index 9e5b4a8..af00758 100644 bool RF24::setDataRate(rf24_datarate_e speed) diff --git a/RF24.h b/RF24.h -index dbd32ae..74ae35d 100644 +index c029c8e..c9d612a 100644 --- a/RF24.h +++ b/RF24.h @@ -16,12 +16,7 @@ @@ -775,7 +770,7 @@ index dbd32ae..74ae35d 100644 /** * @defgroup PALevel Power Amplifier level -@@ -115,29 +110,8 @@ typedef enum +@@ -115,26 +110,8 @@ typedef enum class RF24 { private: @@ -784,18 +779,15 @@ index dbd32ae..74ae35d 100644 -#elif defined(SPI_UART) - SPIUARTClass uspi; -#endif -- ++ RF24_hal *hal; + -#if defined(RF24_LINUX) || defined(XMEGA_D3) /* XMEGA can use SPI class */ - SPI spi; -#endif // defined (RF24_LINUX) || defined (XMEGA_D3) -#if defined(RF24_SPI_PTR) - _SPI* _spi; -#endif // defined (RF24_SPI_PTR) --#if defined(MRAA) -- GPIO gpio; --#endif -+ RF24_hal *hal; - +- - rf24_gpio_pin_t ce_pin; /* "Chip Enable" pin, activates the RX or TX role */ - rf24_gpio_pin_t csn_pin; /* SPI Chip select */ - uint32_t spi_speed; /* SPI Bus Speed */ @@ -806,7 +798,7 @@ index dbd32ae..74ae35d 100644 uint8_t status; /* The status byte returned from every SPI transaction */ uint8_t payload_size; /* Fixed size of payloads */ uint8_t pipe0_reading_address[5]; /* Last address set on pipe 0 for reading. */ -@@ -146,16 +120,6 @@ private: +@@ -143,16 +120,6 @@ private: bool _is_p0_rx; /* For keeping track of pipe 0's usage in user-triggered RX mode. */ protected: @@ -823,7 +815,7 @@ index dbd32ae..74ae35d 100644 /** Whether ack payloads are enabled. */ bool ack_payloads_enabled; /** The address width to use (3, 4 or 5 bytes). */ -@@ -198,30 +162,15 @@ public: +@@ -195,30 +162,15 @@ public: * * See [Related Pages](pages.html) for device specific information * @@ -858,7 +850,7 @@ index dbd32ae..74ae35d 100644 #if defined(RF24_LINUX) virtual ~RF24() {}; -@@ -243,58 +192,16 @@ public: +@@ -240,58 +192,16 @@ public: */ bool begin(void); @@ -869,15 +861,16 @@ index dbd32ae..74ae35d 100644 - * @note This function assumes the `SPI::begin()` method was called before to - * calling this function. - * -- * @warning This function is for the Arduino platforms only -- * + * @warning This function is for the Arduino platforms only + * - * @param spiBus A pointer or reference to an instantiated SPI bus object. - * The `_SPI` datatype is a "wrapped" definition that will represent - * various SPI implementations based on the specified platform. - * @see Review the [Arduino support page](md_docs_arduino.html). -- * -- * @return same result as begin() -- */ ++ * @param _hal A pointer to the device specific hardware abstraction layer + * + * @return same result as begin() + */ - bool begin(_SPI* spiBus); - - /** @@ -887,8 +880,8 @@ index dbd32ae..74ae35d 100644 - * @note This function assumes the `SPI::begin()` method was called before to - * calling this function. - * - * @warning This function is for the Arduino platforms only - * +- * @warning This function is for the Arduino platforms only +- * - * @param spiBus A pointer or reference to an instantiated SPI bus object. - * The `_SPI` datatype is a "wrapped" definition that will represent - * various SPI implementations based on the specified platform. @@ -896,8 +889,7 @@ index dbd32ae..74ae35d 100644 - * @param _cspin The pin attached to Chip Select (often labeled CSN) on the radio module. - * - For the Arduino Due board, the [Arduino Due extended SPI feature](https://www.arduino.cc/en/Reference/DueExtendedSPI) - * is not supported. This means that the Due's pins 4, 10, or 52 are not mandated options (can use any digital output pin) for the radio's CSN pin. -+ * @param _hal A pointer to the device specific hardware abstraction layer - * +- * - * @see Review the [Arduino support page](md_docs_arduino.html). - * - * @return same result as begin() @@ -912,14 +904,14 @@ index dbd32ae..74ae35d 100644 - * @param _cspin The pin attached to Chip Select (often labeled CSN) on the radio module. - * - For the Arduino Due board, the [Arduino Due extended SPI feature](https://www.arduino.cc/en/Reference/DueExtendedSPI) - * is not supported. This means that the Due's pins 4, 10, or 52 are not mandated options (can use any digital output pin) for the radio's CSN pin. - * @return same result as begin() - */ +- * @return same result as begin() +- */ - bool begin(rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin); + bool begin(RF24_hal* _hal); /** * Checks if the chip is connected to the SPI bus -@@ -667,12 +574,12 @@ public: +@@ -664,12 +574,12 @@ public: * This function uses much less ram than other `*print*Details()` methods. * * @code @@ -934,17 +926,7 @@ index dbd32ae..74ae35d 100644 * cause undefined behavior. * * Registers names and/or data corresponding to the index of the `encoded_details` array: -@@ -704,9 +611,6 @@ public: - * | 35 | FIFO_STATUS | - * | 36 | DYNPD | - * | 37 | FEATURE | -- * | 38-39 | ce_pin | -- * | 40-41 | csn_pin | -- * | 42 | SPI speed (in MHz) or'd with (isPlusVariant << 4) | - */ - void encodeRadioDetails(uint8_t* encoded_status); - -@@ -1644,6 +1548,7 @@ public: +@@ -1641,6 +1551,7 @@ public: * @return Returns values from 0 to 15. */ uint8_t getARC(void); @@ -952,7 +934,7 @@ index dbd32ae..74ae35d 100644 /** * Set the transmission @ref Datarate -@@ -1896,18 +1801,6 @@ private: +@@ -1893,17 +1804,6 @@ private: */ bool _init_pins(); @@ -967,17 +949,16 @@ index dbd32ae..74ae35d 100644 - * @param mode HIGH to take this unit off the SPI bus, LOW to put it on - */ - void csn(bool mode); -- + /** * Set chip enable - * -diff --git a/RF24_hal.cpp b/RF24_hal.cpp -new file mode 100644 -index 0000000..3cc78e4 ---- /dev/null -+++ b/RF24_hal.cpp -@@ -0,0 +1 @@ -+#include "RF24_hal.h" +@@ -2412,4 +2312,4 @@ private: + * Use `ctrl+c` to quit at any time. + */ + +-#endif // __RF24_H__ +\ No newline at end of file ++#endif // __RF24_H__ diff --git a/RF24_hal.h b/RF24_hal.h new file mode 100644 index 0000000..baceab3 diff --git a/src/config/settings.h b/src/config/settings.h index 397bd627..d898c4b4 100644 --- a/src/config/settings.h +++ b/src/config/settings.h @@ -350,8 +350,6 @@ typedef struct { bool debug; zeroExportGroup_t groups[ZEROEXPORT_MAX_GROUPS]; - - // uint8_t query_device; // 0 - Tibber, 1 - Shelly, 2 - other (rs232?) // char monitor_url[ZEXPORT_ADDR_LEN]; // char json_path[ZEXPORT_ADDR_LEN]; diff --git a/src/plugins/zeroExport/powermeter.h b/src/plugins/zeroExport/powermeter.h index b9817053..792404eb 100644 --- a/src/plugins/zeroExport/powermeter.h +++ b/src/plugins/zeroExport/powermeter.h @@ -417,6 +417,8 @@ class powermeter { 07 01 00 02 08 02 ff #objName: OBIS-Kennzahl für Wirkenergie Einspeisung Tarif2 */ + unsigned long previousMillis = 0; + const std::list smlHandlerList{ {{0x01, 0x00, 0x10, 0x07, 0x00, 0xff}, &smlOBISW, &_powerMeterTotal}, // total - OBIS-Kennzahl für momentane Gesamtwirkleistung @@ -430,6 +432,8 @@ class powermeter { bool getPowermeterWattsTibber(JsonObject logObj, uint8_t group) { bool result = false; + if(millis() - previousMillis <= 3000) return false; // skip when it is to fast + mCfg->groups[group].pmPower = 0; mCfg->groups[group].pmPowerL1 = 0; mCfg->groups[group].pmPowerL2 = 0; @@ -449,7 +453,7 @@ class powermeter { http.begin(url); http.addHeader("Authorization", "Basic " + auth); - if (http.GET() == HTTP_CODE_OK && http.getSize() != 0) { + if (http.GET() == HTTP_CODE_OK && http.getSize() > 0) { String myString = http.getString(); char floatBuffer[20]; @@ -486,18 +490,9 @@ class powermeter { } } break; - - default: - logObj["SML_DEFAULT"] = String(smlCurrentState); - break; } } } - else - { - logObj["result"] = String(result); - logObj["http_size"] = String(http.getSize()); - } http.end(); @@ -506,6 +501,7 @@ class powermeter { logObj["P2"] = mCfg->groups[group].pmPowerL2; logObj["P3"] = mCfg->groups[group].pmPowerL3; + previousMillis = millis(); return result; } diff --git a/src/plugins/zeroExport/zeroExport.h b/src/plugins/zeroExport/zeroExport.h index 0ada3cbd..01343aef 100644 --- a/src/plugins/zeroExport/zeroExport.h +++ b/src/plugins/zeroExport/zeroExport.h @@ -299,17 +299,26 @@ class ZeroExport { * Subscribe section */ void onMqttMessage(JsonObject obj) { - if ((!mIsInitialized) || (!mCfg->enabled)) { - return; - } + if ((!mIsInitialized) || (!mCfg->enabled)) return; + + String topic = String(obj["topic"]); + if(!topic.indexOf("/zero/set/")) return; + + mLog["t"] = "onMqttMessage"; - // MQTT":{"val":0,"path":"zero","cmd":"set","id":0} - if (strcmp(obj["cmd"], "set") != 0 && strcmp(obj["path"], "zero") != 0) + if (obj["path"] == "zero" && obj["cmd"] == "set") { + // "topic":"inverter/zero/set/groups/0/enabled" + if (topic.indexOf("groups") != -1) { + String i = topic.substring(topic.length() - 10, topic.length() - 8); + uint id = i.toInt(); + } + mCfg->enabled = (bool)obj["val"]; + mLog["zero_enable"] = mCfg->enabled; } - mLog["MQTT"] = obj; + mLog["Msg"] = obj; sendLog(); } @@ -457,14 +466,22 @@ class ZeroExport { doLog = true; + String gr; + // Init if (!mIsSubscribed) { mIsSubscribed = true; mMqtt->publish("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false); mMqtt->subscribe("zero/set/enabled", QOS_2); + + gr = "zero/set/groups/" + String(group) + "/enabled"; + mMqtt->publish(gr.c_str(), ((mCfg->groups[group].enabled) ? dict[STR_TRUE] : dict[STR_FALSE]) , false); + mMqtt->subscribe(gr.c_str(), QOS_2); } mMqtt->publish("zero/state/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false); + gr = "zero/state/groups/" + String(group) + "/enabled"; + mMqtt->publish(gr.c_str(), ((mCfg->groups[group].enabled) ? dict[STR_TRUE] : dict[STR_FALSE]) , false); // if (mCfg->groups[group].publishPower) { // mCfg->groups[group].publishPower = false; @@ -472,6 +489,7 @@ class ZeroExport { obj["L2"] = mCfg->groups[group].pmPowerL2; obj["L3"] = mCfg->groups[group].pmPowerL3; obj["Sum"] = mCfg->groups[group].pmPower; + mMqtt->publish("zero/state/powermeter/P", doc.as().c_str(), false); doc.clear(); // } @@ -481,7 +499,7 @@ class ZeroExport { // obj["todo"] = "true"; // obj["L1"] = mCfg->groups[group].pm_P1; // obj["L2"] = mCfg->groups[group].pm_P2; - // obj["L2"] = mCfg->groups[group].pm_P3; + // obj["L3"] = mCfg->groups[group].pm_P3; // obj["Sum"] = mCfg->groups[group].pm_P; // mMqtt->publish("zero/powermeter/W", doc.as().c_str(), false); // doc.clear(); diff --git a/src/publisher/pubMqtt.h b/src/publisher/pubMqtt.h index 2dbf67ec..2734ff19 100644 --- a/src/publisher/pubMqtt.h +++ b/src/publisher/pubMqtt.h @@ -311,6 +311,7 @@ class PubMqtt { DynamicJsonDocument json(128); JsonObject root = json.to(); + root["topic"] = String(topic); bool limitAbs = false; if(len > 0) {