Hans on IoT

ThingSpeak, MATLAB, and the Internet of Things

New Feature: ThingSpeak Now Supports Images

Many users have asked, and it’s finally here: Your devices can upload images to ThingSpeak!

With this new feature, you can create a cloud-based tracking or monitoring system for your important assets by taking photos and uploading them to ThingSpeak image channels. Users with a paid ThingSpeak license can create image channels and then embed the output from the image channel onto a channel view using the image widget.

To help you get started, the ThingSpeak documentation includes two examples for uploading images to ThingSpeak: ESP32-CAM camera module and Raspberry Pi-connected camera.

Many ThingSpeak channels represent a particular IoT project. Previously, to show an image in the channel view you had to go to some lengths – including copying from third-party location using MATLAB visualizations or using an existing photo on the web. The ThingSpeak images feature uses your MATLAB Drive space to store images, so they will be available for your channels whenever you need them.

Here is some MATLAB code that will write to an image channel from your computer. This code will help you get an image into ThingSpeak without needing an IoT device. Save an image to your system and name it “myImage.jpg”.

% Import these libraries to use the HTTP interface.  
% They are in base MATLAB, no extra license is required.
import matlab.net.http.*
import matlab.net.http.field.*
import matlab.net.http.io.*

% Edit this section for your files. Timestamps are optional.
channelId = 'X1X1X1X1X1';
channelApiKey = HeaderField('thingspeak-image-channel-api-key', 'ZZZZZZZZZZZZZZZZ');
filename = 'myImage.jpg';
clientTimestamp = '2022-01-29T15:06:35.642Z'; % Optional Timestamp

provider = FileProvider(['./', filename]);
req = RequestMessage(RequestMethod.POST, [channelApiKey], provider);
url = ['https://data.thingspeak.com/channels/', channelId, '/images/', ...
filename, '/', clientTimestamp ];
response = req.send(url)

If everything worked, you should expect a StatusLine of  ‘HTTP/1.1 202 Accepted’ in the response. If you want to see this image on a channel view, follow the steps in the documentation.

Be careful when saving a regular stream of images, they can fill up your drive space fast. Here is MATLAB code to delete a date range of images.

import matlab.net.http.*
import matlab.net.http.field.*
import matlab.net.http.io.*

% Edit this section with your information.
channelId = 'x1x1x1x1x1';
channelApiKey = HeaderField('thingspeak-image-channel-api-key' ...
, 'xxxxxxxxxxxxxxxx');
endDate = datetime('now');
startDate = endDate - days(3);

fmt = 'yyyy-mm-ddThh:MM:ssZ';
startDate = datestr(startDate,fmt);
endDate = datestr(endDate,fmt);

pathRange = sprintf('/images?timestamp=ingest&start=%sZ&end=%s',...

req = RequestMessage(RequestMethod.DELETE, [channelApiKey]);
url = ['https://data.thingspeak.com/channels/', channelId, ...
response = req.send(url)

In both cases, you will get a status code that you can check using the status endpoint.

  • print


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