forked from 1technophile/OpenMQTTGateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ZgatewayIR.ino
264 lines (248 loc) · 8.05 KB
/
ZgatewayIR.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
/*
OpenMQTTGateway - ESP8266 or Arduino program for home automation
Act as a wifi or ethernet gateway between your 433mhz/infrared IR signal and a MQTT broker
Send and receiving command by MQTT
This gateway enables to:
- receive MQTT data from a topic and send IR signal corresponding to the received MQTT data
- publish MQTT data to a different topic related to received IR signal
Copyright: (c)Florian ROBERT
This file is part of OpenMQTTGateway.
OpenMQTTGateway is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
OpenMQTTGateway is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifdef ZgatewayIR
#ifdef ESP8266
#include <IRremoteESP8266.h>
#include <IRsend.h> // Needed if you want to send IR commands.
#include <IRrecv.h> // Needed if you want to receive IR commands.
IRrecv irrecv(IR_RECEIVER_PIN);
IRsend irsend(IR_EMITTER_PIN);
#else
#include <IRremote.h>
IRrecv irrecv(IR_RECEIVER_PIN);
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
void setupIR()
{
//IR init parameters
#ifdef ESP8266
irsend.begin();
#endif
irrecv.enableIRIn(); // Start the receiver
}
boolean IRtoMQTT(){
decode_results results;
if (irrecv.decode(&results)){
trc(F("Rcv. IR"));
unsigned long MQTTvalue = 0;
String MQTTprotocol;
String MQTTbits;
MQTTvalue = results.value;
MQTTprotocol = String(results.decode_type);
MQTTbits = String(results.bits);
String rawCode = "";
// Dump data
for (int i = 1; i < results.rawlen; i++) {
rawCode = rawCode + (results.rawbuf[i] * USECPERTICK);
if ( i < results.rawlen-1 ) rawCode = rawCode + ","; // ',' not needed on last one
}
trc(rawCode);
irrecv.resume(); // Receive the next value
if (pubIRunknownPrtcl == false && MQTTprotocol == "-1"){ // don't publish unknown IR protocol
trc(F("--no pub. unknown protocol--"));
} else if (!isAduplicate(MQTTvalue) && MQTTvalue!=0) {// conditions to avoid duplications of RF -->MQTT
trc(F("Adv data IRtoMQTT"));
client.publish(subjectIRtoMQTTprotocol,(char *)MQTTprotocol.c_str());
client.publish(subjectIRtoMQTTbits,(char *)MQTTbits.c_str());
client.publish(subjectIRtoMQTTRaw,(char *)rawCode.c_str());
trc(F("Sending IRtoMQTT"));
String value = String(MQTTvalue);
trc(value);
boolean result = client.publish(subjectIRtoMQTT,(char *)value.c_str());
if (repeatIRwMQTT){
trc(F("Pub. IR for repeat"));
client.publish(subjectMQTTtoIR,(char *)value.c_str());
}
return result;
}
}
return false;
}
void MQTTtoIR(char * topicOri, char * datacallback) {
// IR DATA ANALYSIS
//send received MQTT value by IR signal
boolean signalSent = false;
uint64_t data = 0;
String strcallback = String(datacallback);
trc(String(datacallback));
int s = strcallback.length();
//number of "," value count
int count = 0;
for(int i = 0; i < s; i++)
{
if (datacallback[i] == ',') {
count++;
}
}
if(count == 0){
data = strtoul(datacallback, NULL, 10); // standard sending with unsigned long, we will not be able to pass values > 4294967295
}
#ifdef IR_GC
else if(strstr(topicOri, "IR_GC") != NULL){ // sending GC data from https://irdb.globalcache.com
trc("IR_GC");
//buffer allocation from char datacallback
uint16_t GC[count+1];
String value = "";
int j = 0;
for(int i = 0; i < s; i++)
{
if (datacallback[i] != ',') {
value = value + String(datacallback[i]);
}
if ((datacallback[i] == ',') || (i == s - 1))
{
GC[j]= value.toInt();
value = "";
j++;
}
}
irsend.sendGC(GC, j);
signalSent = true;
}
#endif
#ifdef IR_Raw
else if(strstr(topicOri, "IR_Raw") != NULL){ // sending Raw data
trc("IR_Raw");
//buffer allocation from char datacallback
uint16_t Raw[count+1];
String value = "";
int j = 0;
for(int i = 0; i < s; i++)
{
if (datacallback[i] != ',') {
value = value + String(datacallback[i]);
}
if ((datacallback[i] == ',') || (i == s - 1))
{
Raw[j]= value.toInt();
value = "";
j++;
}
}
irsend.sendRaw(Raw, j, 38); // frequency hardcoded as a first approach for test purposes
signalSent = true;
}
#endif
//We look into the subject to see if a special Bits number is defined
String topic = topicOri;
unsigned int valueBITS = 0;
int pos = topic.lastIndexOf(IRbitsKey);
if (pos != -1){
pos = pos + +strlen(IRbitsKey);
valueBITS = (topic.substring(pos,pos + 2)).toInt();
trc(F("Bits nb:"));
trc(String(valueBITS));
}
//We look into the subject to see if a special repeat number is defined
int valueRPT = 0;
int pos2 = topic.lastIndexOf(IRRptKey);
if (pos2 != -1) {
pos2 = pos2 + strlen(IRRptKey);
valueRPT = (topic.substring(pos2,pos2 + 1)).toInt();
trc(F("IR repeat:"));
trc(String(valueRPT));
}
#ifdef ESP8266 // send coolix not available for arduino IRRemote library
#ifdef IR_COOLIX
if (strstr(topicOri, "IR_COOLIX") != NULL){
if (valueBITS == 0) valueBITS = 24;
irsend.sendCOOLIX(data, valueBITS);
signalSent = true;
}
#endif
#endif
if (strstr(topicOri, "IR_NEC") != NULL || strstr(topicOri, subjectMQTTtoIR) != NULL ){
if (valueBITS == 0) valueBITS = 32;
irsend.sendNEC(data, valueBITS);
signalSent = true;
}
#ifdef IR_Whynter
if (strstr(topicOri, "IR_Whynter") != NULL){
if (valueBITS == 0) valueBITS = 32;
irsend.sendWhynter(data, valueBITS);
signalSent = true;
}
#endif
#ifdef IR_LG
if (strstr(topicOri, "IR_LG") != NULL){
if (valueBITS == 0) valueBITS = 28;
irsend.sendLG(data, valueBITS);
signalSent = true;
}
#endif
#ifdef IR_Sony
if (strstr(topicOri, "IR_Sony") != NULL){
if (valueBITS == 0) valueBITS = 12;
if (valueRPT == 0) valueRPT = 2;
irsend.sendSony(data, valueBITS, valueRPT);
signalSent = true;
}
#endif
#ifdef IR_DISH
if (strstr(topicOri, "IR_DISH") != NULL){
if (valueBITS == 0) valueBITS = 16;
irsend.sendDISH(data, valueBITS);
signalSent = true;
}
#endif
#ifdef IR_RC5
if (strstr(topicOri, "IR_RC5") != NULL){
if (valueBITS == 0) valueBITS = 12;
irsend.sendRC5(data, valueBITS);
signalSent = true;
}
#endif
#ifdef IR_Sharp
if (strstr(topicOri, "IR_Sharp") != NULL){
if (valueBITS == 0) valueBITS = 15;
irsend.sendSharpRaw(data, valueBITS);
signalSent = true;
}
#endif
#ifdef IR_SAMSUNG
if (strstr(topicOri, "IR_SAMSUNG") != NULL){
if (valueBITS == 0) valueBITS = 32;
irsend.sendSAMSUNG(data, valueBITS);
signalSent = true;
}
#endif
#ifdef IR_PANASONIC
if (strstr(topicOri, "IR_PANASONIC") != NULL){
irsend.sendPanasonic(PanasonicAddress, data);
signalSent = true;
}
#endif
#ifdef IR_RCMM
if (strstr(topicOri, "IR_RCMM") != NULL){
if (valueBITS == 0) valueBITS = 32;
irsend.sendRCMM(data, valueBITS);
signalSent = true;
}
#endif
if (signalSent){ // 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
boolean result = client.publish(subjectGTWIRtoMQTT, datacallback);
if (result){
trc(F("MQTTtoIR ack pub."));
};
}
irrecv.enableIRIn(); // ReStart the IR receiver (if not restarted it is not able to receive data)
}
#endif