diff --git a/docs/use/rf.md b/docs/use/rf.md index b8442a9..e79f5e8 100644 --- a/docs/use/rf.md +++ b/docs/use/rf.md @@ -122,6 +122,29 @@ Generate your RF signals by pressing a remote button or other and you will see : ![](../img/OpenMQTTGateway_Pilight_Digoo-DG-R8S.png) +### Limit Protocols +It is possible to limit the protocols that Pilight will respond to, this can help reduce noise from unwanted devices and in some cases disable conflicting protocols. + +#### Available protocols +To list the available protocols on the Serial - + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols" -m '{"available":true}'` +#### Limit protocols +To limit the protocols, send a JSON array of protocols as below - + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols -m '{"limit": ["array", "of", "protocols"]}'` + +eg: `mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols" -m '{"limit":["tfa", "ev1527"}'` + +#### Reset protocols +To reset and listen to all protocols - +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols -m '{"reset": true}`' + +#### Enabled protocols +To list the enabled protocols on the Serial - + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols" -m '{"enabled":true}'` + ### Send data by MQTT to transmit a RF signal #### Using a known protocol diff --git a/main/ZgatewayPilight.ino b/main/ZgatewayPilight.ino index 23449d7..6c01a5a 100644 --- a/main/ZgatewayPilight.ino +++ b/main/ZgatewayPilight.ino @@ -93,12 +93,78 @@ void setupPilight() { Log.trace(F("ZgatewayPilight setup done " CR)); } +void savePilightConfig() { + Log.trace(F("saving Pilight config" CR)); + DynamicJsonDocument json(4096); + deserializeJson(json, rf.enabledProtocols()); + + File configFile = SPIFFS.open("/pilight.json", "w"); + if (!configFile) { + Log.error(F("failed to open config file for writing" CR)); + } + + serializeJsonPretty(json, Serial); + serializeJson(json, configFile); + configFile.close(); +} + +void loadPilightConfig() { + Log.trace(F("reading Pilight config file" CR)); + File configFile = SPIFFS.open("/pilight.json", "r"); + if (configFile) { + Log.trace(F("opened Pilight config file" CR)); + DynamicJsonDocument json(configFile.size() * 4); + auto error = deserializeJson(json, configFile); + if (error) { + Log.error(F("deserialize config failed: %s, buffer capacity: %u" CR), error.c_str(), json.capacity()); + } + serializeJson(json, Serial); + if (!json.isNull()) { + String rflimit; + serializeJson(json, rflimit); + rf.limitProtocols(rflimit); + } else { + Log.warning(F("failed to load json config" CR)); + } + configFile.close(); + } +} + void PilighttoMQTT() { rf.loop(); } void MQTTtoPilight(char* topicOri, JsonObject& Pilightdata) { - if (cmpToMainTopic(topicOri, subjectMQTTtoPilight)) { + if (cmpToMainTopic(topicOri, subjectMQTTtoPilightProtocol)) { + bool success = false; + if (Pilightdata.containsKey("reset")) { + rf.limitProtocols(rf.availableProtocols()); + savePilightConfig(); + success = true; + } + if (Pilightdata.containsKey("limit")) { + String output; + serializeJson(Pilightdata["limit"], output); + rf.limitProtocols(output); + savePilightConfig(); + success = true; + } + if (Pilightdata.containsKey("enabled")) { + Log.notice(F("PiLight protocols enabled: %s" CR), rf.enabledProtocols().c_str()); + success = true; + } + if (Pilightdata.containsKey("available")) { + Log.notice(F("PiLight protocols available: %s" CR), rf.availableProtocols().c_str()); + success = true; + } + + if (success) { + pub(subjectGTWPilighttoMQTT, Pilightdata); // 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 { + pub(subjectGTWPilighttoMQTT, "{\"Status\": \"Error\"}"); // Fail feedback + Log.error(F("MQTTtoPilightProtocol Fail json" CR)); + } + } else if (cmpToMainTopic(topicOri, subjectMQTTtoPilight)) { const char* message = Pilightdata["message"]; const char* protocol = Pilightdata["protocol"]; const char* raw = Pilightdata["raw"]; @@ -227,5 +293,6 @@ extern void enablePilightReceive() { rf.initReceiver(RF_RECEIVER_GPIO); pinMode(RF_EMITTER_GPIO, OUTPUT); // Set this here, because if this is the RX pin it was reset to INPUT by Serial.end(); rf.enableReceiver(); + loadPilightConfig(); }; #endif diff --git a/main/config_RF.h b/main/config_RF.h index ec8032b..4e4302d 100644 --- a/main/config_RF.h +++ b/main/config_RF.h @@ -101,10 +101,11 @@ int minimumRssi = 0; /*-------------------ESPPiLight topics & parameters----------------------*/ //433Mhz Pilight MQTT Subjects and keys -#define subjectMQTTtoPilight "/commands/MQTTtoPilight" -#define subjectPilighttoMQTT "/PilighttoMQTT" -#define subjectGTWPilighttoMQTT "/PilighttoMQTT" -#define repeatPilightwMQTT false // do we repeat a received signal by using mqtt with Pilight gateway +#define subjectMQTTtoPilight "/commands/MQTTtoPilight" +#define subjectMQTTtoPilightProtocol "/commands/MQTTtoPilight/protocols" +#define subjectPilighttoMQTT "/PilighttoMQTT" +#define subjectGTWPilighttoMQTT "/PilighttoMQTT" +#define repeatPilightwMQTT false // do we repeat a received signal by using mqtt with Pilight gateway /*-------------------RTL_433 topics & parameters----------------------*/ //433Mhz RTL_433 MQTT Subjects and keys