· 5 years ago · May 14, 2020, 07:24 PM
1% Store the channel ID for the C02 sensor channel.
2channelID = 1045676;
3
4% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
5alertApiKey = 'TAKZV1S0M3PO7WLEB';
6
7% Set the address for the HTTTP call
8alertUrl="https://api.thingspeak.com/alerts/send";
9
10% webwrite uses weboptions to add required headers. Alerts needs a ThingSpeak-Alerts-API-Key header.
11options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]);
12
13% Set the email subject.
14alertSubject = sprintf("[WeatherStation]CCS811 Stopped Transmitting Data");
15
16% Read the recent data.
17ccs811 = thingSpeakRead(channelID,'NumDays',10,'Fields',3);
18
19alertBody = '';
20
21% Check to make sure the data was read correctly from the channel.
22if isempty(ccs811)
23 alertBody = ' No data available ';
24else
25
26 % Get the most recent point in the array of CO2 data.
27 lastValue = ccs811(end);
28 count = 0;
29 % Set the outgoing message
30 if (lastValue == 0)
31 count += 1;
32 end
33
34 if(count == 10)
35 alertBody = ' CCS811 not responding ';
36 count = 0;
37 end
38end
39
40if(isempty(alertBody) == 0)
41 % Catch errors so the MATLAB code does not disable a TimeControl if it fails
42 try
43 webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options);
44 catch someException
45 fprintf("Failed to send alert: %s\n", someException.message);
46 end
47end