How to Transmit and Receive 2 Data using LoRa Ra-02 SX1278

Hello everyone!!!!

I want to transmit 2 analog data using LoRa but I am getting this values:

Received packet 'Left: 408
Right: 947
Received packet 'Left: 428
Right: 947
Received packet 'Left: 408
Right: 948
Received packet 'Left: 408
Right: 946
Received packet 'Left: 408
Right: 947

HERE ARE MY CODES

//TRANSMITTER

#include <LoRa.h>
#include <SPI.h>
 
#define ss 5
#define rst 14
#define dio0 2
 

const int joyLpin = 34;     // left joystick
const int joyRpin = 35;     // right joystick
 
void setup() 
{
  Serial.begin(115200); 
  

  
  while (!Serial);
  Serial.println("LoRa Sender");
 
  LoRa.setPins(ss, rst, dio0);    //setup LoRa transceiver module
  
  while (!LoRa.begin(433E6))     //433E6 - Asia, 866E6 - Europe, 915E6 - North America
  {
    Serial.println(".");
    delay(500);
  }
  LoRa.setSyncWord(0xA5);
  Serial.println("LoRa Initializing OK!");
}
 
void loop() 
{
  int joyL = map(analogRead(joyLpin),0,1024,0,255);
  int joyR = map(analogRead(joyRpin),0,1024,0,255);


  Serial.println("Sending packet: ");
 
  LoRa.beginPacket();   
  LoRa.print(joyL);
  LoRa.print(joyR);
  LoRa.endPacket();

  delay(50);
}
//RECEIVER

#include <LoRa.h>
#include <SPI.h>
 
#define ss 5
#define rst 14
#define dio0 2

int joyL;                 
int joyR;

String inStringL = "";
String inStringR = "";

void setup() 
{
  Serial.begin(115200);
  
  while (!Serial);
  Serial.println("LoRa Receiver");
 
  LoRa.setPins(ss, rst, dio0);    //setup LoRa transceiver module
 
  while (!LoRa.begin(433E6))     //433E6 - Asia, 866E6 - Europe, 915E6 - North America
  {
    Serial.println(".");
    delay(500);
  }
  LoRa.setSyncWord(0xA5);
  Serial.println("LoRa Initializing OK!");
}
 
void loop() 
{
  int packetSize = LoRa.parsePacket();    // try to parse packet
  if (packetSize) 
  {
    
    Serial.print("Received packet '");
 
    while (LoRa.available())              // read packet
    {

      int inCharL = LoRa.read();
      inStringL += (char)inCharL;
      joyL = inStringL.toInt();
      
      int inCharR = LoRa.read();
      inStringR += (char)inCharR;
      joyR = inStringR.toInt();
    }
    inStringL = "";
    inStringR = "";
    LoRa.packetRssi();

    Serial.print("Left: ");
    Serial.println(joyL);
    Serial.print(" Right: ");
    Serial.println(joyR);

  }

}

a separator would help... if joyL is 54 and joyR is 321, you are sending "54321"... how are you going to unpack that on the other end? it could have been 54 and 321 but why not 543 and 21 ? ...

send as "<X,Y>" for example then you can parse to ensure there is the start and end marker and the comma is the separator or send the binary values if you want something compressed

for an example of transmitting multiple data values in a structure using LoRa P2P see need-advice-on-a-home-project

Be very careful changing the syncword from the default, the datasheets might imply you can use whatever you want but it just ain't the case.

Some synwords are leaky, and let packets with different syncwords through.
Some syncwords dont work.
Some syncwords can reduce LoRa sensitivity.
Some syncwords may not be compatible with SX126X and LLCC68 LoRa devices

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.