Tutorial: ThingSpeak and MATLAB

This example shows how to collect data from a source, analyze the data in MATLAB, and act on the data based on the results of the analysis.

In this example, you:

Collect Data in a New Channel

Create a channel to collect data from a weather station. Rather than build your own weather station to send data to ThingSpeak, this example reads data from the public ThingSpeak channel 12397 - Weather Station, and writes it into the channel you create. The public channel collects weather data from a location in North America.

Note: To learn how to build your own weather station, see Arduino Weather Station Data Analysis on MakerZone.


Sign In using either your MathWorks Account or ThingSpeak account, or create a new ThingSpeak account.

Create a Channel

  1. Click Channels, and then My Channels.

  2. Doc ch select
  3. On the Channels page, click New Channel.
  4. Specify these properties:
    • Name: Dew Point Measurement
    • Field 1: Temperature (F)
    • Field 2: Humidity
    • Field 3: Dew Point

    Doc ch ex01

    Use the temperature and humidity data from the public ThingSpeak channel to calculate the dew point data. Write the temperature, humidity, and calculated dew point data to Fields 1, 2 and 3 of your dew point channel.

  5. Click Save Channel. You now see these tabs:
    • Private View: This tab displays information about your channel that only you can see.
    • Public View: If you choose to make your channel publicly available, use this tab to display selected fields and channel visualizations.
    • Channel Settings: This tab shows all the channel options you set at creation. You can edit, clear, or delete the channel from this tab.
    • API Keys: This tab displays your channel API keys. Use the keys to read from and write to your channel.
    • Data Import/Export: This tab enables you to import and export channel data.

Analyze Your Data

This example shows how to read data from ThingSpeak channel 12397 - Weather Station, which collects weather related data from an Arduino device. You read temperature and humidity data into your dew point channel, run calculations to calculate the dew point, and visualize the results.


Write Data to Your Channel

Go to Apps > MATLAB Analysis, and click New. Choose the Custom template, and click Create.

Read Field 3: Humidity and Field 4: Temperature (in Fahrenheit) from ThingSpeak channel 12397 - Weather Station, which collects data from sensors using an Arduino device. Write the data to fields in your dew point channel:

  • Field 1: Temperature (in Fahrenheit)
  • Field 2: Humidity

Set up your dew point channel to read the temperature and humidity from the public channel.

  1. Save the public channel ID and your dew point channel ID to variables.
    readChId = 12397;
    writeChId = 677;
  2. Save your Write API Key to a variable.
    writeKey = 'F6CSCVKX42WFZN9Y';

    To find your Channel ID and Write API Key, refer to Channel Info on the My Channels tab.

    Doc ch ex02

  3. Read the latest 100 points of temperature data with timestamps and humidity data from the public channel into variables.
  4. [temp,time] = thingSpeakRead(readChId,'Fields',4,'NumPoints',100);
    humidity = thingSpeakRead(readChId,'Fields',3,'NumPoints',100);

Calculate the Dew Point

To calculate the dew point using temperature and humidity readings from ThingSpeak channel 12397 - Weather Station, write and run this MATLAB code:

  1. Convert the temperature (temp) from Fahrenheit to Celsius.
    tempC = (5/9)*(temp-32); 
  2. Specify the constants for water vapor (b) and barometric (c) pressure.
    b = 17.62;
    c = 243.5;
  3. Calculate the dew point in Celsius.
    gamma = log(humidity/100) + b*tempC ./ (c+tempC);
    dewPoint = c*gamma ./ (b-gamma)
  4. Convert the result back to Fahrenheit.
    dewPointF = (dewPoint*1.8) + 32;
  5. Write data to your dew point channel. This code performs a batch update and requires the timestamp to write data correctly.
  6. thingSpeakWrite(writeChId,[temp,humidity,dewPointF],'Fields',[1,2,3],'TimeStamps',time,'Writekey',writeKey);
  7. Click Run and Save to validate your code.

    Doc ch ex03 tn

  • To see if your code ran successfully, click on your Dew Point Measurement channel link in the Channel Info panel.
  • Doc ch ex14

    The Dew Point Measurement channel Private View shows charts with channel data.

    Doc ch ex06

    Schedule Code

    Use the TimeControl app to schedule the dew point calculation in your MATLAB Analysis code. Schedule it to read data from the weather station every 5 minutes and calculate the dew point.

    1. On your MATLAB Analysis page, scroll to the bottom, and click TimeControl to open the app with MATLAB Analysis preselected in the Actions field and the Dew Point Calculation as the Code to execute.
    2. Name your new TimeControl Dew Point TC
    3. Choose Recurring in the Frequency field.
    4. Choose Minute in the Recurrence field.
    5. Select 5 in the Every — minutes field.
    6. Keep the Start Time at the default value.
    7. Verify that the Action is MATLAB Analysis, and the Code to execute is your Dew Point Calculation.
    8. Click Save TimeControl

    9. Doc ch ex13 tn

    Visualize Dew Point Measurement

    Use the MATLAB Visualizations app to visualize the measured dew point data, temperature, and humidity from your Dew Point Measurement channel. This example uses the thingSpeakPlot function to show all three data points in a single visualization.

    Go to Apps > MATLAB Visualizations, and click New to create a visualization.

    Alternately, you can click on MATLAB Visualization in your Dew Point channel view.

    Doc ch ex15 tn
    1. Name the visualization "Dew Point."
    2. Create variables for your dew point channel ID and your Read API Key.
    3. readChId = 677
      readKey = '36LPYCQ19U37ANLE'
    4. Read data from your channel fields, and get the last 100 points of data for:
      • Temperature: from Field 1
      • Humidity: from Field 2
      • Dew point: from Field 3
      [dewPointData,timeStamps] = thingSpeakRead(readChId,'fields',[1,2,3],'NumPoints',100,'ReadKey',readKey);
    5. Plot the data with x and y labels, a title, and a legend.
      thingSpeakPlot(timeStamps,dewPointData,'xlabel','TimeStamps','ylabel','Measured Values','title','Dew Point Measurement','Legend',{'Temperature','Humidity','Dew Point'},'grid','on');
    6. Your code should look like this:

      Doc ch ex07 tn
    7. Click Run and Save. If your MATLAB code has no errors, the output should look like this:

      Doc ch ex08 tn

    Act On Your Data

    This example shows how to send a tweet when your dew point data exceeds 60°F using ThingTweet and React apps.


    Attach Twitter Account

    1. Go to Apps > ThingTweet.
    2. On the ThingTweet page, click Link Twitter Account to link your Twitter account to your ThingSpeak account.
    3. Enter your Twitter username and password, and click Authorize App.

      Doc ch ex09 tn

    4. On the Authorization page, click Back to ThingTweet. Notice that your Twitter account is linked to ThingSpeak.

      Doc ch ex10

    React to Change in Dew Point

    This example shows how to send a Tweet each time the dew point level goes over 60°F by checking the channel every 10 minutes.

    1. Go to Apps > React, and click New React.
    2. Name it "Dew Point Tweet."
    3. Set the Condition Type to Numeric.
    4. Set the Test Frequency to Every 10 Minutes.
    5. Set the condition to trigger when the humidity value in your channel reaches or goes above 60:
      • If channel: select your dew point measurement channel.
      • field: select 3 (Dew Point).
      • For the condition type, select is greater than or equal to.
      • For the condition value enter 60.

      • Doc ch ex05
      • Set Action to ThingTweet.
      • Enter this string in then tweet:
        Turn off that humidifier! It's above 60F
      • Select your Twitter account in using Twitter account.
      • In Options, choose Run action each time the condition is met.
      • Click Save React.

        Doc ch ex11 tn

      • You will receive this Tweet every time the humidity level goes above 60°F.

        Doc ch ex12