您尚未登录。

#1 2020-05-13 01:04:42

sven1234
会员
注册时间: 2020-02-26
累计积分: 18

分享一个自己做的wifi时钟代码

买了个VFD屏,无聊做了个时钟,但感觉用RTC自己跑时间久了总是不准,所以用ESP8266做了个NTP Client并且分解了年月日。
代码如下。写得有点粗糙见笑了。刚入坛,多见谅。


#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

char ssid[] = "SSID";  //  your network SSID (name)
char pass[] = "password";       // your network password

unsigned int localPort = 2390;      // local port to listen for UDP packets

IPAddress timeServerIP; 
const char* ntpServerName = "202.118.1.130";//"1.cn.pool.ntp.org";
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
WiFiUDP udp;


// 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();
}

void setup()
{
  Serial.begin(9600);
  Serial.println();
  Serial.println();

  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, pass);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());

  Serial.println("Starting UDP");
  udp.begin(localPort);
  Serial.print("Local port: ");
  Serial.println(udp.localPort());
}

unsigned char rmdp[12]={0,31,59,90,120,151,181,212,243,273,304,334};
unsigned char rmdr[12]={0,31,60,91,121,152,181,213,244,274,305,335};
void loop()
{
  //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(1000);
  
  int cb = udp.parsePacket();
  if (!cb) {
    Serial.println("no packet yet");
  }
  else {
    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;
    secsSince1900=secsSince1900+28800;
    Serial.print("Seconds since Jan 1 1900 = " );
    Serial.println(secsSince1900);
    
    unsigned long second=secsSince1900%60;
    unsigned long minute=secsSince1900/60%60;
    unsigned long hour=secsSince1900/3600%24;

    unsigned long num_date=secsSince1900/86400;
    unsigned long num_four_year=(num_date-365)/1461;
    unsigned long last_date=(num_date-365)%1461;
    unsigned long last_num_year=last_date/365;

    unsigned long last_year_date=last_date-last_num_year*365;

    unsigned long year=1901+num_four_year*4+last_num_year;
    unsigned long month=last_year_date/30+1;
    unsigned long date=0;
    if(last_num_year==3){
      date=last_year_date-rmdr[month-1]+1;
    }else{
      date=last_year_date-rmdp[month-1]+1;
    }
    unsigned long day=num_date%7+1;
    
    Serial.print(year);
    Serial.print("-");
    Serial.print(month);
    Serial.print("-");
    Serial.print(date);
    Serial.print("  ");
    Serial.print(day);
    Serial.print("  ");
    Serial.print(hour);
    Serial.print(":");
    Serial.print(minute);
    Serial.print(":");
    Serial.println(second);   
/////////////////////////////////////////////////////////////////////////////////////////////////
    Serial.print("CMD");
    Serial.write(year%100/10);Serial.write(year%10);
    Serial.write(month/10);Serial.write(month%10);
    Serial.write(date/10);Serial.write(date%10);
    Serial.write(day);
    Serial.write(hour/10);Serial.write(hour%10);
    Serial.write(minute/10);Serial.write(minute%10);
    Serial.write(second/10);Serial.write(second%10);
    
    Serial.write(0x0d);Serial.write(0x0a);
    
  }
  // wait ten seconds before asking for the time again
  delay(10000);
}

离线

#2 2020-05-13 10:13:18

642242855
会员
注册时间: 2019-10-31
累计积分: 135

Re: 分享一个自己做的wifi时钟代码

标记下

离线

#3 2020-05-13 10:23:37

哇酷小二
管理员
注册时间: 2020-04-22
累计积分: 1,876

Re: 分享一个自己做的wifi时钟代码

感谢支持, 有没有靓照?

离线

#4 2020-05-13 10:27:18

夏雨夜寐
会员
注册时间: 2019-08-23
累计积分: 72

Re: 分享一个自己做的wifi时钟代码

又不爆照……

离线

#5 2020-05-13 20:41:43

sven1234
会员
注册时间: 2020-02-26
累计积分: 18

Re: 分享一个自己做的wifi时钟代码

应广大坛友要求晒上丑陋的高清大照……
IMG_20200513_203003.jpg
IMG_20200513_203036.jpg
IMG_20200513_203126.jpg
IMG_20200513_203145.jpg
IMG_20200513_203205.jpg
IMG_20200513_203218.jpg
IMG_20200513_203235.jpg
IMG_20200513_203339.jpg

离线

#6 2020-05-15 15:26:03

hzqlz
会员
注册时间: 2018-03-23
累计积分: 94

Re: 分享一个自己做的wifi时钟代码

这飞线技术了得!

离线

#7 2020-05-23 23:11:22

hyghyg1234
会员
注册时间: 2020-05-23
累计积分: 9

Re: 分享一个自己做的wifi时钟代码

现在做板子那么便宜,还飞线。

离线

#8 2020-05-24 20:57:42

sunshine11140
会员
注册时间: 2020-05-24
累计积分: 2

Re: 分享一个自己做的wifi时钟代码

很有参考价值,跟着学习一下

离线

#9 2020-05-31 14:57:24

TDA2030
会员
注册时间: 2019-10-17
累计积分: 11

Re: 分享一个自己做的wifi时钟代码

这才是真正的DIY,这么多板子用飞线连接,楼主厉害呀

离线

#10 2021-01-01 20:09:47

kiss
会员
注册时间: 2017-11-01
累计积分: 11

Re: 分享一个自己做的wifi时钟代码

看着像老外的手艺 哈哈

离线

#11 2021-01-01 20:56:59

EE
会员
注册时间: 2020-10-04
累计积分: 75

Re: 分享一个自己做的wifi时钟代码

kiss 说:

看着像老外的手艺 哈哈

+1

氖光就更赞了

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn