升级!

添加了12h/24h 的开关,还有标准/ 夏令时开关!见步骤7 & 步骤8。

你是否曾想要一个和办公室时间来源全然准确的表?

这就有一个网络的办公时间server,你能够根据它并同步你的时间。

大多数人用电脑来进行设置,如今Arduino也能够相同做到。

(GPS时间client。详见 http://arduinotronics.blogspot.com/2014/03/gps-on-lcd.html

你仅仅须要一个Arduino和一个以太网插板,可是我们也加一个LCD显示屏。随后可能还添加闹钟功能。

Arduino
UNO


Arduino
Ethernet Shield

可选:

串口
LCD 显示屏

步骤1:连接硬件

首先,记下印在以太网插板顶端的MAC地址。你接下来会用到。

看起来像 90 A2 DA 00 23 36,可是在代码中插入须要变成 0×90,0xA2,0xDA。0×00,0×23,0×36.

将以太网插板查到Arduino UNO顶部。用一根网线把它连到路由器上。

步骤2:代码

仅仅须要在Arduino库目录下额外安装一个库。那就是 Time Library。可找 http://www.pjrc.com/teensy/td_libs_Time.html

你讲须要以太网插板顶部的那个MAC地址。可是IP,网关。子网掩码都能够通过DHCP获得。你还须要时间server地址(见以下步骤)。

须要下载到Arduino上的程序例如以下:

//sample code originated at http://www.openreefs.com/ntpServer

//modified by Steve Spence, http://arduinotronics.blogspot.com

#include <SPI.h>

#include <Ethernet.h>

#include <EthernetUdp.h>

#include <Time.h>

/* ******** Ethernet Card Settings ******** */

// Set this to your Ethernet Card Mac Address

byte mac[] = { 0×90, 0xA2, 0xDA, 0×00, 0×23, 0×36 };

/* ******** NTP Server Settings ******** */

/* us.pool.ntp.org NTP server

(Set to your time server of choice) */

IPAddress timeServer(216, 23, 247, 62);

/* Set this to the offset (in seconds) to your local time

This example is GMT – 4 */

const long timeZoneOffset = -14400L;

/* Syncs to NTP server every 15 seconds for testing,

set to 1 hour or more to be reasonable */

unsigned int ntpSyncTime = 3600;

/* ALTER THESE VARIABLES AT YOUR OWN RISK */

// local port to listen for UDP packets

unsigned int localPort = 8888;

// NTP time stamp is in the first 48 bytes of the message

const int NTP_PACKET_SIZE= 48;

// Buffer to hold incoming and outgoing packets

byte packetBuffer[NTP_PACKET_SIZE];

// A UDP instance to let us send and receive packets over UDP

EthernetUDP Udp;

// Keeps track of how long ago we updated the NTP server

unsigned long ntpLastUpdate = 0;

// Check last time clock displayed (Not in Production)

time_t prevDisplay = 0;

void setup() {

Serial.begin(9600);

// Ethernet shield and NTP setup

int i = 0;

int DHCP = 0;

DHCP = Ethernet.begin(mac);

//Try to get dhcp settings 30 times before giving up

while( DHCP == 0 && i < 30){

delay(1000);

DHCP = Ethernet.begin(mac);

i++;

}

if(!DHCP){

Serial.println(“DHCP FAILED”);

for(;;); //Infinite loop because DHCP Failed

}

Serial.println(“DHCP Success”);

//Try to get the date and time

int trys=0;

while(!getTimeAndDate() && trys<10) {

trys++;

}

}

// Do not alter this function, it is used by the system

int getTimeAndDate() {

int flag=0;

Udp.begin(localPort);

sendNTPpacket(timeServer);

delay(1000);

if (Udp.parsePacket()){

Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

unsigned long highWord, lowWord, epoch;

highWord = word(packetBuffer[40], packetBuffer[41]);

lowWord = word(packetBuffer[42], packetBuffer[43]);

epoch = highWord << 16 | lowWord;

epoch = epoch – 2208988800 + timeZoneOffset;

flag=1;

setTime(epoch);

ntpLastUpdate = now();

}

return flag;

}

// Do not alter this function, it is used by the system

unsigned long sendNTPpacket(IPAddress& address)

{

memset(packetBuffer, 0, NTP_PACKET_SIZE);

packetBuffer[0] = 0b11100011;

packetBuffer[1] = 0;

packetBuffer[2] = 6;

packetBuffer[3] = 0xEC;

packetBuffer[12]  = 49;

packetBuffer[13]  = 0x4E;

packetBuffer[14]  = 49;

packetBuffer[15]  = 52;

Udp.beginPacket(address, 123);

Udp.write(packetBuffer,NTP_PACKET_SIZE);

Udp.endPacket();

}

// Clock display of the time and date (Basic)

void clockDisplay(){

Serial.print(hour());

printDigits(minute());

printDigits(second());

Serial.print(” “);

Serial.print(day());

Serial.print(” “);

Serial.print(month());

Serial.print(” “);

Serial.print(year());

Serial.println();

}

// Utility function for clock display: prints preceding colon and leading 0

void printDigits(int digits){

Serial.print(“:”);

if(digits < 10)

Serial.print(’0′);

Serial.print(digits);

}

// This is where all the magic happens…

void loop() {

// Update the time via NTP server as often as the time you set at the top

if(now()-ntpLastUpdate > ntpSyncTime) {

int trys=0;

while(!getTimeAndDate() && trys<10){

trys++;

}

if(trys<10){

Serial.println(“ntp server update success”);

}

else{

Serial.println(“ntp server update failed”);

}

}

// Display the time if it has changed by more than a second.

if( now() != prevDisplay){

prevDisplay = now();

clockDisplay();

}

}

步骤3:时钟server地址

假设你知道一个工作时间serverIP地址,在你的代码中键入它。

/* ******** NTP Server Settings ******** */

/* us.pool.ntp.org NTP server

(Set to your time server of choice) */

IPAddress timeServer(216, 23, 247, 62);

Otherwise, run this sketch to get a valid time server ip. If you really want to get techy, merge the following code into the main sketch so that it finds a valid time server on every update. Don’t forget to update your MAC address below.

/*

DHCP-based IP printer

This sketch uses the DHCP extensions to the Ethernet library

to get an IP address via DHCP and print the address obtained.

using an Arduino Wiznet Ethernet shield.

Circuit:

* Ethernet shield attached to pins 10, 11, 12, 13

created 12 April 2011

by Tom Igoe

*/

#include <SPI.h>

#include <Ethernet.h>

#include <Dns.h>

// Enter a MAC address for your controller below.

// Newer Ethernet shields have a MAC address printed on a sticker on the shield

byte mac[] = { 0×00, 0xAA, 0xBB, 0xCC, 0xDE, 0×02 };

// Initialize the Ethernet client library

// with the IP address and port of the server

// that you want to connect to (port 80 is default for HTTP):

EthernetClient client;

void setup() {

// start the serial library:

Serial.begin(9600);

pinMode(4,OUTPUT);

digitalWrite(4,HIGH);

// start the Ethernet connection:

if (Ethernet.begin(mac) == 0) {

Serial.println(“Failed to configure Ethernet using DHCP”);

// no point in carrying on, so do nothing forevermore:

for(;;)

;

}

// print your local IP address:

Serial.print(“My IP address: “);

for (byte thisByte = 0; thisByte < 4; thisByte++) {

// print the value of each byte of the IP address:

Serial.print(Ethernet.localIP()[thisByte], DEC);

Serial.print(“.”);

}

Serial.println();

IPAddress testIP;

DNSClient dns;

dns.begin(Ethernet.dnsServerIP());

dns.getHostByName(“pool.ntp.org”,testIP);

Serial.print(“NTP IP from the pool: “);

Serial.println(testIP);

}

void loop() {

}

步骤4:时间偏移

attachment_id=6371" rel="attachment wp-att-6371" style="margin:0px; padding:0px; border:0px; font-family:inherit; font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline; color:rgb(33,117,155); text-decoration:none">

你须要增加你的时区的时间偏移量。

就像我当前在East Coast DayLight Saving Time,我须要-14400。这是和GMT(世界时)实际那相差的秒数。可參看下表:

http://www.epochconverter.com/epoch/timezones.php

在代码中找到这部分:

/* Set this to the offset (in seconds) to your local time

This example is GMT – 4 */

const long timeZoneOffset = -14400L;

步骤5:它能工作了吗?

attachment_id=6372" rel="attachment wp-att-6372" style="margin:0px; padding:0px; border:0px; font-family:inherit; font-size:undefined; font-style:inherit; font-variant:inherit; font-weight:inherit; line-height:inherit; vertical-align:baseline; color:rgb(33,117,155); text-decoration:none">

至此。硬件已连接好(UNO和以太网插板),连接路由器,把你的MAC地址和时钟server地址输入到Arduino中,你应该能看到类似例如以下:

步骤6:加LCD屏

假设你正使用串口LCD屏,如今就能连上。

这有一个额外的库,是I2C(接口)LCD库。你能够在这找到:

http://arduinotronics.blogspot.com/2014/02/sainsmart-i2c-lcd.html

LCD       Arduino UNO

SCL        A5

SDA       A4

VCC       +5v

GND       Gnd

上述带有LCD添加项的NTP代码例如以下:

//sample code originated at http://www.openreefs.com/ntpServer

//modified by Steve Spence, http://arduinotronics.blogspot.com

#include <SPI.h>

#include <Ethernet.h>

#include <EthernetUdp.h>

#include <Time.h>

#include <Wire.h>

#include <LCD.h>

#include <LiquidCrystal_I2C.h>

//LCD Settings

#define I2C_ADDR    0x3F // <<—– Add your address here.  Find it from I2C Scanner

#define BACKLIGHT_PIN     3

#define En_pin  2

#define Rw_pin  1

#define Rs_pin  0

#define D4_pin  4

#define D5_pin  5

#define D6_pin  6

#define D7_pin  7

LiquidCrystal_I2C lcd(I2C_ADDR,En_pin,Rw_pin,Rs_pin,D4_pin,D5_pin,D6_pin,D7_pin);

/* ******** Ethernet Card Settings ******** */

// Set this to your Ethernet Card Mac Address

byte mac[] = { 0×90, 0xA2, 0xDA, 0×00, 0×23, 0×36 };

/* ******** NTP Server Settings ******** */

/* us.pool.ntp.org NTP server

(Set to your time server of choice) */

IPAddress timeServer(216, 23, 247, 62);

/* Set this to the offset (in seconds) to your local time

This example is GMT – 4 */

const long timeZoneOffset = -14400L;

/* Syncs to NTP server every 15 seconds for testing,

set to 1 hour or more to be reasonable */

unsigned int ntpSyncTime = 3600;

/* ALTER THESE VARIABLES AT YOUR OWN RISK */

// local port to listen for UDP packets

unsigned int localPort = 8888;

// NTP time stamp is in the first 48 bytes of the message

const int NTP_PACKET_SIZE= 48;

// Buffer to hold incoming and outgoing packets

byte packetBuffer[NTP_PACKET_SIZE];

// A UDP instance to let us send and receive packets over UDP

EthernetUDP Udp;

// Keeps track of how long ago we updated the NTP server

unsigned long ntpLastUpdate = 0;

// Check last time clock displayed (Not in Production)

time_t prevDisplay = 0;

void setup() {

lcd.begin (16,2);

lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);

lcd.setBacklight(HIGH);

Serial.begin(9600);

// Ethernet shield and NTP setup

int i = 0;

int DHCP = 0;

DHCP = Ethernet.begin(mac);

//Try to get dhcp settings 30 times before giving up

while( DHCP == 0 && i < 30){

delay(1000);

DHCP = Ethernet.begin(mac);

i++;

}

if(!DHCP){

Serial.println(“DHCP FAILED”);

for(;;); //Infinite loop because DHCP Failed

}

Serial.println(“DHCP Success”);

//Try to get the date and time

int trys=0;

while(!getTimeAndDate() && trys<10) {

trys++;

}

}

