买了个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);
}
离线
标记下
离线
又不爆照……
离线
应广大坛友要求晒上丑陋的高清大照……
离线
这飞线技术了得!
在线
现在做板子那么便宜,还飞线。
离线
很有参考价值,跟着学习一下
离线
这才是真正的DIY,这么多板子用飞线连接,楼主厉害呀
离线
看着像老外的手艺 哈哈
离线
能做出来就非常不错了,赞一个。。。
离线
离线