LoRa Receive Parsing

Folks,

Beginner here, i am trying to parse a simple string of data that i send from a sender Arduino-LoRa to a receiver Arduino-LoRa.

When I receive data, I use the simple code:

Serial.print((char)LoRa.read());

And I get a string like this:

"33.80,21.50,309,192,1.23439"

I want to be able to parse this data into individual float numbers.
Can somebody help me understand a method to do this? Or show me a simple example?

I have searched the forums, but am having trouble understanding a solution for my experience level.

Thanks kindly

Please use the flag button to ask a moderator to move your post to a more suitable category.

For parsing simple sentences like that, the C-string functions strtok() and atof() work well.

See the Serial Input Basics tutorial on this forum.

If you are in control of the sending, why not send raw (digital) float values instead of converting it to a string, that you later have to parse?

Hover your mouse over "Installation and Troubleshooting".

From Adafruit's GPS code:


  if (strstr(nmea, "$GPRMC")) {
   // found RMC
    char *p = nmea;

    // get time
    p = strchr(p, ',')+1;
    float timef = atof(p);
    uint32_t time = timef;
    hour = time / 10000;
    minute = (time % 10000) / 100;
    seconds = (time % 100);

    milliseconds = fmod(timef, 1.0) * 1000;
    p = strchr(p, ',')+1;  // A/V?
    p = strchr(p, ',')+1;  // lat
    p = strchr(p, ',')+1;  // N/S?
    p = strchr(p, ',')+1;  // lon
    p = strchr(p, ',')+1;  // E/W?
    p = strchr(p, ',')+1;  // speed
    p = strchr(p, ',')+1;  // angle

    p = strchr(p, ',')+1;
    uint32_t fulldate = atof(p);
    day = fulldate / 10000;
    month = (fulldate % 10000) / 100;
    year = (fulldate % 100);
    // we dont parse the remaining, yet!
    return true;
  }

  return false;
}

Here is an example of using strtok(), atof() and atoi() functions:

// example strtok(), atof() and atoi()

char delim[] = " :";  //substring delimiters

void setup() {
  char* pch; //pointer to substring
  char message[] = "X: 35.3 Y: 44 Z:10.5"; //sample data

  Serial.begin(115200);
  while (!Serial); //wait for serial monitor to connect

  Serial.print("message >");
  Serial.print(message);
  Serial.println("<");


  pch = strtok(message, delim);  // first token: X

  Serial.print("strtok returns >");
  Serial.print(pch);
  Serial.println("<");

  pch = strtok(NULL, delim);  //second token, a number
  Serial.print("strtok returns >");
  Serial.print(pch);
  Serial.println("<");

  Serial.print("atoi returns: "); //convert to int
  Serial.println(atoi(pch));
  Serial.print("atof returns: "); //convert to float
  Serial.println(atof(pch));
  // etc.
}

void loop() {}

@anon57585045 suggested why not send raw (digital) float values instead of converting it to a string, that you later have to parse
have a look at decimals-strings-and-lora where raw binary data is transmitted from slave nodes to a master node as structures (containing floats, ints, characters, etc)
what Arduino and lora module are you using?

I'm using Arduino 2.0.3. the LoRa is Ra-02. The reason going to a string or a struct is because there will be data from multiple sensors. As soon as I've got the ultrasonic sesnsor working as the "x" component, I'll add the voltage sensor as a "y". If we sent multiple raw data to the receiver then it would have to be unpacked and processed there. I think just trading one set of problems for another. I think if it were a good solution , I'd have seen examples of it, but in several years on this, I never have.

I couldn't find anything online about the benefit of doing it one way or the other, so I asked my "friend" (artificial Intelligence at chatGPT for an opinion....

If you are using multiple sensors and storing the data in a structure, it's a good idea to process the data before transmitting it. This will allow you to combine the data from multiple sensors into a single structure and perform any necessary data validation or correction. Processing the data before transmitting it can also help to reduce the amount of data that needs to be transmitted, which can help to conserve battery power and reduce transmission costs.

Additionally, by processing the data before it is transmitted, you can also combine the data from multiple sensors in a way that makes it more useful for your application. For example, you can calculate derived values or perform data analysis that combines data from multiple sensors.

You may also want to consider adding some error checking and handling routines to your code, to detect and handle any errors that may occur during the data processing. This will help to ensure that the data transmitted is accurate and reliable.

Overall, processing the data before transmitting it can help to improve the accuracy of the sensor readings, reduce errors in the data and make the data more useful for your application.

He makes some good points..... Love that new program.

the structures suggested contain a sequence number to check for lost or duplicate packets
LoRa does error check so if packets are received they should be OK
however, you could add a CRC check to the structure for extra error checking

another thought: by converting data to text, e.g. "X: 35.3 Y: 44 Z:10.5"` you could be loosing precision - transferring binary avoids this
However, one has to take care if the transmitter and receiver are different processors in case word lengths or endianness is different

One issue to be aware of is that if you are receiveing stuff like sensor data then you need to be sure that the packet received really is one of yours, i.e. one that you intended to receive. There could be packets from other LoRa transmitters or the ocaisional phantom packet that the LoRa recever can generate internally. These rogue packets need to be ignored.

Some of these issues are discussed here;

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