cstram
Published © GPL3+

Send LoRa Encrypted data using REYAX RYLR998

If you need to send encrypted data using LoRa protocol, check out this easy tutorial.

BeginnerFull instructions provided3 hours2,531
Send LoRa Encrypted data using REYAX RYLR998

Things used in this project

Hardware components

Resistor 10k ohm
Resistor 10k ohm
×2
Resistor 4.75k ohm
Resistor 4.75k ohm
×3
Arduino Nano R3
Arduino Nano R3
×2
push button
×1
LYLR998 LoRa module from Reyax
×1
i2c oled display 0.91" 128x32
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

Receiver Schematics

Receiver Schematics for Arduino

Transmitter

Code

LoRa Transmit Code

Arduino
You need to load in in the Arduino transmitter
/*
 * RYLR998 Encrypted Transmit 
 * December 31st 2022 
 * Carlo Stramaglia
 * https://www.youtube.com/c/CarloStramaglia
 * To change the Password, change the 8 digits in the below function LoRa.print("AT+CPIN=11223344\r\n");
 * You need to have the same password on both Receiver and Transmitter
 */

#include <SoftwareSerial.h>
#include "OneButton.h"

SoftwareSerial LoRa(6, 5); // First (6) connected to TX of RYLR998, Second (5) to RX of RYLR998

#define PIN_INPUT 2
#define PIN_LED 13

OneButton button(PIN_INPUT, true);
int ledState = HIGH;

String msg = "";
int waiting = 200;
int i=0, j=0;
char messageNumber[10] = "";


void setup() { 
  pinMode(PIN_LED, OUTPUT);
  digitalWrite(PIN_LED, ledState);
  button.attachDoubleClick(doubleClick);
  button.attachClick(Click);
  LoRa.begin(57600);
  set_AT_commands ("51","18");
  delay(2000);
  LoRa.print("AT+BAND=868100000\r\n"); // Placed this command because sometimes the board was going back to US standard
  delay(2000);
}

void loop() {  
  button.tick();
  i=i+1;
  if (i>300) {
    j=j+1;
    sprintf (messageNumber, "@%i$",j);
    send_AT_message("50", messageNumber);
    i=0;
  }
  delay(10);

}


void get_serial_data(){
  if (LoRa.available()){
    msg = LoRa.readString();
  }
}

void set_AT_commands(String address, String network_ID){
    LoRa.print("AT\r\n");                           // Just a check if the module is well connected
    delay(waiting);   
    LoRa.print("AT+ADDRESS=" + address + "\r\n");   // Device Address will be stored in Flash
    delay(waiting);
    LoRa.print("AT+NETWORKID="+network_ID+"\r\n");  // Network ID will be stored in flash
    delay(waiting);   
    LoRa.print("AT+PARAMETER=9,7,1,12\r\n");        // Default data. Can be deleted
    delay(waiting);
    LoRa.print("AT+IPR=57600\r\n");                 // Lower serial speed. My Arduino was not working properly at 115200
    delay(waiting);
    LoRa.print("AT+RESET\r\n");                     // Reset is required otherwise seems not to work properly at startup
    delay(waiting);
    LoRa.print("AT+BAND=868100000\r\n");            // Eurpean Bandwdth
    delay(waiting);
    LoRa.print("AT+CPIN=11223344\r\n");             // Encrypted password should be the same as the receiver
    delay(waiting);
    
}


void send_AT_message(String address, char* textmessage){
    int Lenght=0;
    Lenght = strlen (textmessage);
    LoRa.print("AT+SEND=" + address + "," + Lenght + "," + textmessage + "\r\n");
    delay(waiting);
}

void doubleClick()
{
  ledState = LOW;
  digitalWrite(PIN_LED, ledState);  
  LoRa.print("AT+CPIN=12345678\r\n");    // Change Password
  //  LoRa.print("AT+BAND=868100000\r\n");
} 

void Click()
{
  ledState = HIGH;
  digitalWrite(PIN_LED, ledState);
  LoRa.print("AT+CPIN=11223344\r\n");   // Put back the Password
  //  LoRa.print("AT+BAND=868100000\r\n");
   
}

LoRa Receive Code

Arduino
You need to load this code on the Arduino receiver
/*
 * RYLR998 Encrypted Receiver December 31st 2022 
 * Carlo Stramaglia
 * https://www.youtube.com/c/CarloStramaglia
 * To change the Password, change the 8 digits in the below function LoRa.print("AT+CPIN=11223344\r\n");
 * You need to have the same password on both Receiver and Transmitter
 */

#include <SoftwareSerial.h>
#include <U8g2lib.h>

