arduino数码管显示0到9_一款简单的互联网时间显示器

81f09772446b624cb4941139c650b559.png

如头图所示,如果绑两根绳,也可以称之为手表。

本例子使用的硬件如下:

  1. tm1637:4位数码管1块
  2. NodeMCU:esp8266开发板一枚
  3. microUSB:电源线1条
  4. 杜邦线:4根

需要的软件,就是arduino了。在arduino的首选项加入如下:

https://arduino.esp8266.com/stable/package_esp8266com_index.json

以获得对esp8266的支持。

08c59ed48b3745f199ae24bfd8406b9b.png

打开开发板管理器,搜索esp8266并安装。

6298b29807fcc4f038d04b502272871f.png

安装后,在开发板里就可以选择NodeMCU1.0(ESP-12E Module)了。

967efbdba41be1f74a5d54afbebb0d8f.png

然后打开库管理器,搜索tm1637(4位数码管驱动),按照下图所示,点击安装。

1cfdfb58b5e6e39db8c23be619c6a327.png

然后,继续安装wifimanager

4621d412f8d3a5975f559d97b0c5d8d5.png

wifimanager的作用是,用来配网。

硬件连接

tm1637的CLK对应nodeMCU的D0 (gpio 16)

tm1637的DIO对应nodeMCU的D4 (gpio 2)

b1e378aa7d6dda66fc1c421ce206d46c.png

之后,源码在这里:

#include <ESP8266WiFi.h>
#include <time.h>
#include <TM1637Display.h>
//needed for library
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h>         //https://github.com/tzapu/WiFiManager


// NTP Config
int timezone = 8 * 3600;
int dst = 0;

// Module connection pins (Digital Pins)
#define CLK D0  //gpio 16
#define DIO D4  //gpio 2
#define TEST_DELAY   2000

const uint8_t SEG_CONN[] = {
  SEG_A | SEG_D | SEG_E | SEG_F,                   // C
  SEG_C | SEG_D | SEG_E | SEG_G,                   // o
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_C | SEG_E | SEG_G                            // n
  };

const uint8_t SEG_PASS[] = {
  SEG_A | SEG_B | SEG_E | SEG_F | SEG_G,           // P
  SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,   // A
  SEG_A | SEG_C | SEG_D | SEG_F | SEG_G,           // S
  SEG_A | SEG_C | SEG_D | SEG_F | SEG_G            // S
  };  

const uint8_t SEG_FAIL[] = {
  SEG_A | SEG_E | SEG_F | SEG_G,                   // F
  SEG_A | SEG_B | SEG_C | SEG_E | SEG_F | SEG_G,   // A
  SEG_F | SEG_G,                                   // I
  SEG_D | SEG_F | SEG_G                            // L
  };    

const uint8_t SEG_DONE[] = {
  SEG_B | SEG_C | SEG_D | SEG_E | SEG_G,           // d
  SEG_A | SEG_B | SEG_C | SEG_D | SEG_E | SEG_F,   // O
  SEG_C | SEG_E | SEG_G,                           // n
  SEG_A | SEG_D | SEG_E | SEG_F | SEG_G            // E
  };  

TM1637Display display(CLK, DIO);

String lastTimeStr;
unsigned long lastShowColon = 0;
bool showColon = false;
bool hourcheck=false;
  
void setup()
{
  display.setBrightness(0x01);// 0x01~0x0e
  WiFiManager wifiManager;
  wifiManager.autoConnect("ClockAP");
  
  display.setSegments(SEG_CONN);
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
  }
  display.setSegments(SEG_PASS);
  delay(1000);

  // Get time from NTP server
  configTime(timezone, dst, "ntp.ku.ac.th", "fw.eng.ku.ac.th", "ilm.live.rmutt.ac.th");
  while (!time(nullptr)) 
  {
    delay(500);
  }
  display.setSegments(SEG_DONE);
  delay(1000);
}

void loop()
{
  uint8_t hour;
  bool hourcheck;
  time_t now = time(nullptr);
  struct tm* p_tm = localtime(&now);
  
  hour=p_tm->tm_hour;

  if(hour<1)
  {
    hourcheck=true;
  }
  else
  {
    hourcheck=false;
  }
  if((p_tm->tm_sec % 2) == 0)
  {
    display.showNumberDecEx(hour*100+p_tm->tm_min,0x40 ,hourcheck);
  }
  else
  {
    display.showNumberDecEx(hour*100+p_tm->tm_min,0x00, hourcheck );
  }
}

烧录后,重新上电,你会搜索到一个名为的ClockAP的热点。该热点无密码,直接连接就好。连接后,输入192.168.4.1,会得到如下界面:

09c0c0425fb6a732bcaceeb2247c7ff4.png

点击config wifi,输入你路由器的热点和密码,进行配置。

106599c79a9433ebeba58b1d546ca020.png

之后点击save按钮保存。

这个时候,会出现如下界面。

3f94f489e98975468cdd11232e16438d.png

因为esp8266已经去连接你设置的热点了,所以它自己的热点就不开放了。这个时候,你的电子时钟,应该已经显示时间了(如果你硬件没问题并且连接都正确)。

a18f8e38e260cce2b28c4d58bc22e588.png

最后,源代码在:

hello-esp8266/arduino-tm1637-clock​github.com
48d91f5f44f149c78e9c021cd3c23380.png

如果你懒得使用arduino编译,可以直接使用烧录工具烧录二进制,直接在release下载二进制就好了。

https://github.com/hello-esp8266/arduino-tm1637-clock/releases​github.com

如有任何问题,欢迎留言。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值