Browse Source

first steps MQTT

pull/1551/head
DanielR92 6 months ago
parent
commit
6ab3c6fb5e
  1. 95
      patches/RF24_Hal.patch
  2. 2
      src/config/settings.h
  3. 16
      src/plugins/zeroExport/powermeter.h
  4. 32
      src/plugins/zeroExport/zeroExport.h
  5. 1
      src/publisher/pubMqtt.h

95
patches/RF24_Hal.patch

@ -1,5 +1,5 @@
diff --git a/RF24.cpp b/RF24.cpp diff --git a/RF24.cpp b/RF24.cpp
index 9e5b4a8..af00758 100644 index 2e500b6..af00758 100644
--- a/RF24.cpp --- a/RF24.cpp
+++ b/RF24.cpp +++ b/RF24.cpp
@@ -12,228 +12,24 @@ @@ -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(_SPI* spiBus, rf24_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin)
+bool RF24::begin(RF24_hal* _hal) -{
{
- ce_pin = _cepin; - ce_pin = _cepin;
- csn_pin = _cspin; - csn_pin = _cspin;
- return begin(spiBus); - 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_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin)
-{ +bool RF24::begin(RF24_hal* _hal)
{
- ce_pin = _cepin; - ce_pin = _cepin;
- csn_pin = _cspin; - csn_pin = _cspin;
+ hal = _hal; + hal = _hal;
@ -670,17 +670,12 @@ index 9e5b4a8..af00758 100644
bool RF24::_init_pins() bool RF24::_init_pins()
{ {
if (!isValid()) { if (!isValid()) {
@@ -1028,46 +527,7 @@ bool RF24::_init_pins() @@ -1028,41 +527,7 @@ bool RF24::_init_pins()
return false; return false;
} }
-#if defined(RF24_LINUX) -#if defined(RF24_LINUX)
- -
- #if defined(MRAA)
- GPIO();
- gpio.begin(ce_pin, csn_pin);
- #endif
-
- pinMode(ce_pin, OUTPUT); - pinMode(ce_pin, OUTPUT);
- ce(LOW); - ce(LOW);
- delay(100); - 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() 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) 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; return read_register(OBSERVE_TX) & 0x0F;
} }
@ -758,7 +753,7 @@ index 9e5b4a8..af00758 100644
bool RF24::setDataRate(rf24_datarate_e speed) bool RF24::setDataRate(rf24_datarate_e speed)
diff --git a/RF24.h b/RF24.h diff --git a/RF24.h b/RF24.h
index dbd32ae..74ae35d 100644 index c029c8e..c9d612a 100644
--- a/RF24.h --- a/RF24.h
+++ b/RF24.h +++ b/RF24.h
@@ -16,12 +16,7 @@ @@ -16,12 +16,7 @@
@ -775,7 +770,7 @@ index dbd32ae..74ae35d 100644
/** /**
* @defgroup PALevel Power Amplifier level * @defgroup PALevel Power Amplifier level
@@ -115,29 +110,8 @@ typedef enum @@ -115,26 +110,8 @@ typedef enum
class RF24 class RF24
{ {
private: private:
@ -784,18 +779,15 @@ index dbd32ae..74ae35d 100644
-#elif defined(SPI_UART) -#elif defined(SPI_UART)
- SPIUARTClass uspi; - SPIUARTClass uspi;
-#endif -#endif
- + RF24_hal *hal;
-#if defined(RF24_LINUX) || defined(XMEGA_D3) /* XMEGA can use SPI class */ -#if defined(RF24_LINUX) || defined(XMEGA_D3) /* XMEGA can use SPI class */
- SPI spi; - SPI spi;
-#endif // defined (RF24_LINUX) || defined (XMEGA_D3) -#endif // defined (RF24_LINUX) || defined (XMEGA_D3)
-#if defined(RF24_SPI_PTR) -#if defined(RF24_SPI_PTR)
- _SPI* _spi; - _SPI* _spi;
-#endif // defined (RF24_SPI_PTR) -#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 ce_pin; /* "Chip Enable" pin, activates the RX or TX role */
- rf24_gpio_pin_t csn_pin; /* SPI Chip select */ - rf24_gpio_pin_t csn_pin; /* SPI Chip select */
- uint32_t spi_speed; /* SPI Bus Speed */ - 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 status; /* The status byte returned from every SPI transaction */
uint8_t payload_size; /* Fixed size of payloads */ uint8_t payload_size; /* Fixed size of payloads */
uint8_t pipe0_reading_address[5]; /* Last address set on pipe 0 for reading. */ 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. */ bool _is_p0_rx; /* For keeping track of pipe 0's usage in user-triggered RX mode. */
protected: protected:
@ -823,7 +815,7 @@ index dbd32ae..74ae35d 100644
/** Whether ack payloads are enabled. */ /** Whether ack payloads are enabled. */
bool ack_payloads_enabled; bool ack_payloads_enabled;
/** The address width to use (3, 4 or 5 bytes). */ /** 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 * See [Related Pages](pages.html) for device specific information
* *
@ -858,7 +850,7 @@ index dbd32ae..74ae35d 100644
#if defined(RF24_LINUX) #if defined(RF24_LINUX)
virtual ~RF24() {}; virtual ~RF24() {};
@@ -243,58 +192,16 @@ public: @@ -240,58 +192,16 @@ public:
*/ */
bool begin(void); bool begin(void);
@ -869,15 +861,16 @@ index dbd32ae..74ae35d 100644
- * @note This function assumes the `SPI::begin()` method was called before to - * @note This function assumes the `SPI::begin()` method was called before to
- * calling this function. - * 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. - * @param spiBus A pointer or reference to an instantiated SPI bus object.
- * The `_SPI` datatype is a "wrapped" definition that will represent - * The `_SPI` datatype is a "wrapped" definition that will represent
- * various SPI implementations based on the specified platform. - * various SPI implementations based on the specified platform.
- * @see Review the [Arduino support page](md_docs_arduino.html). - * @see Review the [Arduino support page](md_docs_arduino.html).
- * + * @param _hal A pointer to the device specific hardware abstraction layer
- * @return same result as begin() *
- */ * @return same result as begin()
*/
- bool begin(_SPI* spiBus); - bool begin(_SPI* spiBus);
- -
- /** - /**
@ -887,8 +880,8 @@ index dbd32ae..74ae35d 100644
- * @note This function assumes the `SPI::begin()` method was called before to - * @note This function assumes the `SPI::begin()` method was called before to
- * calling this function. - * 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. - * @param spiBus A pointer or reference to an instantiated SPI bus object.
- * The `_SPI` datatype is a "wrapped" definition that will represent - * The `_SPI` datatype is a "wrapped" definition that will represent
- * various SPI implementations based on the specified platform. - * 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. - * @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) - * - 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. - * 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). - * @see Review the [Arduino support page](md_docs_arduino.html).
- * - *
- * @return same result as begin() - * @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. - * @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) - * - 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. - * 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_gpio_pin_t _cepin, rf24_gpio_pin_t _cspin);
+ bool begin(RF24_hal* _hal); + bool begin(RF24_hal* _hal);
/** /**
* Checks if the chip is connected to the SPI bus * 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. * This function uses much less ram than other `*print*Details()` methods.
* *
* @code * @code
@ -934,17 +926,7 @@ index dbd32ae..74ae35d 100644
* cause undefined behavior. * cause undefined behavior.
* *
* Registers names and/or data corresponding to the index of the `encoded_details` array: * Registers names and/or data corresponding to the index of the `encoded_details` array:
@@ -704,9 +611,6 @@ public: @@ -1641,6 +1551,7 @@ 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:
* @return Returns values from 0 to 15. * @return Returns values from 0 to 15.
*/ */
uint8_t getARC(void); uint8_t getARC(void);
@ -952,7 +934,7 @@ index dbd32ae..74ae35d 100644
/** /**
* Set the transmission @ref Datarate * Set the transmission @ref Datarate
@@ -1896,18 +1801,6 @@ private: @@ -1893,17 +1804,6 @@ private:
*/ */
bool _init_pins(); 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 - * @param mode HIGH to take this unit off the SPI bus, LOW to put it on
- */ - */
- void csn(bool mode); - void csn(bool mode);
-
/** /**
* Set chip enable * Set chip enable
* @@ -2412,4 +2312,4 @@ private:
diff --git a/RF24_hal.cpp b/RF24_hal.cpp * Use `ctrl+c` to quit at any time.
new file mode 100644 */
index 0000000..3cc78e4
--- /dev/null -#endif // __RF24_H__
+++ b/RF24_hal.cpp \ No newline at end of file
@@ -0,0 +1 @@ +#endif // __RF24_H__
+#include "RF24_hal.h"
diff --git a/RF24_hal.h b/RF24_hal.h diff --git a/RF24_hal.h b/RF24_hal.h
new file mode 100644 new file mode 100644
index 0000000..baceab3 index 0000000..baceab3

2
src/config/settings.h

@ -350,8 +350,6 @@ typedef struct {
bool debug; bool debug;
zeroExportGroup_t groups[ZEROEXPORT_MAX_GROUPS]; zeroExportGroup_t groups[ZEROEXPORT_MAX_GROUPS];
// uint8_t query_device; // 0 - Tibber, 1 - Shelly, 2 - other (rs232?) // uint8_t query_device; // 0 - Tibber, 1 - Shelly, 2 - other (rs232?)
// char monitor_url[ZEXPORT_ADDR_LEN]; // char monitor_url[ZEXPORT_ADDR_LEN];
// char json_path[ZEXPORT_ADDR_LEN]; // char json_path[ZEXPORT_ADDR_LEN];

16
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 07 01 00 02 08 02 ff #objName: OBIS-Kennzahl für Wirkenergie Einspeisung Tarif2
*/ */
unsigned long previousMillis = 0;
const std::list<OBISHandler> smlHandlerList{ const std::list<OBISHandler> smlHandlerList{
{{0x01, 0x00, 0x10, 0x07, 0x00, 0xff}, &smlOBISW, &_powerMeterTotal}, // total - OBIS-Kennzahl für momentane Gesamtwirkleistung {{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 getPowermeterWattsTibber(JsonObject logObj, uint8_t group) {
bool result = false; bool result = false;
if(millis() - previousMillis <= 3000) return false; // skip when it is to fast
mCfg->groups[group].pmPower = 0; mCfg->groups[group].pmPower = 0;
mCfg->groups[group].pmPowerL1 = 0; mCfg->groups[group].pmPowerL1 = 0;
mCfg->groups[group].pmPowerL2 = 0; mCfg->groups[group].pmPowerL2 = 0;
@ -449,7 +453,7 @@ class powermeter {
http.begin(url); http.begin(url);
http.addHeader("Authorization", "Basic " + auth); 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(); String myString = http.getString();
char floatBuffer[20]; char floatBuffer[20];
@ -486,17 +490,8 @@ class powermeter {
} }
} }
break; break;
default:
logObj["SML_DEFAULT"] = String(smlCurrentState);
break;
}
} }
} }
else
{
logObj["result"] = String(result);
logObj["http_size"] = String(http.getSize());
} }
http.end(); http.end();
@ -506,6 +501,7 @@ class powermeter {
logObj["P2"] = mCfg->groups[group].pmPowerL2; logObj["P2"] = mCfg->groups[group].pmPowerL2;
logObj["P3"] = mCfg->groups[group].pmPowerL3; logObj["P3"] = mCfg->groups[group].pmPowerL3;
previousMillis = millis();
return result; return result;
} }

