-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathSN_GW_802154_VA1.ino
641 lines (520 loc) · 18.4 KB
/
SN_GW_802154_VA1.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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
/*
MQTT-SN to MQTT Gateway implementation based on MRF24J40 ESP12 NodeMCU with Arduino code
This is an MQTT-SN to MQTT Gateway implementation based on MRF24J40 and NodeMCU with Arduino code
The implementation is based ot MQTT-SN Specification Version 1.2.
An ESP12 NodeMCU module is used for both executing the Gateway functions and connecting to the Internet over WiFi.
Version Alpha 1 (VA1)
This is a very early implementation. Part of the whole message list according specs is supported so far.
Topics should be predefined on the client and gateway sides and are fixed to 2 positions alpha-numeric string.
Messages supported:
CONNECT
CONNACK
PINGREQ
PINGRESP
PUBLISH (both directions)
SUBSCRIBE
SUBACK
*/
#include <SPI.h>
#include <mrf24j.h> // *** Please use the modified library found within the same repo on GitHub ***
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
//#include <String.h>
//WiFi settings
const char* ssid = "Enter your WiFi SSID here";
const char* password = "Enter your WiFi Password here";
//MQTT Settings
const char* mqtt_server = "Enter your MQTT Server IP address here";
#define MQTT_CLIENT "SN_Gateway"
char conn_addr[5];
const char* conn_msg = "CONN";
const char* tout_msg = "TOUT";
const int pin_reset = 5; // GPIO 5, 6 = default
const int pin_cs = 15; // GPIO 15, 10 = default CS pin on ATmega8/168/328
const int pin_interrupt = 4; // GPIO 4, 2 = default interrupt pin on ATmega8/168/328
Mrf24j mrf(pin_reset, pin_cs, pin_interrupt);
WiFiClient espClient;
PubSubClient mqttClient(mqtt_server, 1883, espClient);
long current_time;
long last_time;
long tx_interval = 1000;
long last_update;
long update_interval = 10000;
boolean message_received = false;
boolean mqtt_msg_received = false;
char rx_buffer[127];
uint8_t tx_buffer[127];
uint8_t mqtt_rx_topic[3];
uint8_t mqtt_rx_payload[16]; // TBD
int rx_len;
typedef struct
{
uint8_t Length;
uint8_t MsgType;
uint8_t Var[127];
} Message;
Message Msg;
Message TxMsg;
typedef struct
{
uint8_t Flags;
uint8_t ProtocolID;
uint8_t Duration[2];
char ClientID[16];
} ConnectMsg;
ConnectMsg ConnMsg;
typedef struct
{
char ClientID[16];
} PingMsg;
PingMsg PingreqMsg;
typedef struct
{
uint16_t address;
boolean client_connected;
long timer;
long duration;
} Clients;
Clients clients[255];
typedef struct
{
uint8_t address[2];
uint8_t topic_id[2];
} Subscriptions;
Subscriptions sub_table[1024];
typedef struct
{
uint8_t Flags;
uint8_t topic_id[2];
uint8_t msg_id[2];
uint8_t data[15];
} PublishMsg;
PublishMsg pubMsg;
typedef struct
{
uint8_t Flags;
uint8_t msg_id[2];
uint8_t topic_id[2];
} SubscribeMsg;
SubscribeMsg subMsg;
uint8_t clientsn[4] = "N/A";
uint8_t clients_total;
uint16_t client_address;
uint16_t client_duration;
// MQTT-SN Commands
#define CONNECT 0x04
#define CONNACK 0x05
#define PINGREQ 0x16
#define PINGRESP 0x17
#define PUBLISH 0x0C
#define PUBACK 0x0D
#define SUBSCRIBE 0x12
#define SUBACK 0x13
#define UNSUBSCRIBE 0x14
#define UNSUBACK 0x15
#define DISCONNECT 0x18
// States
#define CONNECTED 0x01
#define NOTCONNECTED 0x00
void callback(char* topic, byte* payload, unsigned int length);
void setup() {
WiFi.mode(WIFI_STA);
Serial.begin(115200);
Serial.println("MQTT-SN Gateway implementation VA1");
Serial.println("==================================");
mrf.reset();
mrf.init();
mrf.set_pan(0xcfce); // pan ID = 0xCFCE (Cat FaCE)
mrf.address16_write(0x8266); // own address = 0x8266
mrf.rx_flush();
// uncomment if you want to receive any packet on this channel
//mrf.set_promiscuous(true);
// uncomment if you want to enable PA/LNA external control
//mrf.set_palna(true);
// uncomment if you want to buffer all PHY Payload
//mrf.set_bufferPHY(true);
attachInterrupt(4, interrupt_routine, CHANGE); // interrupt connected to GPIO4
interrupts();
mqttClient.setCallback(callback);
current_time = millis();
last_time = current_time + update_interval + 1;
Serial.println("Module MRF24J40MA Init done.");
Serial.print("Connecting WiFi Network");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("Connecting MQTT Server");
while (!mqttClient.connect(MQTT_CLIENT))
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("MQTT Connected");
pinMode(LED_BUILTIN, OUTPUT);
clients[0].address = 0xFFFF; // record 0 = 0xFFFF
clients[0].client_connected = false; // no clients connected
clients_total = 0;
sub_table[0].address[0]=0xFF;
sub_table[0].address[1]=0xFF;
}
void interrupt_routine() {
mrf.interrupt_handler(); // mrf24 object interrupt routine
}
void loop()
{
uint8_t i, j, move_counter;
mrf.check_flags(&handle_rx, &handle_tx);
if(message_received)
{
// mrf.send16(0x1414,rx_buffer,rx_len); // Forward message to 0x1414 for test purposes only
Msg.Length = rx_buffer[0]; // Fill in the message fields
Msg.MsgType = rx_buffer[1];
for(i=2;i<Msg.Length;i++)
{
Msg.Var[i-2] = rx_buffer[i];
}
switch (Msg.MsgType) // Check message type
{
case(CONNECT):
{
ConnMsg.Flags = rx_buffer[2];
ConnMsg.ProtocolID = rx_buffer[3];
ConnMsg.Duration[0] = rx_buffer[4];
ConnMsg.Duration[1] = rx_buffer[5];
client_duration = 256*rx_buffer[4] + rx_buffer[5];
// Serial.print("CONNECT from 0x");
// Serial.println(client_address,HEX);
for(i=6;i<Msg.Length-2;i++)
{
// Serial.println("x");
// ConnMsg.ClientID[i-6] = rx_buffer[i];
// Serial.print((char)ConnMsg.ClientID[i-6]);
}
client_address = 256*rx_buffer[i] + rx_buffer[i+1];
Serial.print("CONNECT from 0x");
Serial.println(client_address,HEX);
// Serial.println("\nClient address: ");
// Serial.println(Msg.Length,HEX);
// ConnMsg.ClientID[i-6]=0x00; // line edn of client id
i=0;
do
{
if(clients[i].address == client_address)
{
if(clients[i].client_connected)
{
Serial.print("Client is already connected: 0x");
Serial.println(client_address,HEX);
break;
}
// else
// {
// Serial.print("CONNACK to: 0x");
// Serial.println(client_address,HEX);
// tx_buffer[0] = 0x03; // Fill in the message fields
// tx_buffer[1] = CONNACK;
// tx_buffer[2] = 0x00; // TODO - Change with defined constant
// mrf.send16((word)client_address,(char*)tx_buffer, tx_buffer[0]); // Send CONNACK back
// clients[i].client_connected = true;
// clients[i].timer = 10;
// }
}
if(clients[i].address == 0xFFFF)
{
clients[i].address = client_address;
clients[i].duration = client_duration;
clients[i].timer = clients[i].duration;
clients[i+1].address = 0xFFFF;
Serial.print("New connection from: 0x");
Serial.println(client_address,HEX);
Serial.print("Client duration: 0x");
Serial.println(clients[i].duration,HEX);
String(client_address,HEX).toCharArray(conn_addr,5);
mqttClient.publish(conn_addr,conn_msg); // Send message to MQTT broker for the new client connected
Serial.print("CONNACK to 0x");
Serial.println(client_address,HEX);
tx_buffer[0] = 0x03; // Fill in the message fields
tx_buffer[1] = CONNACK;
tx_buffer[2] = 0x00; // TODO - Change with defined constant
mrf.send16((word)client_address,(char*)tx_buffer, tx_buffer[0]); // Send CONNACK back
// mrf.send16((word)client_address,(char*)tx_buffer, tx_buffer[0]);
clients[i].client_connected = true;
break;
}
i++;
} while(i<0xFF);
} break;
case(PINGREQ):
{
for(i=2;i<Msg.Length-2;i++);
// {
// PingreqMsg.ClientID[i-2] = rx_buffer[i];
// Serial.print((char)PingreqMsg.ClientID[i-2]);
// }
client_address = 256*rx_buffer[i] + rx_buffer[i+1];
Serial.print("PINGREQ from 0x");
Serial.println(client_address,HEX);
for(i=0;(clients[i].address!=client_address)&&(clients[i].address!=0xFFFF);i++);
if(clients[i].address==0xFFFF)
{
Serial.print("\nClient is not connected: 0x");
Serial.println(client_address,HEX);
}
else if(clients[i].address == client_address)
{
if(clients[i].client_connected)
{
// Serial.print("\nClient connected: 0x");
// Serial.println(client_address,HEX);
// PingreqMsg.ClientID[(i-2)+1]=0x00;
// client_address = 256*rx_buffer[i] + rx_buffer[i+1];
// Serial.println();
tx_buffer[0] = 0x02; // Fill in the message fields
tx_buffer[1] = PINGRESP;
mrf.send16((word)client_address,(char*)tx_buffer,tx_buffer[0]);
clients[i].timer = clients[i].duration;
clients[i].client_connected = true;
Serial.print("PINGRESP to 0x");
Serial.println(client_address,HEX);
}
else
{
Serial.println("Client is not connected");
}
}
} break;
case(PUBLISH):
{
uint8_t dest[3];
Serial.print("PUBLISH received (hex): ");
pubMsg.Flags = rx_buffer[2];
pubMsg.topic_id[0] = rx_buffer[3];
pubMsg.topic_id[1] = rx_buffer[4];
pubMsg.msg_id[0] = rx_buffer[5];
pubMsg.msg_id[1] = rx_buffer[6];
for(i=7;i<Msg.Length;i++)
{
pubMsg.data[i-7] = rx_buffer[i];
Serial.print(pubMsg.data[i-7],HEX);
Serial.print(" ");
}
pubMsg.data[i]=0x00;
// while(i<23)
// {
//// Serial.println("Clearing buffer...");
//
// pubMsg.data[i-7]=0x00; // Clean up the rest of the buffer
// i++;
// }
Serial.println();
dest[0] = pubMsg.topic_id[0];
dest[1] = pubMsg.topic_id[1];
dest[3] = 0x00;
Serial.print("Destination: ");
Serial.println((char *)dest);
mqttClient.publish((char *)dest,(char *)pubMsg.data);
} break;
case(SUBSCRIBE):
{
uint8_t topic[3];
Serial.println("SUBSCRIBE received");
subMsg.Flags = rx_buffer[2];
subMsg.msg_id[0] = rx_buffer[3];
subMsg.msg_id[1] = rx_buffer[4];
subMsg.topic_id[0] = rx_buffer[5];
subMsg.topic_id[1] = rx_buffer[6];
topic[0] = subMsg.topic_id[0];
topic[1] = subMsg.topic_id[1];
topic[2] = 0x00;
i=0;
boolean already_subscribed = false;
while((sub_table[i].address[0] != 0xFF) && (sub_table[i].address[1] != 0xFF))
{
if(sub_table[i].address[0]==subMsg.msg_id[0])
if(sub_table[i].address[1]==subMsg.msg_id[1])
if(sub_table[i].topic_id[0]==subMsg.topic_id[0])
if(sub_table[i].topic_id[1]==subMsg.topic_id[1])
{
Serial.println("Client is already subscribed for that topic.");
already_subscribed = true;
}
i++;
}
Serial.print("Topics registered so far: ");
Serial.println(i);
if(!already_subscribed)
{
Serial.print("Subscribing topic: ");
Serial.println((char *)topic);
mqttClient.subscribe((char *)topic);
sub_table[i].address[0]=subMsg.msg_id[0];
sub_table[i].address[1]=subMsg.msg_id[1];
sub_table[i].topic_id[0]=subMsg.topic_id[0];
sub_table[i].topic_id[1]=subMsg.topic_id[1];
sub_table[i+1].address[0]=0xFF;
sub_table[i+1].address[1]=0xFF;
}
tx_buffer[0] = 0x08; // Fill in the message fields
tx_buffer[1] = SUBACK;
tx_buffer[2] = 0x00; // Flags;
tx_buffer[3] = subMsg.topic_id[0];
tx_buffer[4] = subMsg.topic_id[1];
tx_buffer[5] = subMsg.msg_id[0];
tx_buffer[6] = subMsg.msg_id[1];
tx_buffer[7] = 0x00; // Return code = Accepted
client_address = 256*subMsg.msg_id[0] + subMsg.msg_id[1];
Serial.print("Sending SUBACK back to ");
Serial.println(client_address,HEX);
delay(5000);
mrf.send16((word)client_address,(char*)tx_buffer, tx_buffer[0]); // Send SUBACK back
} break;
case(UNSUBSCRIBE):
{
Serial.println("UNSUBSCRIBE received. Not implemented yet.");
} break;
case(DISCONNECT):
{
Serial.println("DISCONNECT received. Not implemented yet.");
} break;
}
message_received = false;
}
// -------------------------Routines for gateway operations -------------------------
current_time = millis();
if(current_time - last_update > update_interval)
{
last_update = current_time;
i=0;
while(clients[i].address!=0xFFFF)
{
if(clients[i].client_connected == true)
{
clients[i].timer--;
// Serial.print("Timeout in ");
// Serial.print(clients[i].timer);
// Serial.println(" s");
if(clients[i].timer < 0)
{
Serial.print("Client 0x");
Serial.print(clients[i].address,HEX);
Serial.println(" timed out.");
// mqttClient.publish((char *)clients[i].address,tout_msg);
String(clients[i].address,HEX).toCharArray(conn_addr,5);
mqttClient.publish(conn_addr,tout_msg); // Send message to MQTT broker for the new client connected
// clients[i].client_connected = false;
// clients[i].address = 0xFFFF;
// find the last record and move it
// into this position
// to prevent holes
for(move_counter=i;clients[move_counter].address!=0xFFFF;move_counter++);
// while(clients[move_counter++].address != 0xFFFF); // find the end of the records
move_counter--; // get the latest record
Serial.println(move_counter);
clients[i].address = clients[move_counter].address; // move the last record at the free place
clients[move_counter].address = 0xFFFF;
clients[move_counter].client_connected = false;
}
}
i++;
}
for(i=0;clients[i].address!=0xFFFF;i++);
Serial.print("Clients connected: ");
Serial.println(i);
if(i==0) clientsn[0] = '0';
if(i==1) clientsn[0] = '1';
if(i==2) clientsn[0] = '2';
if(i==3) clientsn[0] = '3';
if(i>3) clientsn[0] = 'U';
// clientsn[1] = 0x00;
mqttClient.publish("clientsn",(char *)clientsn);
}
// ------------------------------------------------------------------------------
if(mqtt_msg_received)
{
uint16_t dest_address;
Serial.print("Topic: ");
Serial.println((char *)mqtt_rx_topic);
Serial.print("Payload: ");
Serial.println((char *)mqtt_rx_payload);
i=0;
while((sub_table[i].address[0] != 0xFF) && (sub_table[i].address[1] != 0xFF))
{
if((sub_table[i].topic_id[0] == mqtt_rx_topic[0]) && (sub_table[i].topic_id[1] == mqtt_rx_topic[1]))
{
tx_buffer[1] = PUBLISH;
tx_buffer[2] = 0x00;
tx_buffer[3] = mqtt_rx_topic[0];
tx_buffer[4] = mqtt_rx_topic[1];
tx_buffer[5] = sub_table[i].address[0];
tx_buffer[6] = sub_table[i].address[1];
j=0;
while(mqtt_rx_payload[j])
{
tx_buffer[6+j] = mqtt_rx_payload[j];
j++;
}
tx_buffer[0] = 6+j; // total msg len
// Serial.println(sub_table[i].address[0],HEX);
// Serial.println(sub_table[i].address[1],HEX);
dest_address = sub_table[i].address[0] << 8;
// Serial.println(dest_address,HEX);
dest_address = dest_address + sub_table[i].address[1];
// Serial.println(dest_address,HEX);
mrf.send16(dest_address,(char *)tx_buffer,6+j); // TODO Construct destination address
}
i++;
}
mqtt_msg_received = false;
}
mqttClient.loop();
}
void handle_rx() {
uint8_t i;
digitalWrite(LED_BUILTIN, LOW);
// Serial.println("Packet received, waiting 100 msec...");
// delay(500);
rx_len = mrf.rx_datalength();
for (i = 0; i < rx_len; i++)
{
// rx_buffer[i] = mrf.get_rxbuf()[i];
rx_buffer[i] = mrf.get_rxinfo()->rx_data[i];
// delay(100);
}
message_received = true;
digitalWrite(LED_BUILTIN, HIGH);
}
void handle_tx() {
// if (mrf.get_txinfo()->tx_ok) {
// Serial.println("TX went ok, got ack");
// } else {
// Serial.print("TX failed after ");Serial.print(mrf.get_txinfo()->retries);Serial.println(" retries\n");
// }
}
void callback(char* topic, byte* payload, unsigned int length) {
uint8_t i;
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
if(length>15)
{
Serial.println("Received message too long");
}
else
{
for(i=0;i<2;i++)
mqtt_rx_topic[i] = topic[i];
mqtt_rx_topic[i] = 0x00;
for (i=0;i<length;i++)
{
Serial.print((char)payload[i]);
mqtt_rx_payload[i] = payload[i];
}
mqtt_rx_payload[i] = 0x00;
mqtt_msg_received = true;
}
Serial.println();
}