// Do not alter this function, it is used by the system

int getTimeAndDate() {

int flag=0;

Udp.begin(localPort);

sendNTPpacket(timeServer);

delay(1000);

if (Udp.parsePacket()){

Udp.read(packetBuffer,NTP_PACKET_SIZE);  // read the packet into the buffer

unsigned long highWord, lowWord, epoch;

highWord = word(packetBuffer[40], packetBuffer[41]);

lowWord = word(packetBuffer[42], packetBuffer[43]);

epoch = highWord << 16 | lowWord;

epoch = epoch – 2208988800 + timeZoneOffset;

flag=1;

setTime(epoch);

ntpLastUpdate = now();

}

return flag;

}

// Do not alter this function, it is used by the system

unsigned long sendNTPpacket(IPAddress& address)

{

memset(packetBuffer, 0, NTP_PACKET_SIZE);

packetBuffer[0] = 0b11100011;

packetBuffer[1] = 0;

packetBuffer[2] = 6;

packetBuffer[3] = 0xEC;

packetBuffer[12]  = 49;

packetBuffer[13]  = 0x4E;

packetBuffer[14]  = 49;

packetBuffer[15]  = 52;

Udp.beginPacket(address, 123);

Udp.write(packetBuffer,NTP_PACKET_SIZE);

Udp.endPacket();

}