U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE);   // Adafruit Feather ESP8266/32u4 Boards + FeatherWing OLED
SoftwareSerial LoRa(6, 5); // RX, TX

//String incomingstring;
String msg = "", received_msg = "";
int waiting = 200;


void setup() {
  LoRa.begin(57600);
  u8g2.begin();
  set_AT_commands ("50","18");
  msg="";
  u8g2.clearBuffer();          // clear the internal memory
  u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
  u8g2.drawStr(0,10,"Ready to Receive");  // write something to the internal memory
  u8g2.sendBuffer();          // transfer internal memory to the display  
}

void loop() {
  delay (50);
  get_serial_data();
  if(msg != ""){ 
      int delimiter_1, delimiter_2;
      delimiter_1 = msg.indexOf("@");
      delimiter_2 = msg.indexOf("$", delimiter_1 + 1);

      received_msg = msg.substring(delimiter_1 + 1, delimiter_2);
      int str_len = received_msg.length() + 1;
      char messageLocal[str_len];

      received_msg.toCharArray(messageLocal, str_len);

      u8g2.clearBuffer();          // clear the internal memory
      u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
      u8g2.drawStr(0,10,"Receiving...");  // write something to the internal memory      
      u8g2.drawStr(0,25,messageLocal);  // write something to the internal memory
      u8g2.sendBuffer();          // transfer internal memory to the display    
    }
}


void get_serial_data(){
  if (LoRa.available()){
    msg = LoRa.readString();
  }
}

void set_AT_commands(String address, String network_ID){
    LoRa.print("AT\r\n");                           // Just a check if the module is well connected
    delay(waiting);
    LoRa.print("AT+ADDRESS=" + address + "\r\n");   // Device Address will be stored in Flash
    delay(waiting);
    LoRa.print("AT+NETWORKID="+network_ID+"\r\n");  // Network ID will be stored in flash
    delay(waiting);        
    LoRa.print("AT+PARAMETER=9,7,1,12\r\n");        // Default data. Can be deleted
    delay(waiting);   
    LoRa.print("AT+IPR=57600\r\n");                 // Lower serial speed. My Arduino was not working properly at 115200
    delay(waiting);
    LoRa.print("AT+RESET\r\n");                     // Reset is required otherwise seems not to work properly at startup
    delay(waiting);
    LoRa.print("AT+BAND=868100000\r\n");            // Eurpean Bandwdth 
    delay(waiting);
    LoRa.print("AT+CPIN=11223344\r\n");             // Encrypted password should be the same as the receiver
    delay(waiting);
}

Setup code for LoRa module

Arduino
This code has to be loaded once on both transmitters and receivers. This code modifies the baud rate of the serial connection from 115.200 to 57.600. Check the video.
/*
 * DO THIS ONLY ONCE ON BOTH RECEIVER AND TRANSMITTER
 * RYLR998 Setup code for 57600 baud communication 
 * December 31st 2022 
 * Carlo Stramaglia
 * https://www.youtube.com/c/CarloStramaglia
 *
 */

#include <SoftwareSerial.h>

SoftwareSerial LoRa(6, 5); // First (6) connected to TX of RYLR998, Second (5) to RX of RYLR998


String msg = "";
int waiting = 200;
int i=0, j=0;
char messageNumber[10] = "";


void setup() { 
  LoRa.begin(115200);
  set_AT_commands ("100","18");
  delay(5000);
}

void loop() {  
  
}


void get_serial_data(){
  if (LoRa.available()){
    msg = LoRa.readString();
  }
}

void set_AT_commands(String address, String network_ID){
    LoRa.print("AT\r\n");                           // Just a check if the module is well connected
    delay(waiting);   
    LoRa.print("AT+ADDRESS=" + address + "\r\n");   // Device Address will be stored in Flash
    delay(waiting);
    LoRa.print("AT+NETWORKID="+network_ID+"\r\n");  // Network ID will be stored in flash
    delay(waiting);   
    LoRa.print("AT+PARAMETER=9,7,1,12\r\n");        // Default data. Can be deleted
    delay(waiting);
    LoRa.print("AT+IPR=57600\r\n");                 // Lower serial speed. My Arduino was not working properly at 115200
    delay(waiting);
    LoRa.print("AT+RESET\r\n");                     // Reset is required otherwise seems not to work properly at startup
    delay(waiting);
    LoRa.print("AT+BAND=868100000\r\n");            // Eurpean Bandwdth
    delay(waiting);
    
}

Credits

cstram

cstram

16 projects • 21 followers
Passionate about IT, Electronics and DIY. Strong believer in Raspberry and Arduino devices. Experience in digital television and security.

Comments