A
Arduino3w ago
Earl

Arduino Sim800l v2 module and SX1278 LoRa Code

I have a flood level detection system project that uses arduino uno as it microcontroller board. I have two microcontrollers; one for the receiver and one for the transmitter. Can someone fix the errors and the missing lines in my code? Here's my flow chart, circuit diagram and code.
15 Replies
pseud0
pseud03w ago
Check your braces placement closely.
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
inString = "";

while (LoRa.available()) {
int inChar = LoRa.read();
inString += (char)inChar;
data = inString.toInt();
}

data = inString.toInt();

int firstComma = inString.indexOf(',');

if (firstComma == -1) {

if (data == 11) {
digitalWrite(gled, LOW);
digitalWrite(yled, HIGH);
digitalWrite(rled, LOW);
if (beepCount <= maxBeepCountY) {
tone(piezo, 1000);
delay(300);
noTone(piezo);
delay(200);
beepCount++;
}
Serial.println("Status Code: Code Yellow! WARNING! Water detected!");
}

else {
waterSensorCM = data;
Serial.print("Water Depth: ");
Serial.print(waterSensorCM);
Serial.println("cm");
}
}

else {
data = inString.substring(0, firstComma).toInt();
waterLevel = inString.substring(firstComma + 1).toInt();

Serial.println("Status Code: Code Red! WARNING! Water has accumulated to 2 inches!");
Serial.print("Water Level: ");
Serial.println(waterLevel);

String msg = "⚠ Potential flood detected! Water has reached " + String(waterLevel) + " cm and is rapidly increasing.";
sendAllNumbers(msg);

Serial.println("SMS Sent!");
}

Serial.print("RSSI: ");
Serial.println(LoRa.packetRssi());
}
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
inString = "";

while (LoRa.available()) {
int inChar = LoRa.read();
inString += (char)inChar;
data = inString.toInt();
}

data = inString.toInt();

int firstComma = inString.indexOf(',');

if (firstComma == -1) {

if (data == 11) {
digitalWrite(gled, LOW);
digitalWrite(yled, HIGH);
digitalWrite(rled, LOW);
if (beepCount <= maxBeepCountY) {
tone(piezo, 1000);
delay(300);
noTone(piezo);
delay(200);
beepCount++;
}
Serial.println("Status Code: Code Yellow! WARNING! Water detected!");
}

else {
waterSensorCM = data;
Serial.print("Water Depth: ");
Serial.print(waterSensorCM);
Serial.println("cm");
}
}

else {
data = inString.substring(0, firstComma).toInt();
waterLevel = inString.substring(firstComma + 1).toInt();

Serial.println("Status Code: Code Red! WARNING! Water has accumulated to 2 inches!");
Serial.print("Water Level: ");
Serial.println(waterLevel);

String msg = "⚠ Potential flood detected! Water has reached " + String(waterLevel) + " cm and is rapidly increasing.";
sendAllNumbers(msg);

Serial.println("SMS Sent!");
}

