MQTT publish to Thingspeak with a JSON payload

34 visualizaciones (últimos 30 días)
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:
  1. 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 ?
  2. My Channel is private.

Respuesta aceptada

Christopher Stapels
Christopher Stapels el 15 de Feb. de 2023
Two ways to automatically trigger code:
React (on Data insert)
TimeControl (group data processed since last job)

Más respuestas (2)

Christopher Stapels
Christopher Stapels el 13 de Feb. de 2023
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.

Mazlum Serkan Ürkmez
Mazlum Serkan Ürkmez el 14 de Feb. de 2023
Editada: Christopher Stapels el 14 de Feb. de 2023
@Christopher Stapels Thank you for the response.
following your answer ; This code works; read data from field1 of read channel, analyse, write data to field1 of write channel.
Device published to MQTT with a JSON payload.
Topic: channels/<readChannelID>/publish/fields/field1
Payload: {"temperature":30}
readChannelID = [<readChannelID>];
writeChannelID = [<writeChannelID>];
writeAPIKey = '<writeAPIKey>';
%% Read data from field1 of read channel as outputting a Time Table.
data = thingSpeakRead(readChannelID, 'Fields', [1], OutputFormat='TimeTable');
disp(data);
%% Get the value in the 1. row as an array.
data = data.(1);
disp(data);
%% Remove unwanted characters
data = replace(data, '"temperature":', '');
data = replace(data, '{', '');
data = replace(data, '}', '');
data = replace(data, '{', '');
data = replace(data, '}', '');
%% Get the 1. and the only index of array.
data = data{1};
%% Convert to numeric value
data = str2num(data);
disp(data);
%% Write final value to field1 of write channel.
thingSpeakWrite(writeChannelID, data, 'Fields', [1], 'WriteKey', writeAPIKey);
Dispaly Output:
Timestamps Scaklk
____________________ ______________________
14-Feb-2023 20:42:39 {'{"temperature":30}'}
{'{"temperature":30}'}
30
How to automate Matlab code run for every new data published by device?

Categorías

Más información sobre Write Data to Channel en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by