# Parsing Problem

**URL:** <https://community.particle.io/t/parsing-problem/54307>\
**Category:** Connectivity\
**Tags:** photon\
**Created:** [January 21, 2020, 4:53pm UTC](https://community.particle.io/t/parsing-problem/54307 "2020-01-21T16:53:08Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JTravers555](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@JTravers555](https://community.particle.io/u/JTravers555)\
**Post date:** [January 21, 2020, 4:53pm UTC](https://community.particle.io/t/parsing-problem/54307/1 "2020-01-21T16:53:08Z")

</div>

Hello,

My project involves having a Parallax Propeller control my well system for my house. That part I have sorted out and working well, and having learned a little about the Photon, I’ve got one connected to my Propeller, with the intent of having the Prop send a string to the Photon: \<255,324,155,200\> \0. The four ints are numbers from my well I’d like to port over to Adafruit IO and see on a dashboard trend.

The Prop is a 3.3V device, and I have it sending a serial string to the Photon’s RX terminal, and a common ground between the two of course. It’s just a straight connection.

Unfortunately, I’m having no luck getting the parsing accomplished, the below code is me trying to see if I can get the string parsed and into variables so I can see them on my iphone Particle App, or the console. Can anyone tell me what I’m doing wrong?

Thanks!

```cpp
// Example 5 - Receive with start- and end-markers combined with parsing

const byte numChars = 32;
char receivedChars[numChars];
char tempChars[numChars]; // temporary array for use when parsing

      // variables to hold the parsed data
char messageFromPC[numChars] = {0};
int integerFromPC = 0;
int Height, WellGallons, TankGallons, WaterUsed; 

boolean newData = false;

//============

void setup() {
   
    Particle.variable("Height", &Height, INT);
    Particle.variable("WellGallons", &WellGallons, INT);
    Particle.variable("TankGallons", &TankGallons, INT);
    Particle.variable("WaterUsed", &WaterUsed, INT);
    Serial1.begin(9600, SERIAL_8N1);
 
}

//============

void loop() {
    recvWithStartEndMarkers();
    if (newData == true) {
        strcpy(tempChars, receivedChars);
            // this temporary copy is necessary to protect the original data
            // because strtok() used in parseData() replaces the commas with \0
        parseData();
     
        newData = false;
    }
}

//============

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;

    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }

        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

//============

void parseData() { // split the data into its parts

    char * strtokIndx; // this is used by strtok() as an index
 
    strtokIndx = strtok(NULL, ","); // this continues where the previous call left off
    Height = atoi(strtokIndx); // convert this part to an integer

    strtokIndx = strtok(NULL, ",");
    WellGallons = atoi(strtokIndx); // convert this part to an int

    strtokIndx = strtok(NULL, ",");
    TankGallons = atoi(strtokIndx); // convert this part to an int
    
    strtokIndx = strtok(NULL, ",");
    WaterUsed = atoi(strtokIndx); // convert this part to an int
}

```

---

<div class="post-metadata">

**Author:** ![ScruffR](https://sea2.discourse-cdn.com/flex026/user_avatar/community.particle.io/scruffr/32/6952_2.png) [@ScruffR](https://community.particle.io/u/ScruffR)\
**Post date:** [January 21, 2020, 6:21pm UTC](https://community.particle.io/t/parsing-problem/54307/2 "2020-01-21T18:21:33Z")

</div>

The first call to `strtok()` must have the source string as first parameter, only _ **after** _ that you can use `NULL` to _continue_ on the same string. But without actually knowing which string to work on it’s impossible to _continue_.

You even state that fact in your comment `// this continues where the previous call left off`.  
No previous call left off anywhere AFAICT:

BTW, you can use the `Stream` functions like [`readBytesUntil()`](https://docs.particle.io/reference/device-os/firmware/photon/#readbytesuntil-) to read your string with much less effort and [`sscanf()`](http://www.cplusplus.com/reference/cstdio/sscanf/) to parse the data more concisely.

You are also using outdated syntax for `Particle.variable()`  
For quite some time now your variables would simply be registered like this

```cpp
    Particle.variable("Height", Height);
    Particle.variable("WellGallons", WellGallons);
    Particle.variable("TankGallons", TankGallons);
    Particle.variable("WaterUsed", WaterUsed);

```

---

<div class="post-metadata">

**Author:** ![JTravers555](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@JTravers555](https://community.particle.io/u/JTravers555)\
**Post date:** [January 21, 2020, 10:54pm UTC](https://community.particle.io/t/parsing-problem/54307/3 "2020-01-21T22:54:20Z")

</div>

Hey thanks for your reply. I’ve ammended the Particle variable calls, and put in recievedChars in place of the NULL for the first Parsing call.

I copied that code largely from an Arduino tutorial of course, and didn’t delete out the comment.

I flashed it, but still no joy. Tomorrows a new day and I’ll try to figure out those better ways you mentioned. Thanks.

---

<div class="post-metadata">

**Author:** ![JTravers555](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@JTravers555](https://community.particle.io/u/JTravers555)\
**Post date:** [January 23, 2020, 11:29pm UTC](https://community.particle.io/t/parsing-problem/54307/4 "2020-01-23T23:29:08Z")

</div>

Got it! I had a couple ‘Serial’ calls in there, not ‘Serial1’, also I had to add in a couple Particle.process() calls to keep it from breathing green. It’s my first real project that made it to IoT stage, and it’s awesome! Thanks for your help!
