Lora how to send and receive separate multiple values

Lora RFM95w with Arduino, node to node, I need to send multiple variable values from one node to another, I can send a string containing ALL the values but they cannot be used at the receiver, they need to be separate values. Is there a special format that I need to use or is this just not possible as i have not found any but the most simple examples with only one variable being sent.
thanks in advance.

1 Like

Heya Doug,

You may find some nuggets in this tutorial:

Of note, how we use ustruct.pack and ustruct.unpack to create packets/payloads which contain various object types

2 Likes

I have devised a way to do this, but there must be a better answer.
I have applied the values to an array, then one by one send the value as a String, at the receiver i take each value as it arrives and convert it to an Int this can then be stored in an array and used to populate an Int variable. Here I have just used it as it arrived.

//Sender
#include <SPI.h>
#include <LoRa.h>
int myVals[] = {50, 100, 150, 200, 254};
int counter = 0;
int i = 0;
void setup() {
  Serial.begin(9600);
  
  while (!Serial);

  Serial.println("LoRa Sender");

  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);

  // send packet
    for (i = 0; i <= 4; i++) {

  LoRa.beginPacket();
  LoRa.print(myVals[i]);
  LoRa.endPacket();
  counter++;
  
  Serial.print(i);
  delay(100);
}    
  delay(500);
}


//Receiver
#include <LoRa.h>
  String LoRaData;
void setup() {
  pinMode(5,OUTPUT);
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
    Serial.println("LoRa Initializing OK!");
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    // read packet
    while (LoRa.available()) {

      //Serial.println((char)LoRa.read());
      LoRaData = LoRa.readString();
      Serial.print(LoRaData);
      // print RSSI of packet
      Serial.print("' with RSSI ");
      Serial.println(LoRa.packetRssi());
       
    int d = LoRaData.toInt();         //convert String to Int
    Serial.print("Int Value = ");
    Serial.println(d);
    Serial.println("");
    analogWrite(5,d);               
    }  
  }
}

Running on an Arduino Nano and Pro Mini Clone. If anyone knows how to convert a String to a Float Variable please post here.

Here are the mods for an Array…(EDITED…this version uses array value 0 as a flag to reset the count seems to be stable.)

//Sender
#include <SPI.h>
#include <LoRa.h>
int myVals[] = {0, 50, 100, 150, 200, 254};
int counter = 0;
int i = 0;
void setup() {
  Serial.begin(9600);
  
  while (!Serial);

  Serial.println("LoRa Sender");

  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  Serial.print("Sending packet: ");
  Serial.println(counter);

  // send packet
    for (i = 0; i <= 5; i++) {

  LoRa.beginPacket();
  LoRa.print(myVals[i]);
  LoRa.endPacket();
  counter++;
  
  Serial.print(i);
  delay(100);
}    
  delay(5000);
}


//Receiver
#include <LoRa.h>
  String LoRaData;
  int i = 0;
  int value1=0;
  int value3=0;
  int value5=0;
  int myVals[] = {0, 0, 0, 0, 0, 0};
void setup() {
  pinMode(5,OUTPUT);
  Serial.begin(9600);
  while (!Serial);

  Serial.println("LoRa Receiver");

  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
    Serial.println("LoRa Initializing OK!");
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
      
  
  
    // read packet
    while (LoRa.available()) {

      //Serial.println((char)LoRa.read());
      LoRaData = LoRa.readString();
      Serial.print(LoRaData);
      // print RSSI of packet
      Serial.print("' with RSSI ");
      Serial.println(LoRa.packetRssi());
       
    int d = LoRaData.toInt();         //convert String to Int  
    //if (i >= 4) {i = 0;}
    if(d == 0) {i = 0;}        
    myVals[i] = d;

    Serial.print("Int Value = ");
    Serial.println(d);   
    Serial.print("Array Int = ");
    Serial.println(myVals[i]);
    Serial.println("");
    i = (i+1);
    
    analogWrite(5,d);               

    value1 = myVals[1];
    value3 = myVals[3];
    value5 = myVals[5];
    
    }
  }
  //just to prove values of variables
Serial.println (value1);Serial.println (value3);Serial.println (value5);
}