How to package 12 double variables into JSON Object (Packets)

<
I am developing Processing GUI for PID control.
I wish to use JSON to share data between the PC and the control board.

But i am having issue packaging the JSON object.
I need send and receive 12 variable (double).
I was able to package upto 9 doubles variables.
If i add one more (10 variables) the packet get truncated
This is the Error package:
{“KP”:41.1,“KI”:23.1,“KD”:10.1,“FSP1”:1.0,“FSP2”:1.2,“TPSP”:36.3,“FL1”:0.9,“FL2”:1.8,“TMP”:34.1"UPT

Well packaged (maximum of 9 variables)
{“KP”:41.1,“KI”:23.1,“KD”:10.1,“FSP1”:1.0,“FSP2”:1.2,“TPSP”:36.3,“FL1”:0.9,“FL2”:1.8,“TMP”:34.1}␊

//===========================================================================
//This Function Send JSON Payload
//===========================================================================
typedef struct
{
double _1FLOWsp; //1 Flow setpoint
double _2FLOWsp; //2 Flow setpoint
double _TEMPsp; //Temperature setpoint
double _kp; //This is PID kp set by user
double _ki; //This is PID ki set by user
double _kd; //This is PID Kd set by user
double _1FLmeas; //This flow rate measured from sensor1
double _2FLmeas; //This flow rate measured from sensor2
double _TEMPmeas;
double _PWRmeas; //This is Power consumed by the system unit
double _1TANKmeas; //This is water level in the upper tank
double _2TANKmeas; //This is water level in the lower tank
}MPACKET;

void JSONpayload()
{
char PAYLOAD[250];
MPACKET data;

data._kp=41.1;
data._ki=23.1;
data._kd=10.1;
data._1FLOWsp=1.0;
data._2FLOWsp=1.2;
data._TEMPsp=36.3;
data._1FLmeas=0.9;
data._2FLmeas=1.8;
data._TEMPmeas=34.1;
data._1TANKmeas=12.1;
data._2TANKmeas=1.1;
data._PWRmeas=21.1;


sprintf(PAYLOAD,
		  "{\"KP\":%.1f,"		//KP
		  "\"KI\":%.1f,"		//KI
		  "\"KD\":%.1f,"		//KD
		  "\"FSP1\":%.1f,"		//Flow rate set-point
		  "\"FSP2\":%.1f,"		//Flow rate set-point
		  "\"TPSP\":%.1f,"		//Temp set-point
		  "\"FL1\":%.1f,"		//1 Flow rate measured
		  "\"FL2\":%.1f,"		//2 FLow rate measured
		  "\"TMP\":%.1f"		//Temperature measured
		  "\"UPT\":%.1f"		//1 Tank water level
		 // "\"DWT\":%.1f"		//2 Tank water level
		 // "\"PWR\":%.1f"		//Power consumed
		  "}\n"

		  ,data._kp
		  ,data._ki
		  ,data._kd
		  ,data._1FLOWsp
		  ,data._2FLOWsp
		  ,data._TEMPsp
		  ,data._1FLmeas
		  ,data._2FLmeas
		  ,data._TEMPmeas
		  ,data._1TANKmeas
		 // ,data._2TANKmeas
		 // ,data._PWRmeas
		  );

  	  UART0->STREAM(D_PORT, PAYLOAD);

}

/>

How can i package 12 variables (double) into JSON.

Hi @avong, You haven’t said what control board you are using, I’m familiar with Arduino but not others. The code you posted has a sprintf so it has to be the controller code. I like sprintf and used to use it in Arduino. On one project it would not compile, and if you Google ‘arduino sprintf problem’ you’ll find lots.

You could avoid using sprintf by printing the pieces of the json packet using separate print statements. At the receiving end it will look just the same.

I am using costumed Atmega328P board, i use eclipse IDE + avr plugin.
The Processing GUI has no issue, i can read once incoming data (JSON format) from the 9 variable i could packet into JSON format.
The challenge is, i can go beyond 9 variables, when i do, i get errors (truncated packet).

I was thinking there is internal buffer declared using char* pointer which truncates my packet. but when i check i could only see extern int sprintf(char *__s, const char *__fmt, …); in stdio.h header.

Yes, alright. I started thinking ‘use 2 shorter pieces for the packet’, then thought ‘don’t bother with the packet, just print all the pieces separately and avoid sprintf completely’. There are also articles saying that using the String data type in Arduino is not very good.

Okay,
I will take your first recommendation.
I ealier thought of breaking my packet into 2, but was convience to post my challenge in public, maybe someone will have experience that before.