thingSpeakWrite
将数据写入ThingSpeak通道
语法
说明
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
— 写入通道的API key
API密钥的特征向量 | API 密钥的字符串标量
通道的写入 API 密钥,指定为逗号分隔的对组,其中包含 'WriteKey'
和表示通道写入 API 密钥的字符向量。将数据写入通道需要 Write API 密钥。您可以在 ThingSpeak通道视图的 API 密钥选项卡上找到通道的写入 API 密钥。保存您的通道为了方便起见,将 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 | 条目的 FieldX 数据,最多八个字段 |
Latitude | 写操作的纬度 |
Longitude | 写操作的经度 |
Channel ID | 通道识别号 |
Created | 写入操作和源条目的日期时间 |
Last EntryID | 最近通道更新的条目 ID |
Created | 通道创建日期的日期时间 |
Updated | 上次更改通道设置的日期时间 |
Last EntryID | 最近通道更新的条目 ID |
Altitude | 写操作的高度 |
局限性
所有时间戳必须是唯一的。您无法写入时间戳与通道中现有数据相匹配的新数据。
通道允许的更新频率根据您的许可证类型而受到限制。有关具体信息,请参阅 Frequently Asked Questions 和 How to Buy。具有多个条目的更新(例如表或时间表中的更新)仅限于较慢的更新频率。对于免费帐户的用户,一次调用
thingSpeakWrite
的消息数量限制为 960 条消息。对于付费帐户的用户,消息数量上限为 14,400 条。尝试以高于允许限制的速度发布将返回以下错误:请求过于频繁。有关更多信息,请参阅文档中的限制。
详细信息
事物说话
ThingSpeak 是一项 IoT 分析平台服务,可让您在云中聚合、可视化和分析实时数据流。有关详细信息,请参阅 License Options 和 Commercial Use。
版本历史记录
在 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)