I am creating a LoRa based Water Level Alarm System. Now I have a problem when the data is being sent to my Receiver, the RX Serial looks like this:
9:26:57.353 -> Received Distance: Ǯ cm
19:27:01.669 -> Received Distance: \���xe�]�۸L�
19:27:01.714 -> 5����~ b:1���A����^;3}Wa >
19:27:01.750 -> ��Ӡeȕ�)��A���%u��M��Yv%�dc<x�|��t��`����܆����l�� VPպ�ʠ�2˾��y�ʵX�����.k�K��5p�_�e��1+'n�����Tۮ��l-|� ���e8��9�gQ[y���{��v�Ӏzx��������23llo�dR&�
0��$�0��k�EV�/��Ǯ\���xe�]�۸L�
19:27:01.983 -> 5����~ b:1���A����^;3}Wa >
19:27:02.017 -> ��Ӡeȕ�)��A���%u��M��Yv%�dc<x�|��t��`����܆����l�� VPպ�ʠ�2˾��y�ʵX�����.k�K��5p�_�e��1+'n�����Tۮ��l-|� ���e8��9�gQ[y���{��v�Ӏzx��������23llo�dR&�
0��$�0��k�EV�/��Ǯ\���xe�]�۸L�
19:27:02.250 -> 5����~ b:1���A����^;3}Wa >
19:27:02.283 -> ��Ӡeȕ�)��A���%u��M��Yv%�dc<x�|��t��`����܆����l�� VPպ�ʠ�2˾��y�ʵX�����.k�K��5p�_�e��1+'n�����Tۮ��l-|� ���e8��9�gQ[y���{��v�Ӏzx��������23llo�dR&�
0��$�0��k�EV�/��Ǯ\���xe�]�۸L�
19:27:02.516 -> 5����~ b:1���A����^;3}Wa >
19:27:02.549 -> ��Ӡeȕ�)��A���%u��M��Yv%�dc<x�|��t��`����܆����l�� VPպ�ʠ�2˾��y�ʵX�����.k�K��5p�_�e��1+'n�����Tۮ��l-|� ���e8��9�gQ[y���{��v�Ӏzx��������23llo�dR&�
0��$�0��k�EV�/��Ǯ\���xe�]�۸L�
19:27:02.782 -> 5����~ b:1���A����^;3}Wa >
19:27:02.815 -> ��Ӡeȕ�)��A���%u��M��Yv%�dc<x�|��t��`����܆����l�� VPպ�ʠ�2˾��y�ʵX�����.k�K��5p�_�e��1+'n�����Tۮ��l-|� ���e8��9�gQ[y���{��v�Ӏzx��������23llo�dR&�
0��$�0��k�EV�/��Ǯ\���xe�]�۸L�
19:27:03.048 -> 5����~ b:1���A����^;3}Wa >
19:27:03.081 -> ��Ӡeȕ�)��A���%u��M��Yv%�dc<x�|��t��`����܆����l�� VPպ�ʠ�2˾��y�ʵX�����.k�K��5p�_�e��1+'n�����Tۮ��l-|� ���e8��9�gQ[y���{��v�Ӏzx��������23llo�dR&�
0��$�0��k�EV�/��Ǯ\���xe�]�۸L�
19:27:03.280 -> 5 cm
//Transmitter Code
#include <SPI.h>
#include <LoRa.h>
#include <NewPing.h>
// LoRa Pins
#define LORA_SCK 13
#define LORA_MISO 12
#define LORA_MOSI 11
#define LORA_SS 10
#define LORA_RST 9
#define LORA_DIO0 2
// Ultrasonic Pins
#define TRIGGER_PIN 5
#define ECHO_PIN 6
#define MAX_DISTANCE 40 // Maximum distance to measure (in cm)
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE);
void setup() {
Serial.begin(9600);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(433E6)) {
Serial.println("LoRa initialization failed!");
while (1);
}
Serial.println("LoRa initialized.");
}
void loop() {
// Get distance from ultrasonic sensor
unsigned long distance = sonar.ping_cm();
// If valid distance is measured, send it via LoRa
if (distance > 0) {
LoRa.beginPacket();
LoRa.print(distance); // Convert the integer to string automatically
LoRa.endPacket();
Serial.print("Sent Distance: ");
Serial.println(distance);
} else {
Serial.println("Distance measurement failed.");
}
delay(5000);
}
//Receiver Code
#include <SPI.h>
#include <LoRa.h>
// LoRa Pins
#define LORA_SCK 13
#define LORA_MISO 12
#define LORA_MOSI 11
#define LORA_SS 10
#define LORA_RST 9
#define LORA_DIO0 2
void setup() {
Serial.begin(9600);
LoRa.setPins(LORA_SS, LORA_RST, LORA_DIO0);
if (!LoRa.begin(433E6)) {
Serial.println("LoRa initialization failed!");
while (1);
}
Serial.println("LoRa initialized.");
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
String receivedData = "";
while (LoRa.available()) {
receivedData += (char)LoRa.read(); // Read the incoming data as a string
}
receivedData.trim(); // Remove any whitespace/newlines
if (receivedData.length() > 0) {
// Print the received distance
Serial.print("Received Distance: ");
Serial.print(receivedData);
Serial.println(" cm");
}
}
}
Is there a way to fix this? I have tried sending 'Hello' earlier, just two LoRa communicating, and It worked without delay. But when I added my Ultrasonic, it became like random characters(?)