32
src/plugins/zeroExport/zeroExport.h

@ -299,17 +299,26 @@ class ZeroExport {
* Subscribe section * Subscribe section
*/ */
void onMqttMessage(JsonObject obj) { void onMqttMessage(JsonObject obj) {
if ((!mIsInitialized) || (!mCfg->enabled)) { if ((!mIsInitialized) || (!mCfg->enabled)) return;
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 (obj["path"] == "zero" && obj["cmd"] == "set")
if (strcmp(obj["cmd"], "set") != 0 && strcmp(obj["path"], "zero") != 0)
{ {
// "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"]; mCfg->enabled = (bool)obj["val"];
mLog["zero_enable"] = mCfg->enabled;
} }
mLog["MQTT"] = obj; mLog["Msg"] = obj;
sendLog(); sendLog();
} }
@ -457,14 +466,22 @@ class ZeroExport {
doLog = true; doLog = true;
String gr;
// Init // Init
if (!mIsSubscribed) { if (!mIsSubscribed) {
mIsSubscribed = true; mIsSubscribed = true;
mMqtt->publish("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false); mMqtt->publish("zero/set/enabled", ((mCfg->enabled) ? dict[STR_TRUE] : dict[STR_FALSE]), false);
mMqtt->subscribe("zero/set/enabled", QOS_2); 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); 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) { // if (mCfg->groups[group].publishPower) {
// mCfg->groups[group].publishPower = false; // mCfg->groups[group].publishPower = false;
@ -472,6 +489,7 @@ class ZeroExport {
obj["L2"] = mCfg->groups[group].pmPowerL2; obj["L2"] = mCfg->groups[group].pmPowerL2;
obj["L3"] = mCfg->groups[group].pmPowerL3; obj["L3"] = mCfg->groups[group].pmPowerL3;
obj["Sum"] = mCfg->groups[group].pmPower; obj["Sum"] = mCfg->groups[group].pmPower;
mMqtt->publish("zero/state/powermeter/P", doc.as<std::string>().c_str(), false); mMqtt->publish("zero/state/powermeter/P", doc.as<std::string>().c_str(), false);
doc.clear(); doc.clear();
// } // }
@ -481,7 +499,7 @@ class ZeroExport {
// obj["todo"] = "true"; // obj["todo"] = "true";
// obj["L1"] = mCfg->groups[group].pm_P1; // obj["L1"] = mCfg->groups[group].pm_P1;
// obj["L2"] = mCfg->groups[group].pm_P2; // 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; // obj["Sum"] = mCfg->groups[group].pm_P;
// mMqtt->publish("zero/powermeter/W", doc.as<std::string>().c_str(), false); // mMqtt->publish("zero/powermeter/W", doc.as<std::string>().c_str(), false);
// doc.clear(); // doc.clear();

1
src/publisher/pubMqtt.h

@ -311,6 +311,7 @@ class PubMqtt {
DynamicJsonDocument json(128); DynamicJsonDocument json(128);
JsonObject root = json.to<JsonObject>(); JsonObject root = json.to<JsonObject>();
root["topic"] = String(topic);
bool limitAbs = false; bool limitAbs = false;
if(len > 0) { if(len > 0) {

Loading…
Cancel
Save