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.

thingSpeakWrite

Write data to ThingSpeak channel

Syntax

thingSpeakWrite(chId,data,'WriteKey','your write api key')
thingSpeakWrite(___,Name,Value)
response = thingSpeakWrite(___)

Description

example

thingSpeakWrite(chId,data,'WriteKey','your write api key') writes the data to the specified channel. The write API key is specified as a comma-separated pair consisting of 'WriteKey' and a character vector or string representing the channel write key.

example

thingSpeakWrite(___,Name,Value) uses additional options specified by one or more Name,Value pair arguments.

example

response = thingSpeakWrite(___) returns the response provided by the ThingSpeak™ server on successful completion of the write operation.

Examples

Write Data to a Channel

Write a scalar value to Field 1 of a channel.

thingSpeakWrite(17504,2.3,'WriteKey','23ZLGOBBU9TWHG2H')

Write numeric values to the first four consecutive fields [1,2,3,4] of a channel.

thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','23ZLGOBBU9TWHG2H')

Write nonnumeric data to the first three consecutive fields [1,2,3] of a channel.

thingSpeakWrite(17504,{2.3,'on','good'},'WriteKey','23ZLGOBBU9TWHG2H')

Write the previous nonnumeric values to nonconsecutive fields [1,4,6] of a channel.

thingSpeakWrite(17504,'Fields',[1,4,6],'Values',{2.3,'on','good'},'WriteKey','23ZLGOBBU9TWHG2H')

Update three fields and write latitude, longitude, and altitude data for the entry.

thingSpeakWrite(17504,[1.1,2.3,4],'Location',[-40,23,3500],'WriteKey','23ZLGOBBU9TWHG2H')    

Write latitude, longitude, and altitude data to a channel without adding values to fields.

thingSpeakWrite(17504,'Location',[-40,23,3500],'WriteKey','23ZLGOBBU9TWHG2H')    

Write a timestamp for the value being written to a channel. The timestamp provided is interpreted as local time.

tStamp = datetime('2/6/2015 9:27:12','InputFormat','MM/dd/yyyy HH:mm:ss')
thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','23ZLGOBBU9TWHG2H ','TimeStamp',tStamp)    

Write values to a channel on a private ThingSpeak installation.

thingSpeakWrite(12,[2.3,1.2,3.2],'URL','http://myServer.subnet.company.com:3000/')

Write a Matrix of Values to Channel

% Generate Random Data
data = randi(10,10,3);

% Generate timestamps for the data
tStamps = datetime('now')-minutes(9):minutes(1):datetime('now');

channelID = (Enter Channel ID)
writeKey  = (Enter Write API Key)

% Write 10 values to each field of your channel along with timestamps
thingSpeakWrite(channelID,data,'TimeStamp',tStamps,'WriteKey',writeKey)

Write a Table of Values to Channel

% Generate random data
dataField1 = randi(10,10,1);
dataField2 = randi(10,10,1);

% Generate timestamps for the data
tStamps = [datetime('now')-minutes(9):minutes(1):datetime('now')]';

% Create table
dataTable = table(tStamps,dataField1,dataField2);
channelID = (Enter Channel ID)
writeKey  = (Enter Write API Key)

% Write 10 values to each field of your channel along with timestamps
thingSpeakWrite(channelID,dataTable,'WriteKey',writeKey)

Write a Timetable of Values to Channel

% Generate random data
dataField1 = randi(10,10,1);
dataField2 = randi(10,10,1);

% Generate timestamps for the data
tStamps = [datetime('now')-minutes(9):minutes(1):datetime('now')]';

% Create timetable
dataTable = timetable(tStamps,dataField1,dataField2);
channelID = (Enter Channel ID)
writeKey  = (Enter Write API Key)

% Write 10 values to each field of your channel along with timestamps
thingSpeakWrite(channelID,dataTable,'WriteKey',writeKey)

Get Response of the Write Operation

View the channel response structure, returned by the thingSpeakWrite function.

channelID = (Enter Channel ID);
writeKey  = (Enter Write API Key);
response = thingSpeakWrite(channelID,{2.3,'on',good'},'Location',[-40,23],'WriteKey',writeKey)
response = 

  struct with fields:

      ChannelID: (Your Channel ID)
        Created: 23-Aug-2016 11:29:20
    LastEntryID: 2713
         Field1: '2.3'
         Field2: 'on'
         Field3: 'good'
         Field4: []
         Field5: []
         Field6: []
         Field7: []
         Field8: []
       Latitude: '-40.0'
      Longitude: '23.0'
       Altitude: []

Input Arguments

collapse all

The channel identification number, specified as a positive integer.

Data Types: double

Data to write to channel, specified as a numeric scalar or numeric vector, a cell array, a string of cell arrays, table, or a timetable. If you specify scalar data, the value is written to the first field in the channel (Field 1). For a vector or a 1-D cell array, data is written to consecutive fields starting with Field 1.

Example: thingSpeakWrite(17504,{2,3,'on','good'},'WriteKey','your write api key');

Example: thingSpeakWrite(17504,string({2,3,'on','good'}),'WriteKey','your write api key');

Data Types: string | double | cell | table

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: thingSpeakWrite(17504,{2,3,'on','good'},'Fields',[1,2,3],'WriteKey','your write api key');

collapse all

Channel fields IDs, specified as a comma-separated pair consisting of 'Fields' and a 1-by-n positive integer value.

Example: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'Fields',[1,2,3],'WriteKey','your write api key');

Data Types: double

Data values, specified as a comma-separated pair consisting of 'Values' and numeric scalar, numeric vector, a cell array, a string of cell arrays, table, or timetable values. Specify the data to be written to channel fields specified by the 'Fields' parameter

Example: thingSpeakWrite(17504,'Fields',[1,3,4],'Values',[1,2,3],'WriteKey','your write api key');

Example: thingSpeakWrite(17504,'Fields',[1,3,4],'Values',string({2,3,'on','good'}),'WriteKey','your write api key');

Data Types: table

Positional information of data in the channel, specified as a comma-separated pair consisting of 'Location' and numeric array. Location information includes latitude, longitude, and altitude. This example writes the same location information for each of the three points provided to field 1.

Example: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'Location',[-40,23,200],'WriteKey','your write api key');

Data Types: double

Server connection timeout period, specified as a comma-separated pair consisting of 'TimeOut' and a numeric value. The timeout period is the number of seconds allowed for thingSpeakRead to connect to the server before the request is terminated.

Example: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','your write api key','Timeout',15);

Data Types: double

Timestamp of data values written to fields in the write channel, specified as a comma-separated pair consisting of 'Timestamp' and a value. The value must be a MATLAB® datetime value.

Note

Do not specify 'Timestamp' if you have specified 'Values' as 'timetable'.

Example: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','your write api key','Timestamp',[datetime('2/6/2015 9:27:12')]);

Data Types: datetime

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

Example: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','your write api key','URL','private.server.url');

Example: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','your write api key','URL',string('private.server.url'));

Data Types: string | char

Output Arguments

collapse all

Channel response of the write operation, returned as a structure. The fields of the structure indicate the values written to the fields of the channel.

Data Types: struct

Was this topic helpful?