今天要講解如標題一樣,Google Sheet是Google所開發的試算表,所以我們要用Google Sheet來存放我們所感測到的數值,那廢話不多說,直接開始。
依照Day25 Esp32s + IFTTT + LINE這篇一樣設置Webhook,到Then That會稍微不同(因為要選擇Google sheet服務)
相同在搜尋欄查找Google Sheet
圖片取自:IFTTT
選擇左邊Add row to spreadsheet
圖片取自:IFTTT
這邊跟上次設置Line相同,可以自己調整格式
圖片取自:IFTTT
如果設定完就可以將事件創建起來囉,後續一樣到Webhook的Documentation,測試是否可以執行成功。
※EventName記得改
圖片取自:IFTTT
觸發成功後,到Google雲端硬碟查看是否多出IFTTT資料夾
圖片取自:IFTTT
如果沒有此資料夾,可能是上面Google Sheet設定中有更動資料夾位置,又或者目前IFTTT連接Google Sheet有問題。
點開後會看見試算表,可以點開看看是否有你剛剛輸入的數值
圖片取自:Google
如果可以看見就代表測試成功囉~
圖片取自:使用者繪製
Esp32s
GND
-> St01(C)-
Esp32sVcc(5V)
-> St01(C)+
Esp32s22
-> St01(C)SCL
Esp32s21
-> St01(C)SDA
第10、11行,需更改ssid及password
第14行,需更改事件名稱及Key
#include <WiFi.h>
#include <WiFiClient.h>
#include <AHT10.h>
#include <Wire.h>
uint8_t readStatus = 0;
AHT10 myAHT10(AHT10_ADDRESS_0X38);
// Replace with your SSID and Password
const char* ssid = "xxxx";
const char* password = "xxxxxxxx";
// Replace with your unique IFTTT URL resource
const char* resource = "/trigger/Esp32sAHT10GoogleSheet/with/key/cEf5MJB";
// How your resource variable should look like, but with your own API KEY (that API KEY below is just an example):
//const char* resource = "/trigger/myAHT1011_readdata/with/key/nxxxxxxL2d_XXXXXgTlNSrxMJdmqy5";
// Maker Webhooks IFTTT
const char* server = "maker.ifttt.com";
// Time to sleep
uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds
// sleep for 30 minutes = 1800 seconds
uint64_t TIME_TO_SLEEP = 60; //1 minutes
void setup() {
Serial.begin(115200);
myAHT10.begin(); // 初始化
delay(1000);
}
void loop() {
initWifi();
makeIFTTTRequest();
//delay(20000); //Send Time
// enable timer deep sleep
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
Serial.println("Going to sleep now");
// start deep sleep for 3600 seconds (60 minutes)
esp_deep_sleep_start();
}
// Establish a Wi-Fi connection with your router
void initWifi() {
Serial.print("Connecting to: ");
Serial.print(ssid);
WiFi.begin(ssid, password);
int timeout = 10 * 4; // 10 seconds
while(WiFi.status() != WL_CONNECTED && (timeout-- > 0)) {
delay(250);
Serial.print(".");
}
Serial.println("");
if(WiFi.status() != WL_CONNECTED) {
Serial.println("Failed to connect, going back to sleep");
}
Serial.print("WiFi connected in: ");
Serial.print(millis());
Serial.print(", IP address: ");
Serial.println(WiFi.localIP());
}
// Make an HTTP request to the IFTTT web service
void makeIFTTTRequest() {
Serial.print("Connecting to ");
Serial.print(server);
WiFiClient client;
int retries = 5;
while(!!!client.connect(server, 80) && (retries-- > 0)) {
Serial.print(".");
}
Serial.println();
if(!!!client.connected()) {
Serial.println("Failed to connect...");
}
Serial.print("Request resource: ");
Serial.println(resource);
// Temperature in Celsius
String jsonObject = String("{\"value1\":\"") + myAHT10.readTemperature() + "\",\"value2\":\"" + myAHT10.readHumidity() + "\"}";
client.println(String("POST ") + resource + " HTTP/1.1");
client.println(String("Host: ") + server);
client.println("Connection: close\r\nContent-Type: application/json");
client.print("Content-Length: ");
client.println(jsonObject.length());
client.println();
client.println(jsonObject);
int timeout = 5 * 10; // 5 seconds
while(!!!client.available() && (timeout-- > 0)){
delay(100);
}
if(!!!client.available()) {
Serial.println("No response...");
}
while(client.available()){
Serial.write(client.read());
}
Serial.println("\nclosing connection");
client.stop();
}
密密麻麻數值,如果嫌時間太快可以嘗試更動delay的數值,使時間可以更快,像預設大約10分鐘,而我展示圖片將延遲降低至1分鐘。
圖片取自:Google
好了,這次是教各位儲放資料的方式,在網路上有很多提供儲存資料的服務,像是Thingspeak不僅有提供資料還有圖表可以提供查看,有興趣的人可以去研究看看,那今天的部分就這樣囉明天再見~。