Send LoRa Encrypted data using REYAX RYLR998
If you need to send encrypted data using LoRa protocol, check out this small project.
Components and supplies
2
2x 4.7kohm Resistors
2
10k Resistor
2
Arduino Nano V3.0
2
RYLR998 LORA from Reyax
1
Push Button
1
SSD1306 OLED Display
Apps and platforms
1
Arduino IDE
Project description
Code
LoRa Transmit Code
c
LoRa Transmit Code
1/* 2 * RYLR998 Encrypted Transmit 3 * December 31st 2022 4 * Carlo Stramaglia 5 * https://www.youtube.com/c/CarloStramaglia 6 * To change the Password, change the 8 digits in the below function LoRa.print("AT+CPIN=11223344\r\n"); 7 * You need to have the same password on both Receiver and Transmitter 8 */ 9 10#include <SoftwareSerial.h> 11#include "OneButton.h" 12 13SoftwareSerial LoRa(6, 5); // First (6) connected to TX of RYLR998, Second (5) to RX of RYLR998 14 15#define PIN_INPUT 2 16#define PIN_LED 13 17 18OneButton button(PIN_INPUT, true); 19int ledState = HIGH; 20 21String msg = ""; 22int waiting = 200; 23int i=0, j=0; 24char messageNumber[10] = ""; 25 26 27void setup() { 28 pinMode(PIN_LED, OUTPUT); 29 digitalWrite(PIN_LED, ledState); 30 button.attachDoubleClick(doubleClick); 31 button.attachClick(Click); 32 LoRa.begin(57600); 33 set_AT_commands ("51","18"); 34 delay(2000); 35 LoRa.print("AT+BAND=868100000\r\n"); // Placed this command because sometimes the board was going back to US standard 36 delay(2000); 37} 38 39void loop() { 40 button.tick(); 41 i=i+1; 42 if (i>300) { 43 j=j+1; 44 sprintf (messageNumber, "@%i$",j); 45 send_AT_message("50", messageNumber); 46 i=0; 47 } 48 delay(10); 49 50} 51 52 53void get_serial_data(){ 54 if (LoRa.available()){ 55 msg = LoRa.readString(); 56 } 57} 58 59void set_AT_commands(String address, String network_ID){ 60 LoRa.print("AT\r\n"); // Just a check if the module is well connected 61 delay(waiting); 62 LoRa.print("AT+ADDRESS=" + address + "\r\n"); // Device Address will be stored in Flash 63 delay(waiting); 64 LoRa.print("AT+NETWORKID="+network_ID+"\r\n"); // Network ID will be stored in flash 65 delay(waiting); 66 LoRa.print("AT+PARAMETER=9,7,1,12\r\n"); // Default data. Can be deleted 67 delay(waiting); 68 LoRa.print("AT+IPR=57600\r\n"); // Lower serial speed. My Arduino was not working properly at 115200 69 delay(waiting); 70 LoRa.print("AT+RESET\r\n"); // Reset is required otherwise seems not to work properly at startup 71 delay(waiting); 72 LoRa.print("AT+BAND=868100000\r\n"); // Eurpean Bandwdth 73 delay(waiting); 74 LoRa.print("AT+CPIN=11223344\r\n"); // Encrypted password should be the same as the receiver 75 delay(waiting); 76 77} 78 79 80void send_AT_message(String address, char* textmessage){ 81 int Lenght=0; 82 Lenght = strlen (textmessage); 83 LoRa.print("AT+SEND=" + address + "," + Lenght + "," + textmessage + "\r\n"); 84 delay(waiting); 85} 86 87void doubleClick() 88{ 89 ledState = LOW; 90 digitalWrite(PIN_LED, ledState); 91 LoRa.print("AT+CPIN=12345678\r\n"); // Change Password 92 // LoRa.print("AT+BAND=868100000\r\n"); 93} 94 95void Click() 96{ 97 ledState = HIGH; 98 digitalWrite(PIN_LED, ledState); 99 LoRa.print("AT+CPIN=11223344\r\n"); // Put back the Password 100 // LoRa.print("AT+BAND=868100000\r\n"); 101 102}
LoRa Receive Code
c
LoRa Receive Code
1/* 2 * RYLR998 Encrypted Receiver December 31st 2022 3 * Carlo Stramaglia 4 * https://www.youtube.com/c/CarloStramaglia 5 * To change the Password, change the 8 digits in the below function LoRa.print("AT+CPIN=11223344\r\n"); 6 * You need to have the same password on both Receiver and Transmitter 7 */ 8 9#include <SoftwareSerial.h> 10#include <U8g2lib.h> 11 12U8G2_SSD1306_128X32_UNIVISION_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // Adafruit Feather ESP8266/32u4 Boards + FeatherWing OLED 13SoftwareSerial LoRa(6, 5); // RX, TX 14 15//String incomingstring; 16String msg = "", received_msg = ""; 17int waiting = 200; 18 19 20void setup() { 21 LoRa.begin(57600); 22 u8g2.begin(); 23 set_AT_commands ("50","18"); 24 msg=""; 25 u8g2.clearBuffer(); // clear the internal memory 26 u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font 27 u8g2.drawStr(0,10,"Ready to Receive"); // write something to the internal memory 28 u8g2.sendBuffer(); // transfer internal memory to the display 29} 30 31void loop() { 32 delay (50); 33 get_serial_data(); 34 if(msg != ""){ 35 int delimiter_1, delimiter_2; 36 delimiter_1 = msg.indexOf("@"); 37 delimiter_2 = msg.indexOf("$", delimiter_1 + 1); 38 39 received_msg = msg.substring(delimiter_1 + 1, delimiter_2); 40 int str_len = received_msg.length() + 1; 41 char messageLocal[str_len]; 42 43 received_msg.toCharArray(messageLocal, str_len); 44 45 u8g2.clearBuffer(); // clear the internal memory 46 u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font 47 u8g2.drawStr(0,10,"Receiving..."); // write something to the internal memory 48 u8g2.drawStr(0,25,messageLocal); // write something to the internal memory 49 u8g2.sendBuffer(); // transfer internal memory to the display 50 } 51} 52 53 54void get_serial_data(){ 55 if (LoRa.available()){ 56 msg = LoRa.readString(); 57 } 58} 59 60void set_AT_commands(String address, String network_ID){ 61 LoRa.print("AT\r\n"); // Just a check if the module is well connected 62 delay(waiting); 63 LoRa.print("AT+ADDRESS=" + address + "\r\n"); // Device Address will be stored in Flash 64 delay(waiting); 65 LoRa.print("AT+NETWORKID="+network_ID+"\r\n"); // Network ID will be stored in flash 66 delay(waiting); 67 LoRa.print("AT+PARAMETER=9,7,1,12\r\n"); // Default data. Can be deleted 68 delay(waiting); 69 LoRa.print("AT+IPR=57600\r\n"); // Lower serial speed. My Arduino was not working properly at 115200 70 delay(waiting); 71 LoRa.print("AT+RESET\r\n"); // Reset is required otherwise seems not to work properly at startup 72 delay(waiting); 73 LoRa.print("AT+BAND=868100000\r\n"); // Eurpean Bandwdth 74 delay(waiting); 75 LoRa.print("AT+CPIN=11223344\r\n"); // Encrypted password should be the same as the receiver 76 delay(waiting); 77}
Setup code for LoRa module
c
Setup code for LoRa modules
1/* 2 * DO THIS ONLY ONCE ON BOTH RECEIVER AND TRANSMITTER 3 * RYLR998 Setup code for 57600 baud communication 4 * December 31st 2022 5 * Carlo Stramaglia 6 * https://www.youtube.com/c/CarloStramaglia 7 * 8 */ 9 10#include <SoftwareSerial.h> 11 12SoftwareSerial LoRa(6, 5); // First (6) connected to TX of RYLR998, Second (5) to RX of RYLR998 13 14 15String msg = ""; 16int waiting = 200; 17int i=0, j=0; 18char messageNumber[10] = ""; 19 20 21void setup() { 22 LoRa.begin(115200); 23 set_AT_commands ("100","18"); 24 delay(5000); 25} 26 27void loop() { 28 29} 30 31 32void get_serial_data(){ 33 if (LoRa.available()){ 34 msg = LoRa.readString(); 35 } 36} 37 38void set_AT_commands(String address, String network_ID){ 39 LoRa.print("AT\r\n"); // Just a check if the module is well connected 40 delay(waiting); 41 LoRa.print("AT+ADDRESS=" + address + "\r\n"); // Device Address will be stored in Flash 42 delay(waiting); 43 LoRa.print("AT+NETWORKID="+network_ID+"\r\n"); // Network ID will be stored in flash 44 delay(waiting); 45 LoRa.print("AT+PARAMETER=9,7,1,12\r\n"); // Default data. Can be deleted 46 delay(waiting); 47 LoRa.print("AT+IPR=57600\r\n"); // Lower serial speed. My Arduino was not working properly at 115200 48 delay(waiting); 49 LoRa.print("AT+RESET\r\n"); // Reset is required otherwise seems not to work properly at startup 50 delay(waiting); 51 LoRa.print("AT+BAND=868100000\r\n"); // Eurpean Bandwdth 52 delay(waiting); 53 54}
Comments
Only logged in users can leave comments