// D0 = 16; D1 = 5; D2 = 4;
// D3 = 0; D4 = 2; D5 = 14;
// D6 = 12; D7 = 13; D8 = 15;
// D9 = 3; D10 = 1;
#include <ESP8266WiFi.h>
#include "DHT.h"
// DHT11の設定
#define DHTPIN 5 // 使用するGPIOピン
#define DHTTYPE DHT11 // DHT 11
DHT dht(DHTPIN, DHTTYPE);
// WiFiアクセスポイントの設定
const char *WIFI_SSID = "PTS-2F";
const char *WIFI_PASSWORD = "";
// ITFFFの設定
const char *IFTTT_HOST = "maker.ifttt.com";
const char *IFTTT_URI = "/trigger/dht11_data/with/key/zaHdzsOGgZ71vZT1Y1FA6"; //zaHdzsOGgZ71vZT1Y1FA6
//enevnt name dht_read
//To trigger an Event
//Make a POST or GET web request to:
//https://maker.ifttt.com/trigger/{event}/with/key/zaHdzsOGgZ71vZT1Y1FA6
// DeepSleepのインターバル
//const unsigned long DEEP_SLEEP_INTERVAL = 60 * 60 * 1000 * 1000; // 1時間
const unsigned long DEEP_SLEEP_INTERVAL = 1 * 1 * 10 * 1000; // 10 sec
void setup() {
Serial.begin(115200);
dht.begin();
// WiFiに接続
Serial.print("connecting to ");
Serial.println(WIFI_SSID);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address is: ");
Serial.println(WiFi.localIP());
}
void loop() {
// DHT11から温度と湿度を読み込む
delay(2000);
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
Serial.println("Temperature: " + String(t) + "C Humidity: " + String(h) + "%");
// IFTTTに温度と湿度を送る
RequestToIFTTT("?value1=" + String(round(t)) + "&value2=" + String(round(h)));
// DeepSleepに入る
Serial.println("Go to sleep...");
//ESP.deepSleep(DEEP_SLEEP_INTERVAL, WAKE_RF_DEFAULT);
delay(20*1000);
Serial.println("DHT11 is working...");
}
// IFTTTにリクエストを送る
void RequestToIFTTT(String param) {
WiFiClientSecure client;
while (!client.connect(IFTTT_HOST, 443)) {
delay(10);
}
client.print(String("GET ") + IFTTT_URI + param +
" HTTP/1.1\r\n" +
"Host: " + IFTTT_HOST + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
Serial.println(String("GET ") + IFTTT_URI + param +
" HTTP/1.1\r\n" +
"Host: " + IFTTT_HOST + "\r\n" +
"User-Agent: ESP8266\r\n" +
"Connection: close\r\n\r\n");
while (!client.available()) {
delay(10);
}
Serial.println(client.readStringUntil('\r'));
client.flush();
client.stop();
}
沒有留言:
張貼留言