-
Notifications
You must be signed in to change notification settings - Fork 2
Description
Send data: pair freq, rssi, max value 3 digits, rssi can even be 2 digits after some transformation like RSSI - 50 to save bandwidth:
#include <SPI.h>
#include <RadioLib.h>
// Initialize LoRa with frequency 915 MHz
SX1276 radio = new Module(10, 2, 9, 3); // (CS, DIO0, RESET, DIO1)
// Binary data array
uint16_t dataToSend[] = {915, 78, 920, 120}; // Your data
size_t dataSize = sizeof(dataToSend) / sizeof(dataToSend[0]);
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Initializing LoRa...");
// Initialize RadioLib with 915 MHz
if (radio.begin(915.0) == RADIOLIB_ERR_NONE) {
Serial.println("LoRa initialized successfully!");
} else {
Serial.println("Failed to initialize LoRa!");
while (true);
}
}
void loop() {
Serial.println("Sending binary data...");
// Convert and send binary data
for (size_t i = 0; i < dataSize; i++) {
uint16_t value = dataToSend[i];
byte binaryData[2];
// Split 16-bit integer into two 8-bit bytes
binaryData[0] = highByte(value);
binaryData[1] = lowByte(value);
// Transmit the binary data
int state = radio.transmit(binaryData, 2); // Send 2 bytes per value
if (state == RADIOLIB_ERR_NONE) {
Serial.print("Data sent: ");
Serial.println(value);
} else {
Serial.print("Failed to send data: ");
Serial.println(state);
}
delay(1000); // Delay 1 second between sends
}
delay(5000); // Send all data every 5 seconds
}
Receiver
#include <SPI.h>
#include <RadioLib.h>
// Initialize LoRa with frequency 915 MHz
SX1276 radio = new Module(10, 2, 9, 3); // (CS, DIO0, RESET, DIO1)
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("Initializing LoRa...");
if (radio.begin(915.0) == RADIOLIB_ERR_NONE) {
Serial.println("LoRa initialized successfully!");
} else {
Serial.println("Failed to initialize LoRa!");
while (true);
}
}
void loop() {
Serial.println("Waiting for binary data...");
byte receivedData[2];
int state = radio.receive(receivedData, 2); // Receive 2 bytes
if (state == RADIOLIB_ERR_NONE) {
// Reconstruct 16-bit integer
uint16_t receivedValue = (receivedData[0] << 8) | receivedData[1];
Serial.print("Received Value: ");
Serial.println(receivedValue);
} else {
Serial.print("Receive failed: ");
Serial.println(state);
}
delay(1000); // Check for new data every 1 second
}