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