Advertisement
LeventeDaradici

Google Home Voice Notification with Wemos D1 mini + DHT22 + BMP280 + LCD1602 - ROMANIAN VERSION

Jul 28th, 2023 (edited)
1,513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.05 KB | None | 0 0
  1. // https://www.youtube.com/c/LeventeDaradici/videos
  2. // Versiune corectata 28.07.2023
  3. //
  4. #include <NTPClient.h>
  5. #include <ESP8266WiFi.h>
  6. #include <WiFiUdp.h>
  7. #include <LCD_I2C.h>
  8. #include <Adafruit_Sensor.h>  
  9. #include <Adafruit_BMP280.h>
  10. #include <DHT.h>
  11. #include <esp8266-google-home-notifier.h>
  12.  
  13. #define DHTPIN 2
  14. #define DHTTYPE DHT22
  15. #define BMP280_I2C_ADDRESS  0x76
  16.  
  17. DHT dht(DHTPIN, DHTTYPE);
  18.  
  19. //#define BMP_SCK 13
  20. //#define BMP_MISO 12
  21. //#define BMP_MOSI 11
  22. //#define BMP_CS 10
  23.  
  24. Adafruit_BMP280 bmp280;
  25.  
  26. LCD_I2C lcd(0x27);
  27.  
  28. WiFiClient client;
  29. WiFiClientSecure sclient;
  30.  
  31. const char *ssid = "Your WIFI SSID";
  32. const char *password = "Your WIFI PASSWORD";
  33. int STARTHOUR = 9;                              //write here the time when the notifications start
  34. int ENDHOUR = 21;                               //write here the time to stop notifications
  35. String Notificare ="";
  36. 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
  37. unsigned long previous_time = 0;
  38. unsigned long pauza = 20000;  // 20 seconds delay
  39. GoogleHomeNotifier ghn;
  40.  
  41. WiFiUDP ntpUDP;
  42. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  43.  
  44. String daysOfTheWeek[7]={"Duminică", "Luni", "Marți", "Miercuri", "Joi", "Vineri", "Sîmbătă"};
  45. String months[12]={"Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octombrie", "Noiembrie", "Decembrie"};
  46.  
  47. int a = 0;
  48. #define ESP_RESET ESP.reset()
  49.  
  50. unsigned long MillisINITIAL;
  51. unsigned long MillisACTUAL;
  52. unsigned long MillisINITIAL2;
  53. unsigned long MillisACTUAL2;
  54. unsigned long last_wifi_check_time = 10;
  55. #define WIFI_TIMEOUT 30000
  56.  
  57. const unsigned long PERIOADA = 5000;
  58. const unsigned long PERIOADAceas = 500;
  59.  
  60. float UMIDITATE;
  61. float TEMPERATURAC;
  62. float TEMPERATURAF;
  63. float PRESIUNE;
  64. float UMIDITATEprecedenta;
  65. float TEMPERATURACprecedenta;
  66. float TEMPERATURAFprecedenta;
  67. float PRESIUNEprecedenta;
  68. bool BackLightState;
  69. int BackLightOnTime, BackLightOffTime;
  70.  
  71. char * messagePadded = "                STATIE METEO CU NOTIFICARE VOCALA PE GOOGLE NEST, CU SENZOR DE TEMPERATURA SI UMIDITATE DHT22 SI SENZOR DE PRESIUNE ATMOSFERICA BMP280 SI CU ECRAN LCD1602                ";
  72.  
  73. void AfiseazaPrezentarea()
  74.     {
  75.        for (int letter = 0; letter <= strlen(messagePadded) - 16; letter++) //From 0 to upto n-16 characters supply to below function
  76.            {
  77.               showLetters(0, letter);
  78.            }      
  79.     }
  80.    
  81. void showLetters(int printStart, int startLetter)
  82.     {
  83.        lcd.setCursor(printStart, 0);
  84.        for (int letter = startLetter; letter <= startLetter + 15; letter++)
  85.            {
  86.               lcd.print(messagePadded[letter]);
  87.            }
  88.        lcd.print(" ");
  89.        delay(200);
  90.     }
  91.  
  92. void VerificaDHTsensor()
  93.    {
  94.         PRESIUNE    = bmp280.readPressure();
  95.         UMIDITATE = dht.readHumidity();
  96.         TEMPERATURAC = dht.readTemperature();
  97.         TEMPERATURAF = dht.readTemperature(true);
  98.          if (isnan(UMIDITATE) || isnan(TEMPERATURAC) || isnan(TEMPERATURAF))
  99.            {
  100.               Serial.println("Nu am reusit sa citesc date de pe senzorul DHT !");
  101.               lcd.clear();
  102.               lcd.setCursor(0,0);
  103.               lcd.print("EROARE DHT22 !");
  104.               delay(2000);
  105.               lcd.clear();
  106.               return;
  107.            } else
  108.                 {
  109.                    lcd.clear();
  110.                    lcd.setCursor(0,0);
  111.                    lcd.print("DHT22 -> OK");      
  112.                    delay(1000);          
  113.                 }
  114.    }
  115.  
  116. void AfiseazaDatelePeEcran()
  117.    {      
  118.         PRESIUNE    = bmp280.readPressure();
  119.         UMIDITATE = dht.readHumidity();
  120.         TEMPERATURAC = dht.readTemperature();
  121.         TEMPERATURAF = dht.readTemperature(true);
  122.  
  123.         Serial.println(timeClient.getFormattedTime());        
  124.         Serial.print("Temperatura = ");        
  125.         Serial.print(TEMPERATURAC,1);
  126.         Serial.println(" °C");
  127.         Serial.print("Umiditate = ");
  128.         Serial.print(UMIDITATE);
  129.         Serial.println(" %");
  130.         Serial.print("Presiune atmosferica = ");
  131.         Serial.print(PRESIUNE/100);
  132.         Serial.println(" hPa");  
  133.         Serial.println("________________________________");  
  134.                              
  135.        
  136.         if (TEMPERATURAC != TEMPERATURACprecedenta)
  137.            {
  138.                lcd.setCursor(10,0);
  139.                lcd.print("     ");
  140.                lcd.setCursor(10,0);
  141.                lcd.print(TEMPERATURAC,1);
  142.                lcd.setCursor(14,0);
  143.                lcd.print("\xDF" "C");
  144.            }
  145.         if (PRESIUNE != PRESIUNEprecedenta)
  146.            {            
  147.                lcd.setCursor(7,1);
  148.                lcd.print("      ");
  149.                lcd.setCursor(7, 1);
  150.                lcd.print(PRESIUNE/100,1);
  151.                lcd.setCursor(13, 1);
  152.                lcd.print("hPA");
  153.            }
  154.         if (UMIDITATE != UMIDITATEprecedenta)
  155.            {    
  156.                lcd.setCursor(0,1);
  157.                lcd.print("      ");
  158.                lcd.setCursor(0,1);
  159.                lcd.print(UMIDITATE,1);
  160.                lcd.setCursor(4,1);
  161.                lcd.print("%");                
  162.            }      
  163.        
  164.         PRESIUNEprecedenta = PRESIUNE;
  165.         UMIDITATEprecedenta = UMIDITATE;
  166.         TEMPERATURACprecedenta = TEMPERATURAC;
  167.         TEMPERATURAFprecedenta = TEMPERATURAF;            
  168.    }
  169.  
  170. void wifi_setup()
  171.      {
  172.       a = 0;
  173.       Serial.println();
  174.       Serial.print("Conectare la ");
  175.       Serial.println(ssid);
  176.       lcd.clear();
  177.       lcd.setCursor(0,0);
  178.       lcd.print("CONECTARE WIFI");
  179.       //lcd.setCursor(0,1);
  180.       //lcd.print(ssid);
  181.       WiFi.begin(ssid, password);
  182.       WiFi.mode(WIFI_STA);
  183.  
  184.       unsigned long connect_start = millis();
  185.       while(WiFi.status() != WL_CONNECTED) {
  186.           delay (500);
  187.           lcd.setCursor(a,1);
  188.           lcd.print(">");
  189.           a = a+1;
  190.           if (a > 15)
  191.              {
  192.                 a = 0;
  193.                 lcd.setCursor(a,1);
  194.                 lcd.print("                ");              
  195.              }
  196.          
  197.  
  198.     if(millis() - connect_start > WIFI_TIMEOUT) {
  199.       Serial.println();
  200.       Serial.print("Am incercat ");
  201.       Serial.print(WIFI_TIMEOUT);
  202.       Serial.print("ms. Restartez ESP-ul acum..");
  203.       lcd.clear();
  204.       lcd.setCursor(0,0);
  205.       lcd.print("NECONECTAT !");
  206.       lcd.setCursor(0,1);
  207.       lcd.print("NECONECTAT !");
  208.       delay(2000);
  209.       ESP_RESET;
  210.     }
  211.     //lcd.clear();
  212.   }
  213.  
  214.   Serial.println("");
  215.   Serial.println("conectat la WiFi");  
  216.   Serial.print("adresa IP: ");
  217.   Serial.println(WiFi.localIP());
  218.   Serial.println();
  219.   lcd.clear();
  220.   lcd.setCursor(0,0);
  221.   lcd.print("CONECTAT:");
  222.   lcd.setCursor(0,1);
  223.   lcd.print(WiFi.localIP());
  224.   delay(2000);
  225.   lcd.clear();
  226.   ConectareLaGoogleHome();
  227. }
  228.  
  229. void ConectareLaGoogleHome()
  230.     {
  231.       lcd.clear();
  232.       lcd.setCursor(0,0);
  233.       lcd.print("CONECTARE LA");
  234.       lcd.setCursor(0,1);
  235.       lcd.print("GOOGLE NEST");
  236.       const char displayName[] = "Entryway speaker";   //Enter the name of your google home speaker here
  237.       Serial.println("CONECTARE LA GOOGLE NEST...");
  238.       if (ghn.device(displayName, "ro") != true)
  239.          {
  240.            Serial.println(ghn.getLastError());
  241.            return;
  242.          }
  243.       Serial.print("am gasit Google Nest.");
  244.       Serial.print(ghn.getIPAddress());
  245.       Serial.print(":");
  246.       Serial.print(ghn.getPort());
  247.       Serial.println(" ");
  248.       lcd.clear();
  249.       lcd.setCursor(0,0);
  250.       lcd.print("CONECTAT LA");
  251.       lcd.setCursor(0,1);
  252.       lcd.print("GOOGLE HOME");
  253.       delay(2000);
  254.       lcd.clear();
  255.       lcd.setCursor(0,0);
  256.       lcd.print("IP: ");
  257.       lcd.setCursor(4,0);
  258.       lcd.print(ghn.getIPAddress());
  259.       lcd.setCursor(0,1);
  260.       lcd.print("PORT: ");
  261.       lcd.setCursor(6,1);
  262.       lcd.print(ghn.getPort());      
  263.       delay(2000);
  264.       lcd.clear();
  265.     }
  266.  
  267. void VoiceNotificationINTRO()
  268.      {
  269.        if (ghn.notify("Bună, Numele meu este D1 Mini, și am să vă spun, via google home, ora exactă, temperatura, umiditatea și presiunea atmosferică măsurate în camera dumneavoastră.") != true)
  270.           {
  271.              Serial.println(ghn.getLastError());
  272.              return;
  273.           }
  274.        Serial.println("Kész.");
  275.        delay(15000);  
  276.        if (ghn.notify("O să trimit notificări, pe boxa dumneavoastră, la fiecare 15 minute, între orele, stabilite la începutul codului încărcat în memoria mea.") != true)
  277.           {
  278.              Serial.println(ghn.getLastError());
  279.              return;
  280.           }
  281.        Serial.println("Gata.");
  282.        timeClient.update();
  283.        delay(10000);  
  284.      }
  285.  
  286. void VoiceNotification()
  287.      {
  288.         timeClient.update();
  289.         unsigned long epochTime = timeClient.getEpochTime();
  290.         struct tm *ptm = gmtime ((time_t *)&epochTime);
  291.         int monthDay = ptm->tm_mday;
  292.         int currentMonth = ptm->tm_mon+1;
  293.         int currentYear = ptm->tm_year+1900;
  294.         String currentMonthName = months[currentMonth-1];
  295.  
  296.         float UMIDITATE = dht.readHumidity();
  297.         float TEMPERATURAC = dht.readTemperature();
  298.         float TEMPERATURAF = dht.readTemperature(true);
  299.  
  300.        if (isnan(UMIDITATE) || isnan(TEMPERATURAC) || isnan(TEMPERATURAF))
  301.           {
  302.              Serial.println("Nu am reusit sa citesc date de pe senzorul DHT!");
  303.              return;
  304.           }
  305.    
  306.         Notificare = Notificare +"Astăzi, ";
  307.         Notificare = Notificare +daysOfTheWeek[timeClient.getDay()];
  308.         Notificare = Notificare +", ";
  309.        
  310.         Notificare = Notificare +monthDay;
  311.         Notificare = Notificare +currentMonthName;
  312.  
  313.         Notificare = Notificare +", ";
  314.         Notificare = Notificare +currentYear;
  315.         Notificare = Notificare +",Este ora  ";
  316.         int currentHour = timeClient.getHours();
  317.         String ScurrentHour = String(currentHour);
  318.         //Notificare = Notificare + timeClient.getHours();
  319.         Notificare = Notificare + ScurrentHour;
  320.        // Serial.println(ScurrentHour);
  321.         if(timeClient.getMinutes() != 0)
  322.            {  
  323.               Notificare = Notificare +",și ";
  324.               Notificare = Notificare + timeClient.getMinutes();
  325.               Notificare = Notificare +"de minute, ";
  326.            }
  327.         Notificare = Notificare +"Temperatura este de ";
  328.         Notificare = Notificare + String(TEMPERATURAC,0);
  329.         Notificare = Notificare + "grade Celsius,";
  330.         Notificare = Notificare + "umiditatea relativă este ";
  331.         Notificare = Notificare + String(UMIDITATE,0);
  332.         Notificare = Notificare + "la sută ";
  333.         Notificare = Notificare + "și presiunea atmosferică este de";
  334.         Notificare = Notificare + String(PRESIUNE/100,0);
  335.         Notificare = Notificare + "hectopascali.";
  336.  
  337.         if (ghn.notify (Notificare.c_str()) != true)
  338.            {
  339.               Serial.println(ghn.getLastError());
  340.               ConectareLaGoogleHome();
  341.               lcd.clear();
  342.               return;
  343.            }
  344.         Notificare = "";
  345.      }
  346.    
  347. void setup()
  348.     {
  349.  
  350.        BackLightState = true;
  351.        BackLightOnTime = 7;
  352.        BackLightOffTime = 23;
  353.        
  354.        Serial.begin(9600);
  355.        lcd.begin();
  356.        lcd.backlight();
  357.        lcd.clear();
  358.        //AfiseazaPrezentarea();  
  359.        lcd.clear();          
  360.        lcd.setCursor(0,0);
  361.        lcd.print("VERIFICARE");
  362.        lcd.setCursor(0,1);
  363.        lcd.print("SENZORI");        
  364.        delay(2000);
  365.        lcd.clear();      
  366.    
  367.        dht.begin();  
  368.        VerificaDHTsensor();    
  369.        if (!bmp280.begin(BMP280_I2C_ADDRESS))
  370.           {  
  371.              Serial.println("Nu am gasit senzor BMP280 valid ! Verificati conexiunile sau adresa i2c !");
  372.             // lcd.clear();
  373.              lcd.setCursor(0,1);
  374.              lcd.print("EROARE BMP280 !");
  375.              while (1);
  376.           } else
  377.                 {
  378.                   // lcd.clear();
  379.                    lcd.setCursor(0,1);
  380.                    lcd.print("BMP280 -> OK");
  381.                    delay(1000);
  382.                 }
  383.        wifi_setup();
  384.        timeClient.begin();
  385.        lcd.clear();
  386.        lcd.setCursor(0,0);
  387.        lcd.print("PREZENTARE AUDIO");
  388.        lcd.setCursor(0,1);
  389.        lcd.print("PE GOOGLE NEST");
  390.        //VoiceNotificationINTRO();
  391.        lcd.clear();    
  392.        AfiseazaDatelePeEcran();
  393.        MillisINITIAL = millis();
  394.        VoiceNotification();
  395.     }
  396.  
  397. void loop()
  398.      {
  399.        MillisACTUAL = millis();  
  400.          
  401.        if (MillisACTUAL - MillisINITIAL >= PERIOADA)  
  402.           {
  403.              AfiseazaDatelePeEcran();
  404.              MillisINITIAL = MillisACTUAL;  
  405.           }  
  406.          
  407.        if (MillisACTUAL - MillisINITIAL >= PERIOADAceas)  
  408.           {          
  409.              timeClient.update();      
  410.              lcd.setCursor(0,0);
  411.              lcd.print(timeClient.getFormattedTime());  
  412.              MillisINITIAL2 = MillisACTUAL2;  
  413.           }    
  414.  
  415.   if(MillisACTUAL - last_wifi_check_time > WIFI_TIMEOUT)
  416.         {
  417.            Serial.print("Verificare conectare la WIFI... ");
  418.            if(WiFi.status() != WL_CONNECTED) {
  419.            Serial.println("Conexiunea WiFi s-a intrerupt ! Incerc sa ma reconectez...");
  420.            wifi_setup();
  421.         } else
  422.                {
  423.                   Serial.println("OK");
  424.                }
  425.             last_wifi_check_time = MillisACTUAL;
  426.          }
  427.  
  428.        if (timeClient.getHours() >= STARTHOUR && timeClient.getHours() < ENDHOUR && timeClient.getHours() > 0)
  429.            {
  430.                      if (timeClient.getMinutes() == 0 && timeClient.getSeconds() < 1)
  431.                         {
  432.                           VoiceNotification();
  433.                           delay(1000);
  434.                         }
  435.                      if (timeClient.getMinutes() == 15 && timeClient.getSeconds() < 1)
  436.                         {
  437.                           VoiceNotification();
  438.                           delay(1000);                        
  439.                         }
  440.                      if (timeClient.getMinutes() == 30 && timeClient.getSeconds() < 1)
  441.                         {
  442.                           VoiceNotification();
  443.                           delay(1000);
  444.                         }
  445.                       if (timeClient.getMinutes() == 45 && timeClient.getSeconds() < 1)
  446.                         {
  447.                           VoiceNotification();
  448.                           delay(1000);                          
  449.                         }        
  450.            }
  451.                
  452.       if (timeClient.getHours() >= BackLightOnTime && timeClient.getHours() <= BackLightOffTime)
  453.           {
  454.             if(BackLightState == false)
  455.             {
  456.               BackLightState = true;
  457.               lcd.backlight();
  458.             }
  459.              
  460.           }
  461.           else
  462.           {
  463.             if(BackLightState == true)
  464.             {
  465.               BackLightState = false;
  466.               lcd.noBacklight();
  467.             }
  468.           }
  469. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement