hello, first o all i would like to say very good course, it has helped me a lot to better understand LoRa.
And second, In my current project, I am sending data from a lora device as follows:
LoRa Sender Code:
This is the first String i send to the receiver:
String message1 = String(temperatura1) + "/" + String(humedad1) + "&" + String(suelohum1); delay(1000); LoRa.beginPacket(); LoRa.print(" "); LoRa.print(message1); LoRa.endPacket(); delay(2000);
This is the second String i send to the receiver:
String message2 = String(temperatura2) + "?" + String(humedad2) + "$" + String(suelohum2); delay(1000); LoRa.beginPacket(); LoRa.print(" "); LoRa.print(message2); LoRa.endPacket(); delay(2000)
This is the third String i send to the receiver: String message3 = String(temperatura3) + "¡" + String(humedad3) + "!" + String(suelohum3); delay(1000); LoRa.beginPacket(); LoRa.print(" "); LoRa.print(message3); LoRa.endPacket(); delay(2000);
all from the same LoRa Device, then for the receiver i modify the getLoRaData() function so that the device can know what data to save in the corresponding variables as advised in another question that someone asked in this forum (see https://rntlab.com/question/receiving-incoming-data-from-multiple-lora-sender/) :
void getLoRaData() { Serial.print("Lora packet received: "); // Read packet while (LoRa.available()) { String LoRaData = LoRa.readString(); // LoRaData format: readingID/temperature&soilMoisture#batterylevel // String example: 1/27.43&654#95.34 Serial.print(LoRaData); if(LoRaData.indexOf('/')){ // Get readingID, temperature and soil moisture int pos1 = LoRaData.indexOf('/'); int pos2 = LoRaData.indexOf('&'); temperatura1 = LoRaData.substring(0, pos1); humedad1 = LoRaData.substring(pos1 +1, pos2); suelohum1 = LoRaData.substring(pos2+1, LoRaData.length()); Serial.println("(IFsensor 1) temperatura1:"+temperatura1+" humedad1:"+humedad1+" suelohum1:"+suelohum1+" "); } if(LoRaData.indexOf('¡')){ // Get readingID, temperature and soil moisture int pos1 = LoRaData.indexOf('¡'); int pos2 = LoRaData.indexOf('!'); temperatura3 = LoRaData.substring(0, pos1); humedad3 = LoRaData.substring(pos1 +1, pos2); suelohum3 = LoRaData.substring(pos2+1, LoRaData.length()); Serial.println("(IFsensor 3) temperatura3:"+temperatura3+" humedad3:"+humedad3+" suelohum3:"+suelohum3+" "); } if(LoRaData.indexOf('?')){ // Get readingID, temperature and soil moisture int pos1 = LoRaData.indexOf('?'); int pos2 = LoRaData.indexOf('$'); temperatura2 = LoRaData.substring(0, pos1); humedad2 = LoRaData.substring(pos1 +1, pos2); suelohum2 = LoRaData.substring(pos2+1, LoRaData.length()); Serial.println("(IFsensor 2 if) temperatura2:"+temperatura2+" humedad2:"+humedad2+" suelohum2:"+suelohum2+" "); } Serial.println("(sensor 1) temperatura1:"+temperatura1+" humedad1:"+humedad1+" suelohum1:"+suelohum1+" "); Serial.println("(sensor 2) temperatura2:"+temperatura2+" humedad2:"+humedad2+" suelohum2:"+suelohum2+" "); Serial.println("(sensor 3) temperatura3:"+temperatura3+" humedad3:"+humedad3+" suelohum3:"+suelohum3+" "); // Get RSSI rssi = LoRa.packetRssi(); Serial.print(" with RSSI "); Serial.println(rssi); } }
As you can see, the variables that meet the requested condition are saved at the end of each if statement, the problem is that when the function reaches its end the other variables are always overwritten, is there any way that they will not be overwritten? so i can store all the variables to show them on a webpage.
Hi Adin.
Your variables are overwritten when you receive a new LoRa packet, right?
Would you like to save all current and previous readings or just the current readings?
If you want to save all readings (current and previous) to show on a web page, the best option is to have a database: https://randomnerdtutorials.com/esp32-esp8266-mysql-database-php/
If you want to display, for example, the last 5 readings and still have access to previous readings, you can save them into a microSD card to use later.
Let me know if this helps.
Regards,
Sara
Hello Sara, and thank you for your answer.
I realized that my problem was in the if statements, here is the corrected version:
while (LoRa.available()) {
String LoRaData = LoRa.readString();
Serial.println(LoRaData);
int pos1 = LoRaData.indexOf('#');
senderID = LoRaData.substring(1, pos1);
Serial.println(senderID);
if (senderID == "1"){
Serial.println("SenderID es igual a 1");
int pos1 = LoRaData.indexOf('#');
int pos2 = LoRaData.indexOf('/');
int pos3 = LoRaData.indexOf('&');
ID=LoRaData.substring(0, pos1);
temperatura1 = LoRaData.substring(pos1 +1, pos2);
humedad1 = LoRaData.substring(pos2+1, pos3);
suelohum1 = LoRaData.substring(pos3+1, LoRaData.length());
temperatura2= "*";
suelohum2= "*";
humedad2= "*";
temperatura3= "*";
suelohum3= "*";
humedad3= "*";
Serial.println("(IFsensor 1) temperatura1:"+temperatura1+" humedad1:"+humedad1+" suelohum1:"+suelohum1+" ");
Serial.println("(sensor 2) temperatura2:"+temperatura2+" humedad2:"+humedad2+" suelohum2:"+suelohum2+" ");
Serial.println("(sensor 3) temperatura3:"+temperatura3+" humedad3:"+humedad3+" suelohum3:"+suelohum3+" ");
}
if (senderID == "2"){
Serial.println("senderID es igual a 2");
int pos1 = LoRaData.indexOf('#');
int pos2 = LoRaData.indexOf('/');
int pos3 = LoRaData.indexOf('&');
ID=LoRaData.substring(0, pos1);
temperatura2 = LoRaData.substring(pos1 +1, pos2);
humedad2 = LoRaData.substring(pos2+1, pos3);
suelohum2 = LoRaData.substring(pos3+1, LoRaData.length());
temperatura1= "*";
suelohum1= "*";
humedad1= "*";
temperatura3= "*";
suelohum3= "*";
humedad3= "*";
Serial.println("(sensor 1) temperatura1:"+temperatura1+" humedad1:"+humedad1+" suelohum1:"+suelohum1+" ");
Serial.println("(IFsensor 2) temperatura2:"+temperatura2+" humedad2:"+humedad2+" suelohum2:"+suelohum2+" ");
Serial.println("(sensor 3) temperatura3:"+temperatura3+" humedad3:"+humedad3+" suelohum3:"+suelohum3+" ");
}
if (senderID == "3"){
Serial.println("senderID es igual a 3");
int pos1 = LoRaData.indexOf('#');
int pos2 = LoRaData.indexOf('/');
int pos3 = LoRaData.indexOf('&');
ID=LoRaData.substring(0, pos1);
temperatura3 = LoRaData.substring(pos1 +1, pos2);
humedad3 = LoRaData.substring(pos2+1, pos3);
suelohum3 = LoRaData.substring(pos3+1, LoRaData.length());
temperatura1= "*";
suelohum1= "*";
humedad1= "*";
temperatura2= "*";
suelohum2= "*";
humedad2= "*";
Serial.println("(sensor 1) temperatura1:"+temperatura1+" humedad1:"+humedad1+" suelohum1:"+suelohum1+" ");
Serial.println("(sensor 2) temperatura2:"+temperatura2+" humedad2:"+humedad2+" suelohum2:"+suelohum2+" ");
Serial.println("(IFsensor 3) temperatura3:"+temperatura3+" humedad3:"+humedad3+" suelohum3:"+suelohum3+" ");
}
Serial.println("(sensor 1) temperatura1:"+temperatura1+" humedad1:"+humedad1+" suelohum1:"+suelohum1+" ");
Serial.println("(sensor 2) temperatura2:"+temperatura2+" humedad2:"+humedad2+" suelohum2:"+suelohum2+" ");
Serial.println("(sensor 3) temperatura3:"+temperatura3+" humedad3:"+humedad3+" suelohum3:"+suelohum3+" ");
// Obtener el RSSI
rssi = LoRa.packetRssi();
Serial.print(" con RSSI ");
Serial.println(rssi);
}
}
My problem was exactly in this line
senderID = LoRaData.substring
(1, pos1)
for some reason the first space of the String that arrived from the other LoRa device was a blank space, therefore when using the position 0 of the String the conditions for entering the if statements were not fulfilled.
I have another question, now I can show the variables on the website, something like the project in this link https://rntlab.com/module-11/lora-long-range-sensor-monitoring-and-data-logging-part-1/ ,but when the webpage its refreshed the program stops working so i cant show more new sensor readings anymore,Is there any way that the page shows the most recent data that have come to the LoRa even after refreshing the web browser?, I just want to show the most recent sensor data, refresh the page from the web browser and after that display the new latest sensor data on the screen.
I hope not to bother you with so many questions and thank you very much for your time.
Hi Adin.
You say that your program stops working. Do you get any more information in the Serial Monitor?
I think that may be happening because of what is described here: https://rntlab.com/question/solved-esp32-web-server-drops-connection-crashes/
Can you follow those steps and see if that solves the problem?
Then, let me know the results.
Regards,
Sara
Hello Sara, and thank you for your quick answer.
it seems that my problem is finally fixed with your solution, thanks a lot , now i can refresh the page without losing data.
Have a nice day and thanks again,