arduino nocemcu v3 NtpTime 0.9oled 显示

arduino nocemcu v3 NtpTime 0.9oled 显示代码为示例 include wire h Only needed for Arduino 1 6 5 and earlier include SSD1306Wire h legacy include SSD1306 h include timelib h include lt timelib h wire h

大家好,我是讯享网,很高兴认识大家。

代码为示例


讯享网

#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier #include "SSD1306Wire.h" // legacy: #include "SSD1306.h" #include <TimeLib.h> #include <ESP8266WiFi.h> #include <WiFiUdp.h> SSD1306Wire display(0x3c, D3, D5); // ADDRESS, SDA, SCL - SDA and SCL usually populate automatically based on your board's pins_arduino.h // SSD1306Wire display(0x3c, D3, D5); // ADDRESS, SDA, SCL struct clock { int hour; int minute; int second; int day; int month; int year; } CLOCK; const char ssid[] = "a5"; // your network SSID (name) const char pass[] = ""; // your network password static const char ntpServerName[] = "us.pool.ntp.org"; const int timeZone = 8; // Central European Time WiFiUDP Udp; unsigned int localPort = 8888; // local port to listen for UDP packets #define DEMO_DURATION 800 int demoMode = 0; int counter = 1; 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(); void digitalClockDisplay(); void sendNTPpacket(IPAddress &address); void sendNTPpacket(IPAddress &address) { // 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] = 0b; // 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(); } 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 - UL + timeZone * SECS_PER_HOUR; } } Serial.println("No NTP Response :-("); return 0; // return 0 if unable to get the time } void setup() { Serial.begin(9600); Serial.println(); CLOCK.second = 1; CLOCK.minute = 26; CLOCK.hour = 14; CLOCK.day = 23; CLOCK.month = 4; CLOCK.year = 2022; //配置wifi WiFi.mode(WIFI_STA); WiFi.begin(ssid, pass); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(); Serial.print("Connected! IP address: "); Serial.println(WiFi.localIP()); Serial.println("Starting UDP"); Udp.begin(localPort); display.init(); display.flipScreenVertically(); display.setFont(ArialMT_Plain_10); display.setTextAlignment(TEXT_ALIGN_CENTER); display.drawString(64, 22, "Starting UDP"); Serial.println("waiting for sync"); setSyncProvider(getNtpTime); setSyncInterval(30000); } String nowTime; void digitalClockDisplay() { CLOCK.hour = hour(); CLOCK.minute = minute(); CLOCK.second = second(); CLOCK.day = day(); CLOCK.month = month(); CLOCK.year = year(); } String ymd; String retrunDigits(int digits) { String str; str = (digits < 10) ? "0" + String(digits) : String(digits); return str; } struct timeString { String hour; String minute; String second; String day; String month; String year; } dateTime; void viewclock() { dateTime.hour = retrunDigits(CLOCK.hour); dateTime.minute = retrunDigits(CLOCK.minute); dateTime.second = retrunDigits(CLOCK.second); dateTime.day = retrunDigits(CLOCK.day); dateTime.month = retrunDigits(CLOCK.month); dateTime.year = retrunDigits(CLOCK.year); display.setFont(ArialMT_Plain_24); display.setTextAlignment(TEXT_ALIGN_CENTER); nowTime = dateTime.hour + ":" + dateTime.minute + ":" + dateTime.second; Serial.println(nowTime); ymd = dateTime.day + " / " + dateTime.month + " / " + dateTime.year; display.drawString(64, 22, nowTime); display.setFont(ArialMT_Plain_10); display.drawString(64, 54, ymd); } long timeSinceLastModeSwitch = 0; void loop() { if (millis() - timeSinceLastModeSwitch > DEMO_DURATION) { digitalClockDisplay(); display.clear(); viewclock(); update(); timeSinceLastModeSwitch = millis(); } display.display(); delay(200); } void update() { CLOCK.second++; if (CLOCK.second == 60) { CLOCK.second = 0; CLOCK.minute++; } if (CLOCK.minute == 60) { CLOCK.minute = 0; CLOCK.hour++; } if (CLOCK.hour == 24) { CLOCK.hour = 0; } }

讯享网
小讯
上一篇 2025-02-10 08:34
下一篇 2025-02-20 20:56

相关推荐

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容,请联系我们,一经查实,本站将立刻删除。
如需转载请保留出处:https://51itzy.com/kjqy/47494.html