05
Feb 16

Uber Ride Analysis with ThingSpeak and MATLAB

Have you ever wondered how long it will take to get an Uber at your location? This project uses ThingSpeak to log the ETA for an Uber service based on your latitude and longitude. We will use ThingSpeak’s MATLAB Analysis and TimeControl apps to track the ETA over time.

Uber Ride Estimate

The Uber API allows you to pass a latitude and longitude to determine the estimated time of arrival for an Uber car. The API also allows you to schedule a car. I have made a button that requests an Uber car and also schedules an Uber at the right time.

MATLAB Analysis Code

% Read the ThingHTTP for 'Uber Ride Estimate'
data = webread('https://api.thingspeak.com/apps/thinghttp/send_request?api_key=XXX')

% Convert the response to a number
eta = str2num(data);

% Write the data to the 'Uber Ride Estimate Data' ThingSpeak Channel
thingSpeakWrite(Channel_ID,eta,'WriteKey','XXX');

Each time the MATLAB Analysis code is executed, it will write the estimated time of arrival (ETA) for Uber to your ThingSpeak channel. To track the ETA over time, schedule the MATLAB code with TimeControl. I am running the code every 5 minutes to get an idea of when the peak times are for Uber to pick me up at my office in Natick, MA. Check out the ThingSpeak channel number 840700 to see the estimated times.

Uber_Ride_Estimate_Data

Step-by-step project details are available at Hackster.io.



21
Jan 16

Reacting to Events in Your Data With MATLAB

Chris Hayhurst uses a solar water heater at his house to lower energy costs and use hot water in his house heated up by the sun. Chris is a consulting manager for The MathWorks and partnered with the IoT team to use ThingSpeak to collect data about his system and use ThingSpeak’s built-in MATLAB app to analyze it. In this project, Adarsh and I are going to show you how to send alerts when events are detected in the data by using the MATLAB Analysis app.

Solar water heating system

Chris’ home solar water heating system is an example of an IoT application that uses multiple sensors to collect data about a physical system.  Chris’s water heater measures ambient temperature, stored water temperature, collector temperature, and pump speed. All of this data gets collect by ThingSpeak and stored in Channel 29633.

Solar water heater

On days when the stored water temperature exceeds 50°C (122°F), there’s no need to use other methods to heat the store of water to a useful working temperature.  The pump should turn on only when the collector temperature is greater than the temperature of the stored water tank. If the pump turns itself on when the collector is cooler than the stored water temperature, valuable heat is lost from the stored water tank. Chris wants to be alerted of this condition, so that he can adjust the controller settings and increase the efficiency of the system.

IoT systems like Chris’ solar water heating system, typically gather large amounts of data but often the real interest is in events that occur infrequently. The ability to take action when these infrequent events occur is important and requires a mechanism to detect such an event and launch an action. We are going to use the data collected by the solar water heating system stored in the ThingSpeak Channel 29633 and use the MATLAB Analysis app to detect a condition and alert him using Twitter.

solar-water-heater-inside

MATLAB Event Detection

To detect an erroneous pump behavior event, create a new MATLAB Analysis on ThingSpeak with the following code:

% Read data from fields 1, 2, and 3 from channel 26633.
% Field 1 represents the stored water temperature
% Field 2 represents the collector temperature
% Field 3 represents the state of the pump - on or off
[data, time] = thingSpeakRead(29633, 'Fields', [1, 2, 3]);

% Assign measurements to individual variables
storeTemp = data(1);
collectorTemp = data(2);
pumpState = data(3);

% Check if collectorTemp is less than storeTemp
isCollectorCooler = collectorTemp < storeTemp;

% Identify if pump is on while the collector is cooler.
% We apply a logical AND operation to detect an event only when collector
% is cooler than store temperature and the pump is on.
eventDetected = isCollectorCooler & pumpState

Press the ‘Run & Save’ button to save the MATLAB Analysis App. The code above sets eventDetected to 1 every time the collector temperature is cooler than stored temperature and if the pump is on. Now that we can detect the event, we need to set the MATLAB App to be run on a schedule. To do this, we will setup a TimeControl to run our MATLAB code every 5 minutes.

TimeControl options

Sending Alerts using MATLAB Analysis

So far, we’ve created a MATLAB Analysis to detect events in the data being collected in the solar water heater data channel. We associated our MATLAB Analysis code with a TimeControl to have it run every 5 mins to check for our event of interest. To receive a notification via Twitter when the pump is on incorrectly, we can use MATLAB Analysis to send a Tweet.

First, you need to link your Twitter account to your ThingSpeak account. Then, add the following lines of code at the end of your MATLAB Analysis code to send a Tweet when an event is detected:

If eventDetected
webwrite('http://api.thingspeak.com/apps/thingtweet/1/statuses/update',
'api_key', '<ThingTweet_APIKey>', 'status', 'Alert! Solar pump error!')
end

Be sure to replace <ThingTweet_APIKey> with your ThingTweet API Key.

If the solar water heater pump turns on at the wrong times, you will get a Tweet to let you know!

Next Steps

This example shows you the power of some of the ThingSpeak apps that we make available to you to experiment with. The MATLAB Analysis app is really powerful and can be used to detect events in your data and send alerts. MATLAB Analysis can be used for all sorts of calculations and orchestrations of different web services. We could have also used MATLAB to control the pump.

Feel free to try this example and take it further…

  • Reading data from fields in different channels
  • Combining data from fields in a channel and data read from a website such as a weather station or weather forecast.

What will you MATLAB?



08
Dec 15

Send Messages From Devices to Slack Using ThingSpeak [tutorial]

Slack is a team collaboration tool to make your work life simpler. It is an extremely popular way to receive messages from team members all in one place and integrate with external web services. One possible integration is with ThingSpeak. ThingSpeak is an open data platform for the Internet of Things. Devices all around the world are using ThingSpeak to collect data from sensors and send data to apps and other devices. In the not too distant future, things will be a part of your team. Relevant equipment statues, sensor readings, and updates will inform decisions and will be shared among team members and other Slack services.

Arduino Slack ThingSpeak

By following our tutorial, you will be able to use ThingSpeak to send messages to your team’s Slack channel. This will also allow devices like an Arduino to use Slack since ThingSpeak will take care of authentication and HTTPS.



04
Dec 15

Arduino WiFi 101 ThingSpeak Data Uploader Tutorial

Arduino has published a tutorial for their WiFi 101 Shield that sends data to ThingSpeak. The Arduino WiFi Shield 101 is a powerful Internet of Things shield with crypto-authentication that connects your Arduino or Genuino board to the internet using WiFi.

Arduino WiFi 101 ThingSpeak

You only need a few things to build a light and temperature sensor that writes data to ThingSpeak:

  • Arduino Zero or Uno Board
  • Arduino Wifi Sheild 101
  • Photocell
  • Temperature Sensor (This example uses a TMP36)
  • 10K Ohm Resistor

Arduino_WiFi_1010_ThingSpeak

Once you have the circuit built, you create a ThingSpeak channel, connect the Arduino WiFi 1010 to your Wi-Fi network, and install the source code from the tutorial on the Arduino.

Data is now being sent to your ThingSpeak Channel. Go to your channel to see two charts of the light and temperature data. To take the project a step further, go to ThingSpeak Apps and use MATLAB to analyze and visualize and trigger actions from the data.

[via Arduino.cc]



11
Nov 15

element14 Webinar: How To Use MATLAB and Simulink With ThingSpeak

element14 is hosting a free webinar, “How To Use MATLAB and Simulink With ThingSpeak“, a free webinar hosted by Eric Wetjen of MathWorks. Join the webinar live on November 12, 2015 at 10am EST or watch a recording at a later time.

Car Counting Camera

This webinar will show how you can use MATLAB and Simulink with ThingSpeak, an Internet of Things data collection platform. ThingSpeak can be used to collect, analyze and act on data sent from devices such as Raspberry Pis and Arduinos. To illustrate this, a car counter is implemented overlooking a busy highway using a Raspberry Pi 2 and a webcam.  In this demonstration, Simulink is used to deploy the car-counting algorithm on the Raspberry Pi which is connected to ThingSpeak. The traffic can be analyzed offline with MATLAB or online using ThingSpeak and its built-in MATLAB Analysis and MATLAB Visualizations apps.

Eric Wetjen MathWorks IoT

Eric Wetjen has been working in Product Marketing at MathWorks for the last 7 years. He focuses on bringing MATLAB analysis capabilities to low cost hardware, Test and Measurement equipment and Internet of Things devices.  Prior to MathWorks, Eric held various positions in Product Management and Application Engineering primarily in the telecom industry.  Eric holds a Ph.D. in Engineering from Brown University.

Sign up at element14



03
Nov 15

Schedule MATLAB Code with TimeControl

Here at our headquarters we have a weather station collecting lots of weather data and sending it to ThingSpeak. We have made that data public for use in your own projects.

MathWorks Weather Station

We write the temperature and humidity values from the weather station to a ThingSpeak channel. At some point in the project, we started to wonder about dew point calculations. We wrote some MATLAB code that combined the temperature and humidity to calculate dew point. I did this using the ThingSpeak app, “MATLAB Analysis”. You can try this out with ThingSpeak now by signing in, selecting Apps, MATLAB Analysis, New, selecting “Calculate Dew point”, and clicking “Create”. This happens to be one of our built-in examples using our weather station’s public data.

It is great that it was easy to calculate dew point with MATLAB, but I want to see this analyzed data over time just like any other sensor data. The solution is a powerful combination of MATLAB Analysis and TimeControl. We use MATLAB Analysis to do the analysis and write the data to a ThingSpeak channel. Then, we use the TimeControl app to repeat the analysis every 5 minutes.

To setup MATLAB Analysis on a schedule, sign into ThingSpeak, select Apps, TimeControl, and New TimeControl.

Dew Point TimeControl in ThingSpeak

My MATLAB code now runs every 5 minutes doing analysis and writing data to my ThingSpeak channel. The TimeControl settings can be tailored to your needs such as executing MATLAB code once a day or only on weekends. This combination of MATLAB Analysis + TimeControl allows you to create continuous analysis of your project data.

To try this out for yourself, we have a public channel of weather station data that we have collected in Natick, MA at our headquarters. You can use that data and do your own MATLAB Analysis and writing the results back to your own channel. Also, Check out the ThingSpeak Documentation where we have a complete tutorial for you to help get started with ThingSpeak and MATLAB.



09
Oct 15

Official ThingSpeak Library for Arduino and Particle

We are thrilled to announce the official ThingSpeak Communication Library for Arduino and Particle devices. This library enables an Arduino or other compatible hardware to write or read data to or from ThingSpeak, an open data platform for the Internet of Things with built-in MATLAB analytics and visualization apps.

Arduino IDE Installation

In the Arduino IDE, choose Sketch/Include Library/Manage Libraries. Click the ThingSpeak Library from the list, and click the Install button.

Particle / Spark IDE Installation

In the Particle/ Spark Web IDE, click the libraries tab, find ThingSpeak, and choose “Include in App”.

Compatible Hardware

  • Arduino or compatible using an Ethernet or Wi-Fi shield (we have tested with Uno and Mega)
  • Arduino Yun running OpenWRT-Yun Release 1.5.3 (November 13th, 2014) or later.
  • Particle Core or Photon (Formally Spark)

ThingSpeak Examples

The library includes several examples to help you get started.

  • CheerLights: Reads the latest CheerLights color on ThingSpeak, and sets an RGB LED.
  • ReadLastTemperature: Reads the latest temperature from the public MathWorks weather station in Natick, MA on ThingSpeak.
  • ReadPrivateChannel: Reads the latest voltage value from a private channel on ThingSpeak.
  • ReadWeatherStation: Reads the latest weather data from the public MathWorks weather station in Natick, MA on ThingSpeak.
  • WriteMultipleVoltages: Reads analog voltages from pins 0-7 and writes them to the 8 fields of a channel on ThingSpeak.
  • WriteVoltage: Reads an analog voltage from pin 0, converts to a voltage, and writes it to a channel on ThingSpeak.

Complete source code and examples for the ThingSpeak Library are available on GitHub.



25
Sep 15

Video Introduction to ThingSpeak and the Internet of Things

Our very own Robert Mawrey produced a video introducing ThingSpeak and the Internet of Things.

ThingSpeak Intro Video

ThingSpeak is an open data platform for the Internet of Things. Your device or application can communicate with ThingSpeak using a RESTful API, and you can either keep your data private, or make it public. In addition, use ThingSpeak to analyze and act on your data. ThingSpeak provides an online text editor to perform data analysis and visualization using MATLAB®. You can also perform actions such as running regularly scheduled MATLAB code or sending a tweet when your data passes a defined threshold. ThingSpeak is used for diverse applications ranging from weather data collection and analysis, to synchronizing the color of lights across the world.

At the heart of ThingSpeak is a time-series database. ThingSpeak provides users with free time-series data storage in channels. Each channel can include up to eight data fields. This tutorial provides an introduction to some of the applications of ThingSpeak, a conceptual overview of how ThingSpeak stores time-series data, and how MATLAB analysis is incorporated in ThingSpeak.

[via MathWorks]



18
Sep 15

Counting Cars and Analyzing Traffic #RaspberryPi #MATLAB #ThingSpeak

The power of any tool becomes magnified when you start combing it with other tools. In this MakerZone project by Eric Wetjen, he demonstrates a powerful project by using a webcam to gather live traffic video of Route 9 in Natick, MA, using Simulink to deploy a car-counting algorithm to a Raspberry Pi, using MATLAB to perform analysis, and using ThingSpeak to collect and share the analyzed data with others.

Car Counting Camera

