LoRaWAN on ESP32 PLCs
Prior configuration
Before attempting to send messages through LoRaWAN, make sure to have registered your device to the The Things Network Stack, as seen in the LoRaWAN setup chapter.
In addition, install the RN2483-Arduino-Library for LoRaWAN purposes.
Code snippet
For ESP32 PLC based PLCs, use the following example code from the previous downloaded RN2483 library:
#include <rn2xx3.h> //create an instance of the rn2xx3 library rn2xx3 myLora(SerialExp2); // the setup routine runs once when you press reset: void setup() { // Open serial communications and wait for port to open: Serial.begin(115200); //serial port to computer SerialExp2.begin(57600); //serial port to radio Serial.println("Startup"); initialize_radio(); //transmit a startup message myLora.tx("TTN Mapper on TTN Enschede node"); delay(2000); } void initialize_radio() { //reset rn2483 pinMode(EXP2_RST, OUTPUT); digitalWrite(EXP2_RST, LOW); delay(1000); digitalWrite(EXP2_RST, HIGH); delay(1000); //wait for the RN2xx3's startup message SerialExp2.flush(); Serial.println("RN2xx3 firmware version:"); Serial.println(myLora.sysver()); //configure your keys and join the network Serial.println("Trying to join TTN"); bool join_result = false; // DevEUI, AppEUI, and AppKey for LoRaWAN const char *devEui = "0011223344556677"; const char *appEui = "0000000000000000"; const char *appKey = "00112233445566778899AABBCCDDEEFF"; join_result = myLora.initOTAA(appEui, appKey, devEui); // Retry joining the network until successful while(!join_result) { Serial.println("Unable to join. Are your keys correct, and do you have TTN coverage?"); delay(60000); //delay a minute before retry join_result = myLora.init(); } Serial.println("Successfully joined TTN"); } // the loop routine runs over and over again forever: void loop() { Serial.println("TXing"); myLora.tx("!"); //one byte, blocking function delay(60000); // wait 60 s between uplinks; check the duty cycle limits of your region }
#include <rn2xx3.h> //create an instance of the rn2xx3 library rn2xx3 myLora(SerialExp2); // the setup routine runs once when you press reset: void setup() { // Open serial communications and wait for port to open: Serial.begin(115200); //serial port to computer SerialExp2.begin(57600); //serial port to radio Serial.println("Startup"); initialize_radio(); //transmit a startup message myLora.tx("TTN Mapper on TTN Enschede node"); delay(2000); } void initialize_radio() { //reset rn2483 pinMode(EXP2_RST, OUTPUT); digitalWrite(EXP2_RST, LOW); delay(1000); digitalWrite(EXP2_RST, HIGH); delay(1000); //wait for the RN2xx3's startup message SerialExp2.flush(); Serial.println("RN2xx3 firmware version:"); Serial.println(myLora.sysver()); //configure your keys and join the network Serial.println("Trying to join TTN"); bool join_result = false; // DevEUI, AppEUI, and AppKey for LoRaWAN const char *devEui = "0011223344556677"; const char *appEui = "0000000000000000"; const char *appKey = "00112233445566778899AABBCCDDEEFF"; join_result = myLora.initOTAA(appEui, appKey, devEui); // Retry joining the network until successful while(!join_result) { Serial.println("Unable to join. Are your keys correct, and do you have TTN coverage?"); delay(60000); //delay a minute before retry join_result = myLora.init(); } Serial.println("Successfully joined TTN"); } // the loop routine runs over and over again forever: void loop() { Serial.println("TXing"); myLora.tx("!"); //one byte, blocking function delay(60000); // wait 60 s between uplinks; check the duty cycle limits of your region }
As the end device is configured to work over OTAA, replace the 3 variables devEui, appEui and appKey with your own and upload the code. In this case copy and paste the keys as they are shown in the TTN Console, in Big-Endian or MSB format.
Rating
0
0
There are no comments for now.
Join this Course
to be the first to leave a comment.