-
Notifications
You must be signed in to change notification settings - Fork 0
/
mqtt_esp8266_rfblaster.ino
479 lines (407 loc) · 11.5 KB
/
mqtt_esp8266_rfblaster.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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
/*
RFBLASTER - part of the TCL IOT project
Tim Waizenegger (c) 2015
MIT License
ESP8266 on arduino, pubsub mqtt client
*/
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
const char* ssid = "hive_iot";
const char* password = "xxx";
/*
These settings are for use with the IBM Watson IoT service. OtherMQTT brokers use different
account types. You need to modify the connect-call later where these values are used.
*/
const char* mq_id = "rf_blaster";
const char* mq_authtoken = "xxx";
const char* mq_serverUrl = "timsrv.de";
WiFiClient espClient;
PubSubClient client(mq_serverUrl, 1883, espClient);
#define RFPIN 12
#define RF_indicator_PIN 13
#define MQTTPIN 14
#define sigOn 0
#define sigOff 1
const long interval = 2000; // millis
unsigned long previousMillis = 0;
/////////////////////////////////////////////////////////////////
// help
int to_int(char const *s)
{
if ( s == NULL || *s == '\0' )
return -1;
bool negate = (s[0] == '-');
if ( *s == '+' || *s == '-' )
++s;
if ( *s == '\0')
return -1;
int result = 0;
while (*s)
{
if ( *s >= '0' && *s <= '9' )
{
result = result * 10 - (*s - '0'); //assume negative number
}
else
return -1;
++s;
}
return negate ? result : -result; //-result is positive!
}
/////////////////////////////////////////////////////////////////
// RF CODES WE KNOW
#define codesForType1Length 12
#define codesForType1Count 16
const char codesForType1[][codesForType1Length + 1] = {
"022202222222", // 1-1-on
"022202222220", // 1-1-off
"022220222222", // 1-2-on
"022220222220", // 1-2-off
"022222022222", // 1-3-on
"022222022220", // 1-3-off
"022222202222", // 1-4-on
"022222202220", // 1-4-off
"222002222222", // 4-1-on
"222002222220", // 4-1-off
"222020222222", // 4-2-on
"222020222220", // 4-2-off
"222022022222", // 4-3-on
"222022022220", // 4-3-off
"222022202222", // 4-4-on
"222022202220" // 4-4-off
};
#define codesForType2Length 33
#define codesForType2Count 6
const char codesForType2[][codesForType2Length + 1] = {
"213222123221322122232123132132221", // 1-on
"213222123221322122232123132222221", // 1-off
"213222123221322122232123132132212", // 2-on
"213222123221322122232123132222212", // 2-off
"213222123221322122232123132132131", // 3-on
"213222123221322122232123132222131" // 3-off
};
#define codesForType3Length 51
#define codesForType3Count 2
const char codesForType3[][codesForType3Length + 1] = {
"0000101101111110101100000s0000001100110110001100000", // 1-on
"0000110101100101100100000s0000011110110010101000000" // 1-off
};
/////////////////////////////////////////////////////////////////
// RF CODE STUFF
void setSigOn() {
digitalWrite(RFPIN, sigOn);
digitalWrite(RF_indicator_PIN, 1);
}
void setSigOff() {
digitalWrite(RFPIN, sigOff);
digitalWrite(RF_indicator_PIN, 0);
}
// this code has 1 long packet, followed by 3 short packets
// the long packet has a header, followed by 3 short suffixes
// the 3 suffixes and the 3 short packets are the same (except the last bit of each packet is a longer tailing pulse).
// the delay between the packets is 14c
// the delay between the suffixes in the first packet is 4c
void sendCodeType3(const char code[]) {
const int cycleLength = 500;
Serial.println(code);
for (char i = 0; i < 25; i++) {
switch (code[i]) {
case '0':
setSigOn();
delayMicroseconds(cycleLength);
setSigOff();
delayMicroseconds(2 * cycleLength);
break;
case '1':
setSigOn();
delayMicroseconds(2*cycleLength);
setSigOff();
delayMicroseconds(cycleLength);
break;
}
}
delayMicroseconds(3 * cycleLength); // pause is 4c, 1c was already spent in the header so we wait 3c here
for (char i = (0+25+1); i < (25+25+1); i++) { // skip the separator and continue with chars 26..EOL
switch (code[i]) {
case '0':
setSigOn();
delayMicroseconds(cycleLength);
setSigOff();
delayMicroseconds(2 * cycleLength);
break;
case '1':
setSigOn();
delayMicroseconds(2*cycleLength);
setSigOff();
delayMicroseconds(cycleLength);
break;
}
}
for (char i = (0+25+1); i < (25+25+1); i++) { // skip the separator and continue with chars 26..EOL
switch (code[i]) {
case '0':
setSigOn();
delayMicroseconds(cycleLength);
setSigOff();
delayMicroseconds(2 * cycleLength);
break;
case '1':
setSigOn();
delayMicroseconds(2*cycleLength);
setSigOff();
delayMicroseconds(cycleLength);
break;
}
}
for (char i = (0+25+1); i < (24+25+1); i++) { // skip the separator and continue with chars 26..EOL
switch (code[i]) {
case '0':
setSigOn();
delayMicroseconds(cycleLength);
setSigOff();
delayMicroseconds(2 * cycleLength);
break;
case '1':
setSigOn();
delayMicroseconds(2*cycleLength);
setSigOff();
delayMicroseconds(cycleLength);
break;
}
}
// send the tailing pulse
setSigOn();
delayMicroseconds(6*cycleLength);
setSigOff();
delayMicroseconds(14*cycleLength); // delay to next short packet
// looks like we don't need to send any more data... receivers already react at this point!
// the protocol would have (a minimum of) 3 additional packets of repeat-codes here...
}
void sendCodeType2(const char code[]) {
const int cycleLengthBpulse = 240;
const int cycleLengthBwait = 300;
const int cycleLengthBhold = 1360;
// init pulse
setSigOn();
delayMicroseconds(cycleLengthBpulse);
setSigOff();
delayMicroseconds(2 * cycleLengthBhold);
//Serial.println("sending code...");
for (char i = 0; i < codesForType2Length; i++) {
//Serial.println(i);
//Serial.println(code[i]);
switch (code[i]) {
case '3':
setSigOn();
delayMicroseconds(cycleLengthBpulse);
setSigOff();
delayMicroseconds(cycleLengthBwait);
case '2':
setSigOn();
delayMicroseconds(cycleLengthBpulse);
setSigOff();
delayMicroseconds(cycleLengthBwait);
case '1':
setSigOn();
delayMicroseconds(cycleLengthBpulse);
setSigOff();
delayMicroseconds(cycleLengthBwait);
break;
}
delayMicroseconds(cycleLengthBhold);
}
}
void sendCodeType1(const char code[]) {
//Serial.println("sending code...");
const int cycleLength = 120;
for (char i = 0; i < codesForType1Length; i++) {
//Serial.println(i);
//Serial.println(code[i]);
switch (code[i]) {
case '0':
setSigOn();
delayMicroseconds(4 * cycleLength);
setSigOff();
delayMicroseconds(12 * cycleLength);
setSigOn();
delayMicroseconds(4 * cycleLength);
setSigOff();
delayMicroseconds(12 * cycleLength);
break;
case '1':
setSigOn();
delayMicroseconds(12 * cycleLength);
setSigOff();
delayMicroseconds(4 * cycleLength);
setSigOn();
delayMicroseconds(12 * cycleLength);
setSigOff();
delayMicroseconds(4 * cycleLength);
break;
case '2':
setSigOn();
delayMicroseconds(4 * cycleLength);
setSigOff();
delayMicroseconds(12 * cycleLength);
setSigOn();
delayMicroseconds(12 * cycleLength);
setSigOff();
delayMicroseconds(4 * cycleLength);
break;
}
}
// sync pulse
setSigOn();
delayMicroseconds(4 * cycleLength);
setSigOff();
}
void sendType1(const char code[]) {
Serial.println("sending code type 1...");
for (char i = 0; i < 4; i++) {
sendCodeType1(code);
delay(15);
}
Serial.println("sending code type 1... done");
}
void sendType2(const char code[]) {
Serial.println("sending code type 2...");
for (char i = 0; i < 4; i++) {
sendCodeType2(code);
delay(15);
}
Serial.println("sending code type 2... done");
}
void sendType3(const char code[]) {
Serial.println("sending code type 3...");
for (char i = 0; i < 4; i++) {
sendCodeType3(code);
delay(15);
}
Serial.println("sending code type 3... done");
}
char currentCode1 = 0;
char currentCode2 = 0;
void sendDemo() {
Serial.println("sendDemo currentCode");
Serial.println(int(currentCode1));
Serial.println(int(currentCode2));
for (char i = 0; i < 10; i++) {
sendType1(codesForType1[currentCode1]);
delay(15);
}
for (char i = 0; i < 10; i++) {
sendType2(codesForType2[currentCode2]);
delay(15);
}
if (++currentCode1 == codesForType1Count) currentCode1 = 0;
if (++currentCode2 == codesForType2Count) currentCode2 = 0;
}
/////////////////////////////////////////////////////////////////
// WLAN/MQTT
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
delay(500);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
String msg = String("");
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
msg += (char)payload[i];
Serial.print((char)payload[i]);
}
Serial.println();
StaticJsonBuffer<200> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(msg);
if (!root.success())
{
Serial.println("parseObject() failed");
return;
}
if (root.containsKey("type1")) {
const char* code = root["type1"];
int c = to_int(code);
if ((c < 0) || (c >= codesForType1Count)) return;
sendType1(codesForType1[c]);
return;
}
if (root.containsKey("type2")) {
const char* code = root["type2"];
int c = to_int(code);
if ((c < 0) || (c >= codesForType2Count)) return;
sendType2(codesForType2[c]);
return;
}
if (root.containsKey("type3")) {
const char* code = root["type3"];
Serial.println(code);
int c = to_int(code);
Serial.println(c);
if ((c < 0) || (c >= codesForType3Count)) return;
sendType3(codesForType3[c]);
return;
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
digitalWrite(MQTTPIN, 0);
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mq_id, mq_id, mq_authtoken)) {
Serial.println("connected");
digitalWrite(MQTTPIN, 1);
// ... and resubscribe
client.subscribe("rf_blaster/code");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
/////////////////////////////////////////////////////////////////
// ARDUINO API
void setup() {
Serial.begin(57600);
pinMode(RFPIN, OUTPUT);
pinMode(RF_indicator_PIN, OUTPUT);
pinMode(MQTTPIN, OUTPUT);
setSigOff();
digitalWrite(MQTTPIN, 0);
setup_wifi();
client.setServer(mq_serverUrl, 1883);
client.setCallback(callback);
}
// the loop routine runs over and over again forever:
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
// unsigned long currentMillis = millis();
//
// // loop for 2 secs to make sure transmission has occured
// if (currentMillis - previousMillis >= 500) {
// previousMillis = currentMillis;
// sendDemo();
//
// }
}