hrsajjad844
Published © MIT

DHT22 to Google sheet & Reset Using ESP8266, LED, Switch

If the button pressed esp will reset and App mode will on. App mode will on then led will on, network is connected led will off.

AdvancedShowcase (no instructions)588
DHT22 to Google sheet & Reset Using ESP8266, LED, Switch

Things used in this project

Hardware components

ESP8266 ESP-12E
Espressif ESP8266 ESP-12E
×1
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
5 mm LED: Red
5 mm LED: Red
×1
Switch Actuator, Head for spring return push-button
Switch Actuator, Head for spring return push-button
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
USB-A to Mini-USB Cable
USB-A to Mini-USB Cable
×1
Resistor 10k ohm
Resistor 10k ohm
×1

Story

Read more

Schematics

Pin Diagram

Here is the define all pin

Code

Code

C/C++
Here is the project code...
//----------------------------------------Include the NodeMCU ESP8266 Library
//----------------------------------------see here: https://www.youtube.com/watch?v=8jMr94B8iN0 to add NodeMCU ESP12E ESP8266 library and board (ESP8266 Core SDK)
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <DNSServer.h>
#include <WiFiManager.h>
#include <DHT.h>
//----------------------------------------

int button = D3;
int led = D1;
int temp = 0;

#define DHTTYPE DHT22 //--> Defines the type of DHT sensor used (DHT11, DHT21, and DHT22), in this project the sensor used is DHT11.

const int DHTPin = D2; //--> The pin used for the DHT11 sensor is Pin D1 = GPIO5
DHT dht(DHTPin, DHTTYPE); //--> Initialize DHT sensor, DHT dht(Pin_used, Type_of_DHT_Sensor);

//----------------------------------------Host & httpsPort
const char* host = "script.google.com";
const int httpsPort = 443;
//----------------------------------------

WiFiClientSecure client; //--> Create a WiFiClientSecure object.

String GAS_ID = "AKfycbwriCAA9Gjh9V0sspRh8l_PPrfINCba-OwKsjBFoElH1B0ITnnGUqn9-1gTVRBLi3b-"; //--> spreadsheet script ID

//--------------------------------------- void setup
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  delay(500);

  pinMode(button, INPUT);
  pinMode(led, OUTPUT);
  Serial.begin(115200);

  dht.begin();  //--> Start reading DHT11 sensors
  delay(500);
  //----------------------------------------Wait for connection
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    digitalWrite(D1, HIGH);
    Serial.print(".");

    WiFiManager wifiManager;
    wifiManager.autoConnect("Harun");
    Serial.println("Connected");
  }


  //----------------------------------------If successfully connected to the wifi router, the IP Address that will be visited is displayed in the serial monitor
  Serial.println("");
  Serial.print("Successfully connected to : ");
  //  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println();
  //--------------------------------

  client.setInsecure();
}

//-----------------resetfunction
void resetwifi() {
  WiFiManager wm;
  wm.resetSettings();
  ESP.restart();
}

void resetwifi() {
  WiFiManager wm;
  wm.resetSettings();
  Serial.println("reset Done");
}


//----------------------------------void loop
void loop() {

  int temp = digitalRead(button);

  if (temp == LOW) {
    resetwifi();
    ESP.restart();
    Serial.println("LED ON");

  }
  else {
    digitalWrite(led, LOW);
    Serial.println("LED OFF");
  }
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  int h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor !");
    delay(500);
    return;
  }
  String Temp = "Temperature : " + String(t) + " C";
  String Humi = "Humidity : " + String(h) + " %";
  Serial.println(Temp);
  Serial.println(Humi);

  sendData(t, h); //--> Calls the sendData Subroutine
}
//============================================================================== void sendData
// Subroutine for sending data to Google Sheets
void sendData(float tem, int hum) {
  Serial.println("==========");
  Serial.print("connecting to ");
  Serial.println(host);

  //----------------------------------------Connect to Google host
  if (!client.connect(host, httpsPort)) {
    Serial.println("connection failed");
    return;
  }
  //----------------------------------------

  //----------------------------------------Processing data and sending data
  String string_temperature =  String(tem);
  // String string_temperature =  String(tem, DEC);
  String string_humidity =  String(hum, DEC);
  String url = "/macros/s/" + GAS_ID + "/exec?temperature=" + string_temperature + "&humidity=" + string_humidity;
  Serial.print("requesting URL: ");
  Serial.println(url);

  client.print(String("GET ") + url + " HTTP/1.1\r\n" +
               "Host: " + host + "\r\n" +
               "User-Agent: BuildFailureDetectorESP8266\r\n" +
               "Connection: close\r\n\r\n");

  Serial.println("request sent");
  //----------------------------------------

  //----------------------------------------Checking whether the data was sent successfully or not
  while (client.connected()) {
    String line = client.readStringUntil('\n');
    if (line == "\r") {
      Serial.println("headers received");
      break;
    }
  }
  String line = client.readStringUntil('\n');
  if (line.startsWith("{\"state\":\"success\"")) {
    Serial.println("esp8266/Arduino CI successfull!");
  } else {
    Serial.println("esp8266/Arduino CI has failed");
  }
  Serial.print("reply was : ");
  Serial.println(line);
  Serial.println("closing connection");
  Serial.println("==========");
  Serial.println();
}

Credits

hrsajjad844
1 project • 16 followers
Contact

Comments

Please log in or sign up to comment.