Skip to content

Commit f2ab806

Browse files
committed
MQTT discovery improvements
-remove gatewayBT -remove device class formaldehyde (not existing in HASS) -add SYStoMQTT new parameters -attach connectivity parameter to the device -add BT gateway infos and switches -refactor modules storage and publication
1 parent cabc869 commit f2ab806

File tree

3 files changed

+113
-101
lines changed

3 files changed

+113
-101
lines changed

main/ZgatewayBT.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ void FormalDiscovery(char* mac) {
230230
{"sensor", "Formal-batt", mac, "battery", jsonBatt, "", "", "%"},
231231
{"sensor", "Formal-temp", mac, "temperature", jsonTempc, "", "", "°C"},
232232
{"sensor", "Formal-hum", mac, "humidity", jsonHum, "", "", "%"},
233-
{"sensor", "Formal-for", mac, "formaldehyde", jsonFor, "", "", "%"}
233+
{"sensor", "Formal-for", mac, "", jsonFor, "", "", "%"}
234234
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
235235
};
236236

main/ZmqttDiscovery.ino

Lines changed: 78 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,11 @@ void createDiscovery(char* sensor_type,
114114
if (child_device) {
115115
StaticJsonBuffer<JSON_MSG_BUFFER> jsonDeviceBuffer;
116116
JsonObject& device = jsonDeviceBuffer.createObject();
117+
char JSONmessageBuffer[JSON_MSG_BUFFER];
118+
modules.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer));
119+
Log.notice(F("Received json : %s" CR), JSONmessageBuffer);
117120
device.set("name", gateway_name);
121+
device.set("model", JSONmessageBuffer);
118122
device.set("manufacturer", DEVICEMANUFACTURER);
119123
device.set("sw_version", OMG_VERSION);
120124
JsonArray& identifiers = device.createNestedArray("identifiers");
@@ -128,21 +132,63 @@ void createDiscovery(char* sensor_type,
128132
void pubMqttDiscovery() {
129133
Log.trace(F("omgStatusDiscovery" CR));
130134
createDiscovery("binary_sensor", //set Type
131-
will_Topic, Gateway_Name, (char*)getUniqueId("", "").c_str(), //set state_topic,name,uniqueId
135+
will_Topic, "SYS: Connectivity", (char*)getUniqueId("connectivity", "").c_str(), //set state_topic,name,uniqueId
132136
will_Topic, "connectivity", "", //set availability_topic,device_class,value_template,
133137
Gateway_AnnouncementMsg, will_Message, "", //set,payload_on,payload_off,unit_of_meas,
134138
0, //set off_delay
135-
Gateway_AnnouncementMsg, will_Message, false, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
139+
Gateway_AnnouncementMsg, will_Message, true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
136140
);
141+
createDiscovery("sensor", //set Type
142+
subjectSYStoMQTT, "SYS: Uptime", (char*)getUniqueId("uptime", "").c_str(), //set state_topic,name,uniqueId
143+
"", "", "{{ value_json.uptime }}", //set availability_topic,device_class,value_template,
144+
"", "", "s", //set,payload_on,payload_off,unit_of_meas,
145+
0, //set off_delay
146+
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
147+
);
148+
149+
# if defined(ESP8266) || defined(ESP32)
150+
createDiscovery("sensor", //set Type
151+
subjectSYStoMQTT, "SYS: Free memory", (char*)getUniqueId("freemem", "").c_str(), //set state_topic,name,uniqueId
152+
"", "", "{{ value_json.freemem }}", //set availability_topic,device_class,value_template,
153+
"", "", "B", //set,payload_on,payload_off,unit_of_meas,
154+
0, //set off_delay
155+
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
156+
);
157+
createDiscovery("sensor", //set Type
158+
subjectSYStoMQTT, "SYS: IP", (char*)getUniqueId("ip", "").c_str(), //set state_topic,name,uniqueId
159+
"", "", "{{ value_json.ip }}", //set availability_topic,device_class,value_template,
160+
"", "", "", //set,payload_on,payload_off,unit_of_meas,
161+
0, //set off_delay
162+
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
163+
);
164+
# ifndef ESP32_ETHERNET
165+
createDiscovery("sensor", //set Type
166+
subjectSYStoMQTT, "SYS: Rssi", (char*)getUniqueId("rssi", "").c_str(), //set state_topic,name,uniqueId
167+
"", "", "{{ value_json.rssi }}", //set availability_topic,device_class,value_template,
168+
"", "", "dB", //set,payload_on,payload_off,unit_of_meas,
169+
0, //set off_delay
170+
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
171+
);
172+
# endif
173+
# endif
174+
# ifdef ESP32
175+
createDiscovery("sensor", //set Type
176+
subjectSYStoMQTT, "SYS: Low Power Mode", (char*)getUniqueId("lowpowermode", "").c_str(), //set state_topic,name,uniqueId
177+
"", "", "{{ value_json.lowpowermode }}", //set availability_topic,device_class,value_template,
178+
"", "", "", //set,payload_on,payload_off,unit_of_meas,
179+
0, //set off_delay
180+
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
181+
);
182+
# endif
137183
createDiscovery("switch", //set Type
138-
will_Topic, "restart OMG", (char*)getUniqueId("restart", "").c_str(), //set state_topic,name,uniqueId
184+
will_Topic, "SYS: Restart gateway", (char*)getUniqueId("restart", "").c_str(), //set state_topic,name,uniqueId
139185
will_Topic, "", "", //set availability_topic,device_class,value_template,
140186
"{\"cmd\":\"restart\"}", "", "", //set,payload_on,payload_off,unit_of_meas,
141187
0, //set off_delay
142188
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoSYSset //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
143189
);
144190
createDiscovery("switch", //set Type
145-
will_Topic, "erase OMG", (char*)getUniqueId("erase", "").c_str(), //set state_topic,name,uniqueId
191+
will_Topic, "SYS: Erase credentials", (char*)getUniqueId("erase", "").c_str(), //set state_topic,name,uniqueId
146192
will_Topic, "", "", //set availability_topic,device_class,value_template,
147193
"{\"cmd\":\"erase\"}", "", "", //set,payload_on,payload_off,unit_of_meas,
148194
0, //set off_delay
@@ -476,18 +522,34 @@ void pubMqttDiscovery() {
476522
# endif
477523

478524
# ifdef ZgatewayBT
479-
// Sensor to display BT received value
480-
Log.trace(F("gatewayBTDiscovery" CR));
481-
char* gatewayBT[8] = {"sensor", "gatewayBT", "", "", jsonId, "", "", ""};
482-
//component type,name,availability topic,device class,value template,payload on, payload off, unit of measurement
483-
484-
Log.trace(F("CreateDiscoverySensor" CR));
485-
//trc(gatewayBT[1]);
486-
createDiscovery(gatewayBT[0],
487-
subjectBTtoMQTT, gatewayBT[1], (char*)getUniqueId(gatewayBT[1], gatewayBT[2]).c_str(),
488-
will_Topic, gatewayBT[3], gatewayBT[4],
489-
gatewayBT[5], gatewayBT[6], gatewayBT[7],
490-
0, "", "", true, "");
525+
createDiscovery("sensor", //set Type
526+
subjectSYStoMQTT, "BT: Interval between scans", (char*)getUniqueId("interval", "").c_str(), //set state_topic,name,uniqueId
527+
"", "", "{{ value_json.interval }}", //set availability_topic,device_class,value_template,
528+
"", "", "ms", //set,payload_on,payload_off,unit_of_meas,
529+
0, //set off_delay
530+
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
531+
);
532+
createDiscovery("sensor", //set Type
533+
subjectSYStoMQTT, "BT: Connnect every X scan(s)", (char*)getUniqueId("scanbcnct", "").c_str(), //set state_topic,name,uniqueId
534+
"", "", "{{ value_json.scanbcnct }}", //set availability_topic,device_class,value_template,
535+
"", "", "", //set,payload_on,payload_off,unit_of_meas,
536+
0, //set off_delay
537+
"", "", true, "" //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
538+
);
539+
createDiscovery("switch", //set Type
540+
will_Topic, "BT: Force scan", (char*)getUniqueId("force_scan", "").c_str(), //set state_topic,name,uniqueId
541+
will_Topic, "", "", //set availability_topic,device_class,value_template,
542+
"{\"interval\":0}", "", "", //set,payload_on,payload_off,unit_of_meas,
543+
0, //set off_delay
544+
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
545+
);
546+
createDiscovery("switch", //set Type
547+
"", "BT: Publish only sensors", (char*)getUniqueId("only_sensors", "").c_str(), //set state_topic,name,uniqueId
548+
"", "", "", //set availability_topic,device_class,value_template,
549+
"{\"onlysensors\":true}", "{\"onlysensors\":false}", "", //set,payload_on,payload_off,unit_of_meas,
550+
0, //set off_delay
551+
Gateway_AnnouncementMsg, will_Message, true, subjectMQTTtoBTset //set,payload_avalaible,payload_not avalaible ,is a child device, command topic
552+
);
491553
# endif
492554
}
493555

main/main.ino

Lines changed: 34 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,14 @@ ReceivedSignal receivedSignal[struct_size] = {{0, 0}, {0, 0}, {0, 0}, {0, 0}};
4848
unsigned long timer_sys_measures = 0;
4949
# define ARDUINOJSON_USE_LONG_LONG 1
5050
#endif
51+
5152
#include <ArduinoJson.h>
5253
#include <ArduinoLog.h>
5354
#include <PubSubClient.h>
5455

56+
StaticJsonBuffer<JSON_MSG_BUFFER> modulesBuffer;
57+
JsonArray& modules = modulesBuffer.createArray();
58+
5559
// Modules config inclusion
5660
#if defined(ZgatewayRF) || defined(ZgatewayRF2) || defined(ZgatewayPilight)
5761
# include "config_RF.h"
@@ -592,81 +596,110 @@ void setup() {
592596

593597
#ifdef ZsensorBME280
594598
setupZsensorBME280();
599+
modules.add(ZsensorBME280);
595600
#endif
596601
#ifdef ZsensorHTU21
597602
setupZsensorHTU21();
603+
modules.add(ZsensorHTU21);
598604
#endif
599605
#ifdef ZsensorAHTx0
600606
setupZsensorAHTx0();
607+
modules.add(ZsensorAHTx0);
601608
#endif
602609
#ifdef ZsensorBH1750
603610
setupZsensorBH1750();
611+
modules.add(ZsensorBH1750);
604612
#endif
605613
#ifdef ZsensorTSL2561
606614
setupZsensorTSL2561();
615+
modules.add(ZsensorTSL2561);
607616
#endif
608617
#ifdef Zgateway2G
609618
setup2G();
619+
modules.add(Zgateway2G);
610620
#endif
611621
#ifdef ZgatewayIR
612622
setupIR();
623+
modules.add(ZgatewayIR);
613624
#endif
614625
#ifdef ZgatewayLORA
615626
setupLORA();
627+
modules.add(ZgatewayLORA);
616628
#endif
617629
#ifdef ZgatewayRF
618630
setupRF();
631+
modules.add(ZgatewayRF);
619632
#endif
620633
#ifdef ZgatewayRF2
621634
setupRF2();
635+
modules.add(ZgatewayRF2);
622636
#endif
623637
#ifdef ZgatewayPilight
624638
setupPilight();
639+
modules.add(ZgatewayPilight);
625640
#endif
626641
#ifdef ZgatewayWeatherStation
627642
setupWeatherStation();
643+
modules.add(ZgatewayWeatherStation);
628644
#endif
629645
#ifdef ZgatewaySRFB
630646
setupSRFB();
647+
modules.add(ZgatewaySRFB);
631648
#endif
632649
#ifdef ZgatewayBT
633650
setupBT();
651+
modules.add(ZgatewayBT);
634652
#endif
635653
#ifdef ZgatewayRFM69
636654
setupRFM69();
655+
modules.add(ZgatewayRFM69);
637656
#endif
638657
#ifdef ZsensorINA226
639658
setupINA226();
659+
modules.add(ZsensorINA226);
640660
#endif
641661
#ifdef ZsensorHCSR501
642662
setupHCSR501();
663+
modules.add(ZsensorHCSR501);
643664
#endif
644665
#ifdef ZsensorHCSR04
645666
setupHCSR04();
667+
modules.add(ZsensorHCSR04);
646668
#endif
647669
#ifdef ZsensorGPIOInput
648670
setupGPIOInput();
671+
modules.add(ZsensorGPIOInput);
649672
#endif
650673
#ifdef ZsensorGPIOKeyCode
651674
setupGPIOKeyCode();
675+
modules.add(ZsensorGPIOKeyCode);
652676
#endif
653677
#ifdef ZactuatorFASTLED
654678
setupFASTLED();
679+
modules.add(ZactuatorFASTLED);
655680
#endif
656681
#ifdef ZactuatorPWM
657682
setupPWM();
683+
modules.add(ZactuatorPWM);
658684
#endif
659685
#ifdef ZsensorDS1820
660686
setupZsensorDS1820();
687+
modules.add(ZsensorDS1820);
661688
#endif
662689
#ifdef ZsensorADC
663690
setupADC();
691+
modules.add(ZsensorADC);
664692
#endif
665693
#ifdef ZsensorDHT
666694
setupDHT();
695+
modules.add(ZsensorDHT);
667696
#endif
668697
#ifdef ZgatewayRS232
669698
setupRS232();
699+
modules.add(ZgatewayRS232);
700+
#endif
701+
#ifdef ZmqttDiscovery
702+
modules.add(ZmqttDiscovery);
670703
#endif
671704
Log.trace(F("mqtt_max_packet_size: %d" CR), mqtt_max_packet_size);
672705
Log.notice(F("Setup OpenMQTTGateway end" CR));
@@ -1337,94 +1370,14 @@ void stateMeasures() {
13371370
SYSdata["mac"] = (char*)mac.c_str();
13381371
SYSdata["wifiprt"] = (int)wifiProtocol;
13391372
# endif
1340-
# endif
1341-
String modules = "";
1342-
# ifdef ZgatewayRF
1343-
modules = modules + ZgatewayRF;
1344-
# endif
1345-
# ifdef ZsensorBME280
1346-
modules = modules + ZsensorBME280;
1347-
# endif
1348-
# ifdef ZsensorHTU21
1349-
modules = modules + ZsensorHTU21;
1350-
# endif
1351-
# ifdef ZsensorAHTx0
1352-
modules = modules + ZsensorAHTx0;
1353-
# endif
1354-
# ifdef ZsensorHCSR04
1355-
modules = modules + ZsensorHCSR04;
1356-
# endif
1357-
# ifdef ZsensorBH1750
1358-
modules = modules + ZsensorBH1750;
1359-
# endif
1360-
# ifdef ZsensorTSL2561
1361-
modules = modules + ZsensorTSL2561;
1362-
# endif
1363-
# ifdef ZsensorDHT
1364-
modules = modules + ZsensorDHT;
1365-
# endif
1366-
# ifdef ZsensorDS1820
1367-
modules = modules + ZsensorDS1820;
1368-
# endif
1369-
# ifdef ZactuatorONOFF
1370-
modules = modules + ZactuatorONOFF;
1371-
# endif
1372-
# ifdef Zgateway2G
1373-
modules = modules + Zgateway2G;
1374-
# endif
1375-
# ifdef ZgatewayIR
1376-
modules = modules + ZgatewayIR;
1377-
# endif
1378-
# ifdef ZgatewayLORA
1379-
modules = modules + ZgatewayLORA;
1380-
# endif
1381-
# ifdef ZgatewayRF2
1382-
modules = modules + ZgatewayRF2;
1383-
# endif
1384-
# ifdef ZgatewayWeatherStation
1385-
modules = modules + ZgatewayWeatherStation;
1386-
# endif
1387-
# ifdef ZgatewayPilight
1388-
modules = modules + ZgatewayPilight;
1389-
# endif
1390-
# ifdef ZgatewaySRFB
1391-
modules = modules + ZgatewaySRFB;
13921373
# endif
13931374
# ifdef ZgatewayBT
1394-
modules = modules + ZgatewayBT;
13951375
# ifdef ESP32
13961376
SYSdata["lowpowermode"] = (int)low_power_mode;
13971377
# endif
13981378
SYSdata["interval"] = BLEinterval;
13991379
SYSdata["scanbcnct"] = BLEscanBeforeConnect;
14001380
# endif
1401-
# ifdef ZgatewayRFM69
1402-
modules = modules + ZgatewayRFM69;
1403-
# endif
1404-
# ifdef ZsensorINA226
1405-
modules = modules + ZsensorINA226;
1406-
# endif
1407-
# ifdef ZsensorHCSR501
1408-
modules = modules + ZsensorHCSR501;
1409-
# endif
1410-
# ifdef ZsensorGPIOInput
1411-
modules = modules + ZsensorGPIOInput;
1412-
# endif
1413-
# ifdef ZsensorGPIOKeyCode
1414-
modules = modules + ZsensorGPIOKeyCode;
1415-
# endif
1416-
# ifdef ZsensorGPIOKeyCode
1417-
modules = modules + ZsensorGPIOKeyCode;
1418-
# endif
1419-
# ifdef ZmqttDiscovery
1420-
modules = modules + ZmqttDiscovery;
1421-
# endif
1422-
# ifdef ZactuatorFASTLED
1423-
modules = modules + ZactuatorFASTLED;
1424-
# endif
1425-
# ifdef ZactuatorPWM
1426-
modules = modules + ZactuatorPWM;
1427-
# endif
14281381
# ifdef ZboardM5STACK
14291382
M5.Power.begin();
14301383
SYSdata["m5-batt-level"] = (int8_t)M5.Power.getBatteryLevel();
@@ -1444,10 +1397,7 @@ void stateMeasures() {
14441397
SYSdata["m5-bat-chargecurrent"] = (float)M5.Axp.GetBatChargeCurrent();
14451398
SYSdata["m5-aps-voltage"] = (float)M5.Axp.GetAPSVoltage();
14461399
# endif
1447-
# ifdef ZGatewayRS232
1448-
modules = modules + ZGatewayRS232;
1449-
# endif
1450-
SYSdata["modules"] = modules;
1400+
SYSdata.set("modules", modules);
14511401
pub(subjectSYStoMQTT, SYSdata);
14521402
}
14531403
#endif

0 commit comments

Comments
 (0)