Advertisement
LeventeDaradici

ESP8266 Wemos D1 mini, DHT22 and Google nest mini

Apr 22nd, 2022
999
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <NTPClient.h>
  3. #include <esp8266-google-home-notifier.h>
  4. #include "DHT.h"
  5.  
  6. #define DHTPIN 2
  7. #define DHTTYPE DHT22
  8.  
  9. DHT dht(DHTPIN, DHTTYPE);
  10. int STARTHOUR = 9;                              //write here the time when the notifications start
  11. int ENDHOUR = 21;                               //write here the time to stop notifications
  12. const char* ssid     = "YOUR WIFI SSID";        //enter here your wifi SSID name
  13. const char* password = "*YOUR WIFI PASSWORD";   //enter here your wifi PASSWORD
  14. String Notificare ="";
  15.  
  16. unsigned long previous_time = 0;
  17. unsigned long pauza = 20000;  // 20 seconds delay
  18.  
  19. GoogleHomeNotifier ghn;
  20.  
  21. const long utcOffsetInSeconds = 10800;           //Edit here to get the right time for your area, delay is in seconds ! This value is valable for Romania CET
  22.  
  23. WiFiUDP ntpUDP;
  24. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  25.  
  26. String daysOfTheWeek[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"};
  27. String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
  28.  
  29. void initWiFi() {
  30.   WiFi.mode(WIFI_STA);
  31.   WiFi.begin(ssid, password);
  32.   Serial.print("Connecting to WIFI network");
  33.   while (WiFi.status() != WL_CONNECTED) {
  34.     Serial.print('.');
  35.     delay(1000);
  36.   }
  37.   Serial.println("connected.");
  38.   Serial.print("IP address: ");
  39.   Serial.println(WiFi.localIP());
  40. }
  41.  
  42. void setup()
  43.      {
  44.        Serial.begin(115200);
  45.        Serial.println("");
  46.        initWiFi();
  47.        const char displayName[] = "Entryway speaker";   //Enter the name of your google home speaker here
  48.  
  49.        Serial.println("connecting to Google Home...");
  50.        if (ghn.device(displayName, "en") != true)
  51.           {
  52.              Serial.println(ghn.getLastError());
  53.              return;
  54.           }
  55.        Serial.print("found Google Home(");
  56.        Serial.print(ghn.getIPAddress());
  57.        Serial.print(":");
  58.        Serial.print(ghn.getPort());
  59.        Serial.println(")");
  60.        dht.begin();  
  61.  
  62.        if (ghn.notify("Hi, My name is D1 Mini, and I will tell you, via google home, the exact time, temperature, and humidity, measured in your room.") != true)
  63.           {
  64.              Serial.println(ghn.getLastError());
  65.              return;
  66.           }
  67.        Serial.println("Done.");
  68.        delay(10000);  
  69.        if (ghn.notify("I will send notifications, to your google nest speaker, every 15 minutes, between the hours, set at the beginning of this code.") != true)
  70.           {
  71.              Serial.println(ghn.getLastError());
  72.              return;
  73.           }
  74.        Serial.println("Done.");
  75.        timeClient.update();
  76.        delay(10000);  
  77.      }
  78.  
  79. void loop()
  80.      {      
  81.         unsigned long current_time = millis();
  82.          if ((WiFi.status() != WL_CONNECTED) && (current_time - previous_time >= pauza))
  83.             {
  84.               Serial.print(millis());
  85.               Serial.println("Reconnecting to WIFI network");
  86.               WiFi.disconnect();
  87.               WiFi.reconnect();
  88.               previous_time = current_time;
  89.             }
  90.  
  91.            
  92.         if (timeClient.getHours() > STARTHOUR)
  93.            {
  94.               if (timeClient.getHours() < ENDHOUR)
  95.                  {
  96.                      if (timeClient.getMinutes() == 0)
  97.                         {
  98.                           VorbesteGoogle();
  99.                           delay(120000);
  100.                         }
  101.                      if (timeClient.getMinutes() == 15)
  102.                         {
  103.                           VorbesteGoogle();
  104.                           delay(120000);
  105.                         }
  106.                      if (timeClient.getMinutes() == 30)
  107.                         {
  108.                           VorbesteGoogle();
  109.                           delay(120000);
  110.                         }
  111.                       if (timeClient.getMinutes() == 45)
  112.                         {
  113.                           VorbesteGoogle();
  114.                           delay(120000);
  115.                         }
  116.                  }
  117.            }
  118.      }
  119.  
  120. void VorbesteGoogle()
  121.      {
  122.         timeClient.update();
  123.         unsigned long epochTime = timeClient.getEpochTime();
  124.         struct tm *ptm = gmtime ((time_t *)&epochTime);
  125.         int monthDay = ptm->tm_mday;
  126.         int currentMonth = ptm->tm_mon+1;
  127.         int currentYear = ptm->tm_year+1900;
  128.         String currentMonthName = months[currentMonth-1];
  129.  
  130.         float h = dht.readHumidity();
  131.         float t = dht.readTemperature();
  132.         float f = dht.readTemperature(true);
  133.  
  134.        if (isnan(h) || isnan(t) || isnan(f))
  135.           {
  136.              Serial.println(F("Failed to read from DHT sensor!"));
  137.              return;
  138.           }
  139.    
  140.         Notificare = Notificare +"It's, ";
  141.         Notificare = Notificare +daysOfTheWeek[timeClient.getDay()];
  142.         Notificare = Notificare +", ";
  143.         Notificare = Notificare +monthDay;
  144.         Notificare = Notificare +currentMonthName;
  145.         Notificare = Notificare +", ";
  146.         Notificare = Notificare +currentYear;
  147.         Notificare = Notificare +",The time is, ";
  148.         Notificare = Notificare + timeClient.getHours();
  149.         Notificare = Notificare +",and ";
  150.         Notificare = Notificare + timeClient.getMinutes();
  151.         Notificare = Notificare +"minutes, ";
  152.         Notificare = Notificare +"The temperature is, ";
  153.         Notificare = Notificare + String(t,1);
  154.         Notificare = Notificare + "degrees celsius,";
  155.         Notificare = Notificare + "the relative Humidity is,";
  156.         Notificare = Notificare + String(h,1);
  157.         Notificare = Notificare + "percent.";
  158.  
  159.         if (ghn.notify (Notificare.c_str()) != true)
  160.            {
  161.               Serial.println(ghn.getLastError());
  162.               return;
  163.            }
  164.         Notificare = "";
  165.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement