Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <TimeLib.h>
- #include <WiFiUdp.h>
- String ClientRequest;
- IPAddress staticIP707_10(192,168,86,10);
- IPAddress gateway707_10(192,168,86,1);
- IPAddress subnet707_10(255,255,255,0);
- WiFiServer server(80);
- int MT;
- int DY;
- int YR;
- int HR;
- int MN;
- int SC;
- int MN_Timer;
- int storeMins;
- int timerMinsSet;
- int setHR = 9;
- int setMN = 48;
- int dur_Timer_MN = setMN + 1;
- char ntpServerName[] = "us.pool.ntp.org";
- int timeZone = -7;
- WiFiUDP Udp;
- unsigned int localPort = 8888;
- time_t getNtpTime();
- void printDigits(int digits);
- void sendNTPpacket(IPAddress &address);
- int digitalClockDisplay(char m){
- int interm;
- if (m=='h'){
- interm=hour();
- return interm;
- }
- if (m=='m'){
- interm=minute();
- return interm;
- }
- if (m=='s'){
- interm=second();
- return interm;
- }
- if (m=='j'){
- interm=day();
- return interm;
- }
- if (m=='n'){
- interm=month();
- return interm;
- }
- if (m=='y'){
- interm=year();
- return interm;
- }
- }
- /*-------- NTP code ----------*/
- const int NTP_PACKET_SIZE = 48; // NTP time is in the first 48 bytes of message
- byte packetBuffer[NTP_PACKET_SIZE]; //buffer to hold incoming & outgoing packets
- time_t getNtpTime()
- {
- IPAddress ntpServerIP; // NTP server's ip address
- while (Udp.parsePacket() > 0) ; // discard any previously received packets
- Serial.println("Transmit NTP Request");
- // get a random server from the pool
- WiFi.hostByName(ntpServerName, ntpServerIP);
- Serial.print(ntpServerName);
- Serial.print(":");
- Serial.println(ntpServerIP);
- sendNTPpacket(ntpServerIP);
- uint32_t beginWait = millis();
- while (millis() - beginWait < 1500) {
- int size = Udp.parsePacket();
- if (size >= NTP_PACKET_SIZE) {
- Serial.println("Receive NTP Response");
- Udp.read(packetBuffer, NTP_PACKET_SIZE); // read packet into the buffer
- unsigned long secsSince1900;
- // convert four bytes starting at location 40 to a long integer
- secsSince1900 = (unsigned long)packetBuffer[40] << 24;
- secsSince1900 |= (unsigned long)packetBuffer[41] << 16;
- secsSince1900 |= (unsigned long)packetBuffer[42] << 8;
- secsSince1900 |= (unsigned long)packetBuffer[43];
- return secsSince1900 - 2208988800UL + timeZone * SECS_PER_HOUR;
- }
- }
- Serial.println("No NTP Response :-(");
- return 0; // return 0 if unable to get the time
- }
- void sendNTPpacket(IPAddress &address)
- {
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- packetBuffer[0] = 0b11100011; // LI, Version, Mode
- packetBuffer[1] = 0; // Stratum, or type of clock
- packetBuffer[2] = 6; // Polling Interval
- packetBuffer[3] = 0xEC; // Peer Clock Precision
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- Udp.beginPacket(address, 123); //NTP requests are to port 123
- Udp.write(packetBuffer, NTP_PACKET_SIZE);
- Udp.endPacket();
- }
- void setup()
- {
- ClientRequest = "";
- Serial.begin(115200);
- WiFi.disconnect();
- delay(3000);
- Serial.println("START");
- WiFi.begin("SSID","PASSWORD");
- while ((!(WiFi.status() == WL_CONNECTED))){
- delay(300);
- Serial.print("..");
- }
- Serial.println("Connected");
- WiFi.config(staticIP707_10, gateway707_10, subnet707_10);
- Serial.println("Your IP is");
- Serial.println((WiFi.localIP().toString()));
- server.begin();
- Udp.begin(localPort);
- setSyncProvider(getNtpTime);
- setSyncInterval(300);
- }
- void loop()
- {
- Serial.println();
- (MT=digitalClockDisplay('n'));
- (DY=digitalClockDisplay('j'));
- (YR=digitalClockDisplay('y'));
- (HR=digitalClockDisplay('h'));
- (MN=digitalClockDisplay('m'));
- (SC=digitalClockDisplay('s'));
- Serial.print(MT);
- Serial.print('-');
- Serial.print(DY);
- Serial.print('-');
- Serial.println(YR);
- Serial.print(HR);
- Serial.print(':');
- Serial.print(MN);
- Serial.print(':');
- Serial.println(SC);
- delay(5000);
- ////////////////////////////////////////////////////////////
- if(HR == setHR && MN <= dur_Timer_MN){
- //digitalWrite(output5, HIGH); //Turn on I/O pin
- Serial.print("Turn on I/O pin");
- }else{
- Serial.print("Turn off I/O pin");
- //digitalWrite(output5, LOW); //Turn ff I/O pin
- }
- //////////////////////////////////////////////////////////
- WiFiClient client = server.available();
- if (!client) { return; }
- while(!client.available()){ delay(1); }
- ClientRequest = (client.readStringUntil('\r'));
- client.flush();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement