Write a table using thingSpeakWrite

Juan Manuel Romero Arguello on 23 May 2022 (Edited on 23 May 2022)
Latest activity Reply by Juan Manuel Romero Arguello on 24 May 2022

I am trying to write a table to a TS channel using thingSpeakWrite. The code below results in error but the help documentation indicates that it can be done. Any suggestion on how to fix this?
Function to call:
thingSpeakWrite(channelID,ship1(1,:),'WriteKey',writeAPIKey);
The complete code:
%Initialize table to be written to ThingSpeak
varNames = ["MMSI", "LAT", "LONG", "SOG", "VesselName", "VesselType", "Length", "Width"];
varTypes = ["double", "double", "double", "double", "categorical", "double", "double", "double"];
ship1 = table('Size',[20 8],'VariableTypes',varTypes,'VariableNames',varNames);
ship1.MMSI(:) = 121212;
ship1.SOG(:) = 10;
ship1(:,5) = {'PRIMODY'};
ship1(:,6) = {37};
ship1.Length(:) = 20;
ship1.Width(:) = 12;
thingSpeakWrite(channelID,ship1(1,:),'WriteKey',writeAPIKey);
Christopher Stapels
Christopher Stapels on 23 May 2022

You need to specify times.

Juan Manuel Romero Arguello
Juan Manuel Romero Arguello on 24 May 2022
Adding time stamps won´t solve the issue. The code: thingSpeakWrite(channelID,ships(1,:),'WriteKey',writeAPIKey,'TimeStamp', datetime('now')); results in the error: Data must be a 1-D cell array.
I think it has to do with the fact that the table has mixed data types: double and categorical. Is it possible to write the whole table in that case?
Christopher Stapels
Christopher Stapels on 24 May 2022 (Edited on 24 May 2022)
The size of your timestamp array needs to match the long dimension of ships(1,:). Right now, you have only one timestamp.
Every feed entry in ThingSpeak needs a unique timestamp. When you send a table of data, ThingSpeak needs a seperate time for each feed entry. See the documentation for thingSpeakWrite(), especially the entry on writing a matrix of data and a timetable. I prefer timetables in ThingSpeak, since they natively have the time column, but with a table, you can also provide the timestamps seperately, as in the matrix example.
I reccomend you write the data to a tiemtable first, then write to ThingSpeak.
The categorical array may also be a problem, but ThingSpeak usually converts everything to a string first.
Christopher Stapels
Christopher Stapels on 24 May 2022 (Edited on 24 May 2022)
I see, you only want to write one entry. Sorry about that. Ill see if I can get it to work.
Christopher Stapels
Christopher Stapels on 24 May 2022 (Edited on 24 May 2022)
You seem to be correct that it is the cartegorical type causing a problem.
Here is a workaround.
newTable=ship1(1,:);
newTable.VesselName=string(newTable.VesselName);
thingSpeakWrite(1587643, newTable.Variables, 'timestamp', datetime('now'));
That way you can preserve the categorical type in your original ships table.
Note you changed ship1 to ships in the second comment I think.
Juan Manuel Romero Arguello
Juan Manuel Romero Arguello on 24 May 2022
This works, thanks! I simply want to add that the syntax newTable.Variables must be used. Otherwise, the error Data must be a 1-D cell array persists. Also that for a single entry write, the time stamp could be ommited.

Tags

No tags entered yet.