-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreplayer_device.pde
547 lines (455 loc) · 11.8 KB
/
replayer_device.pde
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
/*
* ------ Replayer (receives the message from the bridge server and replays it) --------
*
* This program 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.
*
* This program 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/>.
*
* Version: 1.0
* Design: Pierluigi Locatelli, Pietro Spadaccino
* Implementation: Pierluigi Locatelli, Pietro Spadaccino
*/
#include <WaspWIFI_PRO.h>
#include <WaspLoRaWAN.h>
// If DEBUG_PRINTS is 1, USB prints will be done slowing the replay
#define DEBUG_PRINTS 0
// Board hardware sockets
uint8_t lora_socket = SOCKET0;
uint8_t wifi_socket = SOCKET1;
// Wifi config
//char ESSID[] = "rastafan";
//char PASSW[] = "laschiava";
char ESSID[] = "Linkem_3A06E4";
char PASSW[] = "lhd3+cun";
// Remote server config
uint8_t remote_server_handle = 0;
char SERVER_HOST[] = "rastafan.ddns.net";
char SERVER_LOCAL_PORT[] = "3000";
char SERVER_REMOTE_PORT[] = "2003";
// Radio config
uint8_t power = 15;
uint32_t frequency;
char spreading_factor[] = "sf10";
char coding_rate[] = "4/5";
uint16_t bandwidth = 125;
char crc_mode[] = "on";
// Application logic variables
uint8_t error;
uint8_t status;
char* client_role = "replayer";
char buffer[100];
uint8_t radioModuleSetup() {
uint8_t status = 0;
uint8_t e = 0;
//////////////////////////////////////////////
// 1. switch on
//////////////////////////////////////////////
e = LoRaWAN.ON(lora_socket);
// Check status
if (e == 0)
{
USB.println(F("1. Switch ON OK"));
}
else
{
USB.print(F("1. Switch ON error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
if (LoRaWAN._version == RN2483_MODULE || LoRaWAN._version == RN2903_IN_MODULE)
{
frequency = 868100000;
}
else if(LoRaWAN._version == RN2903_MODULE)
{
frequency = 902300000;
}
else if(LoRaWAN._version == RN2903_AS_MODULE)
{
frequency = 917300000;
}
//////////////////////////////////////////////
// 2. Enable P2P mode
//////////////////////////////////////////////
e = LoRaWAN.macPause();
// Check status
if (e == 0)
{
USB.println(F("2. P2P mode enabled OK"));
}
else
{
USB.print(F("2. Enable P2P mode error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
//////////////////////////////////////////////
// 3. Set/Get Radio Power
//////////////////////////////////////////////
// Set power
e = LoRaWAN.setRadioPower(power);
// Check status
if (e == 0)
{
USB.println(F("3.1. Set Radio Power OK"));
}
else
{
USB.print(F("3.1. Set Radio Power error = "));
USB.println(e, DEC);
status = 1;
}
// Get power
e = LoRaWAN.getRadioPower();
// Check status
if (e == 0)
{
USB.print(F("3.2. Get Radio Power OK. "));
USB.print(F("Power: "));
USB.println(LoRaWAN._radioPower);
}
else
{
USB.print(F("3.2. Get Radio Power error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
//////////////////////////////////////////////
// 4. Set/Get Radio Frequency
//////////////////////////////////////////////
// Set frequency
e = LoRaWAN.setRadioFreq(frequency);
// Check status
if (e == 0)
{
USB.println(F("4.1. Set Radio Frequency OK"));
}
else
{
USB.print(F("4.1. Set Radio Frequency error = "));
USB.println(e, DEC);
status = 1;
}
// Get frequency
e = LoRaWAN.getRadioFreq();
// Check status
if (e == 0)
{
USB.print(F("4.2. Get Radio Frequency OK. "));
USB.print(F("Frequency: "));
USB.println(LoRaWAN._radioFreq);
}
else
{
USB.print(F("4.2. Get Radio Frequency error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
//////////////////////////////////////////////
// 5. Set/Get Radio Spreading Factor (SF)
//////////////////////////////////////////////
// Set SF
e = LoRaWAN.setRadioSF(spreading_factor);
// Check status
if (e == 0)
{
USB.println(F("5.1. Set Radio SF OK"));
}
else
{
USB.print(F("5.1. Set Radio SF error = "));
USB.println(e, DEC);
status = 1;
}
// Get SF
e = LoRaWAN.getRadioSF();
// Check status
if (e == 0)
{
USB.print(F("5.2. Get Radio SF OK. "));
USB.print(F("Spreading Factor: "));
USB.println(LoRaWAN._radioSF);
}
else
{
USB.print(F("5.2. Get Radio SF error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
//////////////////////////////////////////////
// 6. Set/Get Radio Coding Rate (CR)
//////////////////////////////////////////////
// Set CR
e = LoRaWAN.setRadioCR(coding_rate);
// Check status
if (e == 0)
{
USB.println(F("6.1. Set Radio CR OK"));
}
else
{
USB.print(F("6.1. Set Radio CR error = "));
USB.println(e, DEC);
status = 1;
}
// Get CR
e = LoRaWAN.getRadioCR();
// Check status
if (e == 0)
{
USB.print(F("6.2. Get Radio CR OK. "));
USB.print(F("Coding Rate: "));
USB.println(LoRaWAN._radioCR);
}
else
{
USB.print(F("6.2. Get Radio CR error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
//////////////////////////////////////////////
// 7. Set/Get Radio Bandwidth (BW)
//////////////////////////////////////////////
// Set BW
e = LoRaWAN.setRadioBW(bandwidth);
// Check status
if (e == 0)
{
USB.println(F("7.1. Set Radio BW OK"));
}
else
{
USB.print(F("7.1. Set Radio BW error = "));
USB.println(e, DEC);
}
// Get BW
e = LoRaWAN.getRadioBW();
// Check status
if (e == 0)
{
USB.print(F("7.2. Get Radio BW OK. "));
USB.print(F("Bandwidth: "));
USB.println(LoRaWAN._radioBW);
}
else
{
USB.print(F("7.2. Get Radio BW error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
//////////////////////////////////////////////
// 8. Set/Get Radio CRC mode
//////////////////////////////////////////////
// Set CRC
e = LoRaWAN.setRadioCRC(crc_mode);
// Check status
if (e == 0)
{
USB.println(F("8.1. Set Radio CRC mode OK"));
}
else
{
USB.print(F("8.1. Set Radio CRC mode error = "));
USB.println(e, DEC);
status = 1;
}
// Get CRC
e = LoRaWAN.getRadioCRC();
// Check status
if (e == 0)
{
USB.print(F("8.2. Get Radio CRC mode OK. "));
USB.print(F("CRC status: "));
USB.println(LoRaWAN._crcStatus);
}
else
{
USB.print(F("8.2. Get Radio CRC mode error = "));
USB.println(e, DEC);
status = 1;
}
USB.println(F("-------------------------------------------------------"));
return status;
}
void setup_wifi_parameters() {
error = WIFI_PRO.ON(wifi_socket);
if (error == 0) {
USB.println(F("1. WiFi switched ON"));
}
else {
USB.println(F("1. WiFi did not initialize correctly"));
}
// 2. Reset to default values
error = WIFI_PRO.resetValues();
if (error == 0) {
USB.println(F("2. WiFi reset to default"));
}
else {
USB.println(F("2. WiFi reset to default ERROR"));
}
// 3. Set ESSID
error = WIFI_PRO.setESSID(ESSID);
if (error == 0) {
USB.println(F("3. WiFi set ESSID OK"));
}
else {
USB.println(F("3. WiFi set ESSID ERROR"));
}
// 4. Set password key (It takes a while to generate the key)
// Authentication modes:
// OPEN: no security
// WEP64: WEP 64
// WEP128: WEP 128
// WPA: WPA-PSK with TKIP encryption
// WPA2: WPA2-PSK with TKIP or AES encryption
error = WIFI_PRO.setPassword(WPA2, PASSW);
if (error == 0) {
USB.println(F("4. WiFi set AUTHKEY OK"));
}
else {
USB.println(F("4. WiFi set AUTHKEY ERROR"));
}
// 5. Software Reset
// Parameters take effect following either a
// hardware or software reset
error = WIFI_PRO.softReset();
if (error == 0) {
USB.println(F("5. WiFi softReset OK"));
}
else {
USB.println(F("5. WiFi softReset ERROR"));
}
}
uint8_t get_local_ip() {
status = WIFI_PRO.isConnected();
// Check if module is connected
if (status == true) {
USB.print(F("WiFi is connected OK"));
USB.println(F("-------------------------------------"));
// 2.1. Get IP address
error = WIFI_PRO.getIP();
if (error == 0) {
USB.print(F("IP address: "));
USB.println(WIFI_PRO._ip);
return 0;
}
else {
USB.println(F("getIP error"));
}
}
else {
USB.println(F("WiFi is not connected"));
}
return 1;
}
uint8_t setup_remote_connection() {
// Open socket with remote server
error = WIFI_PRO.setTCPclient(SERVER_HOST, SERVER_REMOTE_PORT, SERVER_LOCAL_PORT);
if (error == 0)
{
remote_server_handle = WIFI_PRO._socket_handle;
USB.print(F("3.1. Open TCP socket OK in handle: "));
USB.println(remote_server_handle, DEC);
return 0;
}
else
{
USB.println(F("3.1. Error opening socket"));
WIFI_PRO.printErrorCode();
return 1;
}
}
void send_data_on_lora(char* data_to_send) {
uint8_t err;
USB.println(data_to_send);
int len = strlen(data_to_send);
for (uint8_t i = 0; i < len; i++) {
if (((data_to_send[i] < '0') || (data_to_send[i] > '9')) &&
((data_to_send[i] < 'A') || (data_to_send[i] > 'F')) &&
((data_to_send[i] < 'a') || (data_to_send[i] > 'f'))) {
data_to_send[i] = '\0';
break;
}
}
USB.println(strlen(data_to_send), DEC);
err = LoRaWAN.sendRadio(data_to_send);
// Check status
if (err == 0) {
USB.println(F("--> Packet sent OK"));
}
else {
USB.print(F("Error sending packets. error = "));
USB.println(err, DEC);
}
}
void receive_and_replay() {
USB.println(F("Listen on WiFI:"));
error = WIFI_PRO.receive(remote_server_handle, 6000000);
if (error == 0)
{
#if DEBUG_PRINTS >= 1
USB.println(F("\n============== WIFi received ==============="));
USB.print(F("Data: "));
USB.println( WIFI_PRO._buffer, WIFI_PRO._length);
USB.print(F("Length: "));
USB.println( WIFI_PRO._length,DEC);
USB.println(F("=============================================="));
#endif
// Data received, send with LoRa
char data_to_send[WIFI_PRO._length];
memcpy(data_to_send, WIFI_PRO._buffer, WIFI_PRO._length);
send_data_on_lora(data_to_send);
}
}
void send_internet_packet() {
//USB.println(F("Listen to TCP socket:"));
error = WIFI_PRO.send(remote_server_handle, buffer);
if (error == 0) {
USB.println(F("3.2. Send data1 OK"));
}
else {
USB.println(F("3.2. Error calling 'send1' function"));
WIFI_PRO.printErrorCode();
}
}
void init_client_info() {
sprintf(buffer, "init %s", client_role);
send_internet_packet();
}
void setup() {
USB.ON();
// LoRa module setup
error = radioModuleSetup();
if (error == 0)
{
USB.println(F("Module configured OK"));
}
else
{
USB.println(F("Module configured ERROR"));
}
// Wifi setup
setup_wifi_parameters();
while(get_local_ip()) { }
setup_remote_connection();
init_client_info();
}
void loop() {
receive_and_replay();
}