// Clock display of the time and date (Basic)

void clockDisplay(){

Serial.print(hour());

printDigits(minute());

printDigits(second());

Serial.print(” “);

Serial.print(day());

Serial.print(” “);

Serial.print(month());

Serial.print(” “);

Serial.print(year());

Serial.println();

lcd.setCursor (0,0);

if (hour() < 10){

lcd.print(“0″); }

if (hour() > 12){

lcd.print(“0″);

lcd.print(hour()-12); } else {

lcd.print(hour()); }

lcd.print(“:”);

if (minute() < 10){

lcd.print(“0″); }

lcd.print(minute());

lcd.print(“:”);

if (second() < 10){

lcd.print(“0″); }

lcd.print(second());

if (hour() > 12){

lcd.print(” PM”); }

else {

lcd.print(” AM”); }

lcd.setCursor (0,1);

if (month() < 10){

lcd.print(“0″); }

lcd.print(month());

lcd.print(“/”);

if (day() < 10){

lcd.print(“0″); }

lcd.print(day());

lcd.print(“/”);

lcd.print(year());

}

// Utility function for clock display: prints preceding colon and leading 0

void printDigits(int digits){

Serial.print(“:”);

if(digits < 10)

Serial.print(’0′);

Serial.print(digits);

}

// This is where all the magic happens…

