cancel
Showing results for 
Search instead for 
Did you mean: 
SOLVED

Valid JSON is sent via AWS MQTT and recieved in PLC as an Invalid JSON structure

Valid JSON is sent via AWS MQTT and recieved in PLC as an Invalid JSON structure

AndroidzZ
New Contributor

Hello,

We receive a JSON payload from an overhead system, every 15 minutes, via AWS MQTT. We receive this payload via the FB "AWS_IOT.AWSIoTSubscribe". Well, we noticed that several times a day, the payload coming in has one brace too many at the end resulting in invalid JSON structure and thus cannot be parsed. We can reproduce this error message. We have a payload that 100% of the time is sent as Valid JSON from AWS and comes in on the subscribe FB as Invalid JSON (1 curly brache to many at the end). And this while there are no additional process steps in the payload route.

We are using the CodeSyS IIOT libarary (1.9.0.0). CORE version: 1.20, PLC App 1.20.3, CodeSyS version .3.5.19.10.

See attachment: the left text is recieved as string on our PLC side en the right text is being send by the AWS broker.

Kind regards

7 REPLIES 7

CodeShepherd
Community Moderator
Community Moderator

As I do not know the library, just a suggestion.
Could it be that the variable used as an output of the function block is not emptied by the application or the FB and so the new message is only overwriting a part of it? this could result in the additional curly bracket at the end if a number inside the string could be a digit shorter.

Sgilk
Contributor

Hi @AndroidzZ ,

If you cannot find the source of the problem, you could correct for this by looping over the string and pushing each curly brace & bracket to seperate stacks. You then pop off the stack when you encounter an opposing brace or bracket. If you have any items remaining on either stack, you know you need to remove them from the end.

HmiGuide
Community Moderator
Community Moderator

I addition to the 2 comments above you should do the following:

  • Ensure that the data which is sent is really correct, by using a different client, in a different environment. While I never used that interface, I can't give you a hint which client you can use. Maybe in the Amazon docu you can find some clients for testing.
    • Either you realize that the JSON is correct. => than it is a bug in the CoDeSys library
    • or you have the same error => than something on the sender side is not correct.

CoDeSys docu AWS IoT Core Client_en.pdf 

Hello,

I suspect that the problem is in this corner. at the moment I receive a string. If I get a shorter string after this I still see remnants of the previous (longer) string. I have tried to empty the string with: <string> := '';
I've also tried the MemCpy function but the string still shows remnants of the previous longer string.

The problem seems clear now only finding a solution for it is not yet successful. What suggestions could I try?

 

Kind regards

 

HmiGuide
Community Moderator
Community Moderator

Ok. Now this sounds like a string handling problem.

I expect that the function you are using is just copying the received characters, but not the end of string character. That's pretty common for many CoDeSys features. While a string needs a end of string (Ascii code =0) character after the last letter, you have to put this manually after the last letter. The easiest way is to use sysMemSet() from library SysMem. If you do not set this character, the string continues until the next random position of char(0).

 

VAR
  strJson: STRING(1000); // sizeOf(strJson) = 1000+1
END_VAR

// fill the complete string with Char(0)=End of string char
SysMemSet(pDest:=ADR(strJson), udiValue:=0, udiCount:=SIZEOF(strJson));
// insert here your code which copies the data into strJson
// e.g. receive JSON string

 

 

CDSAddons
Established Member

Can you provide us with a copy of the string (as text) or an example?
In addition, which MQTT version is used here?
(Which MQTT parameters are used - Libman, MQTTParams)?

We are using AWS IoT Core Client SL library 1.9.0.0 with MQTT_Clienst_SL 1.9.0.0

The text files in attachment are in structure identical but one with a character more than the other which will trigger the '{' problem 

Parameters we use are: 

AndroidzZ_0-1712822680307.png

 

Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist