From e3e700449b4b3437507a3a0e013705fc43c40592 Mon Sep 17 00:00:00 2001 From: Tamas DAJKA Date: Wed, 26 Apr 2023 00:13:01 +0200 Subject: [PATCH] [RF] Added Pilight RAW packet return support (#1604) * Added Pilight RAW packet return support - can be enabled in User_config.h --- docs/use/rf.md | 20 ++++++++++++++++++- main/ZgatewayPilight.ino | 42 ++++++++++++++++++++++++++++++++++++++++ main/config_RF.h | 1 + 3 files changed, 62 insertions(+), 1 deletion(-) diff --git a/docs/use/rf.md b/docs/use/rf.md index c6827e3..f18487b 100644 --- a/docs/use/rf.md +++ b/docs/use/rf.md @@ -263,6 +263,24 @@ Generate your RF signals by pressing a remote button or other and you will see : ![](../img/OpenMQTTGateway_Pilight_Digoo-DG-R8S.png) +#### Enabling RAW packet return support +First, you need to compile a binary with `Pilight_rawEnabled true` uncommented in config_RF.h. + +Once the device is online, you can turn on the RAW packet return support with the following MQTT command: + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols" -m '{"rawEnabled":true}'` + +The returned JSON looks like this: +`Client (null) received PUBLISH (d0, q0, r0, m0, 'home/OpenMQTTGateway/PilighttoMQTT', ... (176 bytes)) +{"format":"RAW","rawlen":106,"pulsesString":"c:0102010102020202020101010101010102020201020102020202020201010101010101010101010102010102010201020201010203;p:521,944,1924,3845@"}` + +The pulseString format is Pilight's native. For those who are not familiar with it: +c:* are the indexes for the p:* array, which are the different pulse length. (e.g. pulse[0] = 521ms, pulse[1]=944ms..., so c[0], which is a '0' = 521ms pulse, c[1], which is a '1' =944ms pulse etc) + +After gathering all the packets you need, simply turn off the RAW packet support via MQTT: + +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols" -m '{"rawEnabled":false}'` + ### 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. @@ -279,7 +297,7 @@ eg: `mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols" -m #### Reset protocols To reset and listen to all protocols - -`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols -m '{"reset": true}`' +`mosquitto_pub -t "home/OpenMQTTGateway/commands/MQTTtoPilight/protocols -m '{"reset": true}'` #### Enabled protocols To list the enabled protocols on the Serial - diff --git a/main/ZgatewayPilight.ino b/main/ZgatewayPilight.ino index 20c6eb9..c84fead 100644 --- a/main/ZgatewayPilight.ino +++ b/main/ZgatewayPilight.ino @@ -36,6 +36,11 @@ # include ESPiLight rf(RF_EMITTER_GPIO); // use -1 to disable transmitter +# ifdef Pilight_rawEnabled +// raw output support +bool pilightRawEnabled = 0; +# endif + void pilightCallback(const String& protocol, const String& message, int status, size_t repeats, const String& deviceID) { if (status == VALID) { @@ -78,6 +83,28 @@ void pilightCallback(const String& protocol, const String& message, int status, } } +# ifdef Pilight_rawEnabled +void pilightRawCallback(const uint16_t* pulses, size_t length) { + uint16_t pulse; + + if (!pilightRawEnabled) { + Log.trace(F("Pilight RAW not enabled" CR)); + return; + } + + Log.trace(F("Creating RF PiLight buffer" CR)); + StaticJsonDocument jsonBuffer; + JsonObject RFPiLightdata = jsonBuffer.to(); + + RFPiLightdata["format"] = "RAW"; + RFPiLightdata["rawlen"] = length; + RFPiLightdata["pulsesString"] = rf.pulseTrainToString(pulses, length); // c=pulse_array_key;p=pulse_types + + // publish data + pub(subjectPilighttoMQTT, RFPiLightdata); +} +# endif + void setupPilight() { # ifdef ZradioCC1101 //receiving with CC1101 ELECHOUSE_cc1101.Init(); @@ -157,6 +184,16 @@ void MQTTtoPilight(char* topicOri, JsonObject& Pilightdata) { Log.notice(F("PiLight protocols available: %s" CR), rf.availableProtocols().c_str()); success = true; } +# ifdef Pilight_rawEnabled + if (Pilightdata.containsKey("rawEnabled")) { + Log.notice(F("Setting PiLight raw output enabled: %s" CR), Pilightdata["rawEnabled"]); + pilightRawEnabled = (bool)Pilightdata["rawEnabled"]; + disablePilightReceive(); + delay(1); + enablePilightReceive(); + success = true; + } +# endif 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 @@ -297,6 +334,11 @@ extern void enablePilightReceive() { ELECHOUSE_cc1101.SetRx(receiveMhz); // set Receive on # endif rf.setCallback(pilightCallback); +# ifdef Pilight_rawEnabled + if (pilightRawEnabled) { + rf.setPulseTrainCallBack(pilightRawCallback); + } +# endif 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(); diff --git a/main/config_RF.h b/main/config_RF.h index 7cfb8ba..e89f696 100644 --- a/main/config_RF.h +++ b/main/config_RF.h @@ -167,6 +167,7 @@ const char parameters[40][4][24] = { #define subjectPilighttoMQTT "/PilighttoMQTT" #define subjectGTWPilighttoMQTT "/PilighttoMQTT" #define repeatPilightwMQTT false // do we repeat a received signal by using MQTT with Pilight gateway +//#define Pilight_rawEnabled true // enables Pilight RAW return - switchable via MQTT /*-------------------RTL_433 topics & parameters----------------------*/ //433Mhz RTL_433 MQTT Subjects and keys