|
发表于 2022-6-29 18:29:14
|
显示全部楼层
以前刚好搬过一个代码!拿去用吧
- #include <ESP8266WiFi.h>
- #include <WiFiUdp.h>
- //needed for library
- #include <ESP8266WebServer.h>
- #include <DNSServer.h>
- #include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
- // select which pin will trigger the configuration portal when set to LOW
- // ESP-01 users please note: the only pins available (0 and 2), are shared
- // with the bootloader, so always set them HIGH at power-up
- #define TRIGGER_PIN 0
- //NTP
- #define time_zone 0 //时区(要格林尼治时间加减的小时,北京为东八区,要将格林威治时间+8小时)
- unsigned int localPort = 2390; // local port to listen for UDP packets
- /* Don't hardwire the IP address or we won't get the benefits of the pool.
- Lookup the IP address for the host name instead */
- //IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server
- IPAddress timeServerIP; // time.nist.gov NTP server address
- const char* ntpServerName = "ntp1.aliyun.com";
- const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message
- byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets
- // A UDP instance to let us send and receive packets over UDP
- WiFiUDP udp;
- void setup()
- {
- pinMode(TRIGGER_PIN, INPUT);
- //初始化
- Serial.begin(9600);//串口波特率115200
- delay(1000);
- delay(5000);
- pinMode(LED_BUILTIN, OUTPUT);
- Serial.println("\n Starting");
- digitalWrite(LED_BUILTIN, HIGH);
- // is configuration portal requested?
- if ( digitalRead(TRIGGER_PIN) == LOW ) {
- // turn the LED on (HIGH is the voltage level)
- //WiFiManager
- //Local intialization. Once its business is done, there is no need to keep it around
- WiFiManager wifiManager;
- digitalWrite(LED_BUILTIN, LOW);
- //reset settings - for testing
- //wifiManager.resetSettings();
- //sets timeout until configuration portal gets turned off
- //useful to make it all retry or go to sleep
- //in seconds
- //wifiManager.setTimeout(120);
- //it starts an access point with the specified name
- //here "AutoConnectAP"
- //and goes into a blocking loop awaiting configuration
- //WITHOUT THIS THE AP DOES NOT SEEM TO WORK PROPERLY WITH SDK 1.5 , update to at least 1.5.1
- //WiFi.mode(WIFI_STA);
-
- if (!wifiManager.startConfigPortal("LED-Clock")) {
- Serial.println("failed to connect and hit timeout");
- delay(3000);
- //reset and try again, or maybe put it to deep sleep
- ESP.reset();
- delay(5000);
- }
- //if you get here you have connected to the WiFi
- Serial.println("connected...yeey :)");
- }
- digitalWrite(LED_BUILTIN, HIGH); // turn the LED off by making the voltage LOW
- delay(1000);
- udp.begin(localPort);
- Serial.print("Local port: ");
- Serial.println(udp.localPort());
- }
- void loop()
- {
- if (WiFi.status() != WL_CONNECTED)
- {
- Serial.println(".");
- delay(1000);
- }
- else
- {
- //get a random server from the pool
- WiFi.hostByName(ntpServerName, timeServerIP);
- sendNTPpacket(timeServerIP); // send an NTP packet to a time server
- // wait to see if a reply is available
- delay(5000);
- int cb = udp.parsePacket();
- if (!cb) {
- // Serial.println("no packet yet");
- Serial.println("$GPRMC,000000.KLE,V,,,,,,,08092019,,,,");
- Serial.println("$BDRMC,000000.KLE,V,,,,,,,08092019,,,,");
- return;
- }
- else {
- //NTP
- // Serial.print("packet received, length=");
- // Serial.println(cb);
- // We've received a packet, read the data from it
- udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer
- //the timestamp starts at byte 40 of the received packet and is four bytes,
- // or two words, long. First, esxtract the two words:
- unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
- unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
- // combine the four bytes (two words) into a long integer
- // this is NTP time (seconds since Jan 1 1900):
- unsigned long secsSince1900 = highWord << 16 | lowWord;
- // Serial.print("Seconds since Jan 1 1900 = " );
- // Serial.println(secsSince1900);
- // now convert NTP time into everyday time:
- // Serial.print("Unix time = ");
- // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
- const unsigned long seventyYears = 2208988800UL;
- // subtract seventy years:
- unsigned long epoch = secsSince1900 - seventyYears;
- // print Unix time:
- // Serial.println(epoch);
- unsigned long Time = time_zone * 3600 + epoch;
- unsigned long Y2KTime = (Time - 946684800) / 86400;//从2000年开始的天数
- unsigned long YTime;//从今年开始的天数
- unsigned int Year;
- unsigned int Month = 0;
- unsigned long Day;
- unsigned int shi;
- unsigned int fen;
- unsigned int miao;
- //日期
- if (Y2KTime % 146097 <= 36525)
- {
- Year = 2000 + Y2KTime / 146097 * 400 + Y2KTime % 146097 / 1461 * 4 + (Y2KTime % 146097 % 1461 - 1) / 365;
- YTime = (Y2KTime % 146097 % 1461 - 1) % 365 + 1;
- }
- else
- {
- Year = 2000 + Y2KTime / 146097 * 400 + (Y2KTime % 146097 - 1) / 36524 * 100 + ((Y2KTime % 146097 - 1) % 36524 + 1) / 1461 * 4 + (((Y2KTime % 146097 - 1) % 36524 + 1) % 1461 - 1) / 365;
- YTime = (((Y2KTime % 146097 - 1) % 36524 + 1) % 1461 - 1) % 365 + 1;
- }
- Day = YTime;
- unsigned char f = 1; //循环标志
- while (f)
- {
- switch (Month)
- {
- case 0:
- if (Day < 31)
- f = 0;
- else
- Day -= 31;
- break;
- case 1:
- if (Day < 29)
- f = 0;
- else
- {
- if (LY(Year))
- {
- Day -= 29;
- }
- else
- {
- Day -= 28;
- }
- }
- break;
- case 2:
- if (Day < 31)
- f = 0;
- else
- Day -= 31;
- break;
- case 3:
- if (Day < 30)
- f = 0;
- else
- Day -= 30;
- break;
- case 4:
- if (Day < 31)
- f = 0;
- else
- Day -= 31;
- break;
- case 5:
- if (Day < 30)
- f = 0;
- else
- Day -= 30;
- break;
- case 6:
- if (Day < 31)
- f = 0;
- else
- Day -= 31;
- break;
- case 7:
- if (Day < 31)
- f = 0;
- else
- Day -= 31;
- break;
- case 8:
- if (Day < 30)
- f = 0;
- else
- Day -= 30;
- break;
- case 9:
- if (Day < 31)
- f = 0;
- else
- Day -= 31;
- break;
- case 10:
- if (Day < 30)
- f = 0;
- else
- Day -= 30;
- break;
- case 11:
- if (Day < 31)
- f = 0;
- else
- Day -= 31;
- break;
- }
- Month += 1;
- }
- Day += 1;
- /***星期
- switch (Y2KTime % 7) //2000年1月1日是星期六
- {
- case 0: display.drawBitmap(112, 48, weekData6, 16, 16, WHITE); break;
- case 1: display.drawBitmap(112, 48, weekData7, 16, 16, WHITE); break;
- case 2: display.drawBitmap(112, 48, weekData1, 16, 16, WHITE); break;
- case 3: display.drawBitmap(112, 48, weekData2, 16, 16, WHITE); break;
- case 4: display.drawBitmap(112, 48, weekData3, 16, 16, WHITE); break;
- case 5: display.drawBitmap(112, 48, weekData4, 16, 16, WHITE); break;
- case 6: display.drawBitmap(112, 48, weekData5, 16, 16, WHITE); break;
- }
- ***/
- //时间
- // print the hour, minute and second:
- if ((Time % 86400L) / 3600 < 10)
- {
- shi = 0;
- // Serial.print('00');
- }
- // Serial.print("The time is "); // UTC is the time at Greenwich Meridian (GMT)
- // Serial.print((Time % 86400L) / 3600); // print the hour (86400 equals secs per day)
- shi = ((Time % 86400L) / 3600);
- // Serial.print(':');
- if ( ((Time % 3600) / 60) < 10 ) {
- // In the first 10 minutes of each hour, we'll want a leading '0'
- // Serial.print('0');
- fen = 0;
- }
- fen = fen + ((Time % 3600) / 60);
- // Serial.print((Time % 3600) / 60); // print the minute (3600 equals secs per minute)
- // Serial.print(':');
- if ( (Time % 60) < 10 ) {
- // In the first 10 seconds of each minute, we'll want a leading '0'
- // Serial.print('0');
- miao = 0;
- }
- miao = miao + (Time % 60);
- // Serial.print(Time % 60); // print the second
- //China time:23/3/2018 - 17:15:48 - Friday
- //$GPRMC,091548.lbq,A,,,,,,,230318,,,,
- Serial.print("$GPRMC,");
-
- if (shi<10){Serial.print('0');}
- Serial.print(shi);
- if (fen<10){Serial.print('0');}
- Serial.print(fen);
- if (miao<10){Serial.print('0');}
- Serial.print(miao);
- Serial.print(".KLE,A,,,,,,,");
- if ( (Day) < 10 ) {Serial.print('0');}
- Serial.print(Day);
- if ( (Month) < 10 ) {Serial.print('0');}
- Serial.print(Month);
- Serial.print(Year);
- Serial.println(",,,,");
-
- //$BDRMC,091548.lbq,A,,,,,,,230318,,,,
- Serial.print("$BDRMC,");
-
- if (shi<10){Serial.print('0');}
- Serial.print(shi);
- if (fen<10){Serial.print('0');}
- Serial.print(fen);
- if (miao<10){Serial.print('0');}
- Serial.print(miao);
- Serial.print(".KLE,A,,,,,,,");
- if ( (Day) < 10 ) {Serial.print('0');}
- Serial.print(Day);
- if ( (Month) < 10 ) {Serial.print('0');}
- Serial.print(Month);
- Serial.print(Year);
- Serial.println(",,,,");
- }
- // display.update();
- }
- }
- // send an NTP request to the time server at the given address
- unsigned long sendNTPpacket(IPAddress& address)
- {
- // Serial.println("sending NTP packet...");
- // set all bytes in the buffer to 0
- memset(packetBuffer, 0, NTP_PACKET_SIZE);
- // Initialize values needed to form NTP request
- // (see URL above for details on the packets)
- 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
- // 8 bytes of zero for Root Delay & Root Dispersion
- packetBuffer[12] = 49;
- packetBuffer[13] = 0x4E;
- packetBuffer[14] = 49;
- packetBuffer[15] = 52;
- // all NTP fields have been given values, now
- // you can send a packet requesting a timestamp:
- udp.beginPacket(address, 123); //NTP requests are to port 123
- udp.write(packetBuffer, NTP_PACKET_SIZE);
- udp.endPacket();
- }
- unsigned char LY(unsigned int y)//判断是否为闰年
- {
- if (y % 400 == 0)
- return 1;
- if (y % 100 == 0)
- return 0;
- if (y % 4 == 0)
- return 1;
- }
复制代码 |
|