ThingSpeak™ is an IoT analytics platform service that allows you to aggregate, visualize and analyze live data streams in the cloud. ThingSpeak provides instant visualizations of data posted by your devices to ThingSpeak. With the ability to execute MATLAB® code in ThingSpeak you can perform online analysis and processing of the data as it comes in. ThingSpeak is often used for prototyping and proof of concept IoT systems that require analytics.
申請 ThingSpeak IoT 平台免費使用帳號,建立要接收 ESP8266 上傳感測資料之 channel & fields。以瀏覽器工具,練習使用 ThingSpeak API 傳送資料至ThingSpeak IoT 平台
這裡要先到個人 email 信箱去收信,點擊信件中確認 email 的連結
email 確認完成,回到剛剛的註冊頁去
提供密碼,密碼須滿足下面條件
Between 8-50 characters
At least 1 upper and lower case letter
At least 1 number
註冊完成
提供 Channel 資料,假設這裡要記錄三種感測數據,就啟用三個 Fields
網路上查桃園市經緯度
Decimal latitude and longitude coordinates for Taoyuan City (Taiwan): 24.99368, 121.29696
建立的 Channel,都有獨特的 Channel ID。
紀錄下 Channel ID,使用 HTTP API 讀寫 Channel 會使用此參數,設計控制板程式也會使用此參數
初始預設是僅供個人有瀏覽權限。到 Private View 可以設定每個 Field 的參數。
Channel ID: 1408098
設定 Field 參數
設定 Field 2 參數
設定 Field 3 參數
要傳送數據到 ThingSpeak 或從 ThingSpeak 個人 Channel 讀取數據,需要專屬的金鑰(API Key),API Key 需紀錄保存。任何人只要有 API Key 就可以將資料寫入/讀出你的 Channel 中
紀錄下寫入資料時使用之 Write API Key 與 讀取資料時使用之 Read API Key。
NodeMCU 寫入/讀取資料時,分別會使用到 Write API Key/Read API Key。
設定共享(Public),可允許任何人瀏覽你的 Channel
設定共享(Public),允許特定人(提供 Email 帳號)瀏覽你的 Channel
設定分享後,在 Public View 就會看到分享的 Channel,Access 會由 Private 更改為 Public
修改 Public View 的 Field 參數:Public View 的 Field 參數與 Private View 是獨立的。
各 Field 設定參數與 Private View 相同
ThingSpeak REST API 使用語法,參考網站提供的 REST API 使用說明
依網頁說明 HTTP GET 更新 channel 中 field1 資料,API 語法可使用如下三種:
https://api.thingspeak.com/update?api_key=[Write API Key]&field1=xxx
回傳寫入資料第?筆數(只回傳資料點數)
https://api.thingspeak.com/update.json?api_key=[Write API Key]&field1=xxx
回傳寫入資料第?筆數( json 格式)
https://api.thingspeak.com/update.xml?api_key=[Write API Key]&field1=xxx
回傳寫入資料第?筆數( xml 格式)
這裡以第一種 API 語法實作
先找出要寫入 channel 的 Write API Key,例如:EH94XBVULOF80XTC
開瀏覽器,網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field1=666 ,按 ENTER
收到 ThingSpeak 回應 1
每隔 15 秒以上,依序
網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field2=1
收到 ThingSpeak 回應 2
網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field3=888
收到 ThingSpeak 回應 3
資料進 ThingSpeak 了
每隔 15 秒以上,依序
網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field1=888
收到 ThingSpeak 回應 4
網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field2=1
收到 ThingSpeak 回應 5
網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field3=168
收到 ThingSpeak 回應 6
一次寫入多個 Fields
https://api.thingspeak.com/update?api_key=API_Key&field1=???&field2=???&field3=???
每隔 15 秒以上,依序
網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field1=147&field2=1&field3=35&field4=80
網址輸入 https://api.thingspeak.com/update?api_key=EH94XBVULOF80XTC&field1=258&field2=0&field3=25&field4=50
使用 arduino C 取亂數語法,建立四個亂數,模擬光敏電阻類比、數位訊號、DHT11溫度、濕度訊號,並將四個模擬數據上傳 ThingSpeak IoT 平台
開瀏覽器,連線
或 離線版 TUNIOT(執行資料夾中 demo/code/index.html)
Various > Program started since
millis()
Various > Arduino code
IOT > Thingspeak > if Connect Website api.thingspeak.com Port 80 do ....
#include <ESP8266HTTPClient.h>
WiFiClient client;
String thingSpeakAddress= "http://api.thingspeak.com/update?";
String writeAPIKey;
String tsfield1Name;
String request_string;
HTTPClient http;
loop()
if (client.connect("api.thingspeak.com",80)) {
request_string = thingSpeakAddress;
request_string += "key=";
request_string += "";
request_string += "&";
request_string += "field1=";
request_string += "";
http.begin(request_string);
http.GET();
http.end();
request_string="";
}
IOT > Thingspeak > if Connect Website api.thingspeak.com Port 80 do ....
#include <ESP8266HTTPClient.h>
WiFiClient client;
String thingSpeakAddress= "http://api.thingspeak.com/update?";
String writeAPIKey;
String tsfield1Name;
String request_string;
HTTPClient http;
loop()
if (client.connect("api.thingspeak.com",80)) {
request_string = thingSpeakAddress;
request_string += "key=";
request_string += "";
request_string += "&";
request_string += "field1";
request_string += "=";
request_string += "";
http.begin(request_string);
http.GET();
http.end();
}
完成之積木程式
插入的 arduino code:
randomSeed(analogRead(0));
儲存積木檔(.xml),儲存 Arduino 程式碼(.ino)
Arduino IDE 開啟載入程式碼(.ino),選擇正確控制板,選擇正確連接埠,上傳燒錄。開啟序列埠視窗,baudrate 9600
啟用序列埠視窗的時間戳記,可 show 出時間戳記
瀏覽剛剛建立的 Channel > Private View,會看到資料已經發佈到各 Fields 了
Channel Private View 網址為:https://thingspeak.com/channels/1408098/private_show,其中紅色部分為 Channel ID
瀏覽剛剛建立的 Channel > Public View,會看到相同的資料
Channel Public View 網址為:https://thingspeak.com/channels/1408098,其中紅色部分為 Channel ID
傳送 2 Fields, 光敏電阻類比、數位感測數值
開瀏覽器,連線
或 離線版 TUNIOT(執行資料夾中 demo/code/index.html)
完成之積木程式
儲存積木檔(.xml),儲存 Arduino 程式碼(.ino)
同 3.1
開瀏覽器,連線
或 離線版 TUNIOT(執行資料夾中 demo/code/index.html)
完成之積木程式
手機開 Google Play ,搜尋 ThingSpeak viewer,安裝
開啟 App,新增 Channel
當光敏電阻受光亮低時(>800),同時開啟 D0 LED,也把 D0 LED 狀態傳送到 ThingSpeak IoT
開瀏覽器,連線
或 離線版 TUNIOT(執行資料夾中 demo/code/index.html)
完成之積木程式
dht4.begin();
swState = digitalRead(16);