//Test20-spreatsheet_DHT11 #include //#include #include #include #include "arduino_secrets.h" const char ssid[] = SECRET_SSID; // SSID const char pass[] = SECRET_PASS; // password // DHTセンサーの設定 #define DHTPIN 11//2 // DHTセンサーのデータピン #define DHTTYPE DHT11 // 使用するDHTセンサーのタイプ DHT dht(DHTPIN, DHTTYPE); void setup() { // put your setup code here, to run once: Serial.begin(9600); pinMode(0, INPUT_PULLUP); // Initialize the BUILTIN_LED pin as an output //wifi // Wi-Fi接続を試みる while (WiFi.begin(ssid, pass) != WL_CONNECTED) { delay(1000); Serial.println("Wi-Fi接続中..."); } Serial.println("Wi-Fi接続成功!"); // DHTセンサーの初期化 dht.begin(); } int counter = 0; void loop() { // 温湿度データを読み取る float humidity = dht.readHumidity(); float temperature = dht.readTemperature(); // 読み取りが失敗した場合のエラーチェック if (isnan(humidity) || isnan(temperature)) { Serial.println("温湿度の読み取りに失敗しました"); return; } Serial.print("カウンタ: "); Serial.print(counter); // 読み取ったデータをシリアルモニタに表示 Serial.print("温度: "); Serial.print(temperature); Serial.print("℃ 湿度: "); Serial.print(humidity); Serial.println("%"); String sendData = "?temperature=" + String(temperature) + "&humidity=" + String(humidity); // String sendData = "?counter=" + String(counter) + "&temperature=" + String(temperature) + "&humidity=" + String(humidity); gasSend(sendData); delay(10000); counter++; } int gasSend(String data) { const char* host = "script.google.com"; WiFiSSLClient client; Serial.println("Try"); //LineのAPIサーバに接続 if (!client.connect(host, 443)) {//httpsはポート443 Serial.println("Connection failed"); return(0); } Serial.println("Connected"); const String url = "https://script.google.com/macros/s/??????????/exec"; String URL = url + data; Serial.println(URL); client.println("GET " + URL + " HTTP/1.1"); client.println("Host: " + String(host)); client.println("Connection: close"); client.println(); Serial.println("client.connect待ち"); while (client.connected()) { String line = client.readStringUntil('\n'); if (line == "\r") { Serial.println("headers received"); break; } } // if there are incoming bytes available // from the server, read them and print them: while (client.available()) { char c = client.read(); Serial.write(c); } client.stop(); Serial.println("client.stop()"); }