diff --git a/OpenMQTTGateway.ino b/OpenMQTTGateway.ino index 3e55ee9..189bb64 100644 --- a/OpenMQTTGateway.ino +++ b/OpenMQTTGateway.ino @@ -35,6 +35,7 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT */ #include "User_config.h" #include +#include // array to store previous received RFs, IRs codes and their timestamps unsigned long ReceivedSignal[10][2] ={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}; @@ -55,7 +56,7 @@ void callback(char*topic, byte* payload,unsigned int length); PubSubClient client(mqtt_server, mqtt_port, callback, eClient); //MQTT last attemps reconnection date -int lastReconnectAttempt = 0; +unsigned int lastReconnectAttempt = 0; boolean reconnect() { // Loop until we're reconnected @@ -175,20 +176,22 @@ void loop() } else { //connected // MQTT loop client.loop(); + #ifdef ZsensorDHT MeasureTempAndHum(); //Addon to measure the temperature with a DHT #endif - // Receive loop, if data received by RF433 or IR send it by MQTT - #ifdef ZgatewayRF - boolean resultRF = RFtoMQTT(); - if(resultRF) - trc(F("RF successfully sent by MQTT")); - #endif - #ifdef ZgatewayIR - boolean resultIR = IRtoMQTT(); - if(resultIR) - trc(F("IR successfully sent by MQTT")); - #endif + // Receive loop, if data received by RF433 or IR send it by MQTT + #ifdef ZgatewayRF + boolean resultRF = RFtoMQTT(); + if(resultRF) + trc(F("RF successfully sent by MQTT")); + #endif + #ifdef ZgatewayIR + boolean resultIR = IRtoMQTT(); + if(resultIR) + trc(F("IR successfully sent by MQTT")); + #endif + } delay(100); } @@ -240,23 +243,22 @@ return false; } void receivingMQTT(char * topicOri, char * datacallback) { - String topic = topicOri; trc(F("Receiving data by MQTT")); - trc(topic); + trc(String(topicOri)); trc(F("Callback value")); trc(String(datacallback)); unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295 trc(F("Converted value to unsigned long")); trc(String(data)); - // Storing data received - int pos0 = topic.lastIndexOf(subjectMultiGTWKey); - if (pos0 != -1){ - trc(F("Storing signal")); - storeValue(data); - trc(F("Data stored")); - } + if (strstr(topicOri, subjectMultiGTWKey) != NULL) + { + trc(F("Storing signal")); + storeValue(data); + trc(F("Data stored")); + } + #ifdef ZgatewayRF MQTTtoRF(topicOri, datacallback); diff --git a/User_config.h b/User_config.h index 78bfdee..ad49943 100644 --- a/User_config.h +++ b/User_config.h @@ -28,7 +28,7 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT /*-------------DEFINE YOUR MQTT & NETWORK PARAMETERS BELOW----------------*/ //MQTT Parameters definition #define mqtt_server "192.168.1.17" -#define mqtt_user "your_username" // not compulsory only if your broker needs authentication +//#define mqtt_user "your_username" // not compulsory only if your broker needs authentication #define mqtt_password "your_password" // not compulsory only if your broker needs authentication #define mqtt_port 1883 #define Gateway_Name "OpenMQTTGateway" @@ -43,14 +43,14 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT #define wifi_ssid "wifi ssid" #define wifi_password "wifi password" #else // for arduino + W5100 - const byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95 }; //W5100 ethernet shield mac adress + const byte mac[] PROGMEM = { 0xDE, 0xED, 0xBA, 0xFE, 0x54, 0x95 }; //W5100 ethernet shield mac adress #endif -const byte ip[] = { 192, 168, 1, 99 }; //ip adress +const byte ip[] PROGMEM = { 192, 168, 1, 99 }; //ip adress // Advanced network config (optional) if you want to use these parameters uncomment line 158, 172 and comment line 171 of OpenMQTTGateway.ino -const byte gateway[] = { 192, 168, 1, 1 }; //ip adress -const byte Dns[] = { 192, 168, 1, 1 }; //ip adress -const byte subnet[] = { 255, 255, 255, 0 }; //ip adress +const byte gateway[] PROGMEM = { 192, 168, 1, 1 }; //ip adress +const byte Dns[] PROGMEM = { 192, 168, 1, 1 }; //ip adress +const byte subnet[] PROGMEM = { 255, 255, 255, 0 }; //ip adress //Addons and module management, comment the line if you don't use //#define ZsensorDHT @@ -70,17 +70,20 @@ const byte subnet[] = { 255, 255, 255, 0 }; //ip adress #define subjectMultiGTWKey "toMQTT" //433Mhz MQTT Subjects and keys -#define subjectMQTTto433 "home/commands/MQTTto433" -#define subject433toMQTT "home/433toMQTT" +#define subjectMQTTtoRF "home/commands/MQTTto433" +#define subjectRFtoMQTT "home/433toMQTT" #define subjectGTWRFtoMQTT "home/433toMQTT" -#define subject433toMQTTAdvanced "home/433toMQTTAdvanced" +#define subjectRFtoMQTTport "home/433toMQTT/port" +#define subjectRFtoMQTTbits "home/433toMQTT/bits" +#define subjectRFtoMQTTlength "home/433toMQTT/length" #define RFprotocolKey "433_" // protocol will be defined if a subject contains RFprotocolKey followed by a value of 1 digit #define RFpulselengthKey "PLSL_" // pulselength will be defined if a subject contains RFprotocolKey followed by a value of 3 digits //IR MQTT Subjects #define subjectGTWIRtoMQTT "home/IRtoMQTT" #define subjectIRtoMQTT "home/IRtoMQTT" #define subjectMQTTtoIR "home/commands/MQTTtoIR" -#define subjectIRtoMQTTAdvanced "home/IRtoMQTTAdvanced" +#define subjectIRtoMQTTport "home/IRtoMQTT/port" +#define subjectIRtoMQTTbits "home/IRtoMQTT/bits" /* RF supported protocols 433_1 @@ -118,3 +121,4 @@ IR_Whynter //Do we want to see trace for debugging purposes #define TRACE 1 // 0= trace off 1 = trace on + diff --git a/ZgatewayIR.ino b/ZgatewayIR.ino index 559d36a..17159d4 100644 --- a/ZgatewayIR.ino +++ b/ZgatewayIR.ino @@ -37,7 +37,7 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT #ifdef ESP8266 #include - IRrecv irrecv(IR_RECEIVER_PIN); + IRrecv irrecv(IR_RECEIVER_PIN); IRsend irsend(IR_EMITTER_PIN); #else #include @@ -45,8 +45,6 @@ CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFT IRsend irsend; //connect IR emitter pin to D9 on arduino, you need to comment #define IR_USE_TIMER2 and uncomment #define IR_USE_TIMER1 on library IRremote.h so as to free pin D3 for RF RECEIVER PIN #endif -decode_results results; - void setupIR() { //IR init parameters @@ -58,16 +56,21 @@ void setupIR() } boolean IRtoMQTT(){ - unsigned long MQTTvalue = 0; - String valueAdvanced; + decode_results results; + if (irrecv.decode(&results)){ trc(F("Receiving IR signal")); - MQTTvalue=results.value; - valueAdvanced = "Value " + String(MQTTvalue)+" Bit " + String(results.bits) + " Protocol " + String(results.decode_type); + unsigned long MQTTvalue = 0; + int MQTTport = 0; + int MQTTbits = 0; + MQTTvalue = results.value; + MQTTport = results.decode_type; + MQTTbits = results.bits; irrecv.resume(); // Receive the next value if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT - trc(F("Sending advanced signal to MQTT")); - client.publish(subjectIRtoMQTTAdvanced,(char *)valueAdvanced.c_str()); + trc(F("Sending advanced data to MQTT")); + client.publish(subjectIRtoMQTTport,(char *)MQTTport); + client.publish(subjectIRtoMQTTbits,(char *)MQTTbits); trc(F("Sending IR to MQTT")); String value = String(MQTTvalue); trc(value); @@ -79,10 +82,8 @@ boolean IRtoMQTT(){ } void MQTTtoIR(char * topicOri, char * datacallback) { - String topic = topicOri; trc(F("Receiving data by MQTT")); - trc(topic); trc(F("Callback value")); trc(String(datacallback)); unsigned long data = strtoul(datacallback, NULL, 10); // we will not be able to pass values > 4294967295 @@ -93,40 +94,40 @@ void MQTTtoIR(char * topicOri, char * datacallback) { //send received MQTT value by IR signal (example of signal sent data = 1086296175) boolean signalSent = false; #ifdef ESP8266 // send coolix not available for arduino IRRemote library - if (topic.lastIndexOf("IR_COOLIX") != -1 ){ + if (strstr(topicOri, "IR_COOLIX") != NULL){ irsend.sendCOOLIX(data, 24); signalSent = true; } #endif - if (topic.lastIndexOf("IR_NEC")!= -1 || topic == subjectMQTTtoIR){ + if (strstr(topicOri, "IR_NEC") != NULL || strstr(topicOri, subjectMQTTtoIR) != NULL ){ irsend.sendNEC(data, 32); signalSent = true; } - if (topic.lastIndexOf("IR_Whynter")!=-1){ + if (strstr(topicOri, "IR_Whynter") != NULL){ irsend.sendWhynter(data, 32); signalSent = true; } - if (topic.lastIndexOf("IR_LG")!=-1){ + if (strstr(topicOri, "IR_LG") != NULL){ irsend.sendLG(data, 28); signalSent = true; } - if (topic.lastIndexOf("IR_Sony")!=-1){ + if (strstr(topicOri, "IR_Sony") != NULL){ irsend.sendSony(data, 12); signalSent = true; } - if (topic.lastIndexOf("IR_DISH")!=-1){ + if (strstr(topicOri, "IR_DISH") != NULL){ irsend.sendDISH(data, 16); signalSent = true; } - if (topic.lastIndexOf("IR_RC5")!=-1){ + if (strstr(topicOri, "IR_RC5") != NULL){ irsend.sendRC5(data, 12); signalSent = true; } - if (topic.lastIndexOf("IR_Sharp")!=-1){ + if (strstr(topicOri, "IR_Sharp") != NULL){ irsend.sendSharpRaw(data, 15); signalSent = true; } - if (topic.lastIndexOf("IR_SAMSUNG")!=-1){ + if (strstr(topicOri, "IR_SAMSUNG") != NULL){ irsend.sendSAMSUNG(data, 32); signalSent = true; } diff --git a/ZgatewayRF.ino b/ZgatewayRF.ino index a1cd173..18a5cdf 100644 --- a/ZgatewayRF.ino +++ b/ZgatewayRF.ino @@ -50,20 +50,27 @@ void setupRF() } boolean RFtoMQTT(){ - unsigned long MQTTvalue = 0; - String valueAdvanced; + if (mySwitch.available()){ trc(F("Receiving 433Mhz signal")); + unsigned long MQTTvalue = 0; + int MQTTport = 0; + int MQTTbits = 0; + int MQTTlength = 0; MQTTvalue = mySwitch.getReceivedValue(); - valueAdvanced = "Value " + String(MQTTvalue)+" Bit " + String(mySwitch.getReceivedBitlength()) + " Delay " + String(mySwitch.getReceivedDelay()) + " Protocol " + String(mySwitch.getReceivedProtocol()); + MQTTport = mySwitch.getReceivedProtocol(); + MQTTbits = mySwitch.getReceivedBitlength(); + MQTTlength = mySwitch.getReceivedDelay(); mySwitch.resetAvailable(); if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT trc(F("Sending advanced signal to MQTT")); - client.publish(subject433toMQTTAdvanced,(char *)valueAdvanced.c_str()); + client.publish(subjectRFtoMQTTport,(char *)MQTTport); + client.publish(subjectRFtoMQTTbits,(char *)MQTTbits); + client.publish(subjectRFtoMQTTlength,(char *)MQTTlength); trc(F("Sending RF to MQTT")); String value = String(MQTTvalue); trc(value); - boolean result = client.publish(subject433toMQTT,(char *)value.c_str()); + boolean result = client.publish(subjectRFtoMQTT,(char *)value.c_str()); return result; } } @@ -109,7 +116,7 @@ void MQTTtoRF(char * topicOri, char * datacallback) { trc(String(valuePLSL)); } - if ((topic == subjectMQTTto433) && (valuePRT == 0) && (valuePLSL == 0)){ + if ((topic == subjectMQTTtoRF) && (valuePRT == 0) && (valuePLSL == 0)){ trc(F("Sending data by RF, default parameters")); mySwitch.setProtocol(1,350); mySwitch.send(data, 24);