The ThingSpeak IoT service now supports MQTT subscriptions to receive instant updates when a ThingSpeak channel gets updated. MQTT is a powerful standard for IoT systems. ThingSpeak enables clients to update and receive updates from channel feeds via the ThingSpeak MQTT broker. MQTT is a publish/subscribe communication protocol that uses TCP/IP sockets or WebSockets. MQTT over WebSockets can be secured with SSL. A client device connects to the MQTT broker and can publish to a channel or subscribe to updates from that channel.
MQTT in MATLAB
We also published a new File Exchange submission that allows you to publish and subscribe using MQTT within MATLAB. Download and install MQTT in MATLAB to be able to connect to ThingSpeak’s MQTT server or connect to other standard MQTT brokers such as AWS IoT. Using this Add-On in MATLAB allows you to define custom functions to evaluate on receiving messages streaming over subscribed topics.
ThingSpeak MQTT Examples
- MQTT Basics – Learn about the publish/subscribe architecture for connecting bandwidth and power-constrained devices over wireless networks.
- Choose Between REST API and MQTT API – Learn when to use REST and MQTT to update a channel.
- Publish to a Channel Using Desktop MQTT Client – Publish to a channel using desktop MQTT client such as MQTT.fx.
- Publish to a Channel Using Arduino Client – Publish data to a ThingSpeak channel using an Arduino® device.
- Publish Using Particle Photon Client – Publish data to a ThingSpeak channel using a Particle Photon device.
- Publish Using WebSockets in Python on a Raspberry Pi – Publish data using WebSockets to a ThingSpeak channel.
- Subscribe to Channel Updates Using Desktop MQTT Client – Subscribe to channel updates from a ThingSpeak channel using desktop MQTT.
- Subscribe to Channel Updates Using Particle Photon Client – Subscribe to a ThingSpeak channel using a Particle Photon device.
View our ThingSpeak MQTT documentation to learn more about MQTT support on ThingSpeak, and find examples for Arduino, Particle, and Raspberry Pi.
Naman Chauhan from SRM University created a proof-of-concept project that measures your resting heart rate and sends the data for analysis via a $5 Wi-Fi device. The project is fully documented with the source code on either Hackaday or Hackster.
Naman uses an Arduino for processing the heartbeat data and turns the data into heartbeats per minute. Then, periodically, the device sends the data to ThingSpeak for data storage and data analysis using MATLAB. The heart rate monitor is connected to the internet using DFROBOT’s ESP8266 Wi-Fi Bee. The Wi-Fi Bee turns serial data-to-Wi-Fi.
This heart rate monitor sensor is a pulse sensor which is developed based on PPG techniques. This is a simple and low-cost optical technique that can be used to detect blood volume changing in the microvascular bed of tissues. It is relatively easy to detect the pulsatile component of the cardiac cycle according to this theory.
Tides go up and down. But, the question is when and how will the tide levels change in the future. If you are planning a boating trip or trying to understand how the wind affects tide levels during storms, you want to predict the tide levels using data that you have collected locally. In a tutorial published on Hackster.io, you will be able to learn how to use ThingSpeak to collect sensor data that represents the tide height at a given time, use MATLAB to preprocess the data, use MATLAB to predict future tide levels, and use ThingSpeak to send alerts. Here’s what the system looks like installed at a dock in Cape Cod.
The tide height is calculated using an ultrasonic level sensor. This measurement is taken periodically and then sent to ThingSpeak, an IoT analytics cloud platform by MathWorks, using a cellular modem. The system can easily be adapted to collect data about any environmental system such as greenhouses or oyster farms.
Once you have the data in a ThingSpeak channel, you use MATLAB to preprocess and clean up the data. The raw data some times has extraneous values caused by environmental factors such as lighting, cabling, and electrical interference. Sometimes, you have missing data caused by connectivity issues. It is important to clean up the data before you use the data in your analysis.
To predict future tide levels and send alerts when the tide is rising or falling, we use the MATLAB Analysis app on ThingSpeak. With MATLAB, we can use historical data to make a prediction about the future tide levels. This predicted tide level can be used to help schedule a boating trip or plan for a water surge after a storm.
Remembering to check the tide level when fishing or lazing on the beach is not particularly convenient. A much more useful approach is to have the system send a message when the time has come to pack up and start heading back to the dock. The timing of the alert depends on how much water depth is needed by a particular boat. Larger boats need higher water levels in order to move without getting stuck in the mud. One way to send alerts is to use ThingSpeak and MATLAB to detect changes in tidal height and send alerts.
Developing a tide monitoring system provided accurate tide level measurement and tide level prediction, with the added ability to send alerts. Robert has been able to avoid being stuck in the bay by providing enough time to get back to his dock using this system. This project also serves as a useful approach to solving many data-driven puzzles by having a reliable way to collect, analyze, and act on data. Using MATLAB, the accuracy of the tide levels improved by understanding the proper tide levels at a specific location and when the tide levels will change. If you used the general tide forecast, you would have to account for several inches of tide height difference.
- Developing an IoT Analytics System with MATLAB, Machine Learning, and ThingSpeak – MathWorks Technical Article by Robert Mawrey
- Measure and Analyze Tide Levels with ThingSpeak and MATLAB – Hackster tutorial
- TideAlerts.com – Robert’s website dedicated to the exploration of tides
An emerging topic with IoT is Digital Twin (DT).
The digital twin is a federation of data and models that can be analyzed or put into a simulation to create useful information about the past, present, or future of the DT’s physical twin.
Bruce Sinclair of the Iot-Inc. Business Show podcast invite Jim Tung, a MathWorks fellow, to discuss models, simulation, and digital twins. Jim shares information about a few MathWorks customer use cases and our products used for modeling, simulation, and IoT.
Bruce and Jim talk about many interesting and key topics for IoT system development, including:
- The difference between data-driven models and functional models
- Using raw data to calibrate, update and validate functional models
- System level modeling
- The importance model hierarchy to discover insights
- A smart grid digital twin example
- The role of deep learning in digital twin modeling
- The actionable steps of how to create your digital twin
Many IoT projects collect data from a sensor and send the data to ThingSpeak at the same time over and over. To continuously collect and send data to the cloud requires the device to be powered and connected all of the time. A battery-powered IoT device like a Particle Photon or Onion Omega2 would run out of power quickly. There are many IoT applications where you want your IoT device to collect the data offline over a long period of time, then send the data all at once to ThingSpeak for analysis.
The ThingSpeak team at MathWorks is excited to announce Bulk-Update! This new ThingSpeak feature is targeted at IoT devices trying to optimize battery use by allowing the device to update a lot of data at once. To help you get started with bulk-update, we have written examples for Arduino, ESP8266, Particle Photon, and the Raspberry Pi 3.
Once your data is on ThingSpeak, it is easy to analyze using the MATLAB Analysis and Visualization apps within ThingSpeak, MATLAB Online, or Desktop MATLAB. To read data from ThingSpeak into MATLAB, use the ThingSpeak Support Toolbox and the thingSpeakRead command. We have released documentation and examples to help you get started with bulk-update on ThingSpeak.
Resources for Bulk-Update
- ThingSpeak Documentation for Bulk-Update
- Continuously Collect Data and Bulk-Update a ThingSpeak Channel Using an Arduino MKR1000 Board or an ESP8266 Board
- Continuously Collect Data and Bulk-Update a ThingSpeak Channel Using a Particle Photon Board
- Continuously Collect Data and Bulk-Update a ThingSpeak Channel Using a Raspberry Pi Board
- MATLAB thingSpeakRead Documentation
- ThingSpeak Support Toolbox for MATLAB
- MATLAB Online