Waspmote factory default
This code sends basic parameters through the corresponding radio module.

Required Materials

1 x Waspmote 1 x Battery 1 x miniUSB wire 1 x radio module (optional) 1 x gateway + radio module (optional)

Notes

- This is the default factory code for Waspmote v15. So any Waspmote or Plug&Sense! device is received like this. - The code is valid for all XBee modules communication protocols. - Some basic parameters are read by this code, like MAC address of radio module, accelerometer axes values and battery level. - All this data is gathered into a standard Frame packet. - When an XBee is connected the code will output the information both via XBee and USB. - For other communication protocols this code is still valid, but the data is printed via USB only. - By default, SOCKET0 will be used. If no XBee is connected on SOCKET0, then the information will be printed by USB. - This code configures automatically an XBee module with default Waspmote parameters (BD7 [baudrate] and AP2 [AP mode]) - This example can be executed in Waspmote v15. Only Waspmote v15 radio modules are supported.

Code

1
/*
2
------ Waspmote factory default code ------
3
4
Explanation: Send basic parameters through the corresponding
5
radio module.
6
7
Copyright (C) 2019 Libelium Comunicaciones Distribuidas S.L.
8
http://www.libelium.com
9
10
This program is free software: you can redistribute it and/or modify
11
it under the terms of the GNU General Public License as published by
12
the Free Software Foundation, either version 3 of the License, or
13
(at your option) any later version.
14
15
This program is distributed in the hope that it will be useful,
16
but WITHOUT ANY WARRANTY; without even the implied warranty of
17
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18
GNU General Public License for more details.
19
20
You should have received a copy of the GNU General Public License
21
along with this program. If not, see <http://www.gnu.org/licenses/>.
22
23
Version: 4.2
24
Design: David Gascón
25
Implementation: Yuri Carmona, Javier Siscart
26
*/
27
28
// Include libraries
29
#include <WaspXBee802.h>
30
#include <WaspXBeeZB.h>
31
#include <WaspFrame.h>
32
33
34
// Define the Waspmote default ID
35
char node_id[] = "node_id";
36
37
// Define the authentication key
38
char key_access[] = "LIBELIUM";
39
40
// Declare global variables
41
char macHigh[11];
42
char macLow[11];
43
char filename[] = "TEST_SD.TXT";
44
uint8_t lora_error;
45
uint8_t lora_status;
46
uint8_t xbee_error;
47
uint8_t response;
48
49
// Broadcast address
50
char destination[] = "000000000000FFFF";
51
52
// Declare the XBEE_type
53
// 0 - NO radio detected
54
// 1 - 802.15.4
55
// 2 - 900 MHz - 868 MHz
56
// 3 - DigiMesh
57
// 4 - XBee ZB
58
// 5 - 900 MHz Intl
59
// 6 - LoRa (deprecated)
60
// 7 - LoRaWAN
61
// 8 - Sigfox
62
// 9 - WiFi PRO
63
// 10 - 4G
64
int radio_type = 0;
65
66
// Declare the xbee type in the case there is an XBee module
67
uint8_t firmware[4];
68
69
uint8_t errorFlag = 0;
70
71
void setup()
72
{
73
USB.ON();
74
USB.println(F("Starting Waspmote factory default program"));
75
76
///////////////////////////////////////////////////////////////////
77
// Battery checking
78
///////////////////////////////////////////////////////////////////
79
80
uint8_t battery = PWR.getBatteryLevel();
81
82
if (battery == 0)
83
{
84
USB.println(F("************************************************"));
85
USB.print(F("ERROR: Battery issue. Battery level (%): "));
86
USB.println(battery, DEC);
87
USB.println(F("************************************************"));
88
errorFlag |= 1<<0x01;
89
}
90
91
92
///////////////////////////////////////////////////////////////////
93
// 1. Serial ID
94
///////////////////////////////////////////////////////////////////
95
96
// Show '_serial_id' stored by the API when powering up
97
USB.print(F("Global variable '_serial_id':"));
98
USB.printHex(_serial_id[0]);
99
USB.printHex(_serial_id[1]);
100
USB.printHex(_serial_id[2]);
101
USB.printHex(_serial_id[3]);
102
USB.printHex(_serial_id[4]);
103
USB.printHex(_serial_id[5]);
104
USB.printHex(_serial_id[6]);
105
USB.printHex(_serial_id[7]);
106
USB.println();
107
108
// Reading the serial number
109
Utils.readSerialID();
110
USB.print(F("Waspmote serial ID: "));
111
USB.printHex(_serial_id[0]);
112
USB.printHex(_serial_id[1]);
113
USB.printHex(_serial_id[2]);
114
USB.printHex(_serial_id[3]);
115
USB.printHex(_serial_id[4]);
116
USB.printHex(_serial_id[5]);
117
USB.printHex(_serial_id[6]);
118
USB.printHex(_serial_id[7]);
119
USB.println();
120
121
122
///////////////////////////////////////////////////////////////////
123
// 2. Boards ON
124
///////////////////////////////////////////////////////////////////
125
PWR.setSensorPower(SENS_3V3, SENS_ON);
126
127
128
/////////////////////////////////////////////////////////////
129
// 3. Test SD
130
/////////////////////////////////////////////////////////////
131
SD.ON();
132
if (SD.isSD())
133
{
134
SD.del(filename);
135
SD.create(filename);
136
if (SD.writeSD(filename, "Test SD", 0))
137
{
138
USB.println(F("SD OK"));
139
}
140
else
141
{
142
USB.println(F("Error SD"));
143
errorFlag |= 1<<0x02;
144
}
145
}
146
else
147
{
148
USB.println(F("No SD card detected"));
149
errorFlag |= 1<<0x03;
150
}
151
SD.del(filename);
152
USB.println();
153
154
155
/////////////////////////////////////////////////////////////
156
// 4. Store key access in EEPROM
157
/////////////////////////////////////////////////////////////
158
Utils.setAuthKey(key_access);
159
160
161
/////////////////////////////////////////////////////////////
162
// 5. Init RTC and ACC
163
/////////////////////////////////////////////////////////////
164
RTC.ON();
165
ACC.ON();
166
167
168
/////////////////////////////////////////////////////////////
169
// 6. Set Waspmote setting for XBee module for first time.
170
// (baudrate at 115200 and API mode enabled)
171
/////////////////////////////////////////////////////////////
172
// Note: Only valid for SOCKET 0
173
xbee802.ON();
174
175
Utils.setMuxSocket0();
176
delay(500);
177
beginSerial(9600, 0);
178
printString("+++", 0);
179
delay(2000);
180
printString("ATBD7,AP2,WR,CN\r\n", 0);
181
delay(500);
182
183
xbeeZB.OFF();
184
delay(500);
185
xbeeZB.ON();
186
187
// In case of Zigbee modules:
188
// XBee command for 115200bps --> ATBD7
189
uint8_t ATBD7[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x42, 0x44, 0x07, 0x69 };
190
// XBee command for API mode --> ATAP2
191
uint8_t ATAP2[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x41, 0x50, 0x02, 0x63 };
192
// XBee command for saving config --> ATWR
193
uint8_t ATWR[] = { 0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D };
194
195
for (uint8_t i = 0; i < 9; i++)
196
{
197
printByte(ATBD7[i], SOCKET0);
198
}
199
delay(150);
200
closeSerial(SOCKET0);
201
delay(200);
202
beginSerial(115200, SOCKET0);
203
for (uint8_t i = 0; i < 9; i++)
204
{
205
printByte(ATAP2[i], SOCKET0);
206
}
207
delay(150);
208
for (uint8_t i = 0; i < 8; i++)
209
{
210
printByte(ATWR[i], SOCKET0);
211
}
212
delay(150);
213
closeSerial(SOCKET0);
214
215
216
/////////////////////////////////////////////////////////////
217
// 7. LEDs management
218
/////////////////////////////////////////////////////////////
219
Utils.setLED(LED0, LED_OFF);
220
Utils.setLED(LED1, LED_OFF);
221
for (int i = 0 ; i < 4 ; i++)
222
{
223
Utils.blinkLEDs(100);
224
}
225
226
227
/////////////////////////////////////////////////////////////
228
// 8. Identify the radio connected to Waspmote
229
//
230
// Possibilities:
231
// - XBee 802.15.4
232
// - XBee 868LP
233
// - XBee 900HP
234
// - XBee DigiMesh
235
// - XBee ZigBee
236
// - LoRa (SX1272) (deprecated)
237
// - LoRaWAN (RN2483 or RN2903 or ABZ)
238
// - Sigfox (TD1207 or TD1508)
239
// - WiFi PRO
240
// - 4G (in SOCKET1)
241
/////////////////////////////////////////////////////////////
242
uint8_t answer;
243
radio_type = 0;
244
245
246
247
/////////////////////////////////////////////////////////////
248
// 8.1. check for XBee module
249
/////////////////////////////////////////////////////////////
250
251
// define object for UART0
252
WaspUART uart = WaspUART();
253
uint8_t class_buffer[100];
254
uart._buffer = class_buffer;
255
uart._bufferSize = 100;
256
257
// init object in SOCKET0
258
uart.setUART(SOCKET0);
259
260
// select multiplexer
261
Utils.setMuxSocket0();
262
263
// begin serial communication
264
uart.beginUART();
265
266
// power on the socket
267
PWR.powerSocket(SOCKET0, HIGH);
268
delay(500);
269
serialFlush(SOCKET0);
270
271
// check for XBees in SOCKET0
272
uint8_t cmd_xbee[] = {0x7E, 0x00 , 0x04 , 0x08 , 0x01 , 0x56 , 0x52 , 0x4E};
273
274
// send command & receive answer
275
uart.sendCommand(cmd_xbee, sizeof(cmd_xbee));
276
uart.readBuffer(100);
277
278
// check response: 7E00078801565200xxxx??
279
if (uart._length > 0)
280
{
281
if ((uart._buffer[0] == 0x7E)
282
&& (uart._buffer[1] == 0x00)
283
&& (uart._buffer[3] == 0x88)
284
&& (uart._buffer[4] == 0x01)
285
&& (uart._buffer[5] == 0x56)
286
&& (uart._buffer[6] == 0x52)
287
&& (uart._buffer[7] == 0x00))
288
{
289
290
USB.println(F("XBee module is plugged on socket 0:"));
291
292
/*
293
USB.print(F("XBee module in SOCKET0. Firmware: "));
294
USB.printHex(uart._buffer[8]);
295
USB.printHex(uart._buffer[9]);
296
USB.println();
297
*/
298
299
firmware[0] = uart._buffer[8];
300
firmware[1] = uart._buffer[9];
301
firmware[2] = uart._buffer[10];
302
firmware[3] = uart._buffer[11];
303
304
/////////////////////////////////
305
// Get the XBee firmware version
306
/////////////////////////////////
307
308
// Set the XBee firmware type depending ont he previous response
309
if ((firmware[0] < 0x20) && (firmware[1] > 0x80))
310
{
311
radio_type = 1; // 802.15.4
312
USB.println(F("--> XBee type: 802.15.4"));
313
}
314
else if (firmware[0] == 0x20 && firmware[1] > 0x02)
315
{
316
radio_type = 1; // 802.15.4
317
USB.println(F("--> XBee type: 802.15.4 XBee 3"));
318
}
319
else if (firmware[0] == 0x10 && firmware[1] < 0x50)
320
{
321
radio_type = 4; //ZB
322
USB.println(F("--> XBee type: ZigBee 3"));
323
}
324
else if ((firmware[0] < 0x20) && (firmware[1] > 0x00))
325
{
326
radio_type = 2; // 868MHz - 900MHz
327
328
xbee802.OFF();
329
xbee802.ON();
330
331
// send Hardware Serial command
332
if (!xbee802.sendCommandAT("HS#"))
333
{
334
// USB.printHexln(xbee802.commandAT, 3);
335
}
336
337
// check for XBee 900HP
338
if (xbee802.commandAT[0] == 3)
339
{
340
USB.println(F("--> XBee type: 900HP"));
341
342
// check for available frequencies:
343
// US: 00FFFFFFFFFFFFFFFF
344
// BR: 00FFFFFFFE00000FFF
345
// AU: 00FFFFFFFE00000000
346
if (!xbee802.sendCommandAT("AF#"))
347
{
348
//USB.printHexln(xbee802.commandAT, 16);
349
if ((xbee802.commandAT[1] == 0xFF)
350
&& (xbee802.commandAT[2] == 0xFF)
351
&& (xbee802.commandAT[3] == 0xFF)
352
&& (xbee802.commandAT[4] == 0xFF)
353
&& (xbee802.commandAT[5] == 0xFF)
354
&& (xbee802.commandAT[6] == 0xFF)
355
&& (xbee802.commandAT[7] == 0xFF)
356
&& (xbee802.commandAT[8] == 0xFF))
357
{
358
USB.println(F("--> Hardware serie: USA"));
359
}
360
else if ((xbee802.commandAT[1] == 0xFF)
361
&& (xbee802.commandAT[2] == 0xFF)
362
&& (xbee802.commandAT[3] == 0xFF)
363
&& (xbee802.commandAT[4] == 0xFE)
364
&& (xbee802.commandAT[5] == 0x00)
365
&& (xbee802.commandAT[6] == 0x00)
366
&& (xbee802.commandAT[7] == 0x0F)
367
&& (xbee802.commandAT[8] == 0xFF))
368
{
369
USB.println(F("--> Hardware serie: BRAZIL"));
370
}
371
else if ((xbee802.commandAT[1] == 0xFF)
372
&& (xbee802.commandAT[2] == 0xFF)
373
&& (xbee802.commandAT[3] == 0xFF)
374
&& (xbee802.commandAT[4] == 0xFE)
375
&& (xbee802.commandAT[5] == 0x00)
376
&& (xbee802.commandAT[6] == 0x00)
377
&& (xbee802.commandAT[7] == 0x00)
378
&& (xbee802.commandAT[8] == 0x00))
379
{
380
USB.println(F("--> Hardware serie: AUSTRALIA"));
381
}
382
}
383
else
384
{
385
USB.println(F("--> Hardware serie: ERROR"));
386
errorFlag |= 1<<0x04;
387
}
388
}
389
// check for XBee 868LP
390
else if (xbee802.commandAT[0] == 8)
391
{
392
USB.println(F("--> XBee type: 868LP"));
393
}
394
395
}
396
else if (firmware[0] >= 0x80)
397
{
398
radio_type = 3; // DigiMesh
399
USB.println(F("--> XBee type: DigiMesh"));
400
}
401
else if (((firmware[0] >= 0x20) && (firmware[1] < 0xB0)) ||
402
((firmware[0] == 0x70) && (firmware[1] == 0x5B)) )
403
{
404
radio_type = 4; //ZB
405
USB.println(F("--> XBee type: ZigBee"));
406
}
407
else if (firmware[0] == 0x00 && firmware[1] >= 0x02)
408
{
409
radio_type = 5; // 900 MHz Intl
410
USB.println(F("--> XBee type: 900 International"));
411
}
412
else
413
{
414
radio_type = 0;
415
}
416
417
/////////////////////////////////////////////////////////////
418
// Get the XBee MAC address
419
/////////////////////////////////////////////////////////////
420
if (radio_type != 0)
421
{
422
xbee802.OFF();
423
delay(1000);
424
xbee802.ON();
425
delay(1000);
426
xbee802.flush();
427
428
// Get the XBee MAC address
429
int counter = 0;
430
while ((xbee802.getOwnMac() != 0) && (counter < 12))
431
{
432
xbee802.getOwnMac();
433
counter++;
434
}
435
436
437
// convert mac address from array to string
438
Utils.hex2str(xbee802.sourceMacHigh, macHigh, 4);
439
Utils.hex2str(xbee802.sourceMacLow, macLow, 4);
440
441
// Get the XBee MAC address
442
while ((xbee802.getOwnMac() != 0) && (counter < 12))
443
{
444
xbee802.getOwnMac();
445
counter++;
446
}
447
448
// convert mac address from array to string
449
Utils.hex2str(xbee802.sourceMacHigh, macHigh, 4);
450
Utils.hex2str(xbee802.sourceMacLow, macLow, 4);
451
452
USB.print(F("--> MAC address: "));
453
USB.print(macHigh);
454
USB.println(macLow);
455
456
}
457
458
if (radio_type < 5)
459
{
460
USB.print(F("--> Firmware version: "));
461
USB.print(firmware[0], HEX);
462
if (firmware[1] < 0x10)
463
{
464
USB.print(F("0"));
465
USB.println(firmware[1], HEX);
466
}
467
else USB.println(firmware[1], HEX);
468
}
469
470
if (radio_type == 5)
471
{
472
USB.print(F("--> Firmware version: "));
473
USB.printHex(firmware[0]);
474
USB.printHex(firmware[1]);
475
USB.printHex(firmware[2]);
476
USB.printHex(firmware[3]);
477
USB.println();
478
}
479
}
480
}
481
482
483
/////////////////////////////////////////////////////////////
484
// 8.2. check for LORAWAN module
485
/////////////////////////////////////////////////////////////
486
if (radio_type == 0)
487
{
488
// init object in SOCKET0
489
xbee802.OFF();
490
uart.setUART(SOCKET0);
491
uart.setBaudrate(57600);
492
493
// switch module OFF
494
uart.closeUART();
495
Utils.setMuxUSB();
496
PWR.powerSocket(SOCKET0, LOW);
497
498
delay(500);
499
500
// select multiplexer
501
Utils.setMuxSocket0();
502
503
// begin serial communication
504
uart.beginUART();
505
506
// power on the socket
507
PWR.powerSocket(SOCKET0, HIGH);
508
delay(500);
509
serialFlush(SOCKET0);
510
511
// check for XBees in SOCKET0
512
static char cmd_lorawan[] = "sys get ver\r\n";
513
514
// send command & receive answer
515
answer = uart.sendCommand((char*)cmd_lorawan, "\r\n",1000);
516
517
char vers[9], vers_in[21];
518
memset(vers,0x00,sizeof(vers));
519
memset(vers_in,0x00,sizeof(vers_in));
520
memcpy (vers,uart._buffer,8);
521
memcpy (vers_in,uart._buffer,20);
522
523
// send command & receive answer
524
uint8_t dev_eui_answer = uart.sendCommand((char*)"mac get deveui\r\n", "\r\n", 1000);
525
526
// check response:
527
if (strcmp(vers,"RN2483 0")==0 || strcmp(vers,"RN2483 1")==0)
528
{
529
USB.println(F("LoRaWAN module is plugged on socket 0:"));
530
USB.println(F("--> Hardware serie: LoRaWAN EU"));
531
if (dev_eui_answer == 1)
532
{
533
USB.print(F("--> Device EUI: "));
534
USB.print(uart._buffer, uart._length);
535
}
536
radio_type = 7;
537
}
538
else if (strcmp(vers,"RN2903 0")==0 || strcmp(vers,"RN2903 1")==0)
539
{
540
if (strcmp(vers_in,"RN2903 1.0.5C_rc1-In")==0)
541
{
542
USB.println(F("LoRaWAN module is plugged on socket 0:"));
543
USB.println(F("--> Hardware serie: LoRaWAN IN"));
544
if (dev_eui_answer == 1)
545
{
546
USB.print(F("--> Device EUI: "));
547
USB.println(uart._buffer, uart._length);
548
}
549
}
550
else
551
{
552
USB.println(F("LoRaWAN module is plugged on socket 0:"));
553
USB.println(F("--> Hardware serie: LoRaWAN US"));
554
if (dev_eui_answer == 1)
555
{
556
USB.print(F("--> Device EUI: "));
557
USB.println(uart._buffer, uart._length);
558
}
559
}
560
radio_type = 7;
561
}
562
else if (strcmp(vers,"RN2903 S")==0)
563
{
564
USB.println(F("LoRaWAN module is plugged on socket 0:"));
565
USB.println(F("--> Hardware serie: LoRaWAN AU"));
566
if (dev_eui_answer == 1)
567
{
568
USB.print(F("--> Device EUI: "));
569
USB.println(uart._buffer, uart._length);
570
}
571
radio_type = 7;
572
}
573
else if (strcmp(vers,"RN2903 A")==0)
574
{
575
USB.println(F("LoRaWAN module is plugged on socket 0:"));
576
USB.println(F("--> Hardware serie: LoRaWAN ASIA-PAC / LATAM"));
577
if (dev_eui_answer == 1)
578
{
579
USB.print(F("--> Device EUI: "));
580
USB.println(uart._buffer, uart._length);
581
}
582
radio_type = 7;
583
}
584
}
585
586
if (radio_type == 0)
587
{
588
589
// init object in SOCKET0
590
xbee802.OFF();
591
uart.setUART(SOCKET0);
592
uart.setBaudrate(19200);
593
594
// switch module OFF
595
uart.closeUART();
596
Utils.setMuxUSB();
597
PWR.powerSocket(SOCKET0, LOW);
598
599
delay(500);
600
601
// select multiplexer
602
Utils.setMuxSocket0();
603
604
// begin serial communication
605
uart.beginUART();
606
607
// power on the socket
608
PWR.powerSocket(SOCKET0, HIGH);
609
delay(500);
610
serialFlush(SOCKET0);
611
612
// check for XBees in SOCKET0
613
static char cmd_lorawan[] = "at+dev?\r";
614
static char ans_lorawan[] = "ABZ-093";
615
616
// send command & receive answer
617
answer = uart.sendCommand((char*)cmd_lorawan, ans_lorawan,1000);
618
619
if (answer==1)
620
{
621
radio_type = 7;
622
// send command & receive answer
623
uint8_t dev_eui_answer = uart.sendCommand((char*)"at+deveui?\r","+OK=", 1000);
624
625
USB.println(F("LoRaWAN module is plugged on socket 0:"));
626
USB.println(F("--> Hardware serie: LoRaWAN JP/KR"));
627
if (dev_eui_answer == 1)
628
{
629
dev_eui_answer = uart.waitFor("\r",100);
630
if (dev_eui_answer == 1)
631
{
632
USB.print(F("--> Device EUI: "));
633
USB.println(uart._buffer, uart._length);
634
}
635
}
636
}
637
638
}
639
640
641
/////////////////////////////////////////////////////////////
642
// 8.4. check for SIGFOX module
643
/////////////////////////////////////////////////////////////
644
if (radio_type == 0)
645
{
646
// init object in SOCKET0
647
uart.setUART(SOCKET0);
648
uart.setBaudrate(9600);
649
650
// switch module OFF
651
uart.closeUART();
652
Utils.setMuxUSB();
653
PWR.powerSocket(SOCKET0, LOW);
654
655
delay(500);
656
657
// select multiplexer
658
Utils.setMuxSocket0();
659
660
// begin serial communication
661
uart.beginUART();
662
663
// power on the socket
664
PWR.powerSocket(SOCKET0, HIGH);
665
delay(6000);
666
serialFlush(SOCKET0);
667
668
// check for XBees in SOCKET0
669
static char cmd_sigfox[] = "AT&V\r";
670
671
// send command & receive answer
672
answer = uart.sendCommand((char*)cmd_sigfox, "TD1207", "TD1508", 1000);
673
char* pch;
674
char sigfox_id[8];
675
676
// get IMEI
677
if (answer != 0)
678
{
679
// send command & receive answer
680
delay(1000);
681
response = uart.sendCommand((char*)"ATI7\r", "\r\n", 1000);
682
683
uart. waitFor("\r\n", 1000);
684
if (response == 1)
685
{
686
pch = strtok( (char*)uart._buffer, "\r\n");
687
}
688
}
689
690
memcpy(sigfox_id,pch,sizeof(sigfox_id));
691
692
// check response:
693
if (answer == 1)
694
{
695
USB.println(F("SIGFOX module is plugged on socket 0:"));
696
USB.println(F("--> Hardware serie: Sigfox EU"));
697
USB.print(F("--> Serial Number: "));
698
USB.println(sigfox_id);
699
radio_type = 8;
700
}
701
else if (answer == 2)
702
{
703
response = uart.sendCommand((char*)"ATS307?\r","1", "63", 1000);
704
USB.println(F("SIGFOX module is plugged on socket 0:"));
705
if (response == 1)
706
{
707
USB.println(F("--> Hardware serie: Sigfox US"));
708
}
709
else if (response == 2)
710
{
711
USB.println(F("--> Hardware serie: Sigfox AU"));
712
}
713
714
USB.print(F("--> Serial Number: "));
715
USB.println(sigfox_id);
716
radio_type = 8;
717
}
718
}
719
720
721
722
723
/////////////////////////////////////////////////////////////
724
// 8.5. check for WIFI module
725
/////////////////////////////////////////////////////////////
726
if (radio_type == 0)
727
{
728
// init object in SOCKET0
729
uart.setUART(SOCKET0);
730
uart.setBaudrate(115200);
731
732
// switch module OFF
733
uart.closeUART();
734
Utils.setMuxUSB();
735
PWR.powerSocket(SOCKET0, LOW);
736
737
delay(500);
738
739
// select multiplexer
740
Utils.setMuxSocket0();
741
742
// begin serial communication
743
uart.beginUART();
744
745
// power on the socket
746
PWR.powerSocket(SOCKET0, HIGH);
747
delay(3000);
748
serialFlush(SOCKET0);
749
750
// check for XBees in SOCKET0
751
static char cmd_wifi[] = "AT+i\r";
752
753
// send command & receive answer
754
answer = uart.sendCommand((char*)cmd_wifi, "I/OK", 1000);
755
756
// check response:
757
if (answer == 1)
758
{
759
USB.println(F("WIFI PRO module is plugged on socket 0"));
760
radio_type = 9;
761
}
762
763
// switch module OFF
764
uart.closeUART();
765
Utils.setMuxUSB();
766
PWR.powerSocket(SOCKET0, LOW);
767
768
delay(500);
769
770
// select multiplexer
771
Utils.setMuxSocket0();
772
773
// begin serial communication
774
uart.beginUART();
775
776
// power on the socket
777
PWR.powerSocket(SOCKET0, HIGH);
778
delay(3000);
779
serialFlush(SOCKET0);
780
781
// check for XBees in SOCKET0
782
static char cmd_wifi_v3[] = "AT\r";
783
784
// send command & receive answer
785
answer = uart.sendCommand((char*)cmd_wifi_v3, "OK", 1000);
786
787
// check response:
788
if (answer == 1)
789
{
790
USB.println(F("WIFI PRO V3 module is plugged on socket 0"));
791
radio_type = 9;
792
}
793
}
794
795
796
797
/////////////////////////////////////////////////////////////
798
// 8.6. check for 4G module
799
/////////////////////////////////////////////////////////////
800
if (radio_type == 0)
801
{
802
// init object in SOCKET1
803
uart.setUART(SOCKET1);
804
uart.setBaudrate(115200);
805
806
// switch module OFF
807
uart.closeUART();
808
pinMode(GPRS_PW, OUTPUT);
809
digitalWrite(GPRS_PW, LOW);
810
delay(500);
811
812
// select multiplexer
813
Utils.setMuxSocket1();
814
815
// begin serial communication
816
uart.beginUART();
817
818
// Power on the module
819
digitalWrite(GPRS_PW, LOW);
820
delay(500);
821
digitalWrite(GPRS_PW, HIGH);
822
delay(11000);
823
serialFlush(SOCKET1);
824
825
// send command & receive answer
826
answer = uart.sendCommand((char*)"AT#CGMM\r", "LE910-EU", "LE910-NAG", "LE910-AU", 1000);
827
char imei[50];
828
memset(imei, 0x00, sizeof(imei));
829
830
// send CPIN command
831
uint8_t pin_ready;
832
pin_ready = uart.sendCommand((char*)"AT+CPIN?\r", "+CPIN: READY", "ERROR", 1000);
833
if((answer != 0) && (pin_ready != 1)){
834
errorFlag |= 1<<0x05;
835
}
836
837
// get IMEI
838
if (answer != 0)
839
{
840
// send command & receive answer
841
response = uart.sendCommand((char*)"AT#CGSN\r", "OK", 1000);
842
843
if (response == 1)
844
{
845
strcpy(imei, strtok( (char*)uart._buffer, "AT#CGSN: \r\n"));
846
}
847
}
848
849
// check response:
850
if (answer == 1)
851
{
852
USB.println(F("4G module is plugged on socket 1:"));
853
854
answer = uart.sendCommand((char*)"AT#CGMM\r", "LE910-EUG", "LE910-EU V2", 1000);
855
if (answer == 1)
856
USB.println(F("--> Hardware serie: LE910-EUG (4G EUROPE/BRAZIL)"));
857
else if (answer == 2)
858
USB.println(F("--> Hardware serie: LE910-EU V2 (4G EUROPE/BRAZIL)"));
859
860
if (pin_ready == 1)
861
USB.println(F("--> SIM card: OK"));
862
else
863
USB.println(F("--> SIM card: ERROR"));
864
865
USB.print(F("--> IMEI: "));
866
USB.println(imei);
867
radio_type = 10;
868
}
869
else if (answer == 2)
870
{
871
USB.println(F("4G module is plugged on socket 1:"));
872
USB.println(F("--> Hardware serie: LE910-NAG (4G USA)"));
873
if (pin_ready == 1) USB.println(F("--> SIM card: OK"));
874
else USB.println(F("--> SIM card: ERROR"));
875
USB.print(F("--> IMEI: "));
876
USB.println(imei);
877
radio_type = 10;
878
}
879
else if (answer == 3)
880
{
881
USB.println(F("4G module is plugged on socket 1:"));
882
USB.println(F("--> Hardware serie: LE910-AU (4G AUSTRALIA)"));
883
if (pin_ready == 1) USB.println(F("--> SIM card: OK"));
884
else USB.println(F("--> SIM card: ERROR"));
885
USB.print(F("--> IMEI: "));
886
USB.println(imei);
887
radio_type = 10;
888
}
889
}
890
891
892
893
/////////////////////////////////////////////////////////////
894
// 9. Check for modules into socket 1
895
/////////////////////////////////////////////////////////////
896
897
if (radio_type == 0)
898
{
899
USB.println(F("No radio module detected into socket 0\n"));
900
901
/////////////////////////////////////////////////////////////
902
// 9.1 Set Waspmote setting for XBee module for first time.
903
// (baudrate at 115200 and API mode enabled)
904
/////////////////////////////////////////////////////////////
905
closeSerial(SOCKET1);
906
delay(500);
907
xbee802.ON(SOCKET1);
908
909
Utils.setMuxSocket1();
910
delay(500);
911
beginSerial(9600, 1);
912
printString("+++", 1);
913
delay(2000);
914
printString("ATBD7,AP2,WR,CN\r\n", 1);
915
delay(500);
916
917
xbee802.OFF();
918
delay(500);
919
xbeeZB.ON(SOCKET1);
920
921
// In case of Zigbee modules:
922
// XBee command for 115200bps --> ATBD7
923
uint8_t ATBD7[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x42, 0x44, 0x07, 0x69 };
924
// XBee command for API mode --> ATAP2
925
uint8_t ATAP2[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x41, 0x50, 0x02, 0x63 };
926
// XBee command for saving config --> ATWR
927
uint8_t ATWR[] = { 0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D };
928
929
for (uint8_t i = 0; i < 9; i++)
930
{
931
printByte(ATBD7[i], SOCKET1);
932
}
933
delay(150);
934
closeSerial(SOCKET1);