How to send matrices to ThingSpeak?

Greetings! My project is to read in data from two arduinos and then separate the data into four matrices. These matrices are comprised of 8 values and are sent to ThingSpeak when filled (to 8). My issue is that I am attempting to send 4 matrices to ThingSpeak via this line :
thingSpeakWrite('ChID',{heartRateInputA,temperatureInputA,heartRateInputB,temperatureInputB},'WriteKey','myWriteKey');
When I execute this, the data goes through but only the first column of the 1-D array. How do I send the whole array? Attached below is the rest of my code.
%Uses two sets of nested 'if' loops and variables and ThingSpeak channels
%to read in and parse data from each controller
a = Bluetooth('HC-05',1);
b = Bluetooth('IN',1);
a.ReadAsyncMode = 'continuous';
b.ReadAsyncMode = 'continuous';
fopen(a); %data collector A
fopen(b); %data collector B
%A data points
heartRateVarA=0;
temperatureVarA=0;
%B data points
heartRateVarB=0;
temperatureVarB=0;
col = 1; %column counter for matrices
heartRateInputA = [];
temperatureInputA = [];
heartRateInputB = [];
temperatureInputB = [];
while (a.Status == 'open')&(b.Status=='open')
%if data has been sent to thingSpeak, reset the column counter
if(col == 15)
col=0;
end
col = col+1;
for i=1 : 1 : 2
matchA = fgets(a);
matchB = fgets(b);
testA = contains(matchA,'Celsius');
testB = contains(matchB,'Celsius');
%Test A parsing
if (testA==1)
temperatureVarA=matchA;
else
testA=contains(matchA,'BPM');
if(testA==1)
heartRateVarA=matchA;
end
end
%Test B parsing
if (testB==1)
temperatureVarB=matchB;
else
testB=contains(matchB,'BPM');
if(testB==1)
heartRateVarB=matchB;
end
end
end
%Display read and parsed in data
fprintf('Device 1 : \n\n');disp(col);
fprintf('Heart Rate A : '); disp(heartRateVarA);
fprintf('Temperature A : '); disp(temperatureVarA);
fprintf('Device 2 : \n\n');
fprintf('Heart Rate B : '); disp(heartRateVarB);
fprintf('Temperature B : '); disp(temperatureVarB);
%Removes non-numeric characters from data and sends to respective
%matrix
hInputA = str2double(strrep(heartRateVarA,'BPM: ',''));
tInputA = str2double(strrep(temperatureVarA,'Celsius: ',''));
hInputB = str2double(strrep(heartRateVarA,'BPM: ',''));
tInputB = str2double(strrep(temperatureVarB,'Celsius: ',''));
heartRateInputA(1,col) = hInputA;
temperatureInputA(1,col) = tInputA;
heartRateInputB(1,col) = hInputB;
temperatureInputB(1,col) = tInputB;
%create arbitrary time stamps of same size as data points to send data to
%thingspeak
stamps = [datetime('now')-minutes(length(matchA)-1):minutes(1):datetime('now')]';
pause(2); %pauses for 2 seconds to coincide with arduinos
if(col==8)
col=1;
thingSpeakWrite(501358,{heartRateInputA,temperatureInputA,heartRateInputB,temperatureInputB},'WriteKey','JLS6DXUINWFGI6QD');
end
end
Thanks in advanced.

 Accepted Answer

I was unable to find a way to send an entire matrix into a single ThingSpeak field but found a different method. Refer to my last comment for the method I used.
https://www.mathworks.com/matlabcentral/answers/410613-how-to-send-matrices-to-thingspeak#comment_590228

More Answers (1)

Image Analyst
Image Analyst on 16 Jul 2018
Edited: Image Analyst on 16 Jul 2018
You're sending in a cell array since you have braces around your arrays, but since you say it's not working, maybe try converting it to a table variable and sending that in instead.

9 Comments

Thank you, I tried this method but had to combine both heartRateInputA and temperatureInputA into one variable - device1 - and then use cell2table to create a table. When I tried to send this new, 2-by-8 table to field 1 of my ThingSpeak channel, it said :
Number of columns in 'Values' must be equal to the number of fields specified.
Any idea how to solve this?
Not without seeing your cell2table command.
Of course, here is what I attempted :
>> device(1,1:8) = heartRateInputA
device =
0 69 71 56 129 144 113 109
>> device(2,1:8) = temperatureInputA
device =
0 69 71 56 129 144 113 109
0 2381 2405 2377 2377 2431 2394 2397
>> device1Table = cell2table(num2cell(device))
device1Table =
2×8 table
Var1 Var2 Var3 Var4 Var5 Var6 Var7 Var8
____ ____ ____ ____ ____ ____ ____ ____
0 69 71 56 129 144 113 109
0 2381 2405 2377 2377 2431 2394 2397
>> thingSpeakWrite('channelID','Fields',1,'Values',device1Table,'WriteKey','myWriteKey');
Error using thingSpeakWrite
Number of columns in 'Values' must be equal to the number of fields specified.
Well of course you need to actually put the actual numerical channel ID (501358) in there instead of the string 'channelID'. Did you do that?
Yes I have the actual ID, write key, and such in the fields, just left it out for privacy.
Currently, the data goes to ThingSpeak as long as I don't specify a field on the channel. This means that each element of the array is sent to an individual field. for example :
heartRate = 0 69 56 77;
thingSpeakWrite(channel,heartRate);
thingSpeakRead(channel,'Field',1);
ans = 0
thingSpeakRead(channel,'Field',2);
ans = 69
And so on so forth. How can I place all the data from a matrix into one field on the channel?
I just got my devices and haven't yet set up my channel so you're a bit ahead of me. Maybe you can ask Hans Scharler at the Mathworks. That's who I've been talking to and he's very helpful.
Thank you! Fortunately, it seems that I have found another method. Since MATLAB was able to send 8 data points to each channel, I just created a channel for each sensor. To display the data neatly, I created a MATLAB visualization to plot the pairs for their corresponding device as such:
hrB = thingSpeakRead(541121,'ReadKey','readKey'); %heart rate from device B
tempB = thingSpeakRead(541123,'ReadKey','readKey'); %temperature data from device B
%create line plot for deviceB
y3 = hrB;
y4 = (tempB/100);
x = (0.1:length(hrB));
thingSpeakPlotYY(x,y3,x,y4,'Color1','red','Color2','blue','YLabel1','Heart Rate','YLabel2','Temperature','Title','Device A');
The above was for Device B but Device A is almost the same.
Hola James Holland, quisiera preguntarte un pequeño problema que manejo con una Tarjeta de Adquisicion de datos DAQ la cual de una entrada me entrega una matriz de salida, como hago para transferir dato por dato obtenido de la matriz a Thingspeak por medio de simulink, gracias

Sign in to comment.

Communities

More Answers in the  ThingSpeak Community

Categories

Find more on ThingSpeak in Help Center and File Exchange

Products

Release

R2017b

Community Treasure Hunt

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

Start Hunting!