Advertisement
LeventeDaradici

ESP8266 Wemos D1 mini, DHT22 and Google nest mini - HUNGARIAN

Apr 23rd, 2022
1,328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.91 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     = "NETSTREAM";        //enter here your wifi SSID name
  13. const char* password = "*#Magda2007";   //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]={"vasárnap", "hétfő", "kedd", "szerda", "csütörtök", "péntek", "szombat"};
  27. String months[12]={"január", "február", "március", "április", "május", "június", "július", "augusztus", "szeptember", "október", "november", "december"};
  28.  
  29. void initWiFi()
  30.   {
  31.      WiFi.mode(WIFI_STA);
  32.      WiFi.begin(ssid, password);
  33.      Serial.print("Connecting to WIFI network");
  34.      while (WiFi.status() != WL_CONNECTED) {
  35.      Serial.print('.');
  36.      delay(1000);
  37.   }
  38.   Serial.println("connected.");
  39.   Serial.print("IP address: ");
  40.   Serial.println(WiFi.localIP());
  41. }
  42.  
  43. void setup()
  44.      {
  45.        Serial.begin(115200);
  46.        Serial.println("");
  47.        initWiFi();
  48.        const char displayName[] = "Entryway speaker";   //Enter the name of your google home speaker here
  49.  
  50.        Serial.println("connecting to Google Home...");
  51.        if (ghn.device(displayName, "hu") != true)
  52.           {
  53.              Serial.println(ghn.getLastError());
  54.              return;
  55.           }
  56.        Serial.print("found Google Home(");
  57.        Serial.print(ghn.getIPAddress());
  58.        Serial.print(":");
  59.        Serial.print(ghn.getPort());
  60.        Serial.println(")");
  61.        dht.begin();  
  62.  
  63.        if (ghn.notify("Szia, a nevem D1 Mini, és el fogom mondani neked, a google home hangszórón keresztül, a pontos idöt, hömérsékletet, és páratartalmat, amit a szobádban mértem.") != true)
  64.           {
  65.              Serial.println(ghn.getLastError());
  66.              return;
  67.           }
  68.        Serial.println("Done.");
  69.        delay(15000);  
  70.        if (ghn.notify("A memóriámba betöltött kód elején, beállított órák között, 15 percenként, küldök értesítést, a hangszóródra.") != true)
  71.           {
  72.              Serial.println(ghn.getLastError());
  73.              return;
  74.           }
  75.        Serial.println("Done.");
  76.        timeClient.update();
  77.        delay(20000);  
  78.      }
  79.  
  80. void loop()
  81.      {            
  82.         unsigned long current_time = millis();
  83.          if ((WiFi.status() != WL_CONNECTED) && (current_time - previous_time >= pauza))
  84.             {
  85.               Serial.print(millis());
  86.               Serial.println("Reconnecting to WIFI network");
  87.               WiFi.disconnect();
  88.               WiFi.reconnect();
  89.               previous_time = current_time;
  90.             }
  91.  
  92.            
  93.         if (timeClient.getHours() > STARTHOUR)
  94.            {
  95.               if (timeClient.getHours() < ENDHOUR)
  96.                  {
  97.                      if (timeClient.getMinutes() == 0)
  98.                         {
  99.                           VorbesteGoogle();
  100.                           delay(120000);
  101.                         }
  102.                      if (timeClient.getMinutes() == 15)
  103.                         {
  104.                           VorbesteGoogle();
  105.                           delay(120000);
  106.                         }
  107.                      if (timeClient.getMinutes() == 30)
  108.                         {
  109.                           VorbesteGoogle();
  110.                           delay(120000);
  111.                         }
  112.                       if (timeClient.getMinutes() == 45)
  113.                         {
  114.                           VorbesteGoogle();
  115.                           delay(120000);
  116.                         }
  117.                  }
  118.            }
  119.      }
  120.  
  121. void VorbesteGoogle()
  122.      {
  123.         timeClient.update();
  124.         unsigned long epochTime = timeClient.getEpochTime();
  125.         struct tm *ptm = gmtime ((time_t *)&epochTime);
  126.         int monthDay = ptm->tm_mday;
  127.         int currentMonth = ptm->tm_mon+1;
  128.         int currentYear = ptm->tm_year+1900;
  129.         String currentMonthName = months[currentMonth-1];
  130.  
  131.         float h = dht.readHumidity();
  132.         float t = dht.readTemperature();
  133.         float f = dht.readTemperature(true);
  134.  
  135.        if (isnan(h) || isnan(t) || isnan(f))
  136.           {
  137.              Serial.println(F("Failed to read from DHT sensor!"));
  138.              return;
  139.           }
  140.    
  141.         Notificare = Notificare +"Ma, ";
  142.         Notificare = Notificare +daysOfTheWeek[timeClient.getDay()];
  143.         Notificare = Notificare +", ";
  144.         Notificare = Notificare +currentMonthName;
  145.         Notificare = Notificare +monthDay;
  146.         Notificare = Notificare +", ";
  147.         Notificare = Notificare +currentYear;
  148.         Notificare = Notificare +",a pontos idö, ";
  149.         Notificare = Notificare + timeClient.getHours();
  150.         Notificare = Notificare +"óra, és ";
  151.         Notificare = Notificare + timeClient.getMinutes();
  152.         Notificare = Notificare +"perc, ";
  153.         Notificare = Notificare +"a hőmérséklet, ";
  154.         Notificare = Notificare + String(t,1);
  155.         Notificare = Notificare + "fok Celzius,";
  156.         Notificare = Notificare + "és a relatív páratartalom pedig,";
  157.         Notificare = Notificare + String(h,1);
  158.         Notificare = Notificare + "százalék.";
  159.  
  160.         if (ghn.notify (Notificare.c_str()) != true)
  161.            {
  162.               Serial.println(ghn.getLastError());
  163.               return;
  164.            }
  165.         Notificare = "";
  166.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement