thingSpeakWrite
语法
说明
thingSpeakWrite(
将数据写入指定的通道。Write API 密钥指定为以逗号分隔的对组,其中包含 channelID
,data
,'WriteKey','channel write API key
')'WriteKey'
和一个表示通道写入密钥的字符向量或字符串。
thingSpeakWrite(___,
使用一个或多个 Name,Value
)Name,Value
对组参数指定的其他选项。
示例
将数据写入 ThingSpeak 通道
写入多个值并查看响应
将数值写入通道的前四个连续字段 [1,2,3,4]。
response = thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','23ZLGOBBU9TWHG2H')
写入非数值数据
将非数值数据写入通道的前三个连续字段 [1,2,3]。
thingSpeakWrite(17504,{2.3,'on','good'},'WriteKey','23ZLGOBBU9TWHG2H')
写入混合数据
将混合值写入通道的非连续字段 [1,4,6]。
thingSpeakWrite(17504,'Fields',[1,4,6],'Values',{2.3,'on','good'},'WriteKey','23ZLGOBBU9TWHG2H')
写入字段和位置数据
更新三个字段,并为项写入纬度、经度和海拔数据。
thingSpeakWrite(17504,[1.1,2.3,4],'Location',[-40,23,35],'WriteKey','23ZLGOBBU9TWHG2H')
仅写入位置数据
将纬度、经度和海拔数据写入一个通道,而不将值添加到字段中。
thingSpeakWrite(17504,'Location',[-40,23,3500],'WriteKey','23ZLGOBBU9TWHG2H')
写入具有时间戳的数据
为写入通道的值写入时间戳。提供的时间戳解释为本地时间。
tStamp = datetime('now') thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','23ZLGOBBU9TWHG2H','TimeStamp',tStamp)
写入数据矩阵
将一个数据矩阵写入通道的前三个字段。提供的时间戳会被解释为本地时间。
% 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)
写入数据的时间表
将数据的时间表写入通道的前两个字段。提供的时间戳会被解释为本地时间。
% 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)
输入参数
channelID
— 通道标识号
数值
通道标识号,指定为正整数。
data
— 要写入通道的数据
数组 | 数值 | 表 | 时间表
要写入通道的数据,指定为数值标量或数值向量、元胞数组、一串元胞数组、表或时间表。如果指定标量数据,则值写入通道中的第一个字段(字段 1)。对于向量或一维元胞数组,数据从字段 1 开始写入连续字段。
示例: thingSpeakWrite(17504,{2,3,'on','good'},'WriteKey','channel write api key');
channel write API key
— 通道的 Write API 密钥
API 密钥的字符向量 | API 密钥的字符串标量
通道的 Write API 密钥,指定为逗号分隔的对组,其中包含 'WriteKey'
和一个表示通道 Write API 密钥的字符向量。将数据写入通道需要 Write API 密钥。您可以在 ThingSpeak 通道视图的“API 密钥”选项卡上找到通道的 Write API 密钥。为了方便起见,请将您的通道 Write API 密钥保存在变量中。
示例: thingSpeakRead(12397,'WriteKey','XXXXXXXXXXXXXXXX');
名称-值参数
将可选的参数对组指定为 Name1=Value1,...,NameN=ValueN
,其中 Name
是参数名称,Value
是对应的值。名称-值参数必须出现在其他参数之后,但参数对组的顺序无关紧要。
在 R2021a 之前,使用逗号分隔每个名称和值,并用引号将 Name
引起来。
示例: thingSpeakWrite(17504,[1.1,2.2],'Fields',[1,2,3],'WriteKey','channel write api key');
Fields
— 通道字段
数值
通道字段 ID,指定为以逗号分隔的对组,其中包含 'Fields'
和 1×n 正整数值。
示例: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'Fields',[1,2,3,4],'WriteKey','channel write api key');
Values
— 数据值
数值 | 数组 | 表 | 时间表
数据值,指定为以逗号分隔的对组,其中包含 'Values'
和数值标量、数值向量、元胞数组、一串元胞数组、表或时间表值。指定将数据写入到由 'Fields'
参数指定的通道字段。
示例: thingSpeakWrite(17504,'Fields',[1,3,4],'Values',[1,2,3],'WriteKey','channel write api key');
Location
— 数据的位置信息
数值
通道中数据的位置信息,指定为以逗号分隔的对组,其中包含 'Location'
和数值数组。位置信息包括纬度、经度和海拔。此示例为提供给字段 1 的三个点中的每个点写入相同的位置信息。
示例: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'Location',[-40,23,200],'WriteKey',' write api key');
Timeout
— 服务器连接超时期限
10 (默认) | 数值
服务器连接超时期限,指定为以逗号分隔的对组,其中包含 'TimeOut'
和一个数值。超时期限是 thingSpeakRead
尝试连接到服务器所允许的秒数,超过此期限则终止连接请求。
示例: thingSpeakWrite(17504,[2.3,1.2,3.2,0.1],'WriteKey','write API key','Timeout',15);
Timestamp
— 数据值的时间戳
日期时间
在写入通道的字段中写入的数据值的时间戳,指定为以逗号分隔的对组,其中包含 'Timestamp'
和一个值。该值必须为 MATLAB® datetime
值。所有时间戳必须唯一。如果您提交重复的时间戳,则您的所有更新都将被拒绝,否则只有时间戳已在通道中的更新才会被拒绝。
注意
如果您已将 'Values'
指定为时间表,请不要指定 'Timestamp'
。
示例: 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')]);
数据类型: datetime
输出参数
response
— 写入操作的通道响应
结构体
写入操作的通道响应,以结构体形式返回,其中的字段顺序如表中所示。结构体字段指示写入到通道字段的值:
字段 | 描述 |
---|---|
FieldX | 项的字段X 数据,最多八个字段 |
Latitude | 写入操作的纬度 |
Longitude | 写入操作的经度 |
Channel ID | 通道标识号 |
Created | 写入操作和馈送项的日期时间 |
Last EntryID | 最近通道更新的项 ID |
Created | 通道创建日期的日期时间 |
Updated | 上次更改通道设置的日期时间 |
Last EntryID | 最近通道更新的项 ID |
Altitude | 写入操作的高程 |
局限性
详细信息
ThingSpeak
ThingSpeak 是一种物联网分析平台服务,使您能够聚合、可视化和分析云中的实时数据流。有关详细信息,请参阅许可证选项和商业用途。
版本历史记录
在 R2019a 中推出
MATLAB 命令
您点击的链接对应于以下 MATLAB 命令:
请在 MATLAB 命令行窗口中直接输入以执行命令。Web 浏览器不支持 MATLAB 命令。
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list:
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)