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 <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[] = "20130507";       // 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] = 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();
}

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 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;
  }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值