How can I write multiple fields all at once using ThingSpeak library?

93 ビュー (過去 30 日間)
I want to write information for controlling a device in my ThingSpeak channel, can I store the control parameters in ThingSpeak and read all of the paremeters at once back to the device? I'd like to be able to read multiple fields without having to make multiple calls to the ThingSpeak readfield command on my Arduino or esp32 or ESP8266. Is this possible?

採用された回答

MathWorks Internet of Things Team
MathWorks Internet of Things Team 2021 年 7 月 15 日
The updated ThingSpeak library for Arduino and Particle version 2.0.1 includes the ability to read multiple fields in one function call. The example embedded in the Arduino library can be reached in the File > Examples > ThingSpeak menu in the Arduino IDE once you have installed the latest version of the ThingSpeak library.
Reading multiple fields is especially helpful when you are using your ThingSpeak channel for control settings. For example, I have a drip irrigation system in my backyard that uses ThingSpeak channel 597924 to set the water time and the sleep time. You can see the battery level for the solar powered battery for the pump on ThingSpeak channel 592680.
Here are the basic steps using C code.
First include the library and declare variables in the setup function
include “ThingSpeak.h”
unsigned long weatherStationChannelNumber = 12397;
int statusCode = 0;
In the loop function, read all the fields together and then use the type specific built-in functions to get the values you need.
statusCode = ThingSpeak.readMultipleFields(weatherStationChannelNumber);
if(statusCode == 200)
{
int windDir = ThingSpeak.getFieldAsInt(1); // Field 1
float windSpeed = ThingSpeak.getFieldAsFloat(2); // Field 2
int percentHumid = ThingSpeak.getFieldAsInt(3); // Field 3
}
You can read the status, position information and all eight fields. See the help file at the GitHub repo for the ThingSpeak Arduino Library for more information. Your device will save the information locally until you repeat the call to readMultipleFields again.
  1 件のコメント
Suraj
Suraj 2022 年 10 月 15 日
編集済み: Suraj 2022 年 10 月 15 日
i am not getting data in field whereas program load in esp8266 successfully
please help
i am using multiple field i.e field 1 and field2

サインインしてコメントする。

その他の回答 (1 件)

Milena Mendez
Milena Mendez 2022 年 11 月 26 日
編集済み: Milena Mendez 2022 年 11 月 26 日
Hello
You can use the following sentence from your scrip, according with the values to publish:
thingSpeakWrite(channel number, [value1, value2,..., valueN],'WriteKey', appKey);

コミュニティ

その他の回答  ThingSpeak コミュニティ

カテゴリ

Help Center および File ExchangeRead Data from Channel についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by