Parse Lora.readString to create variable and send to API

Hello, I'm sending INA219 sensor parameters through LoRa. On the receiver side, I received LoRaData in the format as shown in the picture or as code snippet below:

From the receiver, I want to send the LoRaData (Value1, Value2, Value3, Value4, Value5) to API. The sensor data will be sent every few seconds. I've tried using dummy data by putting hardcoded data (Value1, Value2, Value3, Value4, Value5), and it worked fine (data able to be sent successfully).

Can anyone advise how would be the code if I want to set/parse the LoRaData (Value1, Value2, Value3, Value4, Value5) automatically into the String serverPath ?


if (packetSize) {
//received a packet
Serial.println("Received packet\n ");

//read packet
while (LoRa.available()) {
LoRaData = LoRa.readString();
/*LoRaData format received:
Bus Voltage: value1 V
Shunt Volatge: value2 mV
Load Voltage: value3 V
Current: value4 mA
Power: value5 mW */
Serial.print(LoRaData);
}
}

//Send an HTTP POST request every 10 minutes
if ((millis() - lastTime) > timerDelay) {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;

  String serverPath = serverName + "00174@0017401010101|Value1|00174@0017401010102|Value2|00174@0017401010103|Value3|00174@0017401010104|Value4|00174@0017401010105|Value5";

Appending an integer to a String will convert the integer to a String:

String serverPath = serverName;
serverPath += "00174@0017401010101|";
serverPath += Value1;
serverPath += "|00174@0017401010102|";
serverPath += Value2;
serverPath += "|00174@0017401010103|";
serverPath += Value3;
serverPath += "|00174@0017401010104|";
serverPath += Value4;
serverPath += "|00174@0017401010105|";
serverPath += Value5;
1 Like

Thanks John ! I'll try your suggestion. But before that, how can we parse (Value1...Value5) from LoRaData (into Arduino sketch)?

Serial.print (LoRaData) will show (below) in Serial Monitor :
Bus Voltage: value1 V
Shunt Volatge: value2 mV
Load Voltage: value3 V
Current: value4 mA
Power: value5 mW

I'm trying to figure out, how can I get (Value1...Value5) from LoRaData into Arduino sketch so that it can be append into your suggestion code:

image

Instead of reading everything into one String:
LoRaData = LoRa.readString();
I would read piece-by-piece:

String LoRaLabel = LoRa.readStringUntil(':');
LoRa.read(); // Throw away the space
if (LoRaLabel.endsWith("Bus Voltage"))
   String Value1 = LoRa.readStringUntil(' ');
if (LoRaLabel.endsWith("Shunt Volatge"))
   String Value2 = LoRa.readStringUntil(' ');
if (LoRaLabel.endsWith("Load Voltage"))
   String Value3 = LoRa.readStringUntil(' ');
if (LoRaLabel.endsWith("Current"))
   String Value4 = LoRa.readStringUntil(' ');
if (LoRaLabel.endsWith("Power"))
   String Value5 = LoRa.readStringUntil(' ');

Thanks John,

However, when I applied this code, the value reading duplicated until 5 times. To make things easier, instead of sending string data in 5 lines, I'm sending data like below:

And using the code below to retrieve the data;

void loop() {

  //try to parse packet
  int packetSize = LoRa.parsePacket();
  if (packetSize) {
    //received a packet
    Serial.println("Received packet\n ");

    //read packet
    while (LoRa.available()) {
      /* LoRaData = LoRa.readString(); */
      /* Serial.print(LoRaData); */
      /*receiving data (LoRaData) format: */
      /* <Bus Voltage:3.12 V, Shunt Voltage:-2.13 mV, Load Voltage:3.12 V, Current:-21.30 mA, Power:66.00 mW>  */

      String LoRaLabel = LoRa.readStringUntil('>');
      // LoRa.read(); // Throw away the space
      if (LoRaLabel.endsWith("Bus Voltage:"))
        String Value1 = LoRa.readStringUntil(' ');
      if (LoRaLabel.endsWith("Shunt Voltage:"))
        String Value2 = LoRa.readStringUntil(' ');
      if (LoRaLabel.endsWith("Load Voltage:"))
        String Value3 = LoRa.readStringUntil(' ');
      if (LoRaLabel.endsWith("Current:"))
        String Value4 = LoRa.readStringUntil(' ');
      if (LoRaLabel.endsWith("Power:"))
        String Value5 = LoRa.readStringUntil(' ');

      Serial.print("Bus Voltage: ");
      Serial.println(Value1);
      Serial.print("Shunt Voltage: ");
      Serial.println(Value2);
      Serial.print("Load Voltage: ");
      Serial.println(Value3);
      Serial.print("Current: ");
      Serial.println(Value4);
      Serial.print("Power: ");
      Serial.println(Value5);
    }
  }

However, it didn't give the correct value. The value I got is 0.

image

What might be the problem is ?

You used the wrong character for the end of the label. Change it back to:
String LoRaLabel = LoRa.readStringUntil(':');

Also, the 'Until' character is not included in the returned string so, for example, "Bus Voltage:" should be "Bus Voltage", like I had it in my example.

John,

Thanks! It's working now. Thanks for your input.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.