Serial.print("RSSI: ");
Serial.println(LoRa.packetRssi());
}
}
The last one's double, for example. In some places it seems to be missing one like
void loop() {
unsigned long currentMillis = millis();
// 1) Read the water sensor (analog) — power it just for the reading
if (sensorValue <= 400) {
digitalWrite(waterSensorPower, HIGH);
delay(3000); // brief stabilization
sensorValue = analogRead(waterLevelSensorPin);
Serial.println(sensorValue);
}

waterLevelCM = (float)(sensorValue - sensorMin) * probeLength / (sensorMax - sensorMin);
// Map analog value to “cm” scale

if (waterLevelCM >= 3) {
// Trigger ultrasonic
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH, 30); // timeout of 30 ms
distance = duration * 0.034 / 2.0;
WaterLevel = sensorToBottom - distance;
Serial.println(distance);

// 3) Send via LoRa at intervals
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
LoRa.beginPacket();
if (sensorValue >= 3) {
LoRa.print("11,");
LoRa.print(WaterLevel);
}
if (WaterLevel >= 15){
LoRa.print("22");
}
if (WaterLevelCM >= 0.50) {
LoRa.print(waterLevelCM);
}
LoRa.endPacket();
// (Optional) also print to Serial for debugging
}
}
void loop() {
unsigned long currentMillis = millis();
// 1) Read the water sensor (analog) — power it just for the reading
if (sensorValue <= 400) {
digitalWrite(waterSensorPower, HIGH);
delay(3000); // brief stabilization
sensorValue = analogRead(waterLevelSensorPin);
Serial.println(sensorValue);
}

waterLevelCM = (float)(sensorValue - sensorMin) * probeLength / (sensorMax - sensorMin);
// Map analog value to “cm” scale

if (waterLevelCM >= 3) {
// Trigger ultrasonic
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH, 30); // timeout of 30 ms
distance = duration * 0.034 / 2.0;
WaterLevel = sensorToBottom - distance;
Serial.println(distance);

// 3) Send via LoRa at intervals
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
LoRa.beginPacket();
if (sensorValue >= 3) {
LoRa.print("11,");
LoRa.print(WaterLevel);
}
if (WaterLevel >= 15){
LoRa.print("22");
}
if (WaterLevelCM >= 0.50) {
LoRa.print(waterLevelCM);
}
LoRa.endPacket();
// (Optional) also print to Serial for debugging
}
}
The code indentation makes it seem like the if (waterLevelCM >= 3) { should be closed before the if (currentMillis - previousMillis >= interval) { statement The whole code seems a bit AI generated to me and then poorly copy pasted together
Earl
EarlOP3w ago
to be honest, some are AI generated I fixed it But, how do I make it so that the water level sensor doesn't make false alarms? Like for example, when it rains, the water level sensor may think of it as flood
pseud0
pseud03w ago
Well, did you analyze the signal coming out of it? Is it only a temporary glitch so that one sensor value is off, causing it to send the alarm? You have to analyze how your signal behaves and what behavior you want to have, add filters, etc. like a low pass filter only letting through the slower changes and ignoring any high speed temporary changes. or sampling your signal multiple times, taking the (moving) averages etc.
Earl
EarlOP3w ago
The rssi shown is -26, -27, 42, -158
pseud0
pseud03w ago
RSSI? That's signal strength of the packet. positive 42 should be impossible
Earl
EarlOP3w ago
wait, which signal do you mean? Ahh wait
pseud0
pseud03w ago
The LoRa signal. the average RSSI of the received packet that is. it sends at +14 dBm by standard, and the signal strength will drop over distance and with obstacles added the sensitivity of LoRa is extremely good, with even packets having RSSI close to -120 dBm still being decodable but +42 is more than your TX power and -158 is way too low -26 is like "the transmitter and receiver are sitting right next to each other". beware of near-field effects.
Earl
EarlOP3w ago
Its common value is -23, but sometimes -158
pseud0
pseud03w ago
No description
pseud0
pseud03w ago
While you are using SF12, even -158 is undecodable. probably a bad print or weird near field effects. lower the transmit power if you're having receiver and transmitter right besides each other, and give them some more distance
Earl
EarlOP3w ago
how low?
pseud0
pseud03w ago
I mean +0 dBm if you can. https://github.com/sandeepmistry/arduino-LoRa/blob/5e0e6f0794eae6829315233db5d1257f5cae373b/src/LoRa.h#L69 It has LoRa.setTxPower(int level) So you can just say LoRa.setTxPower(0); for 0 dBm TX power (1 milliwatt)
Earl
EarlOP3w ago
But if I intend to place both in longer distances, do I just remove it once it's done?
pseud0
pseud03w ago
yes.
Earl
EarlOP3w ago
I observed that even when the water level hasn't reached the sensor's threshold, the red signal still sends I'm gonna sleep now, I still got school tom so I'll be back after Hello. I tried plugging the arduino uno in and now I don't receive any signals anymore the RSSI messages aren't being shown in the serial monitor anymore But both LoRas are on and working

Did you find this page helpful?