Advertisement
LeventeDaradici

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

Apr 23rd, 2022 (edited)
1,941
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.94 KB | None | 0 0
  1. //
  2. // https://www.youtube.com/c/LeventeDaradici/videos
  3. //
  4.  
  5. #include <ESP8266WiFi.h>
  6. #include <NTPClient.h>
  7. #include <esp8266-google-home-notifier.h>
  8. #include "DHT.h"
  9.  
  10. #define DHTPIN 2
  11. #define DHTTYPE DHT22
  12.  
  13. DHT dht(DHTPIN, DHTTYPE);
  14. int STARTHOUR = 9;                              //write here the time when the notifications start
  15. int ENDHOUR = 21;                               //write here the time to stop notifications
  16. const char* ssid     = "NETSTREAM";        //enter here your wifi SSID name
  17. const char* password = "*#Magda2007";   //enter here your wifi PASSWORD
  18. String Notificare ="";
  19.  
  20. unsigned long previous_time = 0;
  21. unsigned long pauza = 20000;  // 20 seconds delay
  22.  
  23. GoogleHomeNotifier ghn;
  24.  
  25. 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
  26.  
  27. WiFiUDP ntpUDP;
  28. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  29.  
  30. String daysOfTheWeek[7]={"Duminică", "Luni", "Marți", "Miercuri", "Joi", "Vineri", "Sîmbătă"};
  31. String months[12]={"Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octombrie", "Noiembrie", "Decembrie"};
  32.  
  33. void initWiFi()
  34.   {
  35.      WiFi.mode(WIFI_STA);
  36.      WiFi.begin(ssid, password);
  37.      Serial.print("Connecting to WIFI network");
  38.      while (WiFi.status() != WL_CONNECTED) {
  39.      Serial.print('.');
  40.      delay(1000);
  41.   }
  42.   Serial.println("connected.");
  43.   Serial.print("IP address: ");
  44.   Serial.println(WiFi.localIP());
  45. }
  46.  
  47. void setup()
  48.      {
  49.        Serial.begin(115200);
  50.        Serial.println("");
  51.        initWiFi();
  52.        const char displayName[] = "Entryway speaker";   //Enter the name of your google home speaker here
  53.  
  54.        Serial.println("connecting to Google Home...");
  55.        if (ghn.device(displayName, "ro") != true)
  56.           {
  57.              Serial.println(ghn.getLastError());
  58.              return;
  59.           }
  60.        Serial.print("found Google Home(");
  61.        Serial.print(ghn.getIPAddress());
  62.        Serial.print(":");
  63.        Serial.print(ghn.getPort());
  64.        Serial.println(")");
  65.        dht.begin();  
  66.  
  67.        if (ghn.notify("Bună, Numele meu este D1 Mini, și am să vă spun, via google home, ora exactă, temperatura, și umiditatea, măsurate în camera dumneavoastră.") != true)
  68.           {
  69.              Serial.println(ghn.getLastError());
  70.              return;
  71.           }
  72.        Serial.println("Done.");
  73.        delay(13000);  
  74.        if (ghn.notify("O să vă trimit notificări, pe boxa dumneavoastră, la fiecare 15 minute, între orele, stabilite la începutul codului încărcat în memoria mea.") != true)
  75.           {
  76.              Serial.println(ghn.getLastError());
  77.              return;
  78.           }
  79.        Serial.println("Done.");
  80.        timeClient.update();
  81.        delay(20000);  
  82.      }
  83.  
  84. void loop()
  85.      {      
  86.         unsigned long current_time = millis();
  87.          if ((WiFi.status() != WL_CONNECTED) && (current_time - previous_time >= pauza))
  88.             {
  89.               Serial.print(millis());
  90.               Serial.println("Reconnecting to WIFI network");
  91.               WiFi.disconnect();
  92.               WiFi.reconnect();
  93.               previous_time = current_time;
  94.             }
  95.  
  96.            
  97.         if (timeClient.getHours() > STARTHOUR)
  98.            {
  99.               if (timeClient.getHours() < ENDHOUR)
  100.                  {
  101.                      if (timeClient.getMinutes() == 0)
  102.                         {
  103.                           VorbesteGoogle();
  104.                           delay(120000);
  105.                         }
  106.                      if (timeClient.getMinutes() == 15)
  107.                         {
  108.                           VorbesteGoogle();
  109.                           delay(120000);
  110.                         }
  111.                      if (timeClient.getMinutes() == 30)
  112.                         {
  113.                           VorbesteGoogle();
  114.                           delay(120000);
  115.                         }
  116.                       if (timeClient.getMinutes() == 45)
  117.                         {
  118.                           VorbesteGoogle();
  119.                           delay(120000);
  120.                         }
  121.                  }
  122.            }
  123.      }
  124.  
  125. void VorbesteGoogle()
  126.      {
  127.         timeClient.update();
  128.         unsigned long epochTime = timeClient.getEpochTime();
  129.         struct tm *ptm = gmtime ((time_t *)&epochTime);
  130.         int monthDay = ptm->tm_mday;
  131.         int currentMonth = ptm->tm_mon+1;
  132.         int currentYear = ptm->tm_year+1900;
  133.         String currentMonthName = months[currentMonth-1];
  134.  
  135.         float h = dht.readHumidity();
  136.         float t = dht.readTemperature();
  137.         float f = dht.readTemperature(true);
  138.  
  139.        if (isnan(h) || isnan(t) || isnan(f))
  140.           {
  141.              Serial.println(F("Failed to read from DHT sensor!"));
  142.              return;
  143.           }
  144.    
  145.         Notificare = Notificare +"Astăzi, ";
  146.         Notificare = Notificare +daysOfTheWeek[timeClient.getDay()];
  147.         Notificare = Notificare +", ";
  148.         Notificare = Notificare +monthDay;
  149.         Notificare = Notificare +currentMonthName;
  150.         Notificare = Notificare +", ";
  151.         Notificare = Notificare +currentYear;
  152.         Notificare = Notificare +",Este ora, ";
  153.         Notificare = Notificare + timeClient.getHours();
  154.         Notificare = Notificare +",și ";
  155.         Notificare = Notificare + timeClient.getMinutes();
  156.         Notificare = Notificare +"minute, ";
  157.         Notificare = Notificare +"Temperatura este de, ";
  158.         Notificare = Notificare + String(t,1);
  159.         Notificare = Notificare + "grade Celsius,";
  160.         Notificare = Notificare + "iar umiditatea relativă este,";
  161.         Notificare = Notificare + String(h,1);
  162.         Notificare = Notificare + "la sută.";
  163.  
  164.         if (ghn.notify (Notificare.c_str()) != true)
  165.            {
  166.               Serial.println(ghn.getLastError());
  167.               return;
  168.            }
  169.         Notificare = "";
  170.      }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement