How to send matrices to ThingSpeak?

2 views (last 30 days)
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

James Holland
James Holland on 17 Jul 2018
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
James Holland
James Holland on 17 Jul 2018
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.
Héctor Cruz Guardo
Héctor Cruz Guardo on 11 Oct 2019
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!