main content

analyze channel data to send email notification -凯发k8网页登录

this example shows how to use the thingspeak alerts service to send notifications as email messages. use the timecontrol app to trigger a matlab analysis app at regular intervals. the matlab analysis app analyzes the data to decide the appropriate email message to generate based on soil moisture data. using the analytical power of matlab, you can generate filtered, targeted, and specific notifications of channel activity from thingspeak data.

channel logs a soil moisture measurement from an office plant. in this example, you usethingspeak alerts to receive an email notification with the last soil sensor value when the plant needs water. see to learn how to set up a soil moisture monitor that records your data on thingspeak.

create a matlab analysis

analyze thingspeak data with matlab. you can use the result of your analysis to trigger web requests, such as a request for email from thingspeak alerts. this analysis reads four weeks of data to calculate a threshold based on historical data. a measurement lower than 10% of the range of data changes the output message.

1) select apps > matlab analysis and select new.

2) select read channel to trigger email in the examples section. the code below is prepopulated in your matlab analysis window.

3) name your analysis and modify the code. change alertapikey to match your alerts api key. to read from your own public channel, change the channelid value. start by setting the channel id and alerts key. all alerts api keys start with tak.

channelid = 276330;
alertapikey = 'takxxxxxxxxxxxxx';

4) set the url and header. the alerts service requires a thingspeak-alerts-api-key header. use weboptions to set the header.

alerturl = "https://api.thingspeak.com/alerts/send";
options = weboptions("headerfields", ["thingspeak-alerts-api-key", alertapikey ]);
alertsubject = sprintf("plant soil information");

5) read the recent data using thingspeakread.

moisturedata = thingspeakread(channelid,'numdays',30,'fields',1);

6) make sure that there is data read from the channel and set the message accordingly. calculate a 10% threshold value from the span of the data. use the most recent value to set the alert body message.

if isempty(moisturedata)
      alertbody = ' no data read from plant. ';
      
 else
    % calculate a 10% threshold value based on recent data.
    span = max(moisturedata) - min(moisturedata);
    dryvalue = 0.1 * span   min(moisturedata);
    % get the most recent point in the array of moisture data.
    lastvalue = moisturedata(end); 
    % set the outgoing message
    if (lastvalue' i need water! ';
    end
    if (lastvalue>dryvalue)
        alertbody = ' no water needed. ';
    end
end

7) user webwrite to send the alert request. wrap the send request in a try/catch to prevent the matlab analysis from being disbled if the request fails for any reason.

try
    webwrite(alerturl , "body", alertbody, "subject", alertsubject, options);
    catch someexception
    fprintf("failed to send alert: %s\n", someexception.message);
end

create a time control to run your analysis

the timecontrol app can evaluate your thingspeak channel data and trigger other events. create an instance of the timecontrol app that calls your matlab analysis code every day. select apps > timecontrol, and then click new timecontrol.

  • name — name the timecontrol.

  • frequency — select recurring.

  • recurrence — select day.

  • action — select matlab analysis. in the code to execute list, select the name of the matlab analysis you wrote previously.

each time the timecontrol app runs, you receive an email letting you know if the plant needs water. the 10% threshold is only an estimate; thingspeak assumes no responsibility for your plant.

note: this configuration in this example consumes one email alert each day. your total number of alerts is limited; if you exceed the limit, you can no longer trigger new email alerts.

see also

| (matlab) | |

related topics

    external websites

      网站地图