Example for LoRa Communication-Transmit-Receive Application

Last revision 2026/01/24

This article offers a detailed guide on implementing LoRa communication using ESP32 and LoRaWAN node modules, covering hardware and software setup, wiring diagrams, sample code for data transmission between nodes, and achieving successful communication results.

In LoRa communication mode, each node device needs to set a custom address (range 1~255):

Address Description
0 Invalid address (not usable)
1~244 Valid reusable addresses, e.g., set two nodes to address 3
255 Broadcast address (when sending to 255, all devices in the network can receive)

Therefore, this example demonstrates: two FireBeetle 2 ESP32-E main controllers, each expanding one node module (addresses 1 and 2 respectively), achieving long-distance, directional data transmission from the temperature and humidity sensor via differentiated address configurations.

Hardware Preparation

Software Preparation

Wiring Diagram

  • Transmitter:

DFR1115-868-Transmitter wiring diagram

  • Receiver:

DFR1115-868-Receiver wiring diagram

Sample Code

Transmitter Program: Set node address to 1, send data to node with address 2.

#include <DFRobot_LWNode.h>
#include <dht11.h>
dht11 DHT;
#define DHT11_PIN 4  
#define FREQ  868100000  
DFRobot_LWNode_IIC node(1); // Set node address to 1

void setup( void ) {
    Serial.begin(115200);
    delay(5000);
    node.begin(/*communication IIC*/&Wire,/*debug UART*/&Serial);
    const uint32_t loraConfig[] = {FREQ, DBM16, 125000, 12};  // Configure LoRa communication parameters
    while(!node.setFreq(loraConfig[0])  || 
          !node.setEIRP(loraConfig[1])  || 
          !node.setBW(loraConfig[2])    || 
          !node.setSF(loraConfig[3])    || 
          !node.start()) {
        Serial.println("LoRa init failed");
        delay(2000);
    }
}

void loop( void ){
    DHT.read(DHT11_PIN);  // Get data from DHT11 temperature and humidity sensor
    Serial.print(DHT.humidity,1);
    Serial.print(",\t");
    Serial.println(DHT.temperature,1);
    String DHT11_DATE = String(DHT.humidity) + "%" + "  " + String(DHT.temperature) + "℃";
    delay(2000);
    node.sendPacket(2, DHT11_DATE);  // Send temperature and humidity data to node with address 2
    node.sleep(5000);
}

Receiver Program: Node with address 2 should burn the following code.

#include <DFRobot_LWNode.h>
#define FREQ  868100000    
DFRobot_LWNode_IIC node(2); // Set node address to 2

void rxCBFunc(uint8_t from, void *buffer, uint16_t size, int8_t rssi, int8_t snr){
    char *p = (char *)buffer;
    Serial.print("recv from: ");
    Serial.println(from, HEX);
    Serial.print("recv data: ");
    for(uint8_t i = 0; i < size; i++){
        Serial.print(p[i]);
    }
    Serial.println();
    Serial.println("Text:");
    Serial.println((char *)buffer);
    Serial.print("rssi="); Serial.println(rssi);
    Serial.print("snr="); Serial.println(snr);
}

void setup( void ){
    Serial.begin(115200);
    delay(5000);
    node.begin(/*communication IIC*/&Wire,/*debug UART*/&Serial);
    const uint32_t config[] = {FREQ, DBM16, 125000, 12};   // Configure LoRa communication parameters
    while(!node.setFreq(config[0])    || 
          !node.setEIRP(config[1])   || 
          !node.setBW(config[2])     || 
          !node.setSF(config[3])     || 
          !node.start()) {
        Serial.println("LoRa init failed, retrying...");
        delay(2000);
    }
    node.setRxCB(rxCBFunc);
}

void loop( void ){
    node.sleep(5000);
}

Results

Communication successful, the serial output from both the transmitter and receiver is as follows:

DFR1115-868-Transmit-Receive result

Was this article helpful?

TOP