RN2483 LoRa library for ESP32 based PLCs

Check out the LoRa library for the RN2483 module, used in ESP32 based PLCs
May 23, 2024 by
RN2483 LoRa library for ESP32 based PLCs
Boot & Work Corp. S.L., Bernat Brunet Pedra

Getting started

ESP32 based PLC with LoRa module from Industrial Shields incorporates the RN2483 module. In a previous post we saw how to interact with it, writing directly to the Serial Port to configure the module, send and receive messages.


As this procedure is slow and not optimal, Industrial Shields developed a new library for the ESP32 based PLCs which include RN2483 module. The development of this library has focused on the integration of the already existing Radio Head libraries, using the same functions and adapting the payload messages to simulate the format.


C++ File


Header


This way, the ESP32 PLC or the ESP32 PLC 14 can communicate with any other Radio Head module with ease, without the need of externally prepare the packet which will be sent, or perform any other strategies that slow down the sending or receiving process. In addition, the communication between the Arduino based PLCs such as the M-Duino LoRa or the Ardbox LoRa is possible.


Check out the previous post for a basic understanding about the module, and let's jump into the library.

Default configuration parameters

The RN2483 LoRa module is configured with the following parameters to match the existing default configuration for the rest of the Radio Head modules:

  • Coding Rate: 4/5
  • Band Width: 125 kHz
  • Preamble Length: 8
  • Spreading Factor: 12
  • TX Power: 13
  • Frequency: 868 MHz
  • Sync Word: 0x12

Each of this values can be modified after starting the module.

First steps with the RN2483 library

The steps to set up the module are very simple:

  • First of all, include the library to the Arduino IDE sketch:
#include <RH_RN2483.h>
  • Initialize the LoRa object with the Serial Port:

For ESP32 PLC:

RH_RN2483 LoRa(&SerialExp2);

For ESP32 PLC 14:

#include <HardwareSerialPIC.h>

RH_RN2483 LoRa(&SerialPIC);
  • Start the Serial Port at 57600 baud rate:
SerialExp2.begin(57600);

For ESP32 PLC 14:

SerialPIC.begin(57600);
  • Reset the LoRa module:
pinMode(EXP_RST, OUTPUT);

delay(1000);

digitalWrite(EXP2_RST, LOW);

delay(1000);

digitalWrite(EXP2_RST, HIGH);

For ESP32 PLC 14:

pinMode(RST, OUTPUT);

delay(1000);

digitalWrite(RST, LOW);

delay(1000);

digitalWrite(RST, HIGH);
  • Finally, start the LoRa module:
LoRa.init();

You can find an example code for your ESP32 based PLC in File -> Examples ->


Interaction with the module

After starting the module, we can start sending and receiving LoRa packets from another module. The send function should have the following form:

uint8_t msg[] = "Hello World";

if (LoRa.send(msg, sizeof(msg))) {
    Serial.println("Message sent");
 }

else {
    Serial.println("Error while sending");
}

The size of the LoRa packet should not exceed 255 bytes, otherwise the send function will fail. The send can take up to almost 2 seconds, so keep in mind that during that time, the module is not able to receive any data from any LoRa module at all.

The receive function should be called with:

uint8_t buf[RH_RN2483_MAX_MESSAGE_LEN];

uint8_t len = sizeof(buf);

if (LoRa.available()) {
    if (LoRa.recv(buf, &len)) {
        Serial.print("Got request: ");
        LoRa.printBuffer(buf, len);
    }
    else {
        Serial.println("recv failed");
    }
}

"LoRa.available()" must be called before attempting to read a LoRa packet to ensure that a valid message was received, otherwise the data read from "LoRa.recv(buf, len)" is unpredictable. The message stored in buf has the following format:

  • If the sender send "A", in the buf array there will be {"4", "1"}, with len = 2. This corresponds to the ASCII equivalent from "A" to decimal 41.

To print just the buffer, use the "LoRa.printBuffer(buf, len)" function. This will print the contents of the received message in a readable format plus a new line character.



​Search in our Blog

RN2483 LoRa library for ESP32 based PLCs
Boot & Work Corp. S.L., Bernat Brunet Pedra May 23, 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 >>>