· 5 years ago · May 14, 2020, 07:32 PM
1% Enter your MATLAB Code below
2
3
4% Store the channel ID for the C02 sensor channel.
5channelID = 1045676;
6
7% Provide the ThingSpeak alerts API key. All alerts API keys start with TAK.
8alertApiKey = 'TAKZV1S0M3PO7WLEB';
9
10% Set the address for the HTTTP call
11alertUrl="https://api.thingspeak.com/alerts/send";
12
13% webwrite uses weboptions to add required headers. Alerts needs a ThingSpeak-Alerts-API-Key header.
14options = weboptions("HeaderFields", ["ThingSpeak-Alerts-API-Key", alertApiKey ]);
15
16% Set the email subject.
17alertSubject = sprintf("[WeatherStation]Emergency! Danger near weather station");
18
19% Read the recent data.
20co2 = thingSpeakRead(channelID,'NumDays',10,'Fields',3);
21tvoc = thingSpeakRead(channelID,'NumDays',10,'Fields',4);
22temperature = thingSpeakRead(channelID,'NumDays',10,'Fields',1);
23humidity = thingSpeakRead(channelID,'NumDays',10,'Fields',2);
24
25alertBody = '';
26
27% Check to make sure the data was read correctly from the channel.
28if isempty(co2)
29 alertBody = ' No data available ';
30else
31
32 span = max(temperature) - min(temperature);
33 fireValue = 0.1 * span + max(temperature);
34 % tre sa modific dupa ce testez cu fum+feon
35
36 % Get the most recent point in the array of CO2 data.
37 lastValueCO2 = co2(end);
38 lastValueTVOC = tvoc(end);
39 lastValueTemperature = temperature(end);
40 lastValueHumidity = humidity(end);
41
42 if ((lastValueCO2) == 7992 && (lastValueTVOC == 1156))
43 if(lastValueTemperature > 30.0)
44 alertBody = 'Fire'
45 else
46 alertBody = 'Dangerous substance'
47 end
48 end
49end
50
51if(isempty(alertBody) == 0)
52 % Catch errors so the MATLAB code does not disable a TimeControl if it fails
53 try
54 webwrite(alertUrl , "body", alertBody, "subject", alertSubject, options);
55 catch someException
56 fprintf("Failed to send alert: %s\n", someException.message);
57 end
58end