fredashbolt - 10 hours ago

I am trying to generate a JSON payload populated with a value that updates for use with a telemetry system, the payload is to be published by MQTT.
I have established the MQTT client and can publish a working JSON payload but I can't get the payload to update as the value (iValue) changes. I am using a timer to trigger the .SetValue function periodically, but the JSON will only give the value as it is when it is first initialized using xFirst IF statement.
Any suggestions?
Thanks

VAR
pJsonData : POINTER TO JSON.JSONData := factory.Create();
fb_JBuilder : JSON.JSONBuilder;
diRootIndex, diObject1Index : DINT;
iValue : INT := 10 ;
jsonArrayWriter : JSON.JSONByteArrayWriter;
wsJsonData : WSTRING(1000);
xFirst : BOOL := TRUE;
utf8JsonData : STRING(2000);
diTempVal : DINT ;
PublishTimer : TON ;
END_VAR

IF xFirst = TRUE THEN
fb_JBuilder ( pJsonData := pJsonData , diRootObj => diRootIndex ) ;
diObject1Index := fb_Jbuilder.SetKeyWithObject ( "Device" , diParentIndex := diRootIndex ) ;
diTempVal := fb_Jbuilder.SetKeyWithValue ( "Temp" , iValue , diParentIndex := diObject1Index ) ;
xFirst := FALSE ;
END_IF

PublishTimer ( IN := PublishTimer.Q = 0 ,
PT := T#5S ) ;

IF PublishTimer.Q THEN
fb_JBuilder.SetValue ( iValue , diTempVal ) ;
END_IF

jsonArrayWriter( xExecute := PublishTimer.Q ,
pwData := ADR(wsJsonData),
udiSize := SIZEOF(wsJsonData),
jsonData := pJsonData^,
xAsyncMode := FALSE ) ;

JSON.Stu.ConvertUTF16toUTF8(
ADR ( wsJsonData ) ,
ADR ( utf8JsonData ) ,
SIZEOF ( utf8JsonData ) ,
bStrictConversion := TRUE ) ;