#ifdef ESP32 #include #else #include #endif #include #include //************************************************************************************** #define DHTPIN 16 //DHT module attached to D16 (RX2) pin of ESP32 development board // Uncomment one of the lines below for whatever DHT sensor type you're using! #define DHTTYPE DHT11 // DHT 11 //#define DHTTYPE DHT21 // DHT 21 (AM2301) //#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321 // Replace with your SSID and Password const char* ssid = "TR-2"; const char* password = "1234"; // Replace with your unique IFTTT URL resource const char* resource = "/trigger/DHTreadings/with/key/bsA-7n0Ms0FOl-mlOMqIx_"; // 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 = 1800; // Initialize DHT sensor for normal 16mhz Arduino DHT dht(DHTPIN, DHTTYPE); float h,t,f; //****************************************************************** void setup() { Serial.begin(115200); delay(2000); dht.begin(); // initialize dht // Check if any reads failed and exit early (to try again). if (isnan(h) || isnan(t) || isnan(f)){ Serial.println("Failed DHT"); return; } initWifi(); makeIFTTTRequest(); #ifdef ESP32 // 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(); #else // Deep sleep mode for 3600 seconds (60 minutes) Serial.println("Going to sleep now"); ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR); #endif } //*********************************************************** void loop() { } //************************************************************ // 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); // Reading temperature or humidity takes about 250 milliseconds! // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor) h = dht.readHumidity(); // Read temperature as Celsius t = dht.readTemperature(); // Read temperature as Fahrenheit f = dht.readTemperature(true); // Compute heat index in Fahrenheit (the default) float hif = dht.computeHeatIndex(f, h); // Compute heat index in Celsius (isFahreheit = false) float hic = dht.computeHeatIndex(t, h, false); Serial.print("Humidity: "); Serial.print(h); Serial.println(" %\t"); Serial.print("Temperature: "); Serial.print(t); Serial.print(" *C "); Serial.print(f); Serial.println(" *F\t"); Serial.print("Heat index: "); Serial.print(hic); Serial.print(" *C "); Serial.print(hif); Serial.println(" *F"); String jsonObject = String("{\"value1\":\"") + h + "\",\"value2\":\"" + t + "\",\"value3\":\"" + f + "\"}"; 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(); }