Hans on IoT

ThingSpeak, MATLAB, and the Internet of Things

Send Email Alerts from ThingSpeak

For this post, I would like to introduce Christopher Stapels as our guest blogger. Christopher builds lots of cool IoT projects and is omnipresent on MATLAB Central and ThingSpeak and MATLAB questions. He is also the product marketing manager for ThingSpeak and MATLAB hardware here at MathWorks. Welcome to the team!

ThingSpeak has added an alerts API to let you to send emails from your ThingSpeak account. Let me say that again… ThingSpeak now offers email alerts!

Use the new alerts API key to trigger emails and check the sent email history using MATLAB code or another HTTP client.  With MATLAB code in ThingSpeak, you can read and analyze channel values and then respond with an email.  You can even add information in the email about the condition that triggered the email.

My Thirsty Plant

Until recently, you needed a third-party service to get email notifications sent about your ThingSpeak channels.  New in 2020, you can now get email alerts sent to you directly from ThingSpeak.

Regular visitors to the public channel 276330 will be familiar with my desk plant.  I have a soil monitor probe set up to measure the conductivity of the soul, and my plant will send me email if it needs refreshment. I can now have the email set directly from ThingSpeak.  The easiest way is to use a MATLAB analysis.  Here is the general format for sending an email.

Make a MATLAB Analysis to Send Email

  1. First get your ThingSpeak Alerts API key from the Account > My Profile The alerts API key will start with ‘TAK’.
  2. Create a new MATLAB analysis at Apps > MATLAB Analysis. Click the New button on the top. Choose the blank template and with this code.
    alert_body = 'This is the text that will be emailed';
    alert_subject = 'This will be the subject of the email';
    alert_api_key = 'YOUR_API_KEY_FROM_STEP_1';
    alert_url= "https://api.thingspeak.com/alerts/send";
    jsonmessage = sprintf(['{"subject": "%s", "body": "%s"}'], alert_subject,alert_body);
    options = weboptions("HeaderFields", {'Thingspeak-Alerts-API-Key', alert_api_key; 'Content-Type','application/json'});
    result = webwrite(alert_url, jsonmessage, options);
  3. Adjust the alert body, subject and API key.
  4. Click save and Run and you will receive an email within a few moments.
  5. Connect the MATLAB Analysis to a TimeControl or React that depends an a channel value you are interested in.

You can see a more detailed example at Analyze Channel Data to Send Email Notification.  There is even a new template for email alerts in the MATLAB analysis app. You can see all the information on how to send an alert or get your alerts history in the documentation.

Let us know what you think!

|

Comments

To leave a comment, please click here to sign in to your MathWorks Account or create a new one.