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')
    example
  • thingSpeakWrite(___,Name,Value)
    example
  • response = thingSpeakWrite(___)
    example

Description

example

thingSpeakWrite(chId,data,'WriteKey','your write api key') writes the data to the specified channel when 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. The API key allows you to write data to a channel.

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','23ZLGOCCU9VWHG2H')

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

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

Write non-numeric data to the first 3 consecutive fields [1,2,3] of a channel.

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

Write the previous non-numeric values to non-consecutive fields [1,4,6] of a channel.

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

Write latitude and longitude to the channel feed along with values to consecutive fields.

thingSpeakWrite(17504,{2.3,'on','good'},'Location',[-40,23],'WriteKey','23ZLGOCCU9VWHG2H')

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

thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'Location',[-40,23,3500],'WriteKey','23ZLGOCCU9VWHG2H')    

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','23ZLGOCCU9VWHG2H','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: []

Related Examples

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 (Field1). If you specify a vector or a 1-D cell array, data is written to consecutive fields starting with Field1, for the number of vectors or columns of data.

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.

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 value must the number of seconds for thingSpeakReadto connect to the server.

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, returned as a structure. The fields of the structure indicate the values written to the fields of the channel.

Data Types: struct

See Also

Functions

Was this topic helpful?