Advertisement
LeventeDaradici

Wemos D1 mini + DHT22 + BMP280 + LCD1602 + NTP clock + Google Nest Voice notification HUNGARIAN 3010

Oct 30th, 2022 (edited)
895
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 15.36 KB | None | 0 0
  1. // https://www.youtube.com/c/LeventeDaradici/videos
  2.  
  3. #include <NTPClient.h>
  4. #include <ESP8266WiFi.h>
  5. #include <WiFiUdp.h>
  6. #include <LCD_I2C.h>
  7. #include <Adafruit_Sensor.h>  
  8. #include <Adafruit_BMP280.h>
  9. #include <DHT.h>
  10. #include <esp8266-google-home-notifier.h>
  11.  
  12. #define DHTPIN 2
  13. #define DHTTYPE DHT22
  14. #define BMP280_I2C_ADDRESS  0x76
  15.  
  16. DHT dht(DHTPIN, DHTTYPE);
  17.  
  18. //#define BMP_SCK 13
  19. //#define BMP_MISO 12
  20. //#define BMP_MOSI 11
  21. //#define BMP_CS 10
  22.  
  23. Adafruit_BMP280 bmp280;
  24.  
  25. LCD_I2C lcd(0x27);
  26.  
  27. WiFiClient client;
  28. WiFiClientSecure sclient;
  29.  
  30. const char *ssid = "WIFI halozatod SSID-je";
  31. const char *password = "A WIFI halozatod jelszava";
  32. int STARTHOUR = 9;                              //write here the time when the notifications start
  33. int ENDHOUR = 21;                               //write here the time to stop notifications
  34. String Notificare ="";
  35. const long utcOffsetInSeconds = 7200;          //Edit here to get the right time for your area, delay is in seconds ! This value is valable for Romania CET
  36. unsigned long previous_time = 0;
  37. unsigned long pauza = 25000;  // 20 seconds delay
  38. GoogleHomeNotifier ghn;
  39.  
  40. WiFiUDP ntpUDP;
  41. NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
  42.  
  43. String daysOfTheWeek[7]={"vasárnap", "hétfő", "kedd", "szerda", "csütörtök", "péntek", "szombat"};
  44. String months[12]={"január", "február", "március", "április", "május", "június", "július", "augusztus", "szeptember", "október", "november", "december"};
  45.  
  46. int a = 0;
  47. #define ESP_RESET ESP.reset()
  48.  
  49. unsigned long MillisINITIAL;
  50. unsigned long MillisACTUAL;
  51. unsigned long MillisINITIAL2;
  52. unsigned long MillisACTUAL2;
  53. unsigned long last_wifi_check_time = 10;
  54. #define WIFI_TIMEOUT 30000
  55.  
  56. const unsigned long PERIOADA = 5000;
  57. const unsigned long PERIOADAceas = 500;
  58.  
  59. float UMIDITATE;
  60. float TEMPERATURAC;
  61. float TEMPERATURAF;
  62. float PRESIUNE;
  63. float UMIDITATEprecedenta;
  64. float TEMPERATURACprecedenta;
  65. float TEMPERATURAFprecedenta;
  66. float PRESIUNEprecedenta;
  67. bool BackLightState;
  68. int BackLightOnTime, BackLightOffTime;
  69.  
  70. char * messagePadded = "                METEOROLOGIAI ALLOMAS HANGERTESITESSEL A GOOGLE NESTBEN, DHT22 HOMERSEKLET ES PARATARTALOM-ERZEKELOVEL ES BMP280 LEGNYOMAS-ERZEKELOVEL, VALAMINT 1602-ES LCD-KIJELZOVEL                ";
  71.  
  72. void AfiseazaPrezentarea()
  73.     {
  74.        for (int letter = 0; letter <= strlen(messagePadded) - 16; letter++) //From 0 to upto n-16 characters supply to below function
  75.            {
  76.               showLetters(0, letter);
  77.            }      
  78.     }
  79.    
  80. void showLetters(int printStart, int startLetter)
  81.     {
  82.        lcd.setCursor(printStart, 0);
  83.        for (int letter = startLetter; letter <= startLetter + 15; letter++)
  84.            {
  85.               lcd.print(messagePadded[letter]);
  86.            }
  87.        lcd.print(" ");
  88.        delay(200);
  89.     }
  90.  
  91. void VerificaDHTsensor()
  92.    {
  93.         PRESIUNE    = bmp280.readPressure();
  94.         UMIDITATE = dht.readHumidity();
  95.         TEMPERATURAC = dht.readTemperature();
  96.         TEMPERATURAF = dht.readTemperature(true);
  97.          if (isnan(UMIDITATE) || isnan(TEMPERATURAC) || isnan(TEMPERATURAF))
  98.            {
  99.               Serial.println("Nem sikerült leolvasni a DHT-érzékelőről!");
  100.               lcd.clear();
  101.               lcd.setCursor(0,0);
  102.               lcd.print("DHT22 HIBA !");
  103.               delay(2000);
  104.               lcd.clear();
  105.               return;
  106.            } else
  107.                 {
  108.                    lcd.clear();
  109.                    lcd.setCursor(0,0);
  110.                    lcd.print("DHT22 -> OK");      
  111.                    delay(1000);          
  112.                 }
  113.    }
  114.  
  115. void AfiseazaDatelePeEcran()
  116.    {      
  117.         PRESIUNE    = bmp280.readPressure();
  118.         UMIDITATE = dht.readHumidity();
  119.         TEMPERATURAC = dht.readTemperature();
  120.         TEMPERATURAF = dht.readTemperature(true);
  121.  
  122.         Serial.println(timeClient.getFormattedTime());        
  123.         Serial.print("Hőmérséklet = ");        
  124.         Serial.print(TEMPERATURAC,1);
  125.         Serial.println(" °C");
  126.         Serial.print("Nedvesség = ");
  127.         Serial.print(UMIDITATE);
  128.         Serial.println(" %");
  129.         Serial.print("Légköri nyomás = ");
  130.         Serial.print(PRESIUNE/100);
  131.         Serial.println(" hPa");  
  132.         Serial.println("________________________________");  
  133.                              
  134.        
  135.         if (TEMPERATURAC != TEMPERATURACprecedenta)
  136.            {
  137.                lcd.setCursor(10,0);
  138.                lcd.print("     ");
  139.                lcd.setCursor(10,0);
  140.                lcd.print(TEMPERATURAC,1);
  141.                lcd.setCursor(14,0);
  142.                lcd.print("\xDF" "C");
  143.            }
  144.         if (PRESIUNE != PRESIUNEprecedenta)
  145.            {            
  146.                lcd.setCursor(7,1);
  147.                lcd.print("      ");
  148.                lcd.setCursor(7, 1);
  149.                lcd.print(PRESIUNE/100,1);
  150.                lcd.setCursor(13, 1);
  151.                lcd.print("hPA");
  152.            }
  153.         if (UMIDITATE != UMIDITATEprecedenta)
  154.            {    
  155.                lcd.setCursor(0,1);
  156.                lcd.print("      ");
  157.                lcd.setCursor(0,1);
  158.                lcd.print(UMIDITATE,1);
  159.                lcd.setCursor(4,1);
  160.                lcd.print("%");                
  161.            }      
  162.        
  163.         PRESIUNEprecedenta = PRESIUNE;
  164.         UMIDITATEprecedenta = UMIDITATE;
  165.         TEMPERATURACprecedenta = TEMPERATURAC;
  166.         TEMPERATURAFprecedenta = TEMPERATURAF;            
  167.    }
  168.  
  169. void wifi_setup()
  170.      {
  171.       a = 0;
  172.       Serial.println();
  173.       Serial.print("Csatlakozás a következőhöz ");
  174.       Serial.println(ssid);
  175.       lcd.clear();
  176.       lcd.setCursor(0,0);
  177.       lcd.print("WIFI CSATLAKOZAS");
  178.       //lcd.setCursor(0,1);
  179.       //lcd.print(ssid);
  180.       WiFi.begin(ssid, password);
  181.       WiFi.mode(WIFI_STA);
  182.  
  183.       unsigned long connect_start = millis();
  184.       while(WiFi.status() != WL_CONNECTED) {
  185.           delay (500);
  186.           lcd.setCursor(a,1);
  187.           lcd.print(">");
  188.           a = a+1;
  189.           if (a > 15)
  190.              {
  191.                 a = 0;
  192.                 lcd.setCursor(a,1);
  193.                 lcd.print("                ");              
  194.              }
  195.          
  196.  
  197.     if(millis() - connect_start > WIFI_TIMEOUT) {
  198.       Serial.println();
  199.       Serial.print("Megpróbáltam ");
  200.       Serial.print(WIFI_TIMEOUT);
  201.       Serial.print("ms. Az ESP újraindítása most.");
  202.       lcd.clear();
  203.       lcd.setCursor(0,0);
  204.       lcd.print("NEM CSATLAKOZOTT !");
  205.       lcd.setCursor(0,1);
  206.       lcd.print("ESP UJRAINDITASA !");
  207.       delay(2000);
  208.       ESP_RESET;
  209.     }
  210.     //lcd.clear();
  211.   }
  212.  
  213.   Serial.println("");
  214.   Serial.println("WiFi csatlakozva");  
  215.   Serial.print("IP-cím: ");
  216.   Serial.println(WiFi.localIP());
  217.   Serial.println();
  218.   lcd.clear();
  219.   lcd.setCursor(0,0);
  220.   lcd.print("CSATLAKOZVA:");
  221.   lcd.setCursor(0,1);
  222.   lcd.print(WiFi.localIP());
  223.   delay(2000);
  224.   lcd.clear();
  225.   ConectareLaGoogleHome();
  226. }
  227.  
  228. void ConectareLaGoogleHome()
  229.     {
  230.       lcd.clear();
  231.       lcd.setCursor(0,0);
  232.       lcd.print("CSATLAKOZAS A");
  233.       lcd.setCursor(0,1);
  234.       lcd.print("GOOGLE NESTHEZ");
  235.       const char displayName[] = "Entryway speaker";   //Enter the name of your google home speaker here
  236.       Serial.println("CSATLAKOZÁS A GOOGLE NESTHEZ...");
  237.       if (ghn.device(displayName, "hu") != true)
  238.          {
  239.            Serial.println(ghn.getLastError());
  240.            return;
  241.          }
  242.       Serial.print("megtalálta a Google Home-ot ");
  243.       Serial.print(ghn.getIPAddress());
  244.       Serial.print(":");
  245.       Serial.print(ghn.getPort());
  246.       Serial.println(" ");
  247.       lcd.clear();
  248.       lcd.setCursor(0,0);
  249.       lcd.print("CSATLAKOZVA A");
  250.       lcd.setCursor(0,1);
  251.       lcd.print("GOOGLE HOMEHOZ");
  252.       delay(2000);
  253.       lcd.clear();
  254.       lcd.setCursor(0,0);
  255.       lcd.print("IP: ");
  256.       lcd.setCursor(4,0);
  257.       lcd.print(ghn.getIPAddress());
  258.       lcd.setCursor(0,1);
  259.       lcd.print("PORT: ");
  260.       lcd.setCursor(6,1);
  261.       lcd.print(ghn.getPort());      
  262.       delay(2000);
  263.       lcd.clear();
  264.     }
  265.  
  266. void VoiceNotificationINTRO()
  267.      {
  268.              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, páratartalmat és a légnyomás értékét, amit a szobádban mérek.") != true)
  269.           {
  270.              Serial.println(ghn.getLastError());
  271.              return;
  272.           }
  273.        Serial.println("Kész.");
  274.        delay(15000);  
  275.        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)
  276.           {
  277.              Serial.println(ghn.getLastError());
  278.              return;
  279.           }
  280.        Serial.println("Kész.");
  281.        timeClient.update();
  282.        delay(10000);  
  283.      }
  284.  
  285. void VoiceNotification()
  286.      {
  287.         timeClient.update();
  288.         unsigned long epochTime = timeClient.getEpochTime();
  289.         struct tm *ptm = gmtime ((time_t *)&epochTime);
  290.         int monthDay = ptm->tm_mday;
  291.         int currentMonth = ptm->tm_mon+1;
  292.         int currentYear = ptm->tm_year+1900;
  293.         String currentMonthName = months[currentMonth-1];
  294.  
  295.         float UMIDITATE = dht.readHumidity();
  296.         float TEMPERATURAC = dht.readTemperature();
  297.         float TEMPERATURAF = dht.readTemperature(true);
  298.  
  299.        if (isnan(UMIDITATE) || isnan(TEMPERATURAC) || isnan(TEMPERATURAF))
  300.           {
  301.              Serial.println("Nem sikerült leolvasni a DHT érzékelőről!");
  302.              return;
  303.           }
  304.    
  305.         Notificare = Notificare +"Ma, ";
  306.         Notificare = Notificare +daysOfTheWeek[timeClient.getDay()];
  307.         Notificare = Notificare +", ";
  308.         Notificare = Notificare +currentMonthName;
  309.         Notificare = Notificare +monthDay;
  310.         Notificare = Notificare +", ";
  311.         Notificare = Notificare +currentYear;
  312.         Notificare = Notificare +",a pontos idö, ";
  313.         Notificare = Notificare + timeClient.getHours();
  314.         Notificare = Notificare +"óra,";
  315.  
  316.             if(timeClient.getMinutes() != 0)
  317.                 {
  318.                     Notificare = Notificare +" és ";  
  319.                     Notificare = Notificare + timeClient.getMinutes();
  320.                     Notificare = Notificare +" perc, ";          
  321.                 }
  322.                
  323.         Notificare = Notificare +" a hőmérséklet, ";
  324.         Notificare = Notificare + String(TEMPERATURAC,1);
  325.         Notificare = Notificare + "fok Celzius,";
  326.         Notificare = Notificare + "a relatív páratartalom pedig,";
  327.         Notificare = Notificare + String(UMIDITATE,1);
  328.         Notificare = Notificare + "százalék ";
  329.         Notificare = Notificare + "és a légnyomás értéke";
  330.         Notificare = Notificare + String(PRESIUNE/100,0);
  331.         Notificare = Notificare + "hektopascal.";
  332.  
  333.         if (ghn.notify (Notificare.c_str()) != true)
  334.            {
  335.               Serial.println(ghn.getLastError());
  336.               ConectareLaGoogleHome();
  337.               lcd.clear();
  338.               return;
  339.            }
  340.         Notificare = "";
  341.      }
  342.  void DingDong()
  343.      {
  344.        Notificare = "Ding dong";
  345.         if (ghn.notify (Notificare.c_str()) != true)
  346.            {
  347.               Serial.println(ghn.getLastError());
  348.               ConectareLaGoogleHome();
  349.               lcd.clear();
  350.               return;
  351.            }
  352.         Notificare = "";
  353.      }
  354.  
  355. void setup()
  356.     {
  357.  
  358.        BackLightState = true;
  359.        BackLightOnTime = 7;
  360.        BackLightOffTime = 23;
  361.        
  362.        Serial.begin(9600);
  363.        lcd.begin();
  364.        lcd.backlight();
  365.        lcd.clear();
  366.        //AfiseazaPrezentarea();  
  367.        lcd.clear();          
  368.        lcd.setCursor(0,0);
  369.        lcd.print("ERZEKELOK");
  370.        lcd.setCursor(0,1);
  371.        lcd.print("ELLENORZESE");        
  372.        delay(2000);
  373.        lcd.clear();      
  374.    
  375.        dht.begin();  
  376.        VerificaDHTsensor();    
  377.        if (!bmp280.begin(BMP280_I2C_ADDRESS))
  378.           {  
  379.              Serial.println("Nem található érvényes BMP280 érzékelő, ellenőrizze a vezetékeket!");
  380.             // lcd.clear();
  381.              lcd.setCursor(0,1);
  382.              lcd.print("BMP280 HIBA !");
  383.              while (1);
  384.           } else
  385.                 {
  386.                   // lcd.clear();
  387.                    lcd.setCursor(0,1);
  388.                    lcd.print("BMP280 -> OK");
  389.                    delay(1000);
  390.                 }
  391.        wifi_setup();
  392.        timeClient.begin();
  393.        lcd.clear();
  394.        //lcd.setCursor(0,0);
  395.        //lcd.print("HANGBEMUTATO");
  396.        //lcd.setCursor(0,1);
  397.        //lcd.print("A GOOGLE NESTEN");
  398.        //VoiceNotificationINTRO();
  399.        //lcd.clear();    
  400.        AfiseazaDatelePeEcran();
  401.        MillisINITIAL = millis();
  402.        //VoiceNotification();
  403.        //DingDong();
  404.     }
  405.  
  406. void loop()
  407.      {
  408.        MillisACTUAL = millis();  
  409.          
  410.        if (MillisACTUAL - MillisINITIAL >= PERIOADA)  
  411.           {
  412.              AfiseazaDatelePeEcran();
  413.              MillisINITIAL = MillisACTUAL;  
  414.           }  
  415.          
  416.        if (MillisACTUAL - MillisINITIAL >= PERIOADAceas)  
  417.           {          
  418.              timeClient.update();      
  419.              lcd.setCursor(0,0);
  420.              lcd.print(timeClient.getFormattedTime());  
  421.              MillisINITIAL2 = MillisACTUAL2;  
  422.           }    
  423.  
  424.   if(MillisACTUAL - last_wifi_check_time > WIFI_TIMEOUT)
  425.         {
  426.            Serial.print("WiFi ellenőrzése... ");
  427.            if(WiFi.status() != WL_CONNECTED) {
  428.            Serial.println("A WiFi kapcsolat megszakadt. Újracsatlakozás...");
  429.            wifi_setup();
  430.         } else
  431.                {
  432.                   Serial.println("OK");
  433.                }
  434.             last_wifi_check_time = MillisACTUAL;
  435.          }
  436.  
  437.        if (timeClient.getHours() >= STARTHOUR && timeClient.getHours() < ENDHOUR && timeClient.getHours() > 0)
  438.            {
  439.                      if (timeClient.getMinutes() == 0 && timeClient.getSeconds() < 1)
  440.                         {
  441.                           VoiceNotification();
  442.                           delay(1000);
  443.                         }
  444.                      if (timeClient.getMinutes() == 15 && timeClient.getSeconds() < 1)
  445.                         {
  446.                           VoiceNotification();
  447.                           delay(1000);                        
  448.                         }
  449.                      if (timeClient.getMinutes() == 30 && timeClient.getSeconds() < 1)
  450.                         {
  451.                           VoiceNotification();
  452.                           delay(1000);
  453.                         }
  454.                       if (timeClient.getMinutes() == 45 && timeClient.getSeconds() < 1)
  455.                         {
  456.                           VoiceNotification();
  457.                           delay(1000);                          
  458.                         }        
  459.            }
  460.                
  461.       if (timeClient.getHours() >= BackLightOnTime && timeClient.getHours() <= BackLightOffTime)
  462.           {
  463.             if(BackLightState == false)
  464.             {
  465.               BackLightState = true;
  466.               lcd.backlight();
  467.             }
  468.              
  469.           }
  470.           else
  471.           {
  472.             if(BackLightState == true)
  473.             {
  474.               BackLightState = false;
  475.               lcd.noBacklight();
  476.             }
  477.           }
  478. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement