Codice per ESP8266
/** * https://lastminuteengineers.com/esp8266-ntp-server-date-time-tutorial/ - works! */ #include <NTPClient.h> #include <ESP8266WiFi.h> #include <WiFiUdp.h> #include<SoftwareSerial.h> SoftwareSerial SUART( 4, 5); //SRX = DPin-D2; STX = DPin-D1 const char *ssid = "your-wifi-ssid"; const char *password = "password"; const long utcOffsetInSeconds = 3600; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; // Define NTP Client to get time WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org", utcOffsetInSeconds); //SETUP void setup(){ SUART.begin(115200); //enable SUART Port Serial.begin(115200); //wifi WiFi.begin(ssid, password); while ( WiFi.status() != WL_CONNECTED ) { delay ( 500 ); Serial.print ( "." ); } timeClient.begin(); } //LOOP void loop() { timeClient.update(); //Serial.print(daysOfTheWeek[timeClient.getDay()]); //Serial.print(", "); //Serial.print(timeClient.getHours()); //Serial.print(":"); //Serial.print(timeClient.getMinutes()); //Serial.print(":"); //Serial.println(timeClient.getSeconds()); // posso remmare i precedenti Serial.println(timeClient.getFormattedTime()); // 16:46:32 //----Send Temp and Humidity signal to NodeMCU via SUART port---- SUART.print(timeClient.getFormattedTime()); // 16:46:32 delay(1000); }
Codice per arduino
#include<SoftwareSerial.h> #include <TimeLib.h> SoftwareSerial SUART(2, 3); //SRX=Dpin-2; STX-DPin-3 //------------------------- boolean dataSync = false; char unixString[11]; long unixTime; //-------------------------------------------- // definisco pin relay int Relay = 4; const int OnHour = 10; //SET TIME TO ON RELAY (24 HOUR FORMAT) const int OnMin = 00; const int OffHour = 19; //SET TIME TO OFF RELAY const int OffMin = 00; //-------------------------------------------- void setup() { Serial.begin(115200); //enable Serial Monitor SUART.begin(115200); //enable SUART Port pinMode(Relay, OUTPUT); digitalWrite(Relay, LOW); } void loop() { //-------- char buffer[40]; int i = 0; // while the ESP output available, push it // into the buffer and set the flag true while (SUART.available()) { buffer[i++] = SUART.read(); dataSync = true; } //-------- //--- byte n = SUART.available(); //n != 0 means a character has arrived if (n != 0) { char x = SUART.read(); //read character Serial.print(x); //show character on Serial Monitor dataSync = true; } if (dataSync == true) { //if ((buffer[0] == 'U') && (buffer[1] == 'N') && (buffer[2] == 'X')) { // if data sent is the UNX token, take it unixString[0] = buffer[3]; unixString[1] = buffer[4]; unixString[2] = buffer[5]; unixString[3] = buffer[6]; unixString[4] = buffer[7]; unixString[5] = buffer[8]; unixString[6] = buffer[9]; unixString[7] = buffer[10]; unixString[8] = buffer[11]; unixString[9] = buffer[12]; unixString[10] = '\0'; /* print the UNX time on the UNO serial Serial.println(); Serial.print("TIME FROM ESP: "); Serial.print(unixString[0]); Serial.print(unixString[1]); Serial.print(unixString[2]); Serial.print(unixString[3]); Serial.print(unixString[4]); Serial.print(unixString[5]); Serial.print(unixString[6]); Serial.print(unixString[7]); Serial.print(unixString[8]); Serial.print(unixString[9]); Serial.println(); */ unixTime = atol(unixString); // Synchronize the time with the internal clock // for external use RTC.setTime(); setTime(unixTime); dataSync = false; delay(1000); } //display internal time //digitalClockDisplay(); // ----comando i relay ma non va /* if(hour() == OnHour && min() == OnMin){ digitalWrite(Relay,HIGH); Serial.println("LIGHT ON"); } else if(hour() == OffHour && min() == OffMin){ digitalWrite(Relay,LOW); Serial.println("LIGHT OFF"); } */ } /** display time from internal clock void digitalClockDisplay(){ // digital clock display of the time Serial.print(year()); Serial.print("-"); if (month() < 10) { Serial.print('0'); } Serial.print(month()); Serial.print("-"); if (day() < 10) { Serial.print('0'); } Serial.print(day()); Serial.print(" "); //Serial.print(dayOfWeek[weekday()]); Serial.print(' '); if (hour() < 10) { Serial.print('0'); } Serial.print(hour()); Serial.print(':'); if (minute() < 10) { Serial.print('0'); } Serial.print(minute()); Serial.print(':'); if (second() < 10) { Serial.print('0'); } Serial.print(second()); Serial.println(); } */
Lascia un commento