SparkJson... Works the first time, then doesn't update

This is my udp read function, which calls a SparkJson parsing function when it works.

The first call is flawless, I am definitely receiving a good data packet over UDP (The packet is ~772 bytes). After that, the JSON object in the second function at the bottom doesn’t appear to update anymore, and the values I pull from thee JSON object don’t change. But I have verified that they are changing.

So I don’t know if the UDP is returning the same data for some reason, or if the parser is not working.

void checkUdp()
{
  int rxErr = 0;
  int count = udp.receivePacket(tempStatus, 1023);  //tempStatus is declared as a global char tempStatus[1024]

  if (count >= 0 && count < 1024) {
    tempStatus[count] = 0;
    rxErr = 0;
    parseTempStatus(tempStatus);
  } else if (count < -1) {
    rxErr = count;
    // need to re-initialize on error
    udp.begin(udpListenPort);
  }
  if (!rxErr) {
    //No Error code, but this could mean count = -1, which is no packet
  }
}

void parseTempStatus(char* tempJSON)
{
    StaticJsonBuffer<4096> jsonBuffer;  //This is probably bigger than it needs to be, but not by much
    JsonObject& root = jsonBuffer.parseObject(tempJSON);
    displayInfo[0] = "Temp: " + String::format("%.1fF",root["Sensor"]["temp"]);
    displayInfo[1] = String(Time.local());
    displayInfo[2] = "Last: " + String::format("%d", root["Sensor"]["TS"]);
    
    blinkLED();
      
}

We could address the issue you are seeing but since there are better alternatives to the SparkJson library you may want to look at these first

  • the JsonParserGeneratorRK library is written and actively maintained by Rick (rickkas7) a particle employee
  • the device OS features a lightweight but slightly less convenient JSON parser
  • the original contributor of SparkJson has also uploaded that library as ArduinoJson and keeps updating that quite frequently (while the SparkJson is now dormant)

For your issue at hand: As you are using a StaticJsonBuffer you may want to clear it before parsing a new JSON string otherwise you just keep reparsing the old contents that’s “statically” stored.

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