ThingSpeak

This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English verison of the page.

Note: This page has been translated by MathWorks. Please click here
To view all translated materals including this page, select Japan from the country navigator on the bottom of this page.

thingSpeakRead

Read data stored in ThingSpeak channel

Syntax

data = thingSpeakRead(chId)
data = thingSpeakRead(chId,Name,Value)
data = thingSpeakRead(___,'ReadKey',rk)
[data,timestamps] = thingSpeakRead(___)
[data,timestamps,chInfo] = thingSpeakRead(___)

Description

example

data = thingSpeakRead(chId) reads the most recent data from all fields of the specified public channel on ThingSpeak.com and returns the data as numeric type. To read non-numeric data, you must specify the ‘OutputFormat'.

example

data = thingSpeakRead(chId,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

example

data = thingSpeakRead(___,'ReadKey',rk) uses the ThingSpeak™ read API key stored in variable rk to read from a private channel.

example

[data,timestamps] = thingSpeakRead(___) also returns timestamps from the specified channel on ThingSpeak.com and can include any of the input arguments in previous syntaxes.

example

[data,timestamps,chInfo] = thingSpeakRead(___) also returns channel information.

Examples

Retrieve Data from a Channel

Retrieve the last five minutes of data from fields 1 and 4 of a public channel and return the data in a table.

data = thingSpeakRead(12397,'Fields',[1,4],'NumMinutes',3,'OutputFormat','table')
data =

  5×3 table

         Timestamps         WindDirectionNorth0Degrees    TemperatureF
    ____________________    __________________________    ____________

    20-Jun-2017 10:00:32    206                           78.9        
    20-Jun-2017 10:01:34    216                           78.9        
    20-Jun-2017 10:02:35    227                           78.8        

Read the last two days of data from fields 1 and 4 of a public channel and return the data in a table.

data = thingSpeakRead(12397,'Fields',[1,4],'NumDays',2,'OutputFormat','table');

Retrieve the most recent result for all fields of a private channel, and return the data in a table.

data = thingSpeakRead(chId,'ReadKey','Your.Read.API.Key','OutputFormat','table')

Retrieve latitude, longitude, and altitude data along with the last 10 channel updates for all fields in a public channel, and return the data as a table.

data = thingSpeakRead(chId,'NumPoints',10,'Location',true,'OutputFormat','table')

Set the timeout for reading 8000 data points from field 1 of a public channel. Return the data as a table.

data = thingSpeakRead(12397,'Fields',1,'NumPoints',8000,'Timeout',10,'OutputFormat','table');

Retrieve Data Along with Timestamp

Retrieve the most recent result for all fields of a public channel, including the timestamp.

[data,time] = thingSpeakRead(12397)
data =

  201.0000    3.1000   71.0000   77.5000         0   29.6000    3.9000   51.0000


time = 

  datetime

   23-Jun-2017 08:45:28

Retrieve the last 10 points of data from fields 1 and 4 of a public channel. Return the data and timestamps in a table, including channel information.

[data,chInfo] = thingSpeakRead(12397,'Fields',[1 4],'NumPoints',10,'OutputFormat','table');

Retrieve the last 10 points of data from fields 1 and 4 of a public channel. Return the data as a timetable, including channel information.

[data,chInfo] = thingSpeakRead(12397,'Fields',[1 4],'NumPoints',10,'OutputFormat','timetable');

Retrieve the last 10 points including the timestamp for all three fields of a public channel on a private ThingSpeak server installation.

myURL = serverURL % Enter URL
[data,time] = thingSpeakRead(6,'NumPoints',10,'URL',myURL);

Retrieve Data Along with Timestamp and Channel Information

Retrieve the last five minutes of data from fields 1 and 4 of a public channel, including the timestamp and channel information.

[data,time,channelInfo] = thingSpeakRead(12397,'Fields',[1,4],'NumMinutes',5)
data =

  2×3 table

         Timestamps         WindDirectionNorth0Degrees    TemperatureF
    ____________________    __________________________    ____________

    23-Jun-2017 08:48:31    193                           77.4        
    23-Jun-2017 08:49:32    181                           77.5        


chInfo = 

  struct with fields:

            ChannelID: 12397
                 Name: 'WeatherStation'
          Description: 'MathWorks Weather Station, West Garage, Natick, MA 01760, USA'
             Latitude: 42.2997
            Longitude: -71.3505
             Altitude: 60
              Created: 20-May-2014 17:50:32
              Updated: 23-Jun-2017 08:49:32
          LastEntryID: 1410086
    FieldDescriptions: {1×8 cell}
             FieldIDs: [1 2 3 4 5 6 7 8]
                  URL: 'https://api.thingspeak.com/channels/12397/feed.json?'

Retrieve the data for August 8, 2014 through August 12, 2014 for fields 1 and 4 of a public channel, including the timestamp and channel information.

[data,time,channelInfo] = thingSpeakRead(12397,'Fields',[1 4],'DateRange',[datetime('Aug 8, 2014'),datetime('Aug 12, 2014')]);

Input Arguments

collapse all

The channel identification number, specified as a positive integer. For private channels, you must also specify the ‘ReadKey' argument.

Data Types: double

Name-Value Pair Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside single quotes (' '). You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: thingSpeakRead(12397,'url','https://api.thingspeak.com');

collapse all

Range of data to be returned, specified as a comma-separated pair consisting of 'DateRange' and an array of values that have [startdate,enddate], in MATLAB® datetime values. ThingSpeak server limits the number of points returned to a maximum of 8000. Adjust your ranges or make multiple calls if you need more than 8000 points of data.

    Note:   You cannot use DateRange with NumDays or NumMinutes.

Example: thingSpeakRead(12397,'DateRange',[datetime('Aug 8, 2014'),datetime('Aug 12, 2014')]);

Data Types: datetime

Channel field IDs specified as a comma-separated pair consisting of 'Fields' and numeric values of the channel field ID to retrieve data from.

Example: thingSpeakRead(12397,'Fields',[1,3,4]);

Data Types: double

Positional information of data from the channel, specified as a comma-separated pair consisting of 'Location' and a logical value. Location information includes latitude, longitude, and altitude.

Example: thingSpeakRead(12397,'Location',true);

Data Types: logical

Number of days of data to extract data from, specified as a comma-separated pair consisting of 'NumDays' and a numeric value. Number of days is measured in 24-hour periods. The ThingSpeak server limits the number of points returned to a maximum of 8000. Adjust your number of days, or make multiple calls if you need more than 8000 points of data.

    Note:   You cannot use NumDays with DateRange or NumMinutes.

Example: thingSpeakRead(12397,'NumDays',2);

Data Types: double

Number of minutes of data to extract data from, specified as a comma-separated pair consisting of 'NumMinutes' and a value. Number of minutes of data to retrieve is measured from the time the command is issued. The ThingSpeak server limits the number of points returned to a maximum of 8000. Adjust your number of minutes, or make multiple calls if you need more than 8000 points of data.

    Note:   You cannot use NumMinutes with DateRange or NumDays.

Example: thingSpeakRead(12397,'NumMinutes',20);

Data Types: double

Number of data points to extract, specified as a comma-separated pair consisting of 'NumPoints' and a numeric value. The number of points or rows of data is counted from the time the command is issued. The ThingSpeak server limits the number of points returned to a maximum of 8000.

Example: thingSpeakRead(12397,'NumPoints',200);

Data Types: double

Class of the output data, specified as a comma-separated pair consisting of 'OutputFormat' and a string value. Valid values are 'matrix', 'table', and 'timetable'. Use 'matrix' format for numeric data. You can use 'timetable' or 'table' for any nonnumeric data. The 'table' format outputs [data,channelInfo], where data contains the timestamps and the data from the fields of the channel. The 'timetable' format outputs [data,channelInfo], where data is a timetable and contains as many variables as the number of requested 'fields'.

Example: thingSpeakRead(12397,'OutputFormat','table');

Example: thingSpeakRead(12397,'OutputFormat','timetable');

Data Types: table

Read API key of the channel, specified as a comma-separated pair consisting of 'ReadKey' and a string value representing the channel read API key. The Read API key allows you to read data from a private channel. If you are reading data from a public channel, you do not need a Read API key. Save your Read API key in a variable for convenience.

Example: thingSpeakRead(12397,'ReadKey','F6CSCVKX42WFZN9Y');

Example: thingSpeakRead(12397,'ReadKey',string('F6CSCVKX42WFZN9Y'));

Data Types: string | char

Server connection timeout period, specified as a comma-seperated pair consisting of 'Timeout' and a numeric value. The value must be the number of seconds for thingSpeakRead to connect to the server.

Example: thingSpeakRead(12397,'Timeout',15);

Data Types: double

URL of a private ThingSpeak server, specified as a comma-seperated pair consisting of 'URL' and a string value. The value must be a valid URL address.

Example: thingSpeakRead(12397,'URL','http://private.server.url');

Example: thingSpeakRead(12397,'URL',string('http://private.server.url'));

Data Types: string | char

Output Arguments

collapse all

Data from the channel, returned as an array, table, or timetable.

Data Types: table

Timestamp of each data element, returned as an array of datetime values.

Data Types: datetime

Channel information, returned as a structure.

        ChannelID: 12397
             Name: 'WeatherStation'
      Description: 'MathWorks Weather Station, West Garage, Natick, MA 01760, USA'
         Latitude: 42.2997
        Longitude: -71.3505
         Altitude: 60
          Created: [1x1 datetime]
          Updated: [1x1 datetime]
        LastEntry: 188212
FieldDescriptions: {1x8 cell}
         FieldIDs: [1 2 3 4 5 6 7 8]
              URL: 'https://api.thingspeak.com/channels/12397/feed.json?'

Data Types: struct

Was this topic helpful?