ESP32 Chapter 10: Being A Weather Forecaster with DHT11

Google Sheets Data Storage

Source: randomnerdtutorials.com
#include <WiFi.h>#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
// Replace with your SSID and Password
const char* ssid = "THALIA";
const char* password = "********";
// Replace with your unique IFTTT URL resource
const char* resource = "<<IFTTT-URL-Resource>>";
// 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 10 minutes = 600 seconds
uint64_t TIME_TO_SLEEP = 600;
#define DHTPIN 4
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);void setup() {
Serial.begin(115200);
delay(2000);
dht.begin();
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() {
// sleeping so wont get here
}
// 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\":\"") + dht.readTemperature() + "\",\"value2\":\"" + (1.8 * dht.readTemperature() + 32) + "\",\"value3\":\"" + dht.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();
}

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

How to develop Server Apps like a boss

How Spotinst in saving cloud infrastructure cost

Algos: RAM, Binary, Fixed-width Intergers

LinkedIn Bio: Skyscrapers and Mountains

HTB Vpn on Kasm using KALI made easy.

ssh and login to your ssh KASM server

Pinguuu Report: 3

Interviews

4 Ways to Install Firefox 98.0 on Ubuntu / Linux Mint / AlmaLinux & Fedora

Install Firefox 98.0 on ubuntu

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Christovito Hidajat

Christovito Hidajat

More from Medium

Touch, Hall Effect, and Temperature Inbuilt Sensors in ESP32

ESP32 Project 5: Output-Display & PWM

ESP32 Project 1: LED Blink

My Very First, Embedded System, Project: ESP32 Internal LED Blink