MQTT publish to Thingspeak with a JSON payload
12 次查看(过去 30 天)
显示 更早的评论
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 or 25 . How to publish JSON payload like from my device to Thindspeak ?
I tried converting the "name" to "field1" in my device's payload and published to channel feed, 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 with a PC MQTT client sofware, 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:
- I can't 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 that a connection error occurs. I can't see documentation how using API keys in MQTT API . Is it related with my JSON issue ?
- My Channel is private.
0 个评论
采纳的回答
Christopher Stapels
2023-2-15
Two ways to automatically trigger code:
React (on Data insert)
TimeControl (group data processed since last job)
0 个评论
更多回答(2 个)
Christopher Stapels
2023-2-13
You are correct that ThingSpeak requires you to have the specific format fieldx= in your payload. You can see all the correct parameters in the MQTT API documentation.
Could you consider using the REST API?
Publishing to a single field might make it possible. Here the payload is taken completely without using fieldx=1. However, ThingSpeak might take the field value as the whole string i.e.'"temperature":25'. Then the field plots wouldnt work automatically, But you could then write a MATLAB script (in ThingSpeak) that will strip away the word "temperature" and then plot the values.
The present version of the MQTT API does not use the API key in the topic or payload. Do not add an API key, the connection will be refused. I assume that since you were successful with the desktop version that your device is set up properly.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Write Data to Channel 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!