Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // https://www.youtube.com/c/LeventeDaradici/videos
- #include <NTPClient.h>
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- #include <LCD_I2C.h>
- #include <Adafruit_Sensor.h>
- #include <Adafruit_BMP280.h>
- #include <DHT.h>
- #include <esp8266-google-home-notifier.h>
- #define DHTPIN 2
- #define DHTTYPE DHT22
- #define BMP280_I2C_ADDRESS 0x76
- DHT dht(DHTPIN, DHTTYPE);
- //#define BMP_SCK 13
- //#define BMP_MISO 12
- //#define BMP_MOSI 11
- //#define BMP_CS 10
- Adafruit_BMP280 bmp280;
- LCD_I2C lcd(0x27);
- WiFiClient client;
- WiFiClientSecure sclient;
- const char *ssid = "SSID-ul retelei tale WIFI";
- const char *password = "PAROLA retelei tale WIFI";
- int STARTHOUR = 9; //write here the time when the notifications start
- int ENDHOUR = 21; //write here the time to stop notifications
- String Notificare ="";
- 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
- unsigned long previous_time = 0;
- unsigned long pauza = 20000; // 20 seconds delay
- GoogleHomeNotifier ghn;
- WiFiUDP ntpUDP;
- NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds);
- String daysOfTheWeek[7]={"Duminică", "Luni", "Marți", "Miercuri", "Joi", "Vineri", "Sîmbătă"};
- String months[12]={"Ianuarie", "Februarie", "Martie", "Aprilie", "Mai", "Iunie", "Iulie", "August", "Septembrie", "Octombrie", "Noiembrie", "Decembrie"};
- int a = 0;
- #define ESP_RESET ESP.reset()
- unsigned long MillisINITIAL;
- unsigned long MillisACTUAL;
- unsigned long MillisINITIAL2;
- unsigned long MillisACTUAL2;
- unsigned long last_wifi_check_time = 10;
- #define WIFI_TIMEOUT 30000
- const unsigned long PERIOADA = 5000;
- const unsigned long PERIOADAceas = 500;
- float UMIDITATE;
- float TEMPERATURAC;
- float TEMPERATURAF;
- float PRESIUNE;
- float UMIDITATEprecedenta;
- float TEMPERATURACprecedenta;
- float TEMPERATURAFprecedenta;
- float PRESIUNEprecedenta;
- bool BackLightState;
- int BackLightOnTime, BackLightOffTime;
- 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 ";
- void AfiseazaPrezentarea()
- {
- for (int letter = 0; letter <= strlen(messagePadded) - 16; letter++) //From 0 to upto n-16 characters supply to below function
- {
- showLetters(0, letter);
- }
- }
- void showLetters(int printStart, int startLetter)
- {
- lcd.setCursor(printStart, 0);
- for (int letter = startLetter; letter <= startLetter + 15; letter++)
- {
- lcd.print(messagePadded[letter]);
- }
- lcd.print(" ");
- delay(200);
- }
- void VerificaDHTsensor()
- {
- PRESIUNE = bmp280.readPressure();
- UMIDITATE = dht.readHumidity();
- TEMPERATURAC = dht.readTemperature();
- TEMPERATURAF = dht.readTemperature(true);
- if (isnan(UMIDITATE) || isnan(TEMPERATURAC) || isnan(TEMPERATURAF))
- {
- Serial.println("Nu am reusit sa citesc date de pe senzorul DHT !");
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("EROARE DHT22 !");
- delay(2000);
- lcd.clear();
- return;
- } else
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("DHT22 -> OK");
- delay(1000);
- }
- }
- void AfiseazaDatelePeEcran()
- {
- PRESIUNE = bmp280.readPressure();
- UMIDITATE = dht.readHumidity();
- TEMPERATURAC = dht.readTemperature();
- TEMPERATURAF = dht.readTemperature(true);
- Serial.println(timeClient.getFormattedTime());
- Serial.print("Temperatura = ");
- Serial.print(TEMPERATURAC,1);
- Serial.println(" °C");
- Serial.print("Umiditate = ");
- Serial.print(UMIDITATE);
- Serial.println(" %");
- Serial.print("Presiune atmosferica = ");
- Serial.print(PRESIUNE/100);
- Serial.println(" hPa");
- Serial.println("________________________________");
- if (TEMPERATURAC != TEMPERATURACprecedenta)
- {
- lcd.setCursor(10,0);
- lcd.print(" ");
- lcd.setCursor(10,0);
- lcd.print(TEMPERATURAC,1);
- lcd.setCursor(14,0);
- lcd.print("\xDF" "C");
- }
- if (PRESIUNE != PRESIUNEprecedenta)
- {
- lcd.setCursor(7,1);
- lcd.print(" ");
- lcd.setCursor(7, 1);
- lcd.print(PRESIUNE/100,1);
- lcd.setCursor(13, 1);
- lcd.print("hPA");
- }
- if (UMIDITATE != UMIDITATEprecedenta)
- {
- lcd.setCursor(0,1);
- lcd.print(" ");
- lcd.setCursor(0,1);
- lcd.print(UMIDITATE,1);
- lcd.setCursor(4,1);
- lcd.print("%");
- }
- PRESIUNEprecedenta = PRESIUNE;
- UMIDITATEprecedenta = UMIDITATE;
- TEMPERATURACprecedenta = TEMPERATURAC;
- TEMPERATURAFprecedenta = TEMPERATURAF;
- }
- void wifi_setup()
- {
- a = 0;
- Serial.println();
- Serial.print("Conectare la ");
- Serial.println(ssid);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("CONECTARE WIFI");
- //lcd.setCursor(0,1);
- //lcd.print(ssid);
- WiFi.begin(ssid, password);
- WiFi.mode(WIFI_STA);
- unsigned long connect_start = millis();
- while(WiFi.status() != WL_CONNECTED) {
- delay (500);
- lcd.setCursor(a,1);
- lcd.print(">");
- a = a+1;
- if (a > 15)
- {
- a = 0;
- lcd.setCursor(a,1);
- lcd.print(" ");
- }
- if(millis() - connect_start > WIFI_TIMEOUT) {
- Serial.println();
- Serial.print("Am incercat ");
- Serial.print(WIFI_TIMEOUT);
- Serial.print("ms. Restartez ESP-ul acum..");
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("NECONECTAT !");
- lcd.setCursor(0,1);
- lcd.print("RESTARTEZ ESP !");
- delay(2000);
- ESP_RESET;
- }
- //lcd.clear();
- }
- Serial.println("");
- Serial.println("conectat la WiFi");
- Serial.print("adresa IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("CONECTAT:");
- lcd.setCursor(0,1);
- lcd.print(WiFi.localIP());
- delay(2000);
- lcd.clear();
- ConectareLaGoogleHome();
- }
- void ConectareLaGoogleHome()
- {
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("CONECTARE LA");
- lcd.setCursor(0,1);
- lcd.print("GOOGLE NEST");
- const char displayName[] = "Entryway speaker"; //Enter the name of your google home speaker here
- Serial.println("CONECTARE LA GOOGLE NEST...");
- if (ghn.device(displayName, "ro") != true)
- {
- Serial.println(ghn.getLastError());
- return;
- }
- Serial.print("am gasit Google Nest.");
- Serial.print(ghn.getIPAddress());
- Serial.print(":");
- Serial.print(ghn.getPort());
- Serial.println(")");
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("CONECTAT LA");
- lcd.setCursor(0,1);
- lcd.print("GOOGLE HOME");
- delay(2000);
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("IP: ");
- lcd.setCursor(4,0);
- lcd.print(ghn.getIPAddress());
- lcd.setCursor(0,1);
- lcd.print("PORT: ");
- lcd.setCursor(6,1);
- lcd.print(ghn.getPort());
- delay(2000);
- lcd.clear();
- }
- void VoiceNotificationINTRO()
- {
- 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)
- {
- Serial.println(ghn.getLastError());
- return;
- }
- Serial.println("Gata.");
- delay(15000);
- 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)
- {
- Serial.println(ghn.getLastError());
- return;
- }
- Serial.println("Gata.");
- timeClient.update();
- delay(10000);
- }
- void VoiceNotification()
- {
- timeClient.update();
- unsigned long epochTime = timeClient.getEpochTime();
- struct tm *ptm = gmtime ((time_t *)&epochTime);
- int monthDay = ptm->tm_mday;
- int currentMonth = ptm->tm_mon+1;
- int currentYear = ptm->tm_year+1900;
- String currentMonthName = months[currentMonth-1];
- float UMIDITATE = dht.readHumidity();
- float TEMPERATURAC = dht.readTemperature();
- float TEMPERATURAF = dht.readTemperature(true);
- if (isnan(UMIDITATE) || isnan(TEMPERATURAC) || isnan(TEMPERATURAF))
- {
- Serial.println("Nu am reusit sa citesc date de pe senzorul DHT!");
- return;
- }
- Notificare = Notificare +"Astăzi, ";
- Notificare = Notificare +daysOfTheWeek[timeClient.getDay()];
- Notificare = Notificare +", ";
- Notificare = Notificare +monthDay;
- Notificare = Notificare +currentMonthName;
- Notificare = Notificare +", ";
- Notificare = Notificare +currentYear;
- Notificare = Notificare +",Este ora, ";
- Notificare = Notificare + timeClient.getHours();
- Notificare = Notificare +",și ";
- Notificare = Notificare + timeClient.getMinutes();
- Notificare = Notificare +"minute, ";
- Notificare = Notificare +"Temperatura este de, ";
- Notificare = Notificare + String(TEMPERATURAC,1);
- Notificare = Notificare + "grade Celsius,";
- Notificare = Notificare + "umiditatea relativă este,";
- Notificare = Notificare + String(UMIDITATE,1);
- Notificare = Notificare + "la sută,";
- Notificare = Notificare + "și presiunea atmosferică este de";
- Notificare = Notificare + String(PRESIUNE/100,0);
- Notificare = Notificare + "hectopascal.";
- if (ghn.notify (Notificare.c_str()) != true)
- {
- Serial.println(ghn.getLastError());
- ConectareLaGoogleHome();
- lcd.clear();
- return;
- }
- Notificare = "";
- }
- void setup()
- {
- BackLightState = true;
- BackLightOnTime = 7;
- BackLightOffTime = 23;
- Serial.begin(9600);
- lcd.begin();
- lcd.backlight();
- lcd.clear();
- //AfiseazaPrezentarea();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("VERIFICARE");
- lcd.setCursor(0,1);
- lcd.print("SENZORI");
- delay(2000);
- lcd.clear();
- dht.begin();
- VerificaDHTsensor();
- if (!bmp280.begin(BMP280_I2C_ADDRESS))
- {
- Serial.println("Nu am gasit senzor BMP280 valid ! Verificati conexiunile sau adresa i2c !");
- // lcd.clear();
- lcd.setCursor(0,1);
- lcd.print("EROARE BMP280 !");
- while (1);
- } else
- {
- // lcd.clear();
- lcd.setCursor(0,1);
- lcd.print("BMP280 -> OK");
- delay(1000);
- }
- wifi_setup();
- timeClient.begin();
- lcd.clear();
- lcd.setCursor(0,0);
- lcd.print("REZENTARE AUDIO");
- lcd.setCursor(0,1);
- lcd.print("PE GOOGLE NEST");
- VoiceNotificationINTRO();
- lcd.clear();
- AfiseazaDatelePeEcran();
- MillisINITIAL = millis();
- //VoiceNotification();
- }
- void loop()
- {
- MillisACTUAL = millis();
- if (MillisACTUAL - MillisINITIAL >= PERIOADA)
- {
- AfiseazaDatelePeEcran();
- MillisINITIAL = MillisACTUAL;
- }
- if (MillisACTUAL - MillisINITIAL >= PERIOADAceas)
- {
- timeClient.update();
- lcd.setCursor(0,0);
- lcd.print(timeClient.getFormattedTime());
- MillisINITIAL2 = MillisACTUAL2;
- }
- if(MillisACTUAL - last_wifi_check_time > WIFI_TIMEOUT)
- {
- Serial.print("Verificare conectare la WIFI... ");
- if(WiFi.status() != WL_CONNECTED) {
- Serial.println("Conexiunea WiFi s-a intrerupt ! Incerc sa ma reconectez...");
- wifi_setup();
- } else
- {
- Serial.println("OK");
- }
- last_wifi_check_time = MillisACTUAL;
- }
- if (timeClient.getHours() >= STARTHOUR && timeClient.getHours() < ENDHOUR && timeClient.getHours() > 0)
- {
- if (timeClient.getMinutes() == 0 && timeClient.getSeconds() < 1)
- {
- VoiceNotification();
- delay(1000);
- }
- if (timeClient.getMinutes() == 15 && timeClient.getSeconds() < 1)
- {
- VoiceNotification();
- delay(1000);
- }
- if (timeClient.getMinutes() == 30 && timeClient.getSeconds() < 1)
- {
- VoiceNotification();
- delay(1000);
- }
- if (timeClient.getMinutes() == 45 && timeClient.getSeconds() < 1)
- {
- VoiceNotification();
- delay(1000);
- }
- }
- if (timeClient.getHours() >= BackLightOnTime && timeClient.getHours() <= BackLightOffTime)
- {
- if(BackLightState == false)
- {
- BackLightState = true;
- lcd.backlight();
- }
- }
- else
- {
- if(BackLightState == true)
- {
- BackLightState = false;
- lcd.noBacklight();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement