MQTT data is received without quotation marks

RichPiano
Posts: 123
Joined: Mon May 18, 2020 2:51 pm

MQTT data is received without quotation marks

Postby RichPiano » Sun Jan 24, 2021 5:53 pm

When I publish to an MQTT topic like this:
mosquitto_pub -p 8883 -t someTopic --cafile /etc/ssl/certs/cert-ourca.crt --tls-version tlsv1.2 -m "[{"hour":6,"minute":0,"second":0,"intensity":0,"profileDetailId":100,"id":254},{"hour":6,"minute":15,"second":0,"intensity":1024,"profileDetailId":100,"id":273},{"hour":9,"minute":0,"second":0,"intensity":1024,"profileDetailId":100,"id":334},{"hour":10,"minute":0,"second":0,"intensity":0,"profileDetailId":100,"id":333},{"hour":17,"minute":0,"second":0,"intensity":0,"profileDetailId":100,"id":321},{"hour":18,"minute":0,"second":0,"intensity":1024,"profileDetailId":100,"id":319},{"hour":21,"minute":0,"second":0,"intensity":1024,"profileDetailId":100,"id":315},{"hour":21,"minute":15,"second":0,"intensity":0,"profileDetailId":100,"id":314}]" --insecure
.. it is received by the default esp MQTT handler like this:
DATA=[{hour:6,minute:0,second:0,intensity:0,profileDetailId:100,id:254},{hour:6,minute:15,second:0,intensity:1024,profileDetailId:100,id:273},{hour:9,minute:0,second:0,intensity:1024,profileDetailId:100,id:334},{hour:10,minute:0,second:0,intensity:0,profileDetailId:100,id:333},{hour:17,minute:0,second:0,intensity:0,profileDetailId:100,id:321},{hour:18,minute:0,second:0,intensity:1024,profileDetailId:100,id:319},{hour:21,minute:0,second:0,intensity:1024,profileDetailId:100,id:315},{hour:21,minute:15,second:0,intensity:0,profileDetailId:100,id:314}]
[{hour:6,minute:0,second:0,intensity:0,profileDetailId:100,id:254},{hour:6,minute:15,second:0,intensity:1024,profileDetailId:100,id:273},{hour:9,minute:0,second:0,intensity:1024,profileDetailId:100,id:334},{hour:10,minute:0,second:0,intensity:0,profileDetailId:100,id:333},{hour:17,minute:0,second:0,intensity:0,profileDetailId:100,id:321},{hour:18,minute:0,second:0,intensity:1024,profileDetailId:100,id:319},{hour:21,minute:0,second:0,intensity:1024,profileDetailId:100,id:315},{hour:21,minute:15,second:0,intensity:0,profileDetailId:100,id:314}]
As you can see, this text is in JSON format and because the quotation marks "" are gone, it isn't being parsed correctly by cJSON. Is there a way to prevent the MQTT parser from removing all the quotation marks?

When I escape the \" before sending it it does work. But I currently don't have this option.

RichPiano
Posts: 123
Joined: Mon May 18, 2020 2:51 pm

Re: MQTT data is received without quotation marks

Postby RichPiano » Tue Mar 02, 2021 5:30 pm

For anyone interested it turned out that this wasn't the problem of the receiver but the sender. I used the mqtt-clients publisher on my RaspberryPi which evidently removes all quotation marks when sending the data to the broker.

tl;dr: mosquitto_pub was the problem.

ESP_Sprite
Posts: 9052
Joined: Thu Nov 26, 2015 4:08 am

Re: MQTT data is received without quotation marks

Postby ESP_Sprite » Wed Mar 03, 2021 1:44 am

It's likely not even anything Mosquitto-related; Bash (your shell) will remove the quotes for you. Escaping them is one way out of that, putting the entire string in single quotes is another:

Code: Select all

mosquitto_pub -p 8883 -t someTopic --cafile /etc/ssl/certs/cert-ourca.crt --tls-version tlsv1.2 -m '[{"hour":6,"minute":0,"second":0,"intensity":0,"profileDetailId":100,"id":254},{"hour":6,"minute":15,"second":0,"intensity":1024,"profileDetailId":100,"id":273},{"hour":9,"minute":0,"second":0,"intensity":1024,"profileDetailId":100,"id":334},{"hour":10,"minute":0,"second":0,"intensity":0,"profileDetailId":100,"id":333},{"hour":17,"minute":0,"second":0,"intensity":0,"profileDetailId":100,"id":321},{"hour":18,"minute":0,"second":0,"intensity":1024,"profileDetailId":100,"id":319},{"hour":21,"minute":0,"second":0,"intensity":1024,"profileDetailId":100,"id":315},{"hour":21,"minute":15,"second":0,"intensity":0,"profileDetailId":100,"id":314}]' --insecure

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 222 guests