ThingSpeak IoT Blog

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!

Collect and Visualize Agricultural Data using The Things Network and ThingSpeak


Long-range wireless communication technology enables the transfer of sensor data over a long distance while using low-power radios for connectivity. This technology can be leveraged to connect sensors covering a large geographic area and give you insights into what is happening. With agricultural applications it is important to measure the soil moisture and efficiently irrigate. A big challenge for agricultural applications is robust connectivity in remote locations. By using a combination of The Things Network and ThingSpeak insightful applications can be built. The Things Network is a protocol and infrastructure that provides a link to cloud applications using LoRaWAN® technology. If you are already a The Things Network user, check out the documentation about the ThingSpeak integration at The Things Network. ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud using MATLAB®. You can send data to ThingSpeak from devices via The Things Network, create instant visualization of live data, and send alerts.

The ThingSpeak team has created a new example that shows you how to leverage The Things Network and build an agricultural data application using ThingSpeak. The sensors send data to The Things Network, which is then forwarded to ThingSpeak for collection, analysis, and visualization. Here’s what the project view on ThingSpeak looks like.

To build a soil moisture sensor device for The Things Network, you need use an Adafruit Feather M0 RFM95 LoRa Radio (900MHz), an Adafruit Ultimate GPS FeatherWing, a SparkFun Soil Moisture Sensor, and a DHT22 temperature and humidity sensor. Once you have the device put together and programmed, you can use this device to measure soil moisture, temperature, humidity, and its location.

Check out the full Collect Agricultural Data over The Things Network example at the MathWorks Documentation site.

Analyze and Visualize Air Quality Data with MATLAB


Have you ever wondered if the air around you is healthy? It is possible to understand air quality by using MATLAB to analyze air quality data collected by an air quality sensor on  ThingSpeak.

What is “healthy” air quality?

Good or moderate air quality is when the Air Quality Index (AQI) is 100 or less. AQI is a relative measurement of five common air pollutants: ground-level ozone, particle pollution, carbon monoxide, sulfur dioxide, and nitrogen dioxide. A high AQI indicates a higher level of pollution and is considered unhealthy over 100.

Air Quality Sensors

We have installed a PurpleAir sensor at the MathWorks Apple Hill campus in Natick, MA. PurpleAir sensors use laser particle counters that provide an accurate and low-cost way to measure smoke, dust, and other particulate in the air. The data from our air quality sensor is available on a public ThingSpeak channel.

PurpleAir produces an interactive map using the air quality data from their sensors deployed around the world. Among other applications, the data is used by researchers to understand the effects of forest fires on air quality.

Air Quality Analysis using MATLAB

On File Exchange, we have posted our MATLAB functions used to analyze the air quality data collected by ThingSpeak. The MATLAB code helps preprocess the sensor data, provides functions to classify the data, and provides functions for visualizing the processed air quality data. The MATLAB visualizations are added to a ThingSpeak channel dashboard so you can see the current air quality near you. The MATLAB analysis code calculates the AQI using the definitions from the United States Environmental Protection Agency (EPA).

You can build a data analysis dashboard on ThingSpeak using publicly available data and MATLAB. This project it makes it easy to explore IoT data analysis using MATLAB to preprocess and visualize data without having IoT hardware.

Download the code from File Exchange and follow along with our video tutorial.

Predictive Maintenance: From Development to IoT Deployment


Interest in predictive maintenance is increasing as more and more companies see it as a key application for data analytics that leverages IoT systems. Branko Dijkstra, a technical consultant at MathWorks, held a talk at the MATLAB Conference and covers the development of predictive maintenance algorithms, as well as their deployment on the two main nodes of IoT systems—the edge and the cloud.

What do you expect from predictive maintenance?

  • Maintenance cares about day-to-day operations – Reduced downtime
  • Operations and IT look at the bigger picture – Improved operating efficiency
  • Engineering groups get product feedback – Better customer experience
  • Upper management wants to drive growth – New revenue streams

Why MATLAB and Simulink for predictive maintenance?

  • Get started quickly
  • Reduce the amount of data you need to store and transmit
  • Deliver the results of your analytics based on your audience
  • Create training data for your algorithm in the absence of real failure data

You can watch Branko’s full talk and download related resources from MathWorks Videos and Webinars.

 

ThingSpeak Library for Arduino


The ThingSpeak team has released an updated version of the ThingSpeak Communication Library for Arduino, ESP8266, and ESP32 devices. The ThingSpeak library is the easiest way to get Arduino devices connected to ThingSpeak IoT services. The library now features a way to separate secret credentials from your main source code. This helps when you want to share your code but don’t want to share your WiFi credentials and ThingSpeak keys.

Installation

The Arduino IDE needs to have the ThingSpeak library installed in order for your devices to know how to send data to ThingSpeak. In the Arduino IDE, choose Sketch, Include Library, and Manage Libraries. Search for “thingspeak” and click Install.

Device Support

The ThingSpeak Communication Library supports many devices. Using the library makes the experience the same for each board type. When you learn one way to work with ThingSpeak, you will be to work with other devices in the same way.

  • Arduino or compatible using a WiFi Shield
  • Arduino or compatible using a WiFi Shield 101
  • Arduino or compatible using an Ethernet Shield
  • Arduino or compatible using a MKR ETH Shield
  • Arduino MKR1000 (use the WiFi101 library version 0.13.0 or older. WiFi101 library versions 0.14.0 and newer have a bug that stops this ThingSpeak library from working properly)
  • Arduino MKR1010
  • Arduino VIDOR 4000
  • Arduino GSM 14000
  • Arduino Yún (Rev1 and Rev2)
  • ESP8266 (tested with SparkFun ESP8266 Thing – Dev Board and NodeMCU 1.0 module)
  • ESP32 (tested with SparkFun ESP32 Thing)

Each supported device includes three ThingSpeak examples.

  • ReadField: Reading from a public channel and a private channel on ThingSpeak
  • WriteSingleField: Writing a sensor value to a single field on ThingSpeak
  • WriteMultipleFields: Writing sensor values to multiple fields and status in one transaction with ThingSpeak

Request Status Codes

To get the best compatibility with ThingSpeak IoT services, we recommend using the ThingSpeak library. The library has extra features that help you troubleshoot and get visibility into any issues with requests to ThingSpeak. I find it useful to store the last status code from ThingSpeak. I can use this code to understand if the request succeeded or failed. Here’s an example of how to use the “getLastReadStatus” method.

 // Read in field 1 of the private channel which is a counter  
 long fieldValue = ThingSpeak.readLongField(myChannelNumber, myFieldNumber, myThingSpeakReadAPIKey);  

  // Check the status of the read operation to see if it was successful
 statusCode = ThingSpeak.getLastReadStatus();
 if(statusCode == 200) {
   Serial.println("Field Value: " + String(fieldValue));
 }
 else {
   Serial.println("Problem reading channel. HTTP error code " + String(statusCode)); 
 }

The code behind the ThingSpeak library is available on GitHub. Discover other MathWorks Open Source and Community Projects on The MathWorks GitHub page.