From 4b1f09b06bb317cca0df74e6db3f93d8e5706195 Mon Sep 17 00:00:00 2001 From: Florian <1technophile@users.noreply.github.com> Date: Sat, 7 Dec 2019 10:18:42 +0100 Subject: [PATCH] Wifi managers improvements (#503) * Add parameters to wifi manager: gateway name and base topic Enable to restart the ESP and erase the parameters by MQTT --- docs/.vuepress/config.js | 6 +- docs/upload/arduino-ide.md | 4 +- docs/upload/binaries.md | 20 +--- docs/upload/pio.md | 2 + docs/upload/portal.md | 24 +++++ docs/use/gateway.md | 9 ++ main/User_config.h | 35 ++++--- main/ZactuatorFASTLED.ino | 17 ++-- main/ZactuatorONOFF.ino | 7 +- main/Zgateway2G.ino | 8 +- main/ZgatewayBT.ino | 12 ++- main/ZgatewayIR.ino | 4 +- main/ZgatewayLORA.ino | 4 +- main/ZgatewayPilight.ino | 2 +- main/ZgatewayRF.ino | 5 +- main/ZgatewayRF2.ino | 6 +- main/ZgatewayRFM69.ino | 4 +- main/ZgatewaySRFB.ino | 2 +- main/ZmqttDiscovery.ino | 2 +- main/config_2G.h | 6 +- main/config_ADC.h | 2 +- main/config_BH1750.h | 2 +- main/config_BME280.h | 2 +- main/config_BT.h | 8 +- main/config_DHT.h | 2 +- main/config_FASTLED.h | 15 +-- main/config_GPIOInput.h | 2 +- main/config_GPIOKeyCode.h | 2 +- main/config_HCSR04.h | 2 +- main/config_HCSR501.h | 2 +- main/config_INA226.h | 2 +- main/config_IR.h | 6 +- main/config_LORA.h | 6 +- main/config_ONOFF.h | 4 +- main/config_RF.h | 18 ++-- main/config_RFM69.h | 6 +- main/config_SRFB.h | 8 +- main/config_TSL2561.h | 2 +- main/config_mqttDiscovery.h | 1 - main/main.ino | 197 ++++++++++++++++++++++++------------ test/Test_config.h | 36 ++++--- 41 files changed, 308 insertions(+), 196 deletions(-) create mode 100644 docs/upload/portal.md create mode 100644 docs/use/gateway.md diff --git a/docs/.vuepress/config.js b/docs/.vuepress/config.js index d0b35c5..824c27a 100644 --- a/docs/.vuepress/config.js +++ b/docs/.vuepress/config.js @@ -61,7 +61,8 @@ module.exports = { children: [ 'upload/binaries', 'upload/pio', - 'upload/arduino-ide' + 'upload/arduino-ide', + 'upload/portal' ] }, { @@ -75,7 +76,8 @@ module.exports = { 'use/gsm', 'use/rfm69', 'use/sensors', - 'use/actuators' + 'use/actuators', + 'use/gateway' ] }, { diff --git a/docs/upload/arduino-ide.md b/docs/upload/arduino-ide.md index 9b8d270..03449ea 100644 --- a/docs/upload/arduino-ide.md +++ b/docs/upload/arduino-ide.md @@ -12,7 +12,7 @@ *Example for the use of RF gateway* ```C++ #define ZgatewayRF "RF" //ESP8266, Arduino, ESP32 -//#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge +//#define ZgatewayIR "IR" //ESP8266, Arduino, Sonoff RF Bridge //#define ZgatewayLORA "LORA" //ESP8266, Arduino, ESP32 //#define ZgatewayPilight "Pilight" //ESP8266, Arduino, ESP32 //#define ZgatewayBT "BT" //ESP8266, ESP32 @@ -24,3 +24,5 @@ * Open the serial monitor and set 115200 bauds * Upload ➡️ * You should see the logs into the serial monitor + +With an ESP if you did not set your network and mqtt parameters manualy you can now open the [web portal configuration](portal.md). diff --git a/docs/upload/binaries.md b/docs/upload/binaries.md index a5361cc..557a2c5 100644 --- a/docs/upload/binaries.md +++ b/docs/upload/binaries.md @@ -32,22 +32,6 @@ https://github.com/marcelstoer/nodemcu-pyflasher/releases * Click on *FlashNodeMCU* The upload details appears. +With an ESP if you did not set your network and mqtt parameters manualy you can now open the [web portal configuration](portal.md). + Note that to reset the wifi and mqtt settings you can check *yes, wipes all data* - -Once loaded into your board you have to set your network parameters with wifi manager portal -From your smartphone search for your OpenMQTTGateway wifi network and connect to it, a web page will appear -* Select your wifi -* Set your wifi password -* Set your MQTT Server IP -* Set your MQTT Server username (not compulsory) -* Set your MQTT Server password (not compulsory) - -The ESP restart and connect to your network. Note that your credentials are saved into the ESP memory, if you want to redo the configuration you have to erase the ESP memory with the flash download tool. - -_The default password for wifi manager is "your_password"_ - -Once done the gateway should connect to your network and your broker, you should see it into the broker in the form of the following messages: -``` -home/OpenMQTTGateway/LWT Online -home/OpenMQTTGateway/version -``` diff --git a/docs/upload/pio.md b/docs/upload/pio.md index 6b05368..17d29a2 100644 --- a/docs/upload/pio.md +++ b/docs/upload/pio.md @@ -111,3 +111,5 @@ If you want to use HASS MQTT discovery you need to have `#define ZmqttDiscovery "HADiscovery"` uncommented. Added to that auto discovery box should be selected into your Home Assistant MQTT integration configuration. + +With an ESP if you did not set your network and mqtt parameters manualy you can now open the [web portal configuration](portal.md). diff --git a/docs/upload/portal.md b/docs/upload/portal.md new file mode 100644 index 0000000..585c44f --- /dev/null +++ b/docs/upload/portal.md @@ -0,0 +1,24 @@ +# Wifi and MQTT configuration portal + +Once loaded into your board and if you don't use the manual configuration you have to set your network parameters with wifi manager portal. +From your smartphone search for your OpenMQTTGateway wifi network and connect to it, a web page will appear +* Select your wifi +* Set your wifi password +* Set your MQTT Server IP +* Set your MQTT Server username (not compulsory) +* Set your MQTT Server password (not compulsory) +* Set your MQTT base topic (you must keep the / at the end) +* Set your gateway name + +The ESP restart and connect to your network. Note that your credentials are saved into the ESP memory, if you want to redo the configuration you have to erase the ESP memory with the flash download tool. + +_The default password for wifi manager is "your_password"_ + +Once done the gateway should connect to your network and your broker, you should see it into the broker in the form of the following messages: +``` +home/OpenMQTTGateway/LWT Online +home/OpenMQTTGateway/version +``` + +Note that the web portal appears only on first boot, if you want to configure again the setting you can do a long press on TRIGGER_PIN. + diff --git a/docs/use/gateway.md b/docs/use/gateway.md new file mode 100644 index 0000000..de946be --- /dev/null +++ b/docs/use/gateway.md @@ -0,0 +1,9 @@ +# System commands (ESP only) + +## Restart the ESP + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/set" -m '{"cmd":"restart"}'` + +## Erase the ESP settings + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoSYS/set" -m '{"cmd":"erase"}'` diff --git a/main/User_config.h b/main/User_config.h index b38a5e2..dbb8e05 100644 --- a/main/User_config.h +++ b/main/User_config.h @@ -50,6 +50,7 @@ #ifndef Gateway_Name #define Gateway_Name "OpenMQTTGateway" #endif +#define Base_Topic "home/" /*-------------DEFINE YOUR NETWORK PARAMETERS BELOW----------------*/ #if defined(ESP8266)||defined(ESP32) // for nodemcu, weemos and esp8266 @@ -71,11 +72,14 @@ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ //MQTT Parameters definition //#define mqtt_server_name "www.mqtt_broker.com" // instead of defining the server by its IP you can define it by its name, uncomment this line and set the correct MQTT server host name -char mqtt_user[20] = "your_username"; // not compulsory only if your broker needs authentication -char mqtt_pass[30] = "your_password"; // not compulsory only if your broker needs authentication -char mqtt_server[40] = "192.168.1.17"; +#define parameters_size 20 +#define mqtt_topic_max_size 100 +char mqtt_user[parameters_size] = "your_username"; // not compulsory only if your broker needs authentication +char mqtt_pass[parameters_size] = "your_password"; // not compulsory only if your broker needs authentication +char mqtt_server[parameters_size] = "192.168.1.17"; char mqtt_port[6] = "1883"; - +char mqtt_topic[mqtt_topic_max_size] = Base_Topic; +char gateway_name[parameters_size * 2] = Gateway_Name; //uncomment the line below to integrate msg value into the subject when receiving //#define valueAsASubject true @@ -119,9 +123,8 @@ const byte Dns[] = { 0, 0, 0, 0 }; //ip adress, if first value is different from const byte subnet[] = { 255, 255, 255, 0 }; //ip adress /*-------------DEFINE YOUR MQTT ADVANCED PARAMETERS BELOW----------------*/ -#define Base_Topic "home/" -#define version_Topic Base_Topic Gateway_Name "/version" -#define will_Topic Base_Topic Gateway_Name "/LWT" +#define version_Topic "/version" +#define will_Topic "/LWT" #define will_QoS 0 #define will_Retain true #define will_Message "offline" @@ -182,9 +185,16 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #endif /*--------------MQTT general topics-----------------*/ // global MQTT subject listened by the gateway to execute commands (send RF, IR or others) -#define subjectMQTTtoX Base_Topic Gateway_Name "/commands/#" -#define subjectMultiGTWKey "toMQTT" -#define subjectGTWSendKey "MQTTto" +#define subjectMQTTtoX "/commands/#" +#define subjectMultiGTWKey "toMQTT" +#define subjectGTWSendKey "MQTTto" + +// key used for launching commands to the gateway +#define restartCmd "restart" +#define eraseCmd "erase" + +// define if we concatenate the values into the topic +//#define valueAsASubject true //variables to avoid duplicates #define time_avoid_duplicate 3000 // if you want to avoid duplicate mqtt message received set this to > 0, the value is the time in milliseconds during which we don't publish duplicates @@ -197,9 +207,10 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #endif #define TimeBetweenReadingSYS 120000 // time between system readings (like memory) -#define subjectSYStoMQTT Base_Topic Gateway_Name "/SYStoMQTT" +#define subjectSYStoMQTT "/SYStoMQTT" +#define subjectMQTTtoSYSset "/commands/MQTTtoSYS/set" -//#define subjectTRACEtoMQTT Base_Topic Gateway_Name "/log" //uncomment if you want to see traces on /log topic +//#define subjectTRACEtoMQTT "OpenMQTTGateway/log" //uncomment if you want to see traces on OpenMQTTGateway/log topic /*-------------------ACTIVATE TRACES----------------------*/ #define TRACE 1 // commented = trace off, uncommented = trace on diff --git a/main/ZactuatorFASTLED.ino b/main/ZactuatorFASTLED.ino index 001923b..aa3b56f 100644 --- a/main/ZactuatorFASTLED.ino +++ b/main/ZactuatorFASTLED.ino @@ -127,14 +127,12 @@ void MQTTtoFASTLEDJSON(char *topicOri, JsonObject& jsonData) { trc(F("MQTTtoFASTLEDJSON: ")); currentLEDState = GENERAL; - String topic = topicOri; - - trc(topic); + trc(topicOri); //number = (long)strtol(&datacallback[1], NULL, 16); - if (topic == subjectMQTTtoFASTLEDsetled) + if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLEDsetled)) != NULL) { trc(F("JSON parsed")); int ledNr = jsonData["led"]; @@ -160,10 +158,9 @@ void MQTTtoFASTLED(char *topicOri, char *datacallback) { trc(F("MQTTtoFASTLED: ")); currentLEDState = GENERAL; - String topic = topicOri; long number = 0; - trc(topic); - if (topic == subjectMQTTtoFASTLED) + trc(topicOri); + if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLED)) != NULL) { number = (long)strtol(&datacallback[1], NULL, 16); trc(number); @@ -173,18 +170,18 @@ void MQTTtoFASTLED(char *topicOri, char *datacallback) } FastLED.show(); } - else if (topic == subjectMQTTtoFASTLEDsetbrightness) + else if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLEDsetbrightness)) != NULL) { number = (long)strtol(&datacallback[1], NULL, 16); trc(number); FastLED.setBrightness(number); FastLED.show(); } - else if (topic == subjectMQTTtoFASTLEDsetanimation) + else if (strstr(topicOri,catToMainTopic(subjectMQTTtoFASTLEDsetanimation)) != NULL) { String payload = datacallback; trc(payload); - if (payload.equals("fire")) + if ( strstr(datacallback,"fire") != NULL) { currentLEDState = FIRE; gPal = HeatColors_p; diff --git a/main/ZactuatorONOFF.ino b/main/ZactuatorONOFF.ino index 90d50b0..80d6796 100644 --- a/main/ZactuatorONOFF.ino +++ b/main/ZactuatorONOFF.ino @@ -33,7 +33,7 @@ #ifdef jsonReceiving void MQTTtoONOFF(char * topicOri, JsonObject& ONOFFdata){ - if (strcmp(topicOri,subjectMQTTtoONOFF) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoONOFF)) != NULL){ trc(F("MQTTtoONOFF json data analysis")); int boolSWITCHTYPE = ONOFFdata["state"] | 99; int pin = ONOFFdata["pin"] | ACTUATOR_ONOFF_PIN; @@ -55,7 +55,7 @@ void MQTTtoONOFF(char * topicOri, JsonObject& ONOFFdata){ #ifdef simpleReceiving void MQTTtoONOFF(char * topicOri, char * datacallback) { - if ((strstr(topicOri,subjectMQTTtoONOFF) != NULL) ){ + if ((strstr(topicOri,catToMainTopic(subjectMQTTtoONOFF)) != NULL) ){ trc(F("MQTTtoONOFF")); int pin = strtol(datacallback, NULL, 10); // we will not be able to pass values > 4294967295 @@ -69,7 +69,8 @@ void MQTTtoONOFF(char * topicOri, char * datacallback) { digitalWrite(pin, ON); // we acknowledge the sending by publishing the value to an acknowledgement topic - pub(subjectGTWONOFFtoMQTT, ON); + char b = ON; + pub(subjectGTWONOFFtoMQTT, &b); } } #endif diff --git a/main/Zgateway2G.ino b/main/Zgateway2G.ino index fff5abc..6bdc26c 100644 --- a/main/Zgateway2G.ino +++ b/main/Zgateway2G.ino @@ -107,7 +107,7 @@ bool _2GtoMQTT(){ String data = datacallback; String topic = topicOri; - if (topic == subjectMQTTto2G) { + if (strstr(topicOri,catToMainTopic(subjectMQTTto2G)) != NULL) { trc(F("MQTTto2G data analysis")); // 2G DATA ANALYSIS String phone_number = ""; @@ -122,7 +122,7 @@ bool _2GtoMQTT(){ if (A6l.sendSMS(phone_number,data) == A6_OK ) { trc(F("SMS OK")); // Acknowledgement to the GTW2G topic - pub(subjectGTW2GtoMQTT, datacallback);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also + pub(subjectGTW2GtoMQTT, "SMS OK");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also }else{ trc(F("SMS KO")); // Acknowledgement to the GTW2G topic @@ -138,7 +138,7 @@ bool _2GtoMQTT(){ #ifdef jsonReceiving void MQTTto2G(char * topicOri, JsonObject& SMSdata) { - if (strcmp(topicOri,subjectMQTTto2G) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTto2G)) != NULL){ const char * sms = SMSdata["message"]; const char * phone = SMSdata["phone"]; trc(F("MQTTto2G json data analysis")); @@ -148,7 +148,7 @@ bool _2GtoMQTT(){ if (A6l.sendSMS(String(phone),String(sms)) == A6_OK ) { trc(F("SMS OK")); // Acknowledgement to the GTW2G topic - pub(subjectGTW2GtoMQTT, SMSdata);// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also + pub(subjectGTW2GtoMQTT, "SMS OK");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also }else{ trc(F("SMS KO")); pub(subjectGTW2GtoMQTT, "SMS KO");// we acknowledge the sending by publishing the value to an acknowledgement topic, for the moment even if it is a signal repetition we acknowledge also diff --git a/main/ZgatewayBT.ino b/main/ZgatewayBT.ino index 3060d02..af29c93 100644 --- a/main/ZgatewayBT.ino +++ b/main/ZgatewayBT.ino @@ -530,7 +530,12 @@ void MiBandDiscovery(char * mac){ while(true){ trc(taskMessage); delay(BLEinterval); - BLEscan(); + if (client.state() == 0) { + BLEscan(); + }else{ + trc("MQTT client disconnected no BLE scan"); + delay(1000); + } } } @@ -993,12 +998,13 @@ void haRoomPresence(JsonObject& HomePresence){ HomePresence["distance"] = distance; trc(F("BLE DISTANCE :")); trc(distance); - pub(subjectHomePresence,HomePresence); + String topic = String(Base_Topic) + "home_presence/" + String(gateway_name); + pub_custom_topic((char *)topic.c_str(),HomePresence); } #endif void MQTTtoBT(char * topicOri, JsonObject& BTdata) { // json object decoding - if (strcmp(topicOri,subjectMQTTtoBTset) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoBTset)) != NULL){ trc(F("MQTTtoBT json set")); // Black list & white list set diff --git a/main/ZgatewayIR.ino b/main/ZgatewayIR.ino index 42b82ca..4455046 100644 --- a/main/ZgatewayIR.ino +++ b/main/ZgatewayIR.ino @@ -152,7 +152,7 @@ void IRtoMQTT(){ storeValue(MQTTvalue); if (repeatIRwMQTT){ trc(F("Pub. IR for rpt")); - pub(subjectMQTTtoIR,MQTTvalue); + pubMQTT(subjectMQTTtoIR,MQTTvalue); } } } @@ -274,7 +274,7 @@ void IRtoMQTT(){ #ifdef jsonReceiving void MQTTtoIR(char * topicOri, JsonObject& IRdata) { - if (strcmp(topicOri,subjectMQTTtoIR) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoIR)) != NULL){ trc(F("MQTTtoIR json")); uint64_t data = IRdata["value"]; const char * raw = IRdata["raw"]; diff --git a/main/ZgatewayLORA.ino b/main/ZgatewayLORA.ino index b13fedc..ac26e6f 100644 --- a/main/ZgatewayLORA.ino +++ b/main/ZgatewayLORA.ino @@ -87,7 +87,7 @@ void LORAtoMQTT(){ #ifdef jsonReceiving void MQTTtoLORA(char * topicOri, JsonObject& LORAdata) { // json object decoding - if (strcmp(topicOri,subjectMQTTtoLORA) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoLORA)) != NULL){ trc(F("MQTTtoLORA json")); const char * message = LORAdata["message"]; int txPower = LORAdata["txpower"]|LORA_TX_POWER; @@ -120,7 +120,7 @@ void LORAtoMQTT(){ #endif #ifdef simpleReceiving void MQTTtoLORA(char * topicOri, char * LORAdata) { // json object decoding - if (strcmp(topicOri,subjectMQTTtoLORA) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoLORA)) != NULL){ LoRa.beginPacket(); LoRa.print(LORAdata); LoRa.endPacket(); diff --git a/main/ZgatewayPilight.ino b/main/ZgatewayPilight.ino index 6064efb..60d418a 100644 --- a/main/ZgatewayPilight.ino +++ b/main/ZgatewayPilight.ino @@ -74,7 +74,7 @@ void MQTTtoPilight(char * topicOri, JsonObject& Pilightdata) { int result = 0; - if (strcmp(topicOri,subjectMQTTtoPilight) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoPilight)) != NULL){ trc(F("MQTTtoPilight json data analysis")); const char * message = Pilightdata["message"]; const char * protocol = Pilightdata["protocol"]; diff --git a/main/ZgatewayRF.ino b/main/ZgatewayRF.ino index 331885b..5ad6b9e 100644 --- a/main/ZgatewayRF.ino +++ b/main/ZgatewayRF.ino @@ -132,7 +132,7 @@ void MQTTtoRF(char * topicOri, char * datacallback) { trc(valueBITS); } - if ((topic == subjectMQTTtoRF) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)){ + if ((strstr(topicOri,catToMainTopic(subjectMQTTtoRF)) != NULL) && (valuePRT == 0) && (valuePLSL == 0) && (valueBITS == 0)){ trc(F("MQTTtoRF dflt")); mySwitch.setProtocol(1,350); mySwitch.send(data, 24); @@ -156,8 +156,7 @@ void MQTTtoRF(char * topicOri, char * datacallback) { #ifdef jsonReceiving void MQTTtoRF(char * topicOri, JsonObject& RFdata) { // json object decoding - - if (strcmp(topicOri,subjectMQTTtoRF) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoRF)) != NULL){ trc(F("MQTTtoRF json")); unsigned long data = RFdata["value"]; if (data != 0) { diff --git a/main/ZgatewayRF2.ino b/main/ZgatewayRF2.ino index 9250bac..42e1d96 100644 --- a/main/ZgatewayRF2.ino +++ b/main/ZgatewayRF2.ino @@ -202,11 +202,11 @@ void rf2Callback(unsigned int period, unsigned long address, unsigned long group trc(F("Adv data MQTTtoRF2 push state via RF2toMQTT")); if (isDimCommand) { MQTTRF2string = subjectRF2toMQTT+String("/")+RF2codeKey+MQTTAddress+String("/")+RF2unitKey+MQTTunit+String("/")+RF2groupKey+MQTTgroupBit+String("/")+RF2dimKey+String("/")+RF2periodKey+MQTTperiod; - pub(MQTTRF2string,MQTTdimLevel); + pub((char *)MQTTRF2string.c_str(),(char *)MQTTdimLevel.c_str()); } else { MQTTRF2string = subjectRF2toMQTT+String("/")+RF2codeKey+MQTTAddress+String("/")+RF2unitKey+MQTTunit+String("/")+RF2groupKey+MQTTgroupBit+String("/")+RF2periodKey+MQTTperiod; - pub(MQTTRF2string,MQTTswitchType); + pub((char *)MQTTRF2string.c_str(),(char *)MQTTswitchType.c_str()); } } } @@ -215,7 +215,7 @@ void rf2Callback(unsigned int period, unsigned long address, unsigned long group #ifdef jsonReceiving void MQTTtoRF2(char * topicOri, JsonObject& RF2data) { // json object decoding - if (strcmp(topicOri,subjectMQTTtoRF2) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoRF2)) != NULL){ trc(F("MQTTtoRF2 json")); int boolSWITCHTYPE = RF2data["switchType"] | 99; if (boolSWITCHTYPE != 99) { diff --git a/main/ZgatewayRFM69.ino b/main/ZgatewayRFM69.ino index fb093d2..15b0b30 100644 --- a/main/ZgatewayRFM69.ino +++ b/main/ZgatewayRFM69.ino @@ -184,7 +184,7 @@ bool RFM69toMQTT(void) { #ifdef simpleReceiving void MQTTtoRFM69(char * topicOri, char * datacallback) { - if(strstr(topicOri, subjectMQTTtoRFM69) != NULL){ + if(strstr(topicOri,catToMainTopic(subjectMQTTtoRFM69)) != NULL){ trc(F("MQTTtoRFM69 data analysis")); char data[RF69_MAX_DATA_LEN+1]; memcpy(data, (void *)datacallback, RF69_MAX_DATA_LEN); @@ -215,7 +215,7 @@ bool RFM69toMQTT(void) { #ifdef jsonReceiving void MQTTtoRFM69(char * topicOri, JsonObject& RFM69data) { - if (strcmp(topicOri,subjectMQTTtoRFM69) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoRFM69)) != NULL){ const char * data = RFM69data["data"]; trc(F("MQTTtoRFM69 json data analysis")); if(data){ diff --git a/main/ZgatewaySRFB.ino b/main/ZgatewaySRFB.ino index 44769ec..c7b134c 100644 --- a/main/ZgatewaySRFB.ino +++ b/main/ZgatewaySRFB.ino @@ -280,7 +280,7 @@ bool _rfbToChar(byte * in, char * out) { // RF DATA ANALYSIS const char * raw = SRFBdata["raw"]; int valueRPT = SRFBdata["repeat"]|1; - if (strcmp(topicOri,subjectMQTTtoSRFB) == 0){ + if (strstr(topicOri,catToMainTopic(subjectMQTTtoSRFB)) != NULL){ trc(F("MQTTtoSRFB json")); if (raw){ // send raw in priority when defined in the json trc(F("MQTTtoSRFB raw ok")); diff --git a/main/ZmqttDiscovery.ino b/main/ZmqttDiscovery.ino index 3f30516..1c1962d 100644 --- a/main/ZmqttDiscovery.ino +++ b/main/ZmqttDiscovery.ino @@ -93,7 +93,7 @@ void createDiscovery(char * sensor_type, sensor.set("device", device); //device sensor is connected to } String topic = String(discovery_Topic) + "/" + String(sensor_type) + "/" + String(unique_id) + "/config"; - pub((char *)topic.c_str(), sensor); + pub_custom_topic((char *)topic.c_str(), sensor); } void pubMqttDiscovery() diff --git a/main/config_2G.h b/main/config_2G.h index 19a55ad..7ad9e59 100644 --- a/main/config_2G.h +++ b/main/config_2G.h @@ -32,9 +32,9 @@ extern void MQTTto2G(char * topicOri, char * datacallback); extern void MQTTto2G(char * topicOri, JsonObject& SMSdata); /*-------------------2G topics & parameters----------------------*/ //433Mhz MQTT Subjects and keys -#define subjectMQTTto2G Base_Topic Gateway_Name "/commands/MQTTto2G" -#define subject2GtoMQTT Base_Topic Gateway_Name "/2GtoMQTT" -#define subjectGTW2GtoMQTT Base_Topic Gateway_Name "/2GtoMQTT" +#define subjectMQTTto2G "/commands/MQTTto2G" +#define subject2GtoMQTT "/2GtoMQTT" +#define subjectGTW2GtoMQTT "/2GtoMQTT" #define _2GPhoneKey "PHO_" // phone number define the phone number to send the SMS MQTT->2G #define _2G_MODULE_BAUDRATE 9600 diff --git a/main/config_ADC.h b/main/config_ADC.h index 7de4aa1..eddc5ea 100644 --- a/main/config_ADC.h +++ b/main/config_ADC.h @@ -30,7 +30,7 @@ extern void setupADC(); extern void ADCtoMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define ADCTOPIC Base_Topic Gateway_Name "/ADCtoMQTT" +#define ADCTOPIC "/ADCtoMQTT" #define TimeBetweenReadingADC 500 // time between 2 ADC readings, minimum 200 to let the time of the ESP to keep the connection #define ThresholdReadingADC 50 // following the comparison between the previous value and the current one +- the threshold the value will be published or not diff --git a/main/config_BH1750.h b/main/config_BH1750.h index 6a32ef9..8220b46 100644 --- a/main/config_BH1750.h +++ b/main/config_BH1750.h @@ -46,7 +46,7 @@ extern void BH1750toMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectBH1750toMQTT Base_Topic Gateway_Name "/BH1750toMQTT" +#define subjectBH1750toMQTT "/BH1750toMQTT" //Time used to wait for an interval before resending measured values unsigned long timebh1750 = 0; int BH1750_i2c_addr = 0x23; // Light Sensor I2C Address diff --git a/main/config_BME280.h b/main/config_BME280.h index 6df7977..d48c05e 100644 --- a/main/config_BME280.h +++ b/main/config_BME280.h @@ -46,7 +46,7 @@ extern void BME280toMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define BMETOPIC Base_Topic Gateway_Name "/CLIMAtoMQTT/bme" +#define BMETOPIC "/CLIMAtoMQTT/bme" //Time used to wait for an interval before resending measured values unsigned long timebme280 = 0; diff --git a/main/config_BT.h b/main/config_BT.h index 7f60c96..10c06fa 100644 --- a/main/config_BT.h +++ b/main/config_BT.h @@ -30,10 +30,10 @@ extern void setupBT(); extern bool BTtoMQTT(); extern void MQTTtoBT(char * topicOri, JsonObject& RFdata); /*----------------------BT topics & parameters-------------------------*/ -#define subjectBTtoMQTT Base_Topic Gateway_Name "/BTtoMQTT/" -#define subjectMQTTtoBTset Base_Topic Gateway_Name "/commands/MQTTtoBT/set" +#define subjectBTtoMQTT "/BTtoMQTT/" +#define subjectMQTTtoBTset "/commands/MQTTtoBT/set" #define TimeBtw_Read 55555 //define default time between 2 scans -#define MinimumRSSI -90 //default minimum rssi value, all the devices below -90 will not be reported +#define MinimumRSSI -100 //default minimum rssi value, all the devices below -90 will not be reported #define Scan_duration 10 //define the time for a scan #define HM-10 //#define HM-11 // uncomment this line if you use HM-11 and comment the line above @@ -43,7 +43,7 @@ extern void MQTTtoBT(char * topicOri, JsonObject& RFdata); /*-------------------HOME ASSISTANT ROOM PRESENCE ----------------------*/ // if not commented Home presence integration with HOME ASSISTANT is activated -#define subjectHomePresence Base_Topic "home_presence/" Gateway_Name // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name) +#define subjectHomePresence "home_presence/" // will send Home Assistant room presence message to this topic (first part is same for all rooms, second is room name) unsigned int BLEinterval ; //time between 2 scans int Minrssi ; //minimum rssi value diff --git a/main/config_DHT.h b/main/config_DHT.h index c8d508c..1af44b5 100644 --- a/main/config_DHT.h +++ b/main/config_DHT.h @@ -30,7 +30,7 @@ extern void setupDHT(); extern void DHTtoMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define DHTTOPIC Base_Topic Gateway_Name "/DHTtoMQTT/dht1" +#define DHTTOPIC "/DHTtoMQTT/dht1" #define dht_always true // if false when the current value for temp or hum is the same as previous one don't send it by MQTT #define TimeBetweenReadingDHT 30000 // time between 2 DHT readings /*-------------DHT SENSOR TYPE-------------*/ diff --git a/main/config_FASTLED.h b/main/config_FASTLED.h index 724f6a7..e8ea371 100644 --- a/main/config_FASTLED.h +++ b/main/config_FASTLED.h @@ -23,16 +23,11 @@ /*-------------------FASTLED topics & parameters----------------------*/ //FASTLED MQTT Subjects -#define subjectMQTTtoFASTLED Base_Topic Gateway_Name "/commands/MQTTtoFASTLED" -#define subjectMQTTtoFASTLEDsetled Base_Topic Gateway_Name "/commands/MQTTtoFASTLED/setled" //set only one LED with JSON struct {"led":0-x,"hex":"#000000","blink":true/false} -#define subjectMQTTtoFASTLEDsetbrightness Base_Topic Gateway_Name "/commands/MQTTtoFASTLED/setbrightness" //set the brightness 0-255 -#define subjectMQTTtoFASTLEDsetanimation Base_Topic Gateway_Name "/commands/MQTTtoFASTLED/setanimation" //Animation Fire2012 by Mark Kriegsman -#define subjectGTWFASTLEDtoMQTT Base_Topic Gateway_Name "/FASTLEDtoMQTT" //same color on all LEDs in #RRGGBB - - - - - +#define subjectMQTTtoFASTLED "/commands/MQTTtoFASTLED" +#define subjectMQTTtoFASTLEDsetled "/commands/MQTTtoFASTLED/setled" //set only one LED with JSON struct {"led":0-x,"hex":"#000000","blink":true/false} +#define subjectMQTTtoFASTLEDsetbrightness "/commands/MQTTtoFASTLED/setbrightness" //set the brightness 0-255 +#define subjectMQTTtoFASTLEDsetanimation "/commands/MQTTtoFASTLED/setanimation" //Animation Fire2012 by Mark Kriegsman +#define subjectGTWFASTLEDtoMQTT "/FASTLEDtoMQTT" //same color on all LEDs in #RRGGBB // How many leds in your strip? #define FASTLED_NUM_LEDS 16 diff --git a/main/config_GPIOInput.h b/main/config_GPIOInput.h index 04f5b67..a587435 100644 --- a/main/config_GPIOInput.h +++ b/main/config_GPIOInput.h @@ -30,7 +30,7 @@ extern void setupGPIOInput(); extern void GPIOInputtoMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectGPIOInputtoMQTT Base_Topic Gateway_Name "/GPIOInputtoMQTT" +#define subjectGPIOInputtoMQTT "/GPIOInputtoMQTT" #define GPIOInputDebounceDelay 60 //debounce time, increase if there are issues /*-------------------PIN DEFINITIONS----------------------*/ diff --git a/main/config_GPIOKeyCode.h b/main/config_GPIOKeyCode.h index f7b90b0..1b09b44 100644 --- a/main/config_GPIOKeyCode.h +++ b/main/config_GPIOKeyCode.h @@ -30,7 +30,7 @@ extern void setupGPIOKeyCode(); extern void GPIOKeyCodetoMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectGPIOKeyCodetoMQTT Base_Topic Gateway_Name "/keycode" +#define subjectGPIOKeyCodetoMQTT "/keycode" #define subjectGPIOKeyCodeStatetoMQTT subjectGPIOKeyCodetoMQTT "/status" #define GPIOKeyCodeDebounceDelay 60 //debounce time, increase if there are issues diff --git a/main/config_HCSR04.h b/main/config_HCSR04.h index 8067e11..5c84c9e 100644 --- a/main/config_HCSR04.h +++ b/main/config_HCSR04.h @@ -45,7 +45,7 @@ extern void MeasureDistance(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectHCSR04 Base_Topic Gateway_Name "/DISTtoMQTT/sr04" +#define subjectHCSR04 "/DISTtoMQTT/sr04" /*-------------------PIN DEFINITIONS----------------------*/ #if defined(ESP8266) diff --git a/main/config_HCSR501.h b/main/config_HCSR501.h index 38d74de..ab45808 100644 --- a/main/config_HCSR501.h +++ b/main/config_HCSR501.h @@ -30,7 +30,7 @@ extern void setupHCSR501(); extern void HCSR501toMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectHCSR501toMQTT Base_Topic Gateway_Name "/HCSR501toMQTT" +#define subjectHCSR501toMQTT "/HCSR501toMQTT" #define TimeBeforeStartHCSR501 60000 //define the time necessary for HC SR501 init diff --git a/main/config_INA226.h b/main/config_INA226.h index d902185..7e9c671 100644 --- a/main/config_INA226.h +++ b/main/config_INA226.h @@ -30,7 +30,7 @@ extern void setupINA226(); extern void INA226toMQTT(); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectINA226toMQTT Base_Topic Gateway_Name "/INA226toMQTT" +#define subjectINA226toMQTT "/INA226toMQTT" #define TimeBetweenReadingINA226 30000 // time between 2 INA226 readings #endif \ No newline at end of file diff --git a/main/config_IR.h b/main/config_IR.h index c4e3c30..307a3d2 100644 --- a/main/config_IR.h +++ b/main/config_IR.h @@ -32,9 +32,9 @@ extern void MQTTtoIR(char * topicOri, char * datacallback); extern void MQTTtoIR(char * topicOri, JsonObject& RFdata); /*-------------------IR topics & parameters----------------------*/ //IR MQTT Subjects -#define subjectGTWIRtoMQTT Base_Topic Gateway_Name "/IRtoMQTT" -#define subjectIRtoMQTT Base_Topic Gateway_Name "/IRtoMQTT" -#define subjectMQTTtoIR Base_Topic Gateway_Name "/commands/MQTTtoIR" +#define subjectGTWIRtoMQTT "/IRtoMQTT" +#define subjectIRtoMQTT "/IRtoMQTT" +#define subjectMQTTtoIR "/commands/MQTTtoIR" // subject monitored to listen traffic processed by other gateways to store data and avoid ntuple #define subjectMultiGTWIR "+/+/IRtoMQTT" diff --git a/main/config_LORA.h b/main/config_LORA.h index 2f9ccfa..faf3ec0 100644 --- a/main/config_LORA.h +++ b/main/config_LORA.h @@ -31,9 +31,9 @@ extern void LORAtoMQTT(); extern void MQTTtoLORA(char * topicOri, char * datacallback); extern void MQTTtoLORA(char * topicOri, JsonObject& RFdata); /*----------------------LORA topics & parameters-------------------------*/ -#define subjectLORAtoMQTT Base_Topic Gateway_Name "/LORAtoMQTT" -#define subjectMQTTtoLORA Base_Topic Gateway_Name "/commands/MQTTtoLORA" -#define subjectGTWLORAtoMQTT Base_Topic Gateway_Name "/LORAtoMQTT" +#define subjectLORAtoMQTT "/LORAtoMQTT" +#define subjectMQTTtoLORA "/commands/MQTTtoLORA" +#define subjectGTWLORAtoMQTT "/LORAtoMQTT" //Default parameters used when the parameters are not set in the json data #define LORA_BAND 868E6 diff --git a/main/config_ONOFF.h b/main/config_ONOFF.h index 9ad05a2..cb2daab 100644 --- a/main/config_ONOFF.h +++ b/main/config_ONOFF.h @@ -31,8 +31,8 @@ extern void MQTTtoONOFF(char * topicOri, char * datacallback); extern void MQTTtoONOFF(char * topicOri, JsonObject& RFdata); /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectMQTTtoONOFF Base_Topic Gateway_Name "/commands/MQTTtoONOFF" -#define subjectGTWONOFFtoMQTT Base_Topic Gateway_Name "/ONOFFtoMQTT" +#define subjectMQTTtoONOFF "/commands/MQTTtoONOFF" +#define subjectGTWONOFFtoMQTT "/ONOFFtoMQTT" #define ONKey "setON" #define OFFKey "setOFF" diff --git a/main/config_RF.h b/main/config_RF.h index 2029fec..349ab05 100644 --- a/main/config_RF.h +++ b/main/config_RF.h @@ -49,9 +49,9 @@ #endif /*-------------------RF topics & parameters----------------------*/ //433Mhz MQTT Subjects and keys -#define subjectMQTTtoRF Base_Topic Gateway_Name "/commands/MQTTto433" -#define subjectRFtoMQTT Base_Topic Gateway_Name "/433toMQTT" -#define subjectGTWRFtoMQTT Base_Topic Gateway_Name "/433toMQTT" +#define subjectMQTTtoRF "/commands/MQTTto433" +#define subjectRFtoMQTT "/433toMQTT" +#define subjectGTWRFtoMQTT "/433toMQTT" #define RFprotocolKey "433_" // protocol will be defined if a subject contains RFprotocolKey followed by a value of 1 digit #define RFbitsKey "RFBITS_" // bits will be defined if a subject contains RFbitsKey followed by a value of 2 digits #define repeatRFwMQTT false // do we repeat a received signal by using mqtt with RF gateway @@ -73,9 +73,9 @@ RF supported protocols /*-------------------RF2 topics & parameters----------------------*/ //433Mhz newremoteswitch MQTT Subjects and keys -#define subjectMQTTtoRF2 Base_Topic Gateway_Name "/commands/MQTTtoRF2" -#define subjectRF2toMQTT Base_Topic Gateway_Name "/RF2toMQTT" -#define subjectGTWRF2toMQTT Base_Topic Gateway_Name "/433toMQTT" +#define subjectMQTTtoRF2 "/commands/MQTTtoRF2" +#define subjectRF2toMQTT "/RF2toMQTT" +#define subjectGTWRF2toMQTT "/433toMQTT" #define RF2codeKey "ADDRESS_" // code will be defined if a subject contains RF2codeKey followed by a value of 7 digits #define RF2periodKey "PERIOD_" // period will be defined if a subject contains RF2periodKey followed by a value of 3 digits #define RF2unitKey "UNIT_" // number of your unit value will be defined if a subject contains RF2unitKey followed by a value of 1-2 digits @@ -84,9 +84,9 @@ RF supported protocols /*-------------------ESPPiLight topics & parameters----------------------*/ //433Mhz Pilight MQTT Subjects and keys -#define subjectMQTTtoPilight Base_Topic Gateway_Name "/commands/MQTTtoPilight" -#define subjectPilighttoMQTT Base_Topic Gateway_Name "/PilighttoMQTT" -#define subjectGTWPilighttoMQTT Base_Topic Gateway_Name "/PilighttoMQTT" +#define subjectMQTTtoPilight "/commands/MQTTtoPilight" +#define subjectPilighttoMQTT "/PilighttoMQTT" +#define subjectGTWPilighttoMQTT "/PilighttoMQTT" #define PilightRAW "RAW" #define repeatPilightwMQTT false // do we repeat a received signal by using mqtt with Pilight gateway diff --git a/main/config_RFM69.h b/main/config_RFM69.h index 2da1d06..8e331c4 100644 --- a/main/config_RFM69.h +++ b/main/config_RFM69.h @@ -33,13 +33,13 @@ extern void MQTTtoRFM69(char * topicOri, JsonObject& RFdata); /*----------------------RFM69 topics & parameters -------------------------*/ // Topic where the message from RFM69 will be published by the gateway, // appended with the nodeID of the sender -#define subjectRFM69toMQTT Base_Topic Gateway_Name "/RFM69toMQTT" +#define subjectRFM69toMQTT "/RFM69toMQTT" // Topic subscribed by the gateway. Messages received will be sent to RFM69 -#define subjectMQTTtoRFM69 Base_Topic Gateway_Name "/commands/MQTTtoRFM69" +#define subjectMQTTtoRFM69 "/commands/MQTTtoRFM69" #define RFM69receiverKey "RCV_" // receiver id will be defined if a subject contains RFM69receiverKey followed by a value of 3 digits // On reception of an ack from RFM69, the message that has been sent is published here -#define subjectGTWRFM69toMQTT Base_Topic Gateway_Name "/RFM69toMQTT/acked" +#define subjectGTWRFM69toMQTT "/RFM69toMQTT/acked" #define defaultRFM69ReceiverId 99 // Default values diff --git a/main/config_SRFB.h b/main/config_SRFB.h index 16fca10..4bb0c5d 100644 --- a/main/config_SRFB.h +++ b/main/config_SRFB.h @@ -33,10 +33,10 @@ extern void MQTTtoSRFB(char * topicOri, char * datacallback); extern void MQTTtoSRFB(char * topicOri, JsonObject& RFdata); /*-------------------RF topics & parameters----------------------*/ //433Mhz MQTT Subjects and keys -#define subjectMQTTtoSRFB Base_Topic Gateway_Name "/commands/MQTTtoSRFB" -#define subjectMQTTtoSRFBRaw Base_Topic Gateway_Name "/commands/MQTTtoSRFB/Raw" -#define subjectSRFBtoMQTT Base_Topic Gateway_Name "/SRFBtoMQTT" -#define subjectGTWSRFBtoMQTT Base_Topic Gateway_Name "/SRFBtoMQTT" +#define subjectMQTTtoSRFB "/commands/MQTTtoSRFB" +#define subjectMQTTtoSRFBRaw "/commands/MQTTtoSRFB/Raw" +#define subjectSRFBtoMQTT "/SRFBtoMQTT" +#define subjectGTWSRFBtoMQTT "/SRFBtoMQTT" #define SRFBRptKey "RPT_" #define SRFBmaxipulselengthKey "Thigh_" #define SRFBminipulselengthKey "Tlow_" diff --git a/main/config_TSL2561.h b/main/config_TSL2561.h index 01ebb61..c07cf16 100644 --- a/main/config_TSL2561.h +++ b/main/config_TSL2561.h @@ -46,7 +46,7 @@ extern void TSL2561toMQTT(); #define TimeBetweenReadingtsl2561 30000 /*----------------------------USER PARAMETERS-----------------------------*/ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ -#define subjectTSL12561toMQTT Base_Topic Gateway_Name "/LIGHTtoMQTT" +#define subjectTSL12561toMQTT "/LIGHTtoMQTT" //Time used to wait for an interval before resending measured values unsigned long timetsl2561 = 0; diff --git a/main/config_mqttDiscovery.h b/main/config_mqttDiscovery.h index a017706..02e38de 100644 --- a/main/config_mqttDiscovery.h +++ b/main/config_mqttDiscovery.h @@ -38,7 +38,6 @@ extern void createDiscovery(char * sensor_type, #define discovery_Topic "homeassistant" -#define DEVICENAME "OpenMQTTGateway " Gateway_Name #define DEVICEMANUFACTURER "OMG_community" #endif \ No newline at end of file diff --git a/main/main.ino b/main/main.ino index 409607d..25e5bab 100644 --- a/main/main.ino +++ b/main/main.ino @@ -204,7 +204,7 @@ void trc(String msg){ digitalWrite(led_info, HIGH); #endif #ifdef subjectTRACEtoMQTT - pub(subjectTRACEtoMQTT,msg); + pubMQTT(subjectTRACEtoMQTT,msg); #endif } @@ -214,7 +214,7 @@ void trc(int msg){ digitalWrite(led_info, HIGH); #endif #ifdef subjectTRACEtoMQTT - pub(subjectTRACEtoMQTT,msg); + pubMQTT(subjectTRACEtoMQTT,msg); #endif } @@ -224,7 +224,7 @@ void trc(unsigned int msg){ digitalWrite(led_info, HIGH); #endif #ifdef subjectTRACEtoMQTT - pub(subjectTRACEtoMQTT,msg); + pubMQTT(subjectTRACEtoMQTT,msg); #endif } @@ -234,7 +234,7 @@ void trc(long msg){ digitalWrite(led_info, HIGH); #endif #ifdef subjectTRACEtoMQTT - pub(subjectTRACEtoMQTT,msg); + pubMQTT(subjectTRACEtoMQTT,msg); #endif } @@ -244,7 +244,7 @@ void trc(unsigned long msg){ digitalWrite(led_info, HIGH); #endif #ifdef subjectTRACEtoMQTT - pub(subjectTRACEtoMQTT,msg); + pubMQTT(subjectTRACEtoMQTT,msg); #endif } @@ -254,7 +254,7 @@ void trc(double msg){ digitalWrite(led_info, HIGH); #endif #ifdef subjectTRACEtoMQTT - pub(subjectTRACEtoMQTT,msg); + pubMQTT(subjectTRACEtoMQTT,msg); #endif } @@ -264,25 +264,25 @@ void trc(float msg){ digitalWrite(led_info, HIGH); #endif #ifdef subjectTRACEtoMQTT - pub(subjectTRACEtoMQTT,msg); + pubMQTT(subjectTRACEtoMQTT,msg); #endif } void trc(JsonObject& data){ - char JSONmessageBuffer[JSON_MSG_BUFFER]; - data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); - trc(JSONmessageBuffer); + char JSONmessageBuffer[JSON_MSG_BUFFER]; + data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); + trc(JSONmessageBuffer); } -void pub(char * topic, char * payload, bool retainFlag){ - client.publish(topic, payload, retainFlag); +void pub(char * topicori, char * payload, bool retainFlag){ + String topic = String(mqtt_topic) + String(topicori); + client.publish((char *)topic.c_str(), payload, retainFlag); } void pub(char * topicori, JsonObject& data){ if(client.connected()) { digitalWrite(led_receive, HIGH); - - String topic = topicori; + String topic = String(mqtt_topic) + String(topicori); #ifdef valueAsASubject unsigned long value = data["value"]; if (value != 0){ @@ -296,7 +296,7 @@ void pub(char * topicori, JsonObject& data){ trc(topic); data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); trc(JSONmessageBuffer); - pub(topic, JSONmessageBuffer); + pubMQTT(topic, JSONmessageBuffer); #endif #ifdef simplePublishing @@ -310,22 +310,22 @@ void pub(char * topicori, JsonObject& data){ trc(p.key); trc(p.value.as()); if (strcmp(p.key, "value") == 0){ // if data is a value we don't integrate the name into the topic - pub(topic,p.value.as()); + pubMQTT(topic,p.value.as()); }else{ // if data is not a value we integrate the name into the topic - pub(topic + "/" + String(p.key),p.value.as()); + pubMQTT(topic + "/" + String(p.key),p.value.as()); } }else if (p.value.is()) { trc(p.key); trc(p.value.as()); - pub(topic + "/" + String(p.key),p.value.as()); + pubMQTT(topic + "/" + String(p.key),p.value.as()); } else if (p.value.is()) { trc(p.key); trc(p.value.as()); - pub(topic + "/" + String(p.key),p.value.as()); + pubMQTT(topic + "/" + String(p.key),p.value.as()); } else if (p.value.is()) { trc(p.key); trc(p.value.as()); - pub(topic + "/" + String(p.key),p.value.as()); + pubMQTT(topic + "/" + String(p.key),p.value.as()); } } #endif @@ -334,76 +334,105 @@ void pub(char * topicori, JsonObject& data){ } } -void pub(char * topic, char * payload){ +void pub(char * topicori, char * payload){ + if(client.connected()) { + String topic = String(mqtt_topic) + String(topicori); + trc(F("Pub ack into:")); + trc(topic); + pubMQTT(topic, payload); + }else{ + trc("client not connected can't pub"); + } +} + +void pub_custom_topic(char * topicori, JsonObject& data){ + if(client.connected()) { + char JSONmessageBuffer[JSON_MSG_BUFFER]; + trc(F("Pub json discovery into:")); + trc(topicori); + data.printTo(JSONmessageBuffer, sizeof(JSONmessageBuffer)); + trc(JSONmessageBuffer); + pubMQTT(topicori, JSONmessageBuffer); + }else{ + trc("client not connected can't pub"); + } +} + +void pubMQTT(char * topic, char * payload){ client.publish(topic, payload); } -void pub(String topic, char * payload){ +void pubMQTT(String topic, char * payload){ client.publish((char *)topic.c_str(),payload); } -void pub(char * topic, unsigned long payload){ +void pubMQTT(char * topic, unsigned long payload){ char val[11]; sprintf(val, "%lu", payload); client.publish(topic,val); } -void pub(char * topic, String payload){ +void pubMQTT(char * topic, String payload){ client.publish(topic,(char *)payload.c_str()); } -void pub(String topic, String payload){ +void pubMQTT(String topic, String payload){ client.publish((char *)topic.c_str(),(char *)payload.c_str()); } -void pub(String topic, int payload){ +void pubMQTT(String topic, int payload){ char val[12]; sprintf(val, "%d", payload); client.publish((char *)topic.c_str(),val); } -void pub(String topic, float payload){ +void pubMQTT(String topic, float payload){ char val[12]; dtostrf(payload,3,1,val); client.publish((char *)topic.c_str(),val); } -void pub(char * topic, float payload){ +void pubMQTT(char * topic, float payload){ char val[12]; dtostrf(payload,3,1,val); client.publish(topic,val); } -void pub(char * topic, int payload){ +void pubMQTT(char * topic, int payload){ char val[6]; sprintf(val, "%d", payload); client.publish(topic,val); } -void pub(char * topic, unsigned int payload){ +void pubMQTT(char * topic, unsigned int payload){ char val[6]; sprintf(val, "%u", payload); client.publish(topic,val); } -void pub(char * topic, long payload){ +void pubMQTT(char * topic, long payload){ char val[11]; sprintf(val, "%l", payload); client.publish(topic,val); } -void pub(char * topic, double payload){ +void pubMQTT(char * topic, double payload){ char val[16]; sprintf(val, "%d", payload); client.publish(topic,val); } -void pub(String topic, unsigned long payload){ +void pubMQTT(String topic, unsigned long payload){ char val[11]; sprintf(val, "%lu", payload); client.publish((char *)topic.c_str(),val); } +char * catToMainTopic(char * toAdd){ + String resultTopic = String(mqtt_topic) + String(toAdd); + return (char *)resultTopic.c_str(); +} + void reconnect() { #ifndef ESPWifiManualSetup @@ -415,22 +444,18 @@ void reconnect() { // Loop until we're reconnected while (!client.connected()) { trc(F("MQTT connection...")); //F function enable to decrease sram usage - if (client.connect(Gateway_Name, mqtt_user, mqtt_pass, will_Topic, will_QoS, will_Retain, will_Message)) { + if (client.connect(gateway_name, mqtt_user, mqtt_pass, will_Topic, will_QoS, will_Retain, will_Message)) { trc(F("Connected to broker")); failure_number = 0; // Once connected, publish an announcement... pub(will_Topic,Gateway_AnnouncementMsg,will_Retain); // publish version pub(version_Topic,OMG_VERSION,will_Retain); - //Subscribing to topic - if (client.subscribe(subjectMQTTtoX)) { + if (client.subscribe(catToMainTopic(subjectMQTTtoX))) { #ifdef ZgatewayRF client.subscribe(subjectMultiGTWRF); // subject on which other OMG will publish, this OMG will store these msg and by the way don't republish them if they have been already published #endif - #ifdef ZgatewayRF315 - client.subscribe(subjectMultiGTWRF315);// subject on which other OMG will publish, this OMG will store these msg and by the way don't republish them if they have been already published - #endif #ifdef ZgatewayIR client.subscribe(subjectMultiGTWIR);// subject on which other OMG will publish, this OMG will store these msg and by the way don't republish them if they have been already published #endif @@ -450,7 +475,7 @@ void reconnect() { } trc(F("failed, rc=")); trc(client.state()); - delay(5000); + delay(10000); } } } @@ -473,6 +498,11 @@ void callback(char* topic, byte* payload, unsigned int length) { free(p); } +void setup_parameters() +{ + strcat(mqtt_topic,gateway_name); +} + void setup() { //Launch serial for debugging purposes @@ -563,6 +593,8 @@ void setup() #endif #endif + setup_parameters(); + client.setCallback(callback); delay(1500); @@ -632,7 +664,7 @@ void setup() trc(F("MQTT_MAX_PACKET_SIZE")); trc(MQTT_MAX_PACKET_SIZE); #if defined(ESP8266) || defined(ESP32) || defined(__AVR_ATmega2560__) || defined(__AVR_ATmega1280__) - if(MQTT_MAX_PACKET_SIZE == 128) trc(F("WRONG PUBSUBCLIENT LIBRARY USED PLEASE INSTALL THE ONE FROM OMG LIB FOLDER")); + if(MQTT_MAX_PACKET_SIZE == 128) trc(F("WRONG PUBSUBCLIENT LIBRARY USED PLEASE INSTALL THE ONE FROM RELEASE PAGE")); #endif trc(F("Setup OpenMQTTGateway end")); } @@ -693,14 +725,26 @@ void checkButton(){ // code from tzapu/wifimanager examples } } } +void eraseAndRestart(){ + #if defined(ESP8266) + WiFi.disconnect(true); + #else + WiFi.disconnect(true,true); + #endif + trc("Formatting requested, result:"); + trc(SPIFFS.format()); + #if defined(ESP8266) + ESP.reset(); + #else + ESP.restart(); + #endif +} void setup_wifimanager(bool reset_settings){ pinMode(TRIGGER_PIN, INPUT_PULLUP); - if(reset_settings) { - trc("Formatting requested, result:"); - trc(SPIFFS.format()); - } + + if(reset_settings) eraseAndRestart(); WiFi.mode(WIFI_STA); @@ -730,10 +774,12 @@ void setup_wifimanager(bool reset_settings){ json.printTo(Serial); if (json.success()) { trc(F("\nparsed json")); - strcpy(mqtt_server, json["mqtt_server"]); - strcpy(mqtt_port, json["mqtt_port"]); - strcpy(mqtt_user, json["mqtt_user"]); - strcpy(mqtt_pass, json["mqtt_pass"]); + if (json.containsKey("mqtt_server")) strcpy(mqtt_server, json["mqtt_server"]); + if (json.containsKey("mqtt_port")) strcpy(mqtt_port, json["mqtt_port"]); + if (json.containsKey("mqtt_user")) strcpy(mqtt_user, json["mqtt_user"]); + if (json.containsKey("mqtt_pass")) strcpy(mqtt_pass, json["mqtt_pass"]); + if (json.containsKey("mqtt_topic")) strcpy(mqtt_topic, json["mqtt_topic"]); + if (json.containsKey("gateway_name")) strcpy(gateway_name, json["gateway_name"]); } else { trc(F("failed to load json config")); } @@ -743,11 +789,13 @@ void setup_wifimanager(bool reset_settings){ // The extra parameters to be configured (can be either global or just in the setup) // After connecting, parameter.getValue() will get you the configured value // id/name placeholder/prompt default length - WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, 40); + WiFiManagerParameter custom_mqtt_server("server", "mqtt server", mqtt_server, parameters_size); WiFiManagerParameter custom_mqtt_port("port", "mqtt port", mqtt_port, 6); - WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, 20); - WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, 30); - + WiFiManagerParameter custom_mqtt_user("user", "mqtt user", mqtt_user, parameters_size); + WiFiManagerParameter custom_mqtt_pass("pass", "mqtt pass", mqtt_pass, parameters_size); + WiFiManagerParameter custom_mqtt_topic("topic", "mqtt base topic", mqtt_topic, mqtt_topic_max_size); + WiFiManagerParameter custom_gateway_name("name", "gateway name", gateway_name, parameters_size * 2); + //WiFiManager //Local intialization. Once its business is done, there is no need to keep it around @@ -766,16 +814,9 @@ void setup_wifimanager(bool reset_settings){ wifiManager.addParameter(&custom_mqtt_port); wifiManager.addParameter(&custom_mqtt_user); wifiManager.addParameter(&custom_mqtt_pass); + wifiManager.addParameter(&custom_gateway_name); + wifiManager.addParameter(&custom_mqtt_topic); - if(reset_settings){ - wifiManager.resetSettings(); - wifiManager.erase(); - #if defined(ESP8266) - ESP.reset(); - #else - ESP.restart(); - #endif - } //set minimum quality of signal so it ignores AP's under that quality wifiManager.setMinimumSignalQuality(MinimumWifiSignalQuality); @@ -802,7 +843,9 @@ void setup_wifimanager(bool reset_settings){ strcpy(mqtt_port, custom_mqtt_port.getValue()); strcpy(mqtt_user, custom_mqtt_user.getValue()); strcpy(mqtt_pass, custom_mqtt_pass.getValue()); - + strcpy(mqtt_topic, custom_mqtt_topic.getValue()); + strcpy(gateway_name, custom_gateway_name.getValue()); + //save the custom parameters to FS if (shouldSaveConfig) { trc(F("saving config")); @@ -812,6 +855,8 @@ void setup_wifimanager(bool reset_settings){ json["mqtt_port"] = mqtt_port; json["mqtt_user"] = mqtt_user; json["mqtt_pass"] = mqtt_pass; + json["mqtt_topic"] = mqtt_topic; + json["gateway_name"] = gateway_name; File configFile = SPIFFS.open("/config.json", "w"); if (!configFile) { @@ -1227,7 +1272,7 @@ void receivingMQTT(char * topicOri, char * datacallback) { #ifdef ZactuatorFASTLED MQTTtoFASTLEDJSON(topicOri, jsondata); #endif - + MQTTtoSYS(topicOri, jsondata); } else { // not a json object --> simple decoding #ifdef simpleReceiving @@ -1269,3 +1314,27 @@ void receivingMQTT(char * topicOri, char * datacallback) { //YELLOW OFF digitalWrite(led_send, HIGH); } + +void MQTTtoSYS(char * topicOri, JsonObject& SYSdata) { // json object decoding + if (strstr(topicOri,catToMainTopic(subjectMQTTtoSYSset)) != NULL){ + trc(F("MQTTtoSYS json set")); + #if defined(ESP8266) || defined(ESP32) + if (SYSdata.containsKey("cmd")){ + trc(F("Command")); + const char * cmd = SYSdata["cmd"]; + trc(cmd); + if (strstr(cmd,restartCmd) != NULL) {//restart + #if defined(ESP8266) + ESP.reset(); + #else + ESP.restart(); + #endif + }else if (strstr(cmd,eraseCmd) != NULL) {//erase and restart + setup_wifimanager(true); + }else{ + trc(F("wrong command")); + } + } + #endif + } +} diff --git a/test/Test_config.h b/test/Test_config.h index cb8859f..825786c 100644 --- a/test/Test_config.h +++ b/test/Test_config.h @@ -31,7 +31,7 @@ #ifndef user_config_h #define user_config_h /*-------------------VERSION----------------------*/ -#define OMG_VERSION "0.9.2beta" +#define OMG_VERSION "0.9.3beta" /*-------------CONFIGURE WIFIMANAGER-------------*/ /* @@ -49,6 +49,10 @@ * Otherwise you can provide these credentials on the web interface after connecting * to the access point with your password (SSID: WifiManager_ssid, password: WifiManager_password) */ +#ifndef Gateway_Name + #define Gateway_Name "OpenMQTTGateway" +#endif +#define Base_Topic "home/" #if defined(ESP8266) // for nodemcu, weemos and esp8266 //#define ESPWifiManualSetup true //uncomment you don't want to use wifimanager for your credential settings on ESP @@ -62,15 +66,17 @@ /*-------------DEFINE YOUR MQTT PARAMETERS BELOW----------------*/ //MQTT Parameters definition //#define mqtt_server_name "www.mqtt_broker.com" // instead of defining the server by its IP you can define it by its name, uncomment this line and set the correct MQTT server host name -char mqtt_user[20] = "your_username"; // not compulsory only if your broker needs authentication -char mqtt_pass[30] = "your_password"; // not compulsory only if your broker needs authentication -char mqtt_server[40] = "192.168.1.17"; +#define parameters_size 20 +#define mqtt_topic_max_size 100 +char mqtt_user[parameters_size] = "your_username"; // not compulsory only if your broker needs authentication +char mqtt_pass[parameters_size] = "your_password"; // not compulsory only if your broker needs authentication +char mqtt_server[parameters_size] = "192.168.1.17"; char mqtt_port[6] = "1883"; +char mqtt_topic[mqtt_topic_max_size] = Base_Topic; +char gateway_name[parameters_size * 2] = Gateway_Name; -#define Gateway_Name "OpenMQTTGateway" -#define Base_Topic "home/" -#define version_Topic Base_Topic Gateway_Name "/version" -#define will_Topic Base_Topic Gateway_Name "/LWT" +#define version_Topic "/version" +#define will_Topic "/LWT" #define will_QoS 0 #define will_Retain true #define will_Message "Offline" @@ -140,10 +146,15 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress /*-------------------CHANGING THEM IS NOT COMPULSORY-----------------------*/ /*--------------MQTT general topics-----------------*/ // global MQTT subject listened by the gateway to execute commands (send RF, IR or others) -#define subjectMQTTtoX Base_Topic Gateway_Name "/commands/#" -#define subjectMultiGTWKey "toMQTT" -#define subjectGTWSendKey "MQTTto" +#define subjectMQTTtoX "/commands/#" +#define subjectMultiGTWKey "toMQTT" +#define subjectGTWSendKey "MQTTto" +// key used for launching commands to the gateway +#define restartCmd "restart" +#define eraseCmd "erase" + +// define if we concatenate the values into the topic #define valueAsASubject true //variables to avoid duplicates @@ -159,6 +170,7 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #define JSON_MSG_BUFFER 64 // Json message max buffer size, don't put 1024 or higher it is causing unexpected behaviour on ESP8266 #endif #define TimeBetweenReadingSYS 120000 // time between system readings (like memory) -#define subjectSYStoMQTT Base_Topic Gateway_Name "/SYStoMQTT" +#define subjectSYStoMQTT "/SYStoMQTT" +#define subjectMQTTtoSYSset "/commands/MQTTtoSYS/set" #endif