Parsing data from a string

I have a a packet received from a LoRa unit saved to a string. The packet was send as [sensor data1],[sensor data 2],[sensor data 3]... where each [sensor data #] is a numeric value (ie 3,126,15...). How can I take each sensor data value and equate it to a variable?

MODERATOR EDIT- added code tags

while (LoRa.available()) {
  incomingdata = (char);

Read up on strtok() , you can use the commas as a separator=delimiter.

Check out Serial Input Basics for reliable methods to receive Serial data.

Here is a sample reader / decoder for CSV using SafeStrings
It has a lot of error checking built in.
Suggest you terminate your LoRa data with a '\n' to simplify things.
change the non-blocking SafeString readUntil from
if (sfInput.readUntil(softSerial, '\n')) {
if (sfInput.readUntil(LoRa, '\n')) {

This example is from my tutorial on Arduino to Arduino via Serial

// csvESP8266SoftwareParser.ino

// download SafeString V4.1.5+ library from the Arduino Library manager or from
#include "SafeString.h"
#include "SoftwareSerial.h"

const int RX_pin = 13; // for ESP8266 use 13  D7 on NodeMCU/wemos-d1-esp8266
const int TX_pin = 12; // for ESP8266 use 12  D6  on NodeMCU/wemos-d1-esp8266
// else you need to define your own RX_pin, TX_pin
SoftwareSerial softSerial(RX_pin, TX_pin); // RX, TX

float longitudeIn;
float latitudeIn;

cSF(sfInput, 60); // enough for 60char input line // ESP8266 software serial has 64byte RX buffer

bool decodeCSV(SafeString &sfCSV, float &longitude, float &latitude) {
  if (!sfCSV.endsWith('\n')) {
    // input filled up but no \n
    Serial.print(F(" missing \\n terminator ")); Serial.println(sfCSV);
    return false;
  Serial.print("sfCSV: "); Serial.print(sfCSV);

  float lng = 0, lat = 0;
  cSF(field, 20); // to hold numbers
  sfCSV.firstToken(field, ',', true); // return empty fields to detect missing data
  if (!field.toFloat(lng)) { // ignores leading and trailing whitespace
    Serial.print(F("longitude not a valid float '")); Serial.print(field); Serial.println("'");
    return false;
  sfCSV.nextToken(field, ',', true);
  if (!field.toFloat(lat)) { // ignores leading and trailing whitespace
    Serial.print(F("latitude not a valid float '")); Serial.print(field); Serial.println("'");
    return false;
  // else check no extra fields
  if (!sfCSV.isEmpty()) {
    Serial.print(F("More than two fields. Remaining data:")); Serial.print(sfCSV);
    return false;
  // else all OK update returns
  longitude = lng;
  latitude = lat;
  return true;

void setup() {
  for (int i = 10; i > 0; i--) {
    Serial.print(' '); Serial.print(i);
  Serial.println(F("Receiver started"));

void loop() {
  if (sfInput.readUntil(softSerial, '\n')) {
    // returns true if found \n OR reached sfInput limit
    // if \n found it is returned.
    if (decodeCSV(sfInput, longitudeIn, latitudeIn)) {
      // got new valid inputs update
      Serial.print(F(" new longitude:")); Serial.print(longitudeIn); Serial.print(F(" new latitude:")); Serial.print(latitudeIn); Serial.println();
    } else {
      // error in CSV nothing updated
    sfInput.clear(); // for next line

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