08 IOT
(2020/12/06)
https://thestempedia.com/tutorials/what-is-iot-getting-started-with-iot-using-adafruit-io-and-pictoblox/
https://thestempedia.com/docs/dabble/
https://thestempedia.com/tutorials/how-to-send-data-to-cloud-thingspeak-using-esp8266/
https://thestempedia.com/tutorials/how-to-retrieve-data-from-cloud-thingspeak-using-esp8266/
https://thestempedia.com/tutorials/how-get-weather-data-from-location-using-openweathermap/
一、上傳資料到Thingspeak
進入Thingspeak https://thingspeak.com/ (免費帳號只能建4個Channel)
欲上傳溫度、濕度
程式:(一定要用上傳模式)
(接在上圖後面)
結果:
二、擷取Thingspeak上的資料
讀取資料並顥示在序列埠上
三、HTTP API Request(s)
https://medium.com/itsems-frontend/api-%E6%98%AF%E4%BB%80%E9%BA%BC-restful-api-%E5%8F%88%E6%98%AF%E4%BB%80%E9%BA%BC-a001a85ab638
https://blynkapi.docs.apiary.io/#reference/0/qr-for-project-cloning
https://mlsdev.com/blog/81-a-beginner-s-tutorial-for-understanding-restful-api
使用API Request將資料上傳到Thingspeak
Thingspeak的API Keys項目可以看到相關資訊,如下
採用上面第一條將資料寫進channel的方法
程式如下:
但發現此GET積木只能用在舞台模式,上傳模式無法使用
結果
上傳溫濕度到Google試算表
要上傳【溫度】、【濕度】到 Google試算表
建立 Google Sheets,於第一列填入表格的表頭
進入 Google Sheets 指令碼編輯器
接著寫入 Function doGet,如下程式碼所示
function doGet(e) {
//取得參數
var params = e.parameter;
var temp = params.temp;
var humidity = params.humidity;
//sheet資訊
var SpreadSheet = SpreadsheetApp.openById("1rilPSySRCNs9RYEgg2lMQbJX1pt79CfQlQ_bfEtWba8");
var Sheet = SpreadSheet.getSheets()[0];
var LastRow = Sheet.getLastRow();
//存入資訊
Sheet.getRange(LastRow+1, 1).setValue(temp);
Sheet.getRange(LastRow+1, 2).setValue(humidity);
//回傳資訊
return ContentService.createTextOutput(“成功”);
}
程式碼說明
doGet 代表 API method 爲 get
取得參數:傳入的全部參數以 e.parameter 取得,再分別以其他變數存放個別參數
Sheet 資訊:程式碼中的 “請輸入自己的 sheet id”,請複製你的 Google Sheet 網址列 https://docs.google.com/spreadsheets/d/ 以後至 edit 中間的代碼。SpreadSheet.getSheets()[0]; 取得要存入的試算表的第一張試算表。Sheet.getLastRow(); 取得該張試算表中,最後一列有值的列數。
存入資訊:Sheet.getRange(LastRow+1, 1).setValue(name); 將資料存入最後有值的下一列,第一欄。其他以此類推。
回傳資訊:return ContentService.createTextOutput(“成功”); 存入過程無誤,回傳成功資訊。若需要其他驗證條件也可以寫 if 條件判斷,並於失敗時,回傳失敗資訊。
部署
部署爲網路應用程式
將具有應用程式存取權的使用者改爲 “任何人"
點選核對權限 -> 選擇自己的帳戶 -> 進階 -> 前往 (不安全) -> 允許,
接著就會看到以下畫面,網路應用程式網址即爲 Call API 的 URL。
要把URL複製下來,如果沒複製再點【發布】/【部署爲網路應用程式】就可找到
到PictoBlox寫程式
程式寫法與上面上傳到Thingspeak 一樣
結果,成功上傳了
從Google試算表下載資料
然後會出現一串網址,別管它,直接點右上的 x 關閉。
取用 Google Sheet 的網址規則,如下
https://spreadsheets.google.com/feeds/cells/[KEY]/[SHEET INDEX]/public/values?alt=json
KEY 同上面介紹過的部份
SHEET INDEX就是第幾個工作表,目前為1
所以,要取本工作表資料的網址:
https://spreadsheets.google.com/feeds/cells/1nRFkQyv4SEv-ZiDVex4xYUYumIFzRm-RoYuDDTHm_Xg/1/public/values?alt=json
程式:
取得JSON格式的檔案,但不知如何用 取得格式http://json.parser.online.fr/