The project uses a Raspberry Pi 2 and USB webcam acting as a sensor. The webcam picks up traffic flowing in both directions. Once the algorithm for detecting cars is modeled in Simulink, the algorithm gets deployed on the Raspberry Pi. The Raspberry Pi sends the raw data to ThingSpeak on regular basis where it is analyzed using the MATLAB Analysis app on ThingSpeak.

MATLAB_car_counting_display

After sending to ThingSpeak, Eric created a MATLAB Analysis app to calculate the daily traffic-volume on ThingSpeak Channel 51671. Now that the data is public, others could use this processed data within apps such as Waze to optimize directions using analyzed traffic flows.

MATLAB Traffic Analysis ThingSpeak Visualization

Check out the MakerZone article for the complete project details and all of the code to get your Raspberry Pi + ThingSpeak analysis project started.

[via MathWorks MakerZone]



09
Sep 15

Updates to the MATLAB Analysis App with Lots of Example Code

When using the MATLAB Analysis app on ThingSpeak, the MATLAB function to represent date and time (datetime) allows you to represent points in time. You can also use datetime(‘now’)datetime(‘today’), datetime(‘yesterday’), or datetime(‘tomorrow’) to create scalar datetimes at or around the current moment. Refer to the link below for additional information about datetime function: http://www.mathworks.com/help/matlab/ref/datetime.html

On ThingSpeak, so far, the datetime function returned time set to UTC time zone by default. Starting at 10 am (EDT) on September 10th 2015, the datetime function will return date and time set to your account time zone (at https://thingspeak.com/account). This will allow you to read data from your channel with timestamps zoned to your local time zone instead of UTC.

For example, my account time zone is set to Eastern Time (US & Canada), and when I ran the following MATLAB code at 12:23 pm, I received:

dt = datetime('now')
dt =
10-Sep-2015 12:23:35

 

Prior to this change, I would have received:

dt =
10-Sep-2015 16:23:35

 

As you can see, the timestamp is 4 hours ahead of my time zone, which was due to MATLAB returning time in UTC.

This change makes it easier for you to perform time related activities in your time zone. Note that this new feature is available for both thingSpeakRead and thingSpeakWrite functions as well. As an example, consider the following request to read data from the MathWorks Weather Channel:

MATLAB Code:

[data, timestamp] = thingSpeakRead(12397);
display(timestamp.TimeZone, 'TimeZone');
Output:
data =
225.0000    3.8000   43.9000   95.8000
0   29.9800    4.3000    0.0300
timestamp =
10-Sep-2015 16:13:54
TimeZone =
America/New_York

 

With this enhancement, you would no longer have to explicitly specify the time zone of your dates and time to read and write data in your time zone.

Here are a few other examples:

  1. Read data corresponding to one entire day in your time zone:
startDateTime = datetime('September 10, 2015 00:00:00')
endDateTime = datetime('September 10, 2015 23:59:59')
readChannelID = 12397;
[data, timeStamps] = thingSpeakRead(readChannelID, 'DateRange', [startDateTime, endDateTime])

  1. Read data between certain hours of a day (between 7 am and 9 pm)
startDateTime = datetime('September 10, 2015 07:00:00')
endDateTime = datetime('September 10, 2015 21:00:00')
readChannelID = 12397;
[data, timeStamps] = thingSpeakRead(readChannelID, 'DateRange', [startDateTime, endDateTime])

  1. Generate a MATLAB plot in your local time zone:
[data, timeStamps] = thingSpeakRead(12397, 'Fields', 3, 'NumPoints', 10);
plot(timeStamps, data)

 

Note that, if at present, you are explicitly setting the time zone to your local time zone, you might see unexpected behavior in your code. Here are a few examples, based on support requests we have received:

  1. If you are using datetime function in your code similar to the example below:
% Set the time now to variable dt
dt = datetime('now')
% Assign time zone to UTC since the dt is unzoned by default
dt.TimeZone = 'UTC';
% Convert the timestamp to ‘America/New_York’
dt.TimeZone = 'America/New_York'

 

To fix this, remove the “TimeZone” assignments since time is now returned in your time zone by default, and use the code below:

% Set the time now to variable dt
dt = datetime('now')

  1. If you are setting the time zone of data returned by thingSpeakRead to your zone:
% Read data from a channel
[data, timeStamps] = thingSpeakRead(12397);
% Set the timezone to match your zone
timeStamps.TimeZone = 'America/New_York';

 

To fix this, remove the line with the “TimeZone” assignment, and use the code below:

% Read data from a channel
[data, timeStamps] = thingSpeakRead(12397);

 

For more information about the datetime function refer to the MATLAB documentation. If you need support, use the MATLAB section of the ThingSpeak Forum.