ThingsSpeak and JSON MQTT messages

4 Ansichten (letzte 30 Tage)
Simo Rounela
Simo Rounela am 10 Aug. 2018
Hi!
I'm using Advanticsys UCM-316 IoT gateway, with integrated analog inputs. It supports MQTT data upload, but I can't get it working with ThingSpeak MQTT server.
There is username (not needed / blaablaa?) and password (Write API Key) authentication. No DNS server support, so broker URL 34.206.80.227 (mqtt.thingspeak.com), port 1883, subscriber identifier "ucm316".
After IO configurations, it will make JSON message to be sent. It's fixed formation, cannot change it. Example:
{
"SN":"86004",
"name":"ucm316",
"header":{
"startTime":"2016-02-07T15:06:00.000Z",
"endTime":"2016-02-07T15:06:00.000Z",
"recordCount":2,
"columns":{
"0":{
"id":"0",
"name":"relay1",
"dataType": "NUMBER",
"format":"unsigned short"
}
},
"data":[
{
"ts":"2016-02-07T15:06:00.000Z",
"f":{
"0":{"v":0}
}
}
]
}
This message is copied from manual, because user cannot access the actual JSON message. There is no error log available.
In the end, nothing happens. Most probably, because MQTT message formation is not correct.
- Is there any way I could use this message structure with Thingspeak?
And in case it's not visible, I'm not really a pro with MQTT yet =)
  1 Kommentar
Simo Rounela
Simo Rounela am 10 Aug. 2018
I just got a new sample from manufacturer, to clear up JSON code:
{
"SN" : "81234", //serial number of device
"name": "MPC-330", //configured name of device, can be MQTT device name, or text from user data, or else
"header":{
"startTime": "2016-04-26T16:20:00.000Z", //archive start time
"endTime": "2016-04-26T16:30:00.000Z", //archive end time
"recordCount":2,
"columns":{
"0":{
"id": "0", //ID of value element in table
"name": "HEADER0", //Header value from table
"dataType": "NUMBER",
"format":"unsigned char" //data type from table
},
"1":{
"id": "1",
"name": "HEADER1",
"dataType": "NUMBER",
"format":"unsigned long"
}
}
},
"data": [
{
"ts": "2016-04-26T16:20:00.000Z",
"f":{
"0":{"v":85},
"1":{"v":235680}
}
},
{
"ts": "2016-04-26T16:30:00.000Z",
"f":{
"0":{"v":86},
"1":{"v":235685}
}
}
]
}
{
"SN" : "81234",
"name": "MPC-330",
"header":{
"startTime": "2016-04-26T16:20:00.000Z",
"endTime": "2016-04-26T16:30:00.000Z",
"recordCount":2,
"columns":{
"0":{
"id": "0",
"name": "HEADER0",
"dataType": "NUMBER",
"format":"unsigned char"
},
"1":{
"id": "1",
"name": "HEADER1",
"dataType": "NUMBER",
"format":"unsigned long"
}
}
},
"data": [
{
"ts": "2016-04-26T16:20:00.000Z",
"f":{
"0":{"v":85},
"1":{"v":235680}
}
},
{
"ts": "2016-04-26T16:30:00.000Z",
"f":{
"0":{"v":86},
"1":{"v":235685}
}
}
]
}

Melden Sie sich an, um zu kommentieren.

Antworten (2)

Jason Winters
Jason Winters am 10 Aug. 2018
It is possible to send formatted JSON to a channel field through MQTT. Make sure you publish to the field (not the feed) using a topic format like this ( check out the documentation ):
channels/12345/publish/fields/field1/ABCDEFGHIJKLMNOP
Two things to note:
1. The maximum number of character a field can hold is 255. So only the first 255 characters will be recorded.
2. Special and other non-printable characters may get either ignored or encoded.
  2 Kommentare
Simo Rounela
Simo Rounela am 23 Aug. 2018
Thanks, but the actual problem is that there is only one topic address in my datalogger. So it will push all the values to one topic and therefore I cannot use field topic address - because the message includes values from multiple fields.
Jason Winters
Jason Winters am 23 Aug. 2018
I understand, now. When updating a channel feed via MQTT, ThingSpeak is expecting a very specifically formatted JSON message. Publishing the JSON messages straight to ThingSpeak MQTT isn't going to work.

Melden Sie sich an, um zu kommentieren.


Mazlum Serkan Ürkmez
Mazlum Serkan Ürkmez am 11 Feb. 2023
I want to ask a very basic question on the same issue.
MQTT publishing Payload format of the client in my IoT device is JSON. It is able to publish only a single property like {"temperature":25} . This {"name":<value>} JSON format can not be changed on my device. So it can not be converted to a plain text something like field1=25(to a channel feed) or 25.(to a channel field feed) . How to publish JSON payload like from my device to Thindspeak broker in MQTT?
I tried converting the "name" to "field1" in my device's payload and published to channel feed, there is no communication error but Thingspeak doesn't accept as a valid data, no visualisation on dashboard;
Topic: channels/<channelID>/publish
Payload: {"field1":25}
Or I tried with the "temperature" in my devices payload and published to channel field feed, it doesn't work, either;
Topic: channels/<channelID>/publish/fields/field1
Payload: {"temperature":25}
Instead of my Device, When I tried one of those blow with a PC MQTT client sofware MQTTBox, yes, works to my channel!
Topic: channels/<channelID>/publish
Payload: field1=25
Topic: channels/<channelID>/publish/fields/field1
Payload: 25
How to work with JSON format ?
Notes:
  1. Can I use my write API KEY in MQTT publish ? I always tried to add KEY at the end of the topics line like channels/<channelID>/publish/<writeAPIKEY> . When published to this topic, then a connection error occurs. I can't see documentation how using API keys in MQTT API . Is it related with my JSON issue ?
  2. Depends on My Channel is private or public ?

Communitys

Weitere Antworten in  ThingSpeak Community

Kategorien

Mehr zu Read Data from Channel finden Sie in Help Center und File Exchange

Produkte

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by