Main Content

thingSpeakWrite

Write data to ThingSpeak channel

Description

thingSpeakWrite(channelID,data,'WriteKey','channel 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.

Note

If you are writing a table of data using thingSpeakWrite and you have a React associated with this channel, your React is triggered only once when the Condition Type matches the Condition, even if the Condition is matched more than once.

example

Examples

collapse all

Write a single numeric 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.

response = 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 mixed 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,35],'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('now')
thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','23ZLGOBBU9TWHG2H','TimeStamp',tStamp)

Write a matrix of data to the first three fields of a channel. The timestamps provided are interpreted as local time.

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

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

channelID = 17504; % Change to your Channel ID
writeKey = '23ZLGOBBU9TWHG2H'; % Change to your Write API Key

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

Write a timetable of data to the first two fields of a channel. The timestamps provided are interpreted as local time.

% 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 = 17504; % Change to your channel ID
writeKey  = '23ZLGOBBU9TWHG2H'; % Change to your Write API Key

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

Input Arguments

collapse all

The channel identification number, specified as a positive integer.

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','channel write api key');

Write API key of the channel, specified as a comma-separated pair consisting of 'WriteKey' and a character vector representing the channel write API key. The Write API key is required to write data into a channel. You can find the Write API key for a channel on the API Keys tab of your ThingSpeak channel view. Save your channel Write API key in a variable for convenience.

Example: thingSpeakRead(12397,'WriteKey','XXXXXXXXXXXXXXXX');

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

Example: thingSpeakWrite(17504,[1.1,2.2],'Fields',[1,2,3],'WriteKey','channel write api key');

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,4],'WriteKey','channel write api key');

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','channel write api key');

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',' write api key');

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','write API key','Timeout',15);

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. All timestamps must be unique. If you submit duplicate timestamps, all your updates are rejected, otherwise only updates with timestamps already in channel are rejected.

Note

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

Example: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','channel write api key','Timestamp',[datetime('2/6/2018 9:27:12','format','MM/dd/uuuu HH:mm:ss')]);

Data Types: datetime

Output Arguments

collapse all

Channel response of the write operation, returned as a structure with fields in the order shown in the table. The fields of the structure indicate the values written to the fields of the channel:

FieldDescription
FieldX

FieldX data for the entry, up to eight fields

Latitude

Latitude for the write operation

Longitude

Longitude for the write operation

Channel ID

Channel Identification number

Created

Datetime of write operation and feed entry

Last EntryID

Entry ID for the most recent channel update

Created

Datetime of channel creation date

Updated

Datetime when the channels settings were last changed

Last EntryID

Entry ID for the most recent channel update

Altitude

Elevation for the write operation

Limitations

  • All timestamps must be unique. You cannot write new data with timestamps that match existing data in the channel.

  • The allowed update frequency for a channel is limited based on your license type. See Frequently Asked Questions and How to Buy for specific information. Updates with multiple entries, such as in a table or timetable, are limited to the slower update frequency. For users of free accounts, the number of messages in a single call to thingSpeakWrite is limited to 960 messages. For users of paid accounts, the limit is 14,400 messages. Attempts to publish at a rate faster than the allowed limit will return the following error: Requests are too frequent. For further information, see Limitations in the documentation.

More About

collapse all

ThingSpeak

ThingSpeak is an IoT analytics platform service that allows you to aggregate, visualize, and analyze live data streams in the cloud. See License Options and the Commercial Use for more information.

Version History

Introduced in R2019a

See Also

Functions