Sending JSON objects via MQTT
-
Some more information on the issue, as I'm still unable to work around it.
This is what I'm sending (or trying to) via MQTT:
{"M001":{"timestamp":1499441290602,"reading":2000}, "M002":{"timestamp":1499441290602,"reading":3000}}
When the data point is configured as a JSON subscribe topic type, as below:
Mango throws an ERROR.
ERROR 2017-07-10T10:25:09,112 (com.infiniteautomation.m2m2.mqtt.client.MqttClien tDataSourceRT.messageArrived:415) - Error during read topic: "test/array2" datap oint: "RPi - M001_RPi" payload: "{"meterOne":{"timestamp":1499678815845,"reading ":2000},"M002":{"timestamp":1499678815845,"reading":3000}}" exception: "ERROR"
Help doesn't work to see how to configure the JSON subscriber :(
If the JSON value path is set to '$.M001', this is the error received:
ERROR 2017-07-10T10:43:04,379 (com.infiniteautomation.m2m2.mqtt.client.MqttClien tDataSourceRT.messageArrived:415) - Error during read topic: "test/array2" datap oint: "RPi - M001_RPi" payload: "{"M001":{"timestamp":1499679891268,"reading":20 00},"M002":{"timestamp":1499679891268,"reading":3000}}" exception: "com.serotoni n.json.type.JsonObject cannot be cast to com.serotonin.json.type.JsonString"
If the data point is configured as 'Plain', then the terminal throws no errors and the message is sent to Mango. I'm trying to access the object named "M001" in different ways, as below:
<div layout="column" layout-fill flex> <md-card flex> <ma-get-point-value point-xid="RPi_M001_reading" point="array2"></ma-get-point-value> <li> array2.value: {{array2.value}}</li> <li>array2.value[1]: {{array2.value[1]}}</li> <li>array2.value.M001: {{array2.value.M001}}</li> <li>array2.value["M001"]: {{array2.value["M001"]}}</li> <li>array2.value[M001]: {{array2.value[M001]}}</li> </md-card> </div>
And these are the results:
I can 'read' the whole string, but I can't individually access the object properties.
Can anyone offer any support please, as I'm running out of ideas?
Thanks in advance and have a great day :)
-
Hi Silvia,
Huh, that's confusing. I notice that in the first error provided the key
M001
doesn't exist, for some reason it is "meterOne" then in the second it makes sense it didn't think{"timestamp":1499679891268,"reading":2000}
was a string. It seems like the whole value path may be different by the page spacing (looks like your second path is longer), but we can't see the value path in the second image.The first error image probably just means key not found in the object, but the error messaging does look like it has room for improvement.
Perhaps Jared or Will can advise you on how to do that dashboarding. It's going to have to involve parsing the string value into an object... some
JSON.parse()
type thing. -
@silvia said in Sending JSON objects via MQTT:
If the JSON value path is set to '$.M001', this is the error received:
ERROR 2017-07-10T10:43:04,379 (com.infiniteautomation.m2m2.mqtt.client.MqttClien
tDataSourceRT.messageArrived:415) - Error during read topic: "test/array2" datap
oint: "RPi - M001_RPi" payload: "{"M001":{"timestamp":1499679891268,"reading":20
00},"M002":{"timestamp":1499679891268,"reading":3000}}" exception: "com.serotoni
n.json.type.JsonObject cannot be cast to com.serotonin.json.type.JsonString"This would be because the data type is set to
Alphanumeric
while the JSON path you specified is for an object. If you set the datatype to plain it doesn't try and parse the JSON so it works.As Phillip said, check your usage of
meterOne
vsM001
Re. parsing out the individual components of the JSON object on the dashboards, I'd recommend not doing this, you wont be able to use the datapoints effectively on any of the Mango pages.
-
@phildunlap and @Jared-Wiltshire, thank you both for your replies.
The issue with
M001
vsmeterOne
isn't there anymore, at some point I decided to change the identifier to see if that was the problem, and I screenshoted some old error. Apologies for that. As you can see, when everything is namedM001
, the same error appears:ERROR 2017-07-17T09:18:36,296 (com.infiniteautomation.m2m2.mqtt.client.MqttClien tDataSourceRT.messageArrived:415) - Error during read topic: "test/array2" datap oint: "RPi - M001_RPi" payload: "{"M001":{"timestamp":1500279612733,"reading":20 00},"M002":{"timestamp":1500279612733,"reading":3000}}" exception: "ERROR"
@Jared-Wiltshire said in Sending JSON objects via MQTT:
Re. parsing out the individual components of the JSON object on the dashboards, I'd recommend not doing this, you wont be able to use the datapoints effectively on any of the Mango pages.
Any alternatives to this? Is there any other format / data type that would allow me to send similar data (a meter identifier, a reading and a timestamp) x 4?
Thanks and regards.
-
@silvia what exactly are you trying to achieve? If the goal is to simply read the two values into Mango then create two data points, both with Data type set to
Numeric
. The Topic type should beJSON
and the JSON value path should be$.M001.reading
for one, and$.M002.reading
for the other. If this doesn't work then there may be a bug in the MQTT datasource. -
Hi @Jared-Wiltshire,
Yes, my objective is to read them and then compare the
M001.reading
with the previous one, see which is the increment (if any), plot it, etc.I've configured new datapoints as instructed (same for M002):
...and these are the errors thrown:
ERROR 2017-07-18T09:19:19,813 (com.infiniteautomation.m2m2.mqtt.client.MqttClien tDataSourceRT.getJsonValue:375) - ERROR ERROR 2017-07-18T09:19:19,813 (com.infiniteautomation.m2m2.mqtt.client.MqttClien tDataSourceRT.messageArrived:415) - Error during read topic: "test/array2" datap oint: "RPi - RPi_M001_reading_new" payload: "{"M001":{"timestamp":1500366055687, "reading":2000},"M002":{"timestamp":1500366055687,"reading":3000}}" exception: " ERROR" ERROR 2017-07-18T09:19:19,954 (com.infiniteautomation.m2m2.mqtt.client.MqttClien tDataSourceRT.getJsonValue:375) - ERROR ERROR 2017-07-18T09:19:19,954 (com.infiniteautomation.m2m2.mqtt.client.MqttClien tDataSourceRT.messageArrived:415) - Error during read topic: "test/array2" datap oint: "RPi - RPi_M002_reading_new" payload: "{"M001":{"timestamp":1500366055687, "reading":2000},"M002":{"timestamp":1500366055687,"reading":3000}}" exception: " ERROR"
I'm afraid that there's a bug in the MQTT datasource.
Would any of you guys be able to have a look into it?Thanks :)
-
-
@silvia Sorry there hasn't been any follow up. We have been sending a few emails backwards and forwards about this. I'll see where we got to and reply back today.
-
I have sent you an email Silvia!
-
Thanks @phildunlap , I'll try what you've sent me asap and post the results back here :)
-
@silvia We have made a v3.1.2 release of the MQTT data source which should rectify the issues you were experiencing.
There we some bugs around referencing values inside JSON objects so we decided to switch the notation over to the JSON pointer spec. Your existing points with
$.
type notation will be automatically converted and hopefully should just work.I've added a help document to the data point edit page for MQTT too (click the blue help icon).