------ Waspmote factory default code ------
Explanation: Send basic parameters through the corresponding
Copyright (C) 2019 Libelium Comunicaciones Distribuidas S.L.
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/>.
Implementation: Yuri Carmona, Javier Siscart
// Define the Waspmote default ID
char node_id[] = "node_id";
// Define the authentication key
char key_access[] = "LIBELIUM";
// Declare global variables
char filename[] = "TEST_SD.TXT";
char destination[] = "000000000000FFFF";
// Declare the xbee type in the case there is an XBee module
USB.println(F("Starting Waspmote factory default program"));
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
uint8_t battery = PWR.getBatteryLevel();
USB.println(F("************************************************"));
USB.print(F("ERROR: Battery issue. Battery level (%): "));
USB.println(battery, DEC);
USB.println(F("************************************************"));
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
// Show '_serial_id' stored by the API when powering up
USB.print(F("Global variable '_serial_id':"));
USB.printHex(_serial_id[0]);
USB.printHex(_serial_id[1]);
USB.printHex(_serial_id[2]);
USB.printHex(_serial_id[3]);
USB.printHex(_serial_id[4]);
USB.printHex(_serial_id[5]);
USB.printHex(_serial_id[6]);
USB.printHex(_serial_id[7]);
// Reading the serial number
USB.print(F("Waspmote serial ID: "));
USB.printHex(_serial_id[0]);
USB.printHex(_serial_id[1]);
USB.printHex(_serial_id[2]);
USB.printHex(_serial_id[3]);
USB.printHex(_serial_id[4]);
USB.printHex(_serial_id[5]);
USB.printHex(_serial_id[6]);
USB.printHex(_serial_id[7]);
///////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////
PWR.setSensorPower(SENS_3V3, SENS_ON);
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
if (SD.writeSD(filename, "Test SD", 0))
USB.println(F("Error SD"));
USB.println(F("No SD card detected"));
/////////////////////////////////////////////////////////////
// 4. Store key access in EEPROM
/////////////////////////////////////////////////////////////
Utils.setAuthKey(key_access);
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// 6. Set Waspmote setting for XBee module for first time.
// (baudrate at 115200 and API mode enabled)
/////////////////////////////////////////////////////////////
// Note: Only valid for SOCKET 0
printString("ATBD7,AP2,WR,CN\r\n", 0);
// In case of Zigbee modules:
// XBee command for 115200bps --> ATBD7
uint8_t ATBD7[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x42, 0x44, 0x07, 0x69 };
// XBee command for API mode --> ATAP2
uint8_t ATAP2[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x41, 0x50, 0x02, 0x63 };
// XBee command for saving config --> ATWR
uint8_t ATWR[] = { 0x7E, 0x00, 0x04, 0x08, 0x01, 0x57, 0x52, 0x4D };
for (uint8_t i = 0; i < 9; i++)
printByte(ATBD7[i], SOCKET0);
beginSerial(115200, SOCKET0);
for (uint8_t i = 0; i < 9; i++)
printByte(ATAP2[i], SOCKET0);
for (uint8_t i = 0; i < 8; i++)
printByte(ATWR[i], SOCKET0);
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
Utils.setLED(LED0, LED_OFF);
Utils.setLED(LED1, LED_OFF);
for (int i = 0 ; i < 4 ; i++)
/////////////////////////////////////////////////////////////
// 8. Identify the radio connected to Waspmote
// - LoRa (SX1272) (deprecated)
// - LoRaWAN (RN2483 or RN2903 or ABZ)
// - Sigfox (TD1207 or TD1508)
/////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////
// 8.1. check for XBee module
/////////////////////////////////////////////////////////////
// define object for UART0
WaspUART uart = WaspUART();
uint8_t class_buffer[100];
uart._buffer = class_buffer;
// init object in SOCKET0
// begin serial communication
PWR.powerSocket(SOCKET0, HIGH);
// check for XBees in SOCKET0
uint8_t cmd_xbee[] = {0x7E, 0x00 , 0x04 , 0x08 , 0x01 , 0x56 , 0x52 , 0x4E};
// send command & receive answer
uart.sendCommand(cmd_xbee, sizeof(cmd_xbee));
// check response: 7E00078801565200xxxx??
if ((uart._buffer[0] == 0x7E)
&& (uart._buffer[1] == 0x00)
&& (uart._buffer[3] == 0x88)
&& (uart._buffer[4] == 0x01)
&& (uart._buffer[5] == 0x56)
&& (uart._buffer[6] == 0x52)
&& (uart._buffer[7] == 0x00))
USB.println(F("XBee module is plugged on socket 0:"));
USB.print(F("XBee module in SOCKET0. Firmware: "));
USB.printHex(uart._buffer[8]);
USB.printHex(uart._buffer[9]);
firmware[0] = uart._buffer[8];
firmware[1] = uart._buffer[9];
firmware[2] = uart._buffer[10];
firmware[3] = uart._buffer[11];
/////////////////////////////////
// Get the XBee firmware version
/////////////////////////////////
// Set the XBee firmware type depending ont he previous response
if ((firmware[0] < 0x20) && (firmware[1] > 0x80))
radio_type = 1; // 802.15.4
USB.println(F("--> XBee type: 802.15.4"));
else if (firmware[0] == 0x20 && firmware[1] > 0x02)
radio_type = 1; // 802.15.4
USB.println(F("--> XBee type: 802.15.4 XBee 3"));
else if (firmware[0] == 0x10 && firmware[1] < 0x50)
USB.println(F("--> XBee type: ZigBee 3"));
else if ((firmware[0] < 0x20) && (firmware[1] > 0x00))
radio_type = 2; // 868MHz - 900MHz
// send Hardware Serial command
if (!xbee802.sendCommandAT("HS#"))
// USB.printHexln(xbee802.commandAT, 3);
if (xbee802.commandAT[0] == 3)
USB.println(F("--> XBee type: 900HP"));
// check for available frequencies:
// US: 00FFFFFFFFFFFFFFFF
// BR: 00FFFFFFFE00000FFF
// AU: 00FFFFFFFE00000000
if (!xbee802.sendCommandAT("AF#"))
//USB.printHexln(xbee802.commandAT, 16);
if ((xbee802.commandAT[1] == 0xFF)
&& (xbee802.commandAT[2] == 0xFF)
&& (xbee802.commandAT[3] == 0xFF)
&& (xbee802.commandAT[4] == 0xFF)
&& (xbee802.commandAT[5] == 0xFF)
&& (xbee802.commandAT[6] == 0xFF)
&& (xbee802.commandAT[7] == 0xFF)
&& (xbee802.commandAT[8] == 0xFF))
USB.println(F("--> Hardware serie: USA"));
else if ((xbee802.commandAT[1] == 0xFF)
&& (xbee802.commandAT[2] == 0xFF)
&& (xbee802.commandAT[3] == 0xFF)
&& (xbee802.commandAT[4] == 0xFE)
&& (xbee802.commandAT[5] == 0x00)
&& (xbee802.commandAT[6] == 0x00)
&& (xbee802.commandAT[7] == 0x0F)
&& (xbee802.commandAT[8] == 0xFF))
USB.println(F("--> Hardware serie: BRAZIL"));
else if ((xbee802.commandAT[1] == 0xFF)
&& (xbee802.commandAT[2] == 0xFF)
&& (xbee802.commandAT[3] == 0xFF)
&& (xbee802.commandAT[4] == 0xFE)
&& (xbee802.commandAT[5] == 0x00)
&& (xbee802.commandAT[6] == 0x00)
&& (xbee802.commandAT[7] == 0x00)
&& (xbee802.commandAT[8] == 0x00))
USB.println(F("--> Hardware serie: AUSTRALIA"));
USB.println(F("--> Hardware serie: ERROR"));
else if (xbee802.commandAT[0] == 8)
USB.println(F("--> XBee type: 868LP"));
else if (firmware[0] >= 0x80)
radio_type = 3; // DigiMesh
USB.println(F("--> XBee type: DigiMesh"));
else if (((firmware[0] >= 0x20) && (firmware[1] < 0xB0)) ||
((firmware[0] == 0x70) && (firmware[1] == 0x5B)) )
USB.println(F("--> XBee type: ZigBee"));
else if (firmware[0] == 0x00 && firmware[1] >= 0x02)
radio_type = 5; // 900 MHz Intl
USB.println(F("--> XBee type: 900 International"));
/////////////////////////////////////////////////////////////
// Get the XBee MAC address
/////////////////////////////////////////////////////////////
// Get the XBee MAC address
while ((xbee802.getOwnMac() != 0) && (counter < 12))
// convert mac address from array to string
Utils.hex2str(xbee802.sourceMacHigh, macHigh, 4);
Utils.hex2str(xbee802.sourceMacLow, macLow, 4);
// Get the XBee MAC address
while ((xbee802.getOwnMac() != 0) && (counter < 12))
// convert mac address from array to string
Utils.hex2str(xbee802.sourceMacHigh, macHigh, 4);
Utils.hex2str(xbee802.sourceMacLow, macLow, 4);
USB.print(F("--> MAC address: "));
USB.print(F("--> Firmware version: "));
USB.print(firmware[0], HEX);
USB.println(firmware[1], HEX);
else USB.println(firmware[1], HEX);
USB.print(F("--> Firmware version: "));
USB.printHex(firmware[0]);
USB.printHex(firmware[1]);
USB.printHex(firmware[2]);
USB.printHex(firmware[3]);
/////////////////////////////////////////////////////////////
// 8.2. check for LORAWAN module
/////////////////////////////////////////////////////////////
// init object in SOCKET0
PWR.powerSocket(SOCKET0, LOW);
// begin serial communication
PWR.powerSocket(SOCKET0, HIGH);
// check for XBees in SOCKET0
static char cmd_lorawan[] = "sys get ver\r\n";
// send command & receive answer
answer = uart.sendCommand((char*)cmd_lorawan, "\r\n",1000);
char vers[9], vers_in[21];
memset(vers,0x00,sizeof(vers));
memset(vers_in,0x00,sizeof(vers_in));
memcpy (vers,uart._buffer,8);
memcpy (vers_in,uart._buffer,20);
// send command & receive answer
uint8_t dev_eui_answer = uart.sendCommand((char*)"mac get deveui\r\n", "\r\n", 1000);
if (strcmp(vers,"RN2483 0")==0 || strcmp(vers,"RN2483 1")==0)
USB.println(F("LoRaWAN module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: LoRaWAN EU"));
USB.print(F("--> Device EUI: "));
USB.print(uart._buffer, uart._length);
else if (strcmp(vers,"RN2903 0")==0 || strcmp(vers,"RN2903 1")==0)
if (strcmp(vers_in,"RN2903 1.0.5C_rc1-In")==0)
USB.println(F("LoRaWAN module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: LoRaWAN IN"));
USB.print(F("--> Device EUI: "));
USB.println(uart._buffer, uart._length);
USB.println(F("LoRaWAN module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: LoRaWAN US"));
USB.print(F("--> Device EUI: "));
USB.println(uart._buffer, uart._length);
else if (strcmp(vers,"RN2903 S")==0)
USB.println(F("LoRaWAN module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: LoRaWAN AU"));
USB.print(F("--> Device EUI: "));
USB.println(uart._buffer, uart._length);
else if (strcmp(vers,"RN2903 A")==0)
USB.println(F("LoRaWAN module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: LoRaWAN ASIA-PAC / LATAM"));
USB.print(F("--> Device EUI: "));
USB.println(uart._buffer, uart._length);
// init object in SOCKET0
PWR.powerSocket(SOCKET0, LOW);
// begin serial communication
PWR.powerSocket(SOCKET0, HIGH);
// check for XBees in SOCKET0
static char cmd_lorawan[] = "at+dev?\r";
static char ans_lorawan[] = "ABZ-093";
// send command & receive answer
answer = uart.sendCommand((char*)cmd_lorawan, ans_lorawan,1000);
// send command & receive answer
uint8_t dev_eui_answer = uart.sendCommand((char*)"at+deveui?\r","+OK=", 1000);
USB.println(F("LoRaWAN module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: LoRaWAN JP/KR"));
dev_eui_answer = uart.waitFor("\r",100);
USB.print(F("--> Device EUI: "));
USB.println(uart._buffer, uart._length);
/////////////////////////////////////////////////////////////
// 8.4. check for SIGFOX module
/////////////////////////////////////////////////////////////
// init object in SOCKET0
PWR.powerSocket(SOCKET0, LOW);
// begin serial communication
PWR.powerSocket(SOCKET0, HIGH);
// check for XBees in SOCKET0
static char cmd_sigfox[] = "AT&V\r";
// send command & receive answer
answer = uart.sendCommand((char*)cmd_sigfox, "TD1207", "TD1508", 1000);
// send command & receive answer
response = uart.sendCommand((char*)"ATI7\r", "\r\n", 1000);
uart. waitFor("\r\n", 1000);
pch = strtok( (char*)uart._buffer, "\r\n");
memcpy(sigfox_id,pch,sizeof(sigfox_id));
USB.println(F("SIGFOX module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: Sigfox EU"));
USB.print(F("--> Serial Number: "));
response = uart.sendCommand((char*)"ATS307?\r","1", "63", 1000);
USB.println(F("SIGFOX module is plugged on socket 0:"));
USB.println(F("--> Hardware serie: Sigfox US"));
USB.println(F("--> Hardware serie: Sigfox AU"));
USB.print(F("--> Serial Number: "));
/////////////////////////////////////////////////////////////
// 8.5. check for WIFI module
/////////////////////////////////////////////////////////////
// init object in SOCKET0
uart.setBaudrate(115200);
PWR.powerSocket(SOCKET0, LOW);
// begin serial communication
PWR.powerSocket(SOCKET0, HIGH);
// check for XBees in SOCKET0
static char cmd_wifi[] = "AT+i\r";
// send command & receive answer
answer = uart.sendCommand((char*)cmd_wifi, "I/OK", 1000);
USB.println(F("WIFI PRO module is plugged on socket 0"));
PWR.powerSocket(SOCKET0, LOW);
// begin serial communication
PWR.powerSocket(SOCKET0, HIGH);
// check for XBees in SOCKET0
static char cmd_wifi_v3[] = "AT\r";
// send command & receive answer
answer = uart.sendCommand((char*)cmd_wifi_v3, "OK", 1000);
USB.println(F("WIFI PRO V3 module is plugged on socket 0"));
/////////////////////////////////////////////////////////////
// 8.6. check for 4G module
/////////////////////////////////////////////////////////////
// init object in SOCKET1
uart.setBaudrate(115200);
pinMode(GPRS_PW, OUTPUT);
digitalWrite(GPRS_PW, LOW);
// begin serial communication
digitalWrite(GPRS_PW, LOW);
digitalWrite(GPRS_PW, HIGH);
// send command & receive answer
answer = uart.sendCommand((char*)"AT#CGMM\r", "LE910-EU", "LE910-NAG", "LE910-AU", 1000);
memset(imei, 0x00, sizeof(imei));
pin_ready = uart.sendCommand((char*)"AT+CPIN?\r", "+CPIN: READY", "ERROR", 1000);
if((answer != 0) && (pin_ready != 1)){
// send command & receive answer
response = uart.sendCommand((char*)"AT#CGSN\r", "OK", 1000);
strcpy(imei, strtok( (char*)uart._buffer, "AT#CGSN: \r\n"));
USB.println(F("4G module is plugged on socket 1:"));
answer = uart.sendCommand((char*)"AT#CGMM\r", "LE910-EUG", "LE910-EU V2", 1000);
USB.println(F("--> Hardware serie: LE910-EUG (4G EUROPE/BRAZIL)"));
USB.println(F("--> Hardware serie: LE910-EU V2 (4G EUROPE/BRAZIL)"));
USB.println(F("--> SIM card: OK"));
USB.println(F("--> SIM card: ERROR"));
USB.print(F("--> IMEI: "));
USB.println(F("4G module is plugged on socket 1:"));
USB.println(F("--> Hardware serie: LE910-NAG (4G USA)"));
if (pin_ready == 1) USB.println(F("--> SIM card: OK"));
else USB.println(F("--> SIM card: ERROR"));
USB.print(F("--> IMEI: "));
USB.println(F("4G module is plugged on socket 1:"));
USB.println(F("--> Hardware serie: LE910-AU (4G AUSTRALIA)"));
if (pin_ready == 1) USB.println(F("--> SIM card: OK"));
else USB.println(F("--> SIM card: ERROR"));
USB.print(F("--> IMEI: "));
/////////////////////////////////////////////////////////////
// 9. Check for modules into socket 1
/////////////////////////////////////////////////////////////
USB.println(F("No radio module detected into socket 0\n"));
/////////////////////////////////////////////////////////////
// 9.1 Set Waspmote setting for XBee module for first time.
// (baudrate at 115200 and API mode enabled)
/////////////////////////////////////////////////////////////
printString("ATBD7,AP2,WR,CN\r\n", 1);
// In case of Zigbee modules:
// XBee command for 115200bps --> ATBD7
uint8_t ATBD7[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x42, 0x44, 0x07, 0x69 };
// XBee command for API mode --> ATAP2
uint8_t ATAP2[] = { 0x7E, 0x00, 0x05, 0x08, 0x01, 0x41, 0x50, 0x02, 0x63