How to Work with LoRaWAN and an ESP32 PLC

Connecting Your ESP32 PLC to LoRaWAN
December 10, 2024 by
How to Work with LoRaWAN and an ESP32 PLC
Boot & Work Corp. S.L., Joan Bello

Introduction

LoRaWAN is a powerful technology for low-power, wide-area networks, making it ideal for industrial applications. In this guide, we will demonstrate how to integrate LoRaWAN with an ESP32 PLC, using an RN2483 LoRa click module and The Things Network (TTN). By the end of this tutorial, you’ll have your ESP32 PLC sending data to TTN.

Hardware Requirements

To get started, ensure you have the following:

  1. ESP32 PLC: Industrial Shields ESP32-based Programmable Logic Controller.
  2. RN2483 Click Module: A LoRaWAN module that connects to your ESP32 PLC.
  3. Connections:
    • Slot 1 or Slot 2 on the ESP32 PLC (Slot 2 is next to the Ethernet connector and requires an additional switch configuration).
  4. Micro USB Cable: For programming the ESP32 PLC.

Software Setup

Before diving into the code, make sure you have:

  1. Arduino IDE: Installed and configured to program ESP32 devices.
  2. RN2483 Arduino Library:

Register Your Device on TTN

  1. Go to The Things Stack and log in.
  2. Create a new device and note the following keys:
    • Device EUI
    • Application EUI
    • App Key

These keys will be used in the Arduino sketch.

Arduino Code

Below is the Arduino code to set up LoRaWAN on the ESP32 PLC and send data to TTN. Replace the placeholders for devEui, appEui, and appKey with your device-specific information from TTN.

#include <rn2xx3.h>

// Constants for OTAA keys
const char* devEui = "0011223344556677";  // Replace with your Device EUI
const char* appEui = "0000000000000000"; // Replace with your Application EUI
const char* appKey = "00112233445566778899AABBCCDDEEFF"; // Replace with your App Key

// Create an instance of the rn2xx3 library
rn2xx3 myLora(SerialExp2);

// The setup routine runs once when you press reset:
void setup() {
  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 ESP32 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;

  join_result = myLora.initOTAA(appEui, appKey, devEui);

  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(200);
}    

Decoder for TTN

To visualize the data, add the following decoder in Payload Formatters for the uplink messages on your TTN console:

function decodeUplink(input) {
  return {
    data: {
      bytes: input.bytes
    },
    warnings: [],
    errors: []
  };
}    

This will display the raw bytes sent from the ESP32 PLC in the TTN console.

Key Steps Recap

  1. Connect the RN2483 click module to the ESP32 PLC (Slot 2 recommended for flexibility).
  2. Set up Arduino IDE with the RN2483 Arduino library.
  3. Register your device on TTN and copy the keys into the Arduino sketch.
  4. Upload the code to the ESP32 PLC.
  5. Monitor the TTN console for incoming data.

Troubleshooting Tips

  • No Join Response: Ensure you have LoRaWAN coverage and that your keys are correctly configured in TTN.
  • Data Not Visible in TTN Console: Double-check the decoder function and your code’s payload format.

Summary

With this setup, your ESP32 PLC is now connected to a LoRaWAN network, transmitting data to TTN. This is a robust foundation for industrial IoT applications such as sensor monitoring, asset tracking, or even smart city solutions.

Happy coding! 🚀

​Search in our Blog

How to Work with LoRaWAN and an ESP32 PLC
Boot & Work Corp. S.L., Joan Bello December 10, 2024
Share this post

Looking for your ideal Programmable Logic Controller?

Take a look at this product comparison with other industrial controllers Arduino-based. 

We are comparing inputs, outputs, communications and other features with the ones of the relevant brands.


Industrial PLC comparison >>>