Arduino + 网络扩展板 制作NTP网络时钟

发布日期:分类:智能硬件 & IOT Arduino + 网络扩展板 制作NTP网络时钟无评论
时效性提醒:本文首次编写发布于8 年前。

使用Arduino+网络扩展板和一个lcd1602来制作,每20s使用NTP同步一次时间,所以绝对无误差,不过必须要在又网络的地方才能用。

首先会尝试使用DHCP来自动获取ip,如果失败则使用静态ip。

点击查看大图

别的不多说了,直接上代码:

#include <LiquidCrystal.h>

#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>

int RS=49;
int RW=47;
int E =45;

int DB4=35;
int DB5=33;
int DB6=31;
int DB7=29;

unsigned long epoch;



byte mac[] = {
  0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

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

char timeServer[] = "time.nist.gov"; // time.nist.gov NTP server

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
EthernetUDP Udp;

IPAddress static_ip(192,168,1,177);

LiquidCrystal lcd(RS, E, DB4, DB5, DB6, DB7);

void setup() {
    pinMode(27, OUTPUT);    //LDE +
    pinMode(25, OUTPUT);    //LDE -
    pinMode(53, OUTPUT);    //VCC
    pinMode(51, OUTPUT);    //V0

    pinMode(RS, OUTPUT);    //RS
    pinMode(RW, OUTPUT);    //RW
    pinMode(E , OUTPUT);    //E
    

    digitalWrite(27, HIGH);
    digitalWrite(25, LOW);
    digitalWrite(53, HIGH);
    digitalWrite(51, LOW);
    digitalWrite(RW, LOW);

    // Open serial communications and wait for port to open:
    Serial.begin(9600);

    lcd.begin(16, 2);
    lcd.blink();
    
    Serial.println("1602 NTP Clock");
    Serial.print("Trying to configure Ethernet using DHCP...");

    lcd.print("1602 NTP Clock");
    lcd.setCursor(0,1);
    lcd.print("Trying DHCP...");
    
    if (Ethernet.begin(mac) == 0){
        Serial.println("Failed");
        Serial.println("Use static IP");
        Ethernet.begin(mac, static_ip);
    }else{
        Serial.println("Success");
    }
    Serial.print("Arduino`s IP is ");
    Serial.println(Ethernet.localIP());

    lcd.clear();
    lcd.home();
    lcd.print("Arduino`s IP is ");
    lcd.setCursor(2,1);
    lcd.print(Ethernet.localIP());
    
    Udp.begin(localPort);
    sendNTPpacket(timeServer);
    delay(1000);
    processNTPpacket();

    lcd.clear();
}

void loop() {
    int i;

    for(i=0;i<19;i++){
        displayTheTime();
        delay(1000);
        epoch+=1;
    }

    displayTheTime();
    sendNTPpacket(timeServer);
    delay(1000);
    processNTPpacket();

}


// send an NTP request to the time server at the given address
void sendNTPpacket(char* 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();
}

void processNTPpacket(){
    if (Udp.parsePacket()) {
        // 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, extract 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;
        
        // 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:
        epoch = secsSince1900 - seventyYears + 1;
        // print Unix time:
        Serial.println(epoch);
    }
}

void displayTheTime(){

    lcd.home();
    lcd.print('%');
    lcd.print(epoch);

    // print the hour, minute and second:    
    lcd.setCursor(0,1);

    lcd.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)
    lcd.print(':');
    if (((epoch % 3600) / 60) < 10) {
        // In the first 10 minutes of each hour, we'll want a leading '0'
        lcd.print('0');
    }
    lcd.print((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    lcd.print(':');
    if ((epoch % 60) < 10) {
        // In the first 10 seconds of each minute, we'll want a leading '0'
        lcd.print('0');
    }
    lcd.print(epoch % 60); // print the second
    lcd.setCursor(15,1);
}

作者:WuSiYu

学生,Web开发者,智能硬件&IOT爱好者

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注