How do I convert LoRa.read to an integer?

How would I get an integer data type out of the LoRa signal? I'm currently able to get a string data type but if I try converting the string data type to an integer value I only get "0" on Blynk.

#define BLYNK_TEMPLATE_ID ""
#define BLYNK_TEMPLATE_NAME ""
#define BLYNK_AUTH_TOKEN ""

#define BLYNK_PRINT Serial

#include <SPI.h>
#include <LoRa.h>
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>


char ssid[] = "";
char pass[] = "";

const int csPin = 18;
const int resetPin = 14;
const int irqPin = 3;
 
String LoRaData;

BlynkTimer timer;

void myTimerEvent()
{
Blynk.virtualWrite(V1, LoRaData);
}

void setup() {
  Serial.begin(9600);
  while (!Serial);
  LoRa.setSpreadingFactor(10);
  LoRa.setPins(csPin, resetPin, irqPin);
  
  if (!LoRa.begin(915E6)) {
    Serial.println("Starting LoRa failed!");
    while (1)
      ;
  }

Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
  timer.setInterval(10000L, myTimerEvent);

}
 
void loop() {
 
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    Serial.print("Received '");
 
    while (LoRa.available()) {
      Serial.print((char)LoRa.read());
      LoRaData = LoRa.readString();
    }
 
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }



  Blynk.run();
  timer.run();

}```

The read consumes one byte. That's a byte that won't be in the String LoRaData. Is there really a byte there that you just want to print and throw away?

There are a number of great tutorials around here on reading and parsing from Serial. What you need to do is exactly the same even though your bytes are coming from a different place. Start with the famous @Robin2 Serial Input Basics thread.

have a look at example code in post need-advice-on-a-home-project which transferrs data between LoRa modules using a structure
no requirement to read bytes and then use binary operators to to create multibyte data types
read a complete structure and then directly access member data

Thanks for the reply. I will basically need to figure out how to store the Lora.read() bytes into a buffer/array and then convert that to an integer, correct?

The LoRa library I use does allow you to use structures to send and receive data.

However its far easier (for me) to use other library functions that allow variables to be written and read directly to and from the LoRa packet buffer in this fashion;

LoRa.startReadSXBuffer(0);               //start buffer read at location 0
LoRa.readBufferChar(receivebuffer);      //read in the character buffer
latitude = LoRa.readFloat();             //read in the latitude
longitude = LoRa.readFloat();            //read in the longitude
altitude = LoRa.readUint16();            //read in the altitude
satellites = LoRa.readUint8();           //read in the number of satellites
voltage = LoRa.readUint16();             //read in the voltage
temperature = LoRa.readInt8();           //read in the temperature
RXPacketL = LoRa.endReadSXBuffer();      //finish packet read, get received packet length

Of course, as in with the structure, you need to write and read variables with matching types in the same order.

What kind of library are you using? I'm using "LoRa by Sandeep Mistry". I don't have the option to directly read the LoRa packet buffer as an integer.

LoRa is a low power low bit rate communications system therefore avoid transmitting data as text (or even JSON) but transmit using low level binary

e.g. to transmit a int16_t as two bytes Least significant then Most significant)

// transmit int16_t over LoRa

#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(115200);
  while (!Serial)
    ;

  Serial.println("LoRa Sender");
  LoRa.setPins(8, 4, 7);  // for Lora 32u4
  if (!LoRa.begin(866E6)) {
    Serial.println("Starting LoRa failed!");
    while (1)
      ;
  }
}

void loop() {
  Serial.print("Sending packet: ");
  // send packet
  static int16_t counter = -1000;
  Serial.println(counter);
  LoRa.beginPacket();
  LoRa.write(counter & 0xff);         // transmit LSB
  LoRa.write((counter >> 8) & 0xff);  // transmit MSB (shifted right 8 bits)
  LoRa.endPacket();
  counter += 100;
  delay(2000);
}

receiver

// receive int16_t over LoRa as two bytes - using bit operators to form 16bit word

#include <SPI.h>
#include <LoRa.h>

void setup() {
  Serial.begin(115200);
  while (!Serial);
  Serial.println("LoRa Receiver");
  LoRa.setPins(8,4,7);   // for Lora 32u4
  if (!LoRa.begin(866E6)) {
    Serial.println("Starting LoRa failed!");
    while (1);
  }
}

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    // read packet
    while (LoRa.available()) {
      int16_t x=LoRa.read();   // read LSB
      x = LoRa.read()<<8 | x;  // read MSB sift left 8 bits OR with LSB to form 16bit word
      Serial.print(x);
    }
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

a run gives - transmitter

Sending packet: -500
Sending packet: -400
Sending packet: -300
Sending packet: -200
Sending packet: -100
Sending packet: 0
Sending packet: 100
Sending packet: 200
Sending packet: 300
Sending packet: 400
Sending packet: 500

receiver

Received packet '-400' with RSSI -93
Received packet '-300' with RSSI -94
Received packet '-200' with RSSI -94
Received packet '-100' with RSSI -94
Received packet '0' with RSSI -94
Received packet '100' with RSSI -92
Received packet '200' with RSSI -93
Received packet '300' with RSSI -93

in lora.h there are two versions of write

 virtual size_t write(uint8_t byte);
  virtual size_t write(const uint8_t *buffer, size_t size);

the first transmits a byte (used in the above program)
the second transmits an array of bytes - size specifies the number of bytes

to use the second the transmitter would be

void loop() {
  Serial.print("Sending packet: ");
  // send packet
  static int16_t counter = -1000;
  Serial.println(counter);
  LoRa.beginPacket();
  LoRa.write((byte *)&counter, sizeof(int16_t));    // transmit 16bit int as two bytes
  LoRa.endPacket();
  counter += 100;
  delay(2000);
}

and the receiver

void loop() {
  // try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    // received a packet
    Serial.print("Received packet '");
    // read packet
    while (LoRa.available()) {
      int16_t x;
      LoRa.readBytes((byte *)&x, sizeof(int16_t));  // read two bytes to form 16 bit int
      Serial.print(x);
    }
    // print RSSI of packet
    Serial.print("' with RSSI ");
    Serial.println(LoRa.packetRssi());
  }
}

to avoid the problem of different microcontrollers using different sized integer types when transferring integer data it is wise to use Fixed width integer types
there can still be a problem with floating point data, e.g. on a UNO double is 4 bytes on an ESP32 is 8 bytes

Thanks for the response and the code horace. I will study this. I am going to try and use LoRa.write() in my transmitter code instead of Lora.print(). That should allow me to receive the information directly as an integer, correct? Then there will be no need for data type conversion in my receiver.

correct - write() transmits the data, e.g. a 16bit signed int

 LoRa.write((byte *)&counter, sizeof(int16_t)); 

and readBytes() reads it, e.g.

LoRa.readBytes((byte *)&x, sizeof(int16_t));  // read two bytes to form 16 bit int

if you require to transfer several data values post need-advice-on-a-home-project shows how to transmit and receive a structure

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