Example Code for Arduino-ABP Network Access
This sample code connects to the LoRaWAN network in ABP (Activation By Personalization) mode (you can enter any DEVEUI when adding the device). It sends data (the string "DFRobot") every 10 seconds and supports receiving downlink data for printing.
Hardware Preparation
- DFR0994 Romeo ESP32-S3 x 1
- LoRaWAN Indoor Gateway (868MHz Version) or LoRaWAN Indoor Gateway (915MHz Version) x1
Software Preparation
- Download Arduino IDE: Click to download Arduino IDE
- Add ESP32 board to Arduino IDE
- Please install the DFRobot_LoRaWAN_ESP32S3 library before use.
- About how to install the library?
Note:
- LoRaWAN protocol stack version: 1.0.3
- You need to set the data parsing format to "text" on the gateway.
Sample Code
#include "DFRobot_LoRaWAN.h"
#define APP_INTERVAL_MS 10000
const uint32_t nodeDevAddr = 0xDF666666;
const uint8_t nodeNwsKey[16] = {
0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF
};
const uint8_t nodeAppsKey[16] = {
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10
};
uint8_t buffer[255];
uint8_t port = 2;
LorawanNode node(nodeDevAddr, nodeNwsKey, nodeAppsKey, CLASS_A);
TimerEvent_t appTimer;
void userSendConfirmedPacket(void)
{
TimerSetValue(&appTimer, APP_INTERVAL_MS);
TimerStart(&appTimer);
const char * data = "DFRobot";
uint32_t datalen = strlen(data);
memcpy(buffer, data, datalen);
node.sendConfirmedPacket(port, buffer, /*size=*/datalen);
printf("Sending Confirmed Packet...\n");
}
void rxCb(void *buffer,uint16_t size,uint8_t port,int16_t rssi,int8_t snr,bool ackReceived,uint16_t uplinkCounter ,uint16_t downlinkCounter)
{
if(size != 0){
printf("data:%s\n", (uint8_t*)buffer);
}
}
void setup()
{
Serial.begin(115200);
delay(5000); // Open the serial port within 5 seconds after uploading to view full print output
if(!(node.init(/*dataRate=*/DR_4, /*txEirp=*/16))){ // Initialize the LoRaWAN node, set the data rate and Tx Eirp
printf("LoRaWAN Init Failed!\nPlease Check: DR or Region\n");
while(1);
}
TimerInit(&appTimer, userSendConfirmedPacket); // Initialize timer event
node.setTxCB(txCb); // Set the callback function for sending data
node.setRxCB(rxCb); // Set the callback function for receiving data
printf("ABP Test\n");
TimerSetValue(&appTimer, APP_INTERVAL_MS);
TimerStart(&appTimer); // Start a timer to send data
}
void loop()
{
delay(1000);
}
Result


Was this article helpful?
