通过TM1637 及esp01的网路功能实现数字网络时钟
#include <NTPClient.h>
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include "TM1637.h"
//TM1637 针脚定义
#define CLK 0
#define DIO 2
TM1637 tm1637(CLK,DIO);
// wifi 信息定义
const char *ssid = "haha";
const char *password = "haha12345678@";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"ntp1.aliyun.com",60*60*8,30*60*1000);
int update = 0;
int delaycount= 20;
int currentHour = 88;
int currentMinute = 88;
void setup()
{
Serial.begin(9600);
tm1637.init();
tm1637.set(BRIGHT_TYPICAL);//BRIGHT_TYPICAL = 2,BRIGHT_DARKEST = 0,BRIGHTEST = 7;
WiFi.begin(ssid, password);
while ( WiFi.status() != WL_CONNECTED ) {
tm1637.point(POINT_ON);
infoTime(currentHour/10,currentHour%10,currentMinute/10,currentMinute%10);
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop()
{
if (update % 2 == 0)
{
tm1637.point(POINT_ON);
} else {
tm1637.point(POINT_OFF);
}
Serial.print("update: ");
Serial.println(update);
update++;
if(update > delaycount ) {
update = 0;
timeClient.update();
currentHour = timeClient.getHours();
Serial.print("Hour: ");
Serial.print(currentHour);
currentMinute = timeClient.getMinutes();
Serial.print("Minutes: ");
Serial.println(currentMinute);
}
infoTime(currentHour/10,currentHour%10,currentMinute/10,currentMinute%10);
delay(500);
}
void infoTime(int h0,int h1, int m0,int m1) {
tm1637.display(0,h0);
tm1637.display(1,h1);
tm1637.display(2,m0);
tm1637.display(3,m1);
}
|