void loop() {

// Update the time via NTP server as often as the time you set at the top

if(now()-ntpLastUpdate > ntpSyncTime) {

int trys=0;

while(!getTimeAndDate() && trys<10){

trys++;

}

if(trys<10){

Serial.println(“ntp server update success”);

}

else{

Serial.println(“ntp server update failed”);

}

}

// Display the time if it has changed by more than a second.

if( now() != prevDisplay){

prevDisplay = now();

clockDisplay();

}

}

步骤7:12h vs. 24h 时间

起初,我用的是24小时的例程。所下面午一点显示为13.很多其它人喜欢12h的始终。带有AM/PM,所以我终于替换了例程。我再三考虑,加了一个能够选择格式的开关。

首先,我们须要读取开关来决定格式,然后我们须要基于读到的结果来控制代码。

开关须要5个引脚。以太网插板本身须要引脚4,10,11,12&13.

将开关连接在引脚5和地之间。你不须要上拉点入,由于我们将使用INPUT_PULLUP命令在Arduino内部实现。

这是眼下须要改动的代码:

lcd.setCursor (0,0);

if (hour() < 10){

lcd.print(“0″); }

if (hour() > 12){

lcd.print(“0″);

lcd.print(hour()-12); } else {

lcd.print(hour()); }

lcd.print(“:”);

if (minute() < 10){

lcd.print(“0″); }

lcd.print(minute());

lcd.print(“:”);

if (second() < 10){

lcd.print(“0″); }

lcd.print(second());

if (hour() > 12){

lcd.print(” PM”); }

else {

lcd.print(” AM”); }

Here is how the new code with the option of switching back and forth would look like:

//12h_24h (at top of sketch before void setup

int timeFormatPin = 5;   // switch connected to digital pin 5

int timeFormatVal= 0;     // variable to store the read value

//put in void setup replaceing the original code listed above

lcd.setCursor (0,0);

if (hour() < 10){

lcd.print(“0″); }

//12h/24h

pinMode(timeFormatPin, INPUT_PULLUP);      // sets the digital pin 5 as input and activates pull up resistor

timeFormatVal= digitalRead(timeFormatPin);   // read the input pin

if (timeFormatVal == 1) {

lcd.print(hour());

} else {

if (hour() > 12){

lcd.print(“0″);

lcd.print(hour()-12); } else {

lcd.print(hour()); }

}

lcd.print(“:”);

if (minute() < 10){

lcd.print(“0″); }

lcd.print(minute());

lcd.print(“:”);

if (second() < 10){

lcd.print(“0″); }

lcd.print(second());

if (timeFormatVal == 1){

lcd.print(“ 24″);

} else {

if (hour() > 12){

lcd.print(” PM”); }

else {

lcd.print(” AM”); }

}

升级中我考虑的内容:

  • 自己主动找寻可用时间server功能
  • 自己主动开关夏令时时间(Daylight Saving Time)
  • 闹钟功能。带有渐亮等,或者继电器。

  • 没有网络启动电源故障的RTC

你想添加什么呢?

Arduino 网络时钟client的更多相关文章

  1. buildroot ntp 网络时钟同步

    /********************************************************************** * buildroot ntp 网络时钟同步 * 说明: ...

  2. 北斗卫星同步时钟(NTP网络时钟服务器)成功投运世界级工程港珠澳大桥

    北斗卫星同步时钟(NTP网络时钟服务器)成功投运世界级工程港珠澳大桥 北斗卫星同步时钟(NTP网络时钟服务器)成功投运世界级工程港珠澳大桥 本文由北京华人开创科技公司提供 原址在 http://www ...

  3. 网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器

    网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器 网络时钟服务器,NTP授时设备,北斗网络校时服务器,GPS时间同步器 论述当下网络时间同步的重要性   北京华人开创科技发展有限公 ...

  4. NTP网络授时服务器部署及网络时钟同步设置说明

    NTP网络授时服务器部署及网络时钟同步设置说明 NTP网络授时服务器部署及网络时钟同步设置说明  本文由安徽京准科技提供@请勿转载. 一.前言 1.NTP简介 NTP是网络时间协议(Network T ...

  5. NTP网络时钟服务器品牌

    NTP网络时钟服务器品牌 在科技的不断进步和发展下,时钟的种类和功能也在发生着变化,以满足人们的各种需求,时钟从原始的机械时钟发展成具有多钟功能的时钟.而时钟服务器主要是给时钟提供时间信息的,时钟服务 ...

  6. Java Socket网络编程Client端详解

    此类实现客户端套接字(也可以就叫“套接字”).套接字是两台机器之间的通信端点. Socket client = new Socket(ip,port);//创建一个流套接字并将其连接到指定 IP 地址 ...

  7. 网络编程(client发信息给server)

    client发信息给server

  8. ESP32S2小项目-FM-网络时钟/电台-Arduino开发环境

    ESP32S2小项目,FM,网络时钟/电台,Arduino开发环境 效果展示 @ 目录 ESP32S2小项目,FM,网络时钟/电台,Arduino开发环境 效果展示 开机动画: 网络时钟: FM模块: ...

  9. 用Arduino+OSC建立一个iPad铁路王国巡视机

    翻译自:http://blog.mydream.com.hk/howto/build-up-a-ipad-plarail-patrol-with-arduino-osc 简单介绍 这个教程告诉你怎样建 ...

随机推荐

  1. angularJs模块ui-router之多视图

    可以给ui-view指定名称,这样一个模板中就可以有多个ui-view.假设您有一个应用,需要动态填充graph.table data和filters,像下面这样: 当您需要使用多视图时,需要用到状态 ...

  2. STM32F407 I2C 个人笔记

    源代码;https://github.com/YuQiao0303/STM32F407-Examples/tree/master/24.IIC 概述 I2C (IIC, Inter-Integrate ...

  3. 面试准备——redis

    https://blog.csdn.net/yangzhong0808/article/details/81196472 http://www.imooc.com/article/36399 http ...

  4. Caffe的Solver参数设置

    Caffe的solver参数设置 http://caffe.berkeleyvision.org/tutorial/solver.html solver是通过协调前向-反向传播的参数更新来控制参数优化 ...

  5. java面试题之hashcode相等两个类一定相等吗?equals呢?相反呢?

    答:hashcode相等,两个类不一定相等,equals也不一定相等: 反过来,equals相等,hashcode一定相等

  6. Tomcat 调优技巧

    Tomcat 调优技巧:1.Tomcat自身调优: ①采用动静分离节约Tomcat的性能: ②调整Tomcat的线程池: ③调整Tomcat的连接器: ④修改Tomcat的运行模式: ⑤禁用AJP连接 ...

  7. 送外卖(codevs 2800)

    题目描述 Description 有一个送外卖的,他手上有n份订单,他要把n份东西,分别送达n个不同的客户的手上.n个不同的客户分别在1~n个编号的城市中.送外卖的从0号城市出发,然后n个城市都要走一 ...

  8. css样式---隐藏元素

    1.通过设置width:0;或height:0 2.将元素的opacity设置成0 3.通过定位将元素移出屏幕范围 4.通过text-indent实现隐藏文字的效果 5.通过z-index隐藏一个元素 ...

  9. Linux下重启就需要重新激活eth0的解决办法(ifup eth0)

    新安装linux系统,网卡不能自动激活去获取ip,每次都需要手工执行以下命令 ifup eth0 后续通过将ONBOOT=yes这句就能开机启动自动激活,就可以解决问题 vim /etc/syscon ...

  10. 计算机windows7连接打印机

    计算机连接打印机 (1)查看打印机的名字. 示例,打印机名为 HP LaserJet M1522 .... (2)打开windows开始菜单,点击[设备和打印机],然后查看打印机和传真那个区域是否有打 ...