Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // https://www.youtube.com/c/LeventeDaradici/videos
- //
- #include <ESP8266WiFi.h>
- #include <NTPClient.h>
- #include <esp8266-google-home-notifier.h>
- #include "DHT.h"
- #define DHTPIN 2
- #define DHTTYPE DHT22
- DHT dht(DHTPIN, DHTTYPE);
- int STARTHOUR = 9; //write here the time when the notifications start
- int ENDHOUR = 21; //write here the time to stop notifications
- const char* ssid = "NETSTREAM"; //enter here your wifi SSID name
- const char* password = "*#Magda2007"; //enter here your wifi PASSWORD
- String Notificare ="";
- unsigned long previous_time = 0;
- unsigned long pauza = 20000; // 20 seconds delay
- GoogleHomeNotifier ghn;
- 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
- 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"};
- void initWiFi()
- {
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid, password);
- Serial.print("Connecting to WIFI network");
- while (WiFi.status() != WL_CONNECTED) {
- Serial.print('.');
- delay(1000);
- }
- Serial.println("connected.");
- Serial.print("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void setup()
- {
- Serial.begin(115200);
- Serial.println("");
- initWiFi();
- const char displayName[] = "Entryway speaker"; //Enter the name of your google home speaker here
- Serial.println("connecting to Google Home...");
- if (ghn.device(displayName, "ro") != true)
- {
- Serial.println(ghn.getLastError());
- return;
- }
- Serial.print("found Google Home(");
- Serial.print(ghn.getIPAddress());
- Serial.print(":");
- Serial.print(ghn.getPort());
- Serial.println(")");
- dht.begin();
- 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)
- {
- Serial.println(ghn.getLastError());
- return;
- }
- Serial.println("Done.");
- delay(13000);
- 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)
- {
- Serial.println(ghn.getLastError());
- return;
- }
- Serial.println("Done.");
- timeClient.update();
- delay(20000);
- }
- void loop()
- {
- unsigned long current_time = millis();
- if ((WiFi.status() != WL_CONNECTED) && (current_time - previous_time >= pauza))
- {
- Serial.print(millis());
- Serial.println("Reconnecting to WIFI network");
- WiFi.disconnect();
- WiFi.reconnect();
- previous_time = current_time;
- }
- if (timeClient.getHours() > STARTHOUR)
- {
- if (timeClient.getHours() < ENDHOUR)
- {
- if (timeClient.getMinutes() == 0)
- {
- VorbesteGoogle();
- delay(120000);
- }
- if (timeClient.getMinutes() == 15)
- {
- VorbesteGoogle();
- delay(120000);
- }
- if (timeClient.getMinutes() == 30)
- {
- VorbesteGoogle();
- delay(120000);
- }
- if (timeClient.getMinutes() == 45)
- {
- VorbesteGoogle();
- delay(120000);
- }
- }
- }
- }
- void VorbesteGoogle()
- {
- 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 h = dht.readHumidity();
- float t = dht.readTemperature();
- float f = dht.readTemperature(true);
- if (isnan(h) || isnan(t) || isnan(f))
- {
- Serial.println(F("Failed to read from DHT sensor!"));
- 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(t,1);
- Notificare = Notificare + "grade Celsius,";
- Notificare = Notificare + "iar umiditatea relativă este,";
- Notificare = Notificare + String(h,1);
- Notificare = Notificare + "la sută.";
- if (ghn.notify (Notificare.c_str()) != true)
- {
- Serial.println(ghn.getLastError());
- return;
- }
- Notificare = "";
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement