top of page

"Maximizing LoRa Module Range: Tips for Successful 10 Km Test With Node MCU, DHT 11 Sensor, and Oled Module"

Updated: Mar 4

The RYLR998 LoRa module from Reyax Technology is a powerful wireless communication solution for long-range and low-power applications. This module utilizes the LoRaWAN protocol, which allows for communication over long distances with minimal power consumption.





One of the key features of the RYLR998 is its long-range capabilities. With a range of up to 15km in rural areas and 2km in urban areas, this module is perfect for applications where traditional wireless communication methods may not be feasible. Additionally, the module's low power consumption means it can operate on a single battery for extended periods, making it ideal for use in remote or hard-to-reach locations.

The RYLR998 also offers high reliability and security. The LoRaWAN protocol used by the module provides end-to-end encryption to ensure that your data stays safe and secure. Additionally, the module's robust design and ability to operate in harsh environments make it reliable in a wide range of applications.

The RYLR998 is compatible with a wide range of microcontrollers and development boards, making it easy to integrate into your project. The module also comes with a comprehensive development kit, which includes sample code and documentation to help you get started quickly and easily.

Overall, the RYLR998 LoRa module from Reyax Technology is a versatile and powerful wireless communication solution for long-range and low-power applications. With its long-range capabilities, low power consumption, and high reliability and security, this module is sure to be a valuable addition to any IoT project.


Circuit Diagram :


LoRa Transmitter Circuit Diagram



LoRa Receiver Circuit Diagram







CODE :

TRANSMITTER CODE :

#include <SoftwareSerial.h>

#include "DHT.h" // include DHT Sensor library

#define RXD2 D7

#define TXD2 D8

SoftwareSerial mySerial(RXD2, TXD2);

#define TARGET_ID 2

#define DHTPIN D5 // Digital pin connected to the DHT sensor

#define DHTTYPE DHT11 // DHT 11

DHT dht(DHTPIN, DHTTYPE); // DHT object

float humidity, temperature;

int data_length;

String sensorsdata;

const unsigned long sendInterval = 5000;

unsigned long previousTime = 0;

String Message = "Perfect Connection";

String content = "";

int counter = 0;

void setup()

{

Serial.begin(115200);

mySerial.begin(115200);

delay(1000);

dht.begin();

pinMode(0, INPUT_PULLUP);

pinMode(2, OUTPUT);

digitalWrite(2, HIGH);

}

//====================== Function to Read the DHT Data ======================================//

void readSensor(){

delay(250);

humidity = dht.readHumidity();

// Read temperature as Celsius (the default)

temperature = dht.readTemperature();

Serial.println("Moisture Details");

Serial.print("Temperature = ");

Serial.print(temperature);

Serial.println(F(" °C"));

Serial.print("Humidity = ");

Serial.print(humidity);

Serial.println(F(" % Rh"));

// Check if any reads failed and exit early (to try again).

if (isnan(humidity) || isnan(temperature)) {

Serial.println(F("Failed to read from DHT sensor!"));

return;

}

}

//====================== Function to Process Readings and create string ==============================//

void processData(){

sensorsdata = sensorsdata + String(temperature) + "%" + String(humidity) + "%" ;

Serial.print("sensor values: ");

Serial.println(sensorsdata);

// find the length of data

data_length = sensorsdata.length();

Serial.print("data length: ");

Serial.println(data_length);

delay(250);

}

//====================== Function to send data Command ==============================//

void send_data(String sensorvalue, int valuelength)

{

String mymessage;

mymessage = mymessage + "AT+SEND=2" + "," + valuelength + "," + sensorvalue + "\r\n";

mySerial.print(mymessage);

Serial.print("Data Sent : ");

Serial.println(mymessage);

delay(250);

}

void loop() {

// put your main code here, to run repeatedly:

unsigned long currentTime = millis();

if (currentTime - previousTime >= sendInterval) {

/* Event code */

//Serial.println("Send The Sensor Readings");

processData();

send_data(sensorsdata , data_length);

/* Update the timing for the next time around */

previousTime = currentTime;

}

readSensor();

sensorsdata = "";

if (Serial.available()){

Serial.println("Writing");

content = Serial.readString();

content.trim();

Serial.println();

content = content + "\r\n";

char* bufc = (char*) malloc(sizeof(char) * content.length() + 1);

content.toCharArray(bufc, content.length() + 1);

mySerial.write(bufc);

free(bufc);

}

if (mySerial.available()) {

String incomming = mySerial.readString();

if (incomming.length() <= 10)

Serial.println(incomming);

else {

String channel_ID = incomming.substring(incomming.indexOf('=') + 1, incomming.indexOf(','));

Serial.println("Channel ID : " + channel_ID);

String str = incomming.substring(incomming.indexOf(',') + 1);

String msgLength = str.substring(0, str.indexOf(','));

Serial.println("Message Length : " + msgLength);

String str2 = str.substring(str.indexOf(',') + 1);

String message = str2.substring(0, str2.indexOf(','));

Serial.println("Message : " + message);

digitalWrite(2, LOW);

delay(2000);

digitalWrite(2, HIGH);

}

}

// When the button is pressed send the message to other module

if (digitalRead(0) == LOW) {

delay(1000);

String data = Message + " - Count: " + counter;

sendLoraData(data, TARGET_ID);

//increase counter on each send

counter++;

}

}

void sendLoraData(String data, int address) {

String myString = "AT+SEND=" + String(address) + "," + String(data.length()) + "," + data + "\r\n";

char* buf = (char*) malloc(sizeof(char) * myString.length() + 1);

Serial.println(myString);

myString.toCharArray(buf, myString.length() + 1);

mySerial.write(buf);

free(buf);

}



RECEIVER CODE :


#include <SoftwareSerial.h>

#include <Wire.h>

#include <Adafruit_GFX.h> // include Adafruit Graphics library

#include <Adafruit_SSD1306.h> // include OLED Display library

#define RXD2 D7

#define TXD2 D8

SoftwareSerial mySerial(RXD2, TXD2);

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

String temperature, humidity;

#define TARGET_ID 1

String Message = "Perfect Connection";

String content = "";

int counter = 0;

void setup()

{

Serial.begin(115200);

mySerial.begin(115200);

pinMode(0, INPUT_PULLUP);

pinMode(2, OUTPUT);

digitalWrite(2, LOW);

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {

Serial.println(F("SSD1306 allocation failed"));

for(;;);

}

delay(2000); // Pause for 2 seconds

}

//====================== Function to Check Incomming Serial Data ====================================//

void checkSerial(){

while (mySerial.available() > 0) {

// get the new byte:

String inString = mySerial.readString();

Serial.print("Received Data : ");

Serial.println(inString);

// Data receiving format : +RCV=<Address>,<Length>,<Data>,<RSSI>,<SNR>

// Example: +RCV=50,5,HELLO,-99,40

// | | | | |

// 0 1 2 3 4

// So to get DHT22 data : String DataIn = getValue(inString, ',', 2);

String DataIn = getValue(inString, ',', 2); //--> data

temperature = getValue(DataIn, '%', 0); //--> Get temperature data

humidity = getValue(DataIn, '%', 1); //--> Get humidity data

displayData();

Serial.print("Temperature = ");

Serial.print(temperature);

Serial.println(F(" °C"));

Serial.print("Humidity = ");

Serial.print(humidity);

Serial.println(F(" % Rh"));

}

}

void loop() {

checkSerial();

if (Serial.available()){

Serial.println("Writing");

content = Serial.readString();

content.trim();

Serial.println();

content = content + "\r\n";

char* bufc = (char*) malloc(sizeof(char) * content.length() + 1);

content.toCharArray(bufc, content.length() + 1);

mySerial.write(bufc);

free(bufc);

}

if (mySerial.available()) {

String incomming = mySerial.readString();

if (incomming.length() <= 10)

Serial.println(incomming);

else {

String channel_ID = incomming.substring(incomming.indexOf('=') + 1, incomming.indexOf(','));

Serial.println("Channel ID : " + channel_ID);

String str = incomming.substring(incomming.indexOf(',') + 1);

String msgLength = str.substring(0, str.indexOf(','));

Serial.println("Message Length : " + msgLength);

String str2 = str.substring(str.indexOf(',') + 1);

String message = str2.substring(0, str2.indexOf(','));

Serial.println("Message : " + message);

//send confirmation message

content = "Message received: " + message;

sendLoraData(content, TARGET_ID);

}

}

// When the button is pressed send the message to other module

if (digitalRead(0) == LOW) {

delay(1000);

String data = Message + " - Count: " + counter;

sendLoraData(data, TARGET_ID);

//increase counter on each send

counter++;

}

}

void sendLoraData(String data, int address) {

String myString = "AT+SEND=" + String(address) + "," + String(data.length()) + "," + data + "\r\n";

char* buf = (char*) malloc(sizeof(char) * myString.length() + 1);

Serial.println(myString);

myString.toCharArray(buf, myString.length() + 1);

mySerial.write(buf);

free(buf);

}

//====================== Function to Disply the Data on OLED ======================================//

void displayData(){

display.clearDisplay();

display.drawRect(0, 0, 126, 62, WHITE);

display.setCursor(6,5);

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(5,5);

display.print("Temp = ");

display.setTextSize(1);

display.setCursor(55,5);

display.print(temperature);

display.setCursor(104,5);

display.setTextSize(1);

display.cp437(true);

display.write(167);

display.setTextSize(1);

display.setCursor(112,5);

display.print("C");

display.setTextSize(1);

display.setTextColor(WHITE);

display.setCursor(5,21);

display.print("Humi = ");

display.setCursor(55,21);

display.setTextSize(1);

display.print(humidity);

display.setCursor(112,21);

display.print("%");

display.display();

delay(250);

}

//====================== Function for parsing the incomming data String ======================================//

String getValue(String data, char separator, int index)

{

int found = 0;

int strIndex[] = { 0, -1 };

int maxIndex = data.length() - 1;

for (int i = 0; i <= maxIndex && found <= index; i++) {

if (data.charAt(i) == separator || i == maxIndex) {

found++;

strIndex[0] = strIndex[1] + 1;

strIndex[1] = (i == maxIndex) ? i+1 : i;

}

}

return found > index ? data.substring(strIndex[0], strIndex[1]) : "";

}

269 views0 comments

留言


bottom of page