Publishing using JSON?

I am doing a single JSON publish every 2 seconds four times over then wait and publish again 20 seconds later and so on. When I look at the console some of the publishes look as expected from your tutorial BUT some of them look like this (notice I don’t get the PRETTY or RAW buttons)…Can someone explain??

Only a blue rectangle?!?!?!
More context would be good.

1 Like

I edited the post…should be able to see what I see

It seems your string is not terminated properly and that’s why you see these trailing characters after the place where the terminating '\0' should be.

Possibly a buffer overflow issue.

So for context and with hope that you can guide me thru this…I create an event every 20 seconds and that generates 4 publishes. After each of the publishes I delay for 2 seconds. 20 seconds later I wake up and do it again. And this occurs for a total of 3 cycles (ie 12 publishes). What I notice is the first two publishes always look like you see above (strange) but the 2nd two publishes are fine. I have shared the code which shows publishing…If I just publish to Particle without using JSON (the remarked out section of code in the state machine piece, I have no issues…works just fine. Can you comment a little more on what may be going on? (Sorry I am new to JSON ) From my perspective I just call this function and it is identical every time I call it and the particle.publish when unremarked uses the same datum and works fine.
Thanks

          case WEB_PUBLISH:
            for (current = 0; current < 4; current++){
              Serial.printlnf("Publishing to Particle CYCLE %d, CURRENT %0.1f mA", ((readCycle > 0) ? readCycle : 3), DAC_C[current]);
/*
              Particle.publish("Event/Cycle/Current/Vbias/SiPM/LED1/LED2/Tw/Tbd",String::format("%lu, %d, %0.1f, %0.2f, %0.2f, %0.2f, %0.2f, %0.1f, %0.1f",\
              events, ((readCycle > 0) ? readCycle : 3), DAC_C[current], SensorDataPerCyclePublish.Vbias[current], \
                SensorDataPerCyclePublish.SiPMCurrent[current], SensorDataPerCyclePublish.LED1Current[current], SensorDataPerCyclePublish.LED2Current[current], \
                SensorDataPerCyclePublish.w_temp[current],SensorDataPerCyclePublish.bd_temp[current]));
*/
              createEventPayload(events, ((readCycle > 0) ? readCycle : 3), SensorDataPerCyclePublish.Vbias[current], \
                SensorDataPerCyclePublish.SiPMCurrent[current], SensorDataPerCyclePublish.LED1Current[current], SensorDataPerCyclePublish.LED2Current[current], \
                SensorDataPerCyclePublish.w_temp[current],SensorDataPerCyclePublish.bd_temp[current]);
              delay(2000);
            }
void createEventPayload(unsigned long postNum, uint8_t cycle, float bias, float sipm, float led1, float led2, float t1, float t2){
  char buf[256];
  JSONBufferWriter jw(buf, sizeof(buf));

  jw.beginObject();
  jw.name("Identifier").value(postNum);
  jw.name("Cycle").value(cycle);
  jw.name("BiasVoltage").value(bias, 2);
  jw.name("SiPMVoltage").value(sipm, 2);
  jw.name("LED1Voltage").value(led1, 2);
  jw.name("LED2Voltage").value(led2, 2);
  jw.name("WaterTemp").value(t1, 1);
  jw.name("BoardTemp").value(t2, 1);
  jw.endObject();

  Particle.publish("Fluorimeter-Data", jw.buffer(), PRIVATE);
}

Can you try increase the size of your buffer and also clear it.

  char buf[320];
  memset(buf, 0, sizeof(buf));
  JSONBufferWriter jw(buf, sizeof(buf)-1);

That’s also what the doc’s samples suggest

2 Likes

Thank you that worked…
Not sure which line above fixed the issue but it now works…I thought the buffer max was 256??
Do you think I have to flush the buffer with memset?

Yes. JSONBufferWriter does not null terminate the buffer, so you either need to clear the buffer with memset or add null terminator. The docs show both ways:

JSONBufferWriter

3 Likes

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