ESP8266 1.5英吋LED网络闹钟
基于NTP网络授时,用ESP8266制做的时钟的最大优点是无需调时,闹钟定时调节非常方便,按下一次功能键后小时十位闪亮,再按下调节键可调时.第二次按功能键后小时个位闪亮,....依次调节定闹时间.第五次按下后恢复正常时钟显示.该闹钟还增加了温湿度显示.时间显示4秒,温度显示1秒,湿度显示1秒,循环进行.求教程及代码,谢谢大佬
本帖最后由 lwq1947 于 2022-3-7 10:38 编辑
3030257ybs 发表于 2022-3-7 09:05
求教程及代码,谢谢大佬
代码如下:
#include <NTPClient.h>
#include "TM1637.h"
#include <ESP8266WiFi.h>
#include "DHT.h"
#include <WiFiUdp.h>
#define DHTPIN 14
#define DHTTYPE DHT11
DHT dht(DHTPIN,DHTTYPE);
#define CLK 4
#define DIO 5
int e=1;
int f=2;
int g=0;
int h=0;
int i=0;
int j=0;
int k=0;
TM1637 tm1637(CLK, DIO);
const char *ssid ="*****";
const char *password ="*****";
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP,"ntp1.aliyun.com",60*60*8,30*60*1000);
void setup(){
dht.begin();
Serial.begin(115200);
tm1637.set(3);
tm1637.init();
WiFi.begin(ssid, password);
pinMode(15, OUTPUT);
pinMode(12, INPUT_PULLUP);
pinMode(2, INPUT_PULLUP);
pinMode(0, INPUT_PULLUP);
digitalWrite(15,LOW);
while ( WiFi.status() != WL_CONNECTED ) {
delay ( 500 );
Serial.print ( "." );
}
timeClient.begin();
}
void loop() {
if(j==1)digitalWrite(15,LOW);
int s=dht.readHumidity();
int t=dht.readTemperature();
timeClient.update();
String comdata=timeClient.getFormattedTime();
String hh=comdata.substring(0,1);
String hl=comdata.substring(1,2);
String mh=comdata.substring(3,4);
String ml=comdata.substring(4,5);
int a=hh.toInt();
int b=hl.toInt();
int c=mh.toInt();
int d=ml.toInt();
if(digitalRead(2)== HIGH) j=0;
if((e==a)&&(f==b)&&(g==c)&&(h==d)&&(digitalRead(2)== LOW)) j=1;
k++;
if(k==6) k=0;
if(digitalRead(12)== LOW) i++;
if(i==5) i=0;
if(i==0) {
if(k<4) {
if(j==1)digitalWrite(15,HIGH);
tm1637.clearDisplay();
tm1637.point(POINT_ON);
if(a>0)tm1637.display(0, a);
tm1637.display(1, b);
tm1637.display(2, c);
tm1637.display(3, d);
delay(500);
tm1637.clearDisplay();
digitalWrite(15,LOW);
tm1637.point(POINT_OFF);
if(a>0)tm1637.display(0, a);
tm1637.display(1, b);
tm1637.display(2, c);
tm1637.display(3, d);
delay(500);
}
if((k==4)&&(j==0)) {
tm1637.clearDisplay();
tm1637.display(0, t/10);
tm1637.display(1, t%10);
tm1637.display(3, 'C');
delay(1000);
}
if((k==5)&&(j==0)) {
tm1637.clearDisplay();
tm1637.display(0, 'H');
tm1637.display(2, s/10);
tm1637.display(3, s%10);
delay(1000);
}
}
if(i==1) {
tm1637.clearDisplay();
tm1637.display(0, e);
tm1637.display(1, f);
tm1637.display(2, g);
tm1637.display(3, h);
delay(500);
tm1637.clearDisplay();
tm1637.display(1, f);
tm1637.display(2, g);
tm1637.display(3, h);
delay(500);
if(digitalRead(0)== LOW) e++;
if(e==3) e=0;
}
if(i==2) {
tm1637.clearDisplay();
tm1637.display(0, e);
tm1637.display(1, f);
tm1637.display(2, g);
tm1637.display(3, h);
delay(500);
tm1637.clearDisplay();
tm1637.display(0, e);
tm1637.display(2, g);
tm1637.display(3, h);
delay(500);
if(digitalRead(0)== LOW) f++;
if((e<2)&&(f==10)) f=0;
if((e==2)&&(f==4)) f=0;
}
if(i==3) {
tm1637.clearDisplay();
tm1637.display(0, e);
tm1637.display(1, f);
tm1637.display(2, g);
tm1637.display(3, h);
delay(500);
tm1637.clearDisplay();
tm1637.display(0, e);
tm1637.display(1, f);
tm1637.display(3, h);
delay(500);
if(digitalRead(0)== LOW) g++;
if(g==6) g=0;
}
if(i==4) {
tm1637.clearDisplay();
tm1637.display(0, e);
tm1637.display(1, f);
tm1637.display(2, g);
tm1637.display(3, h);
delay(500);
tm1637.clearDisplay();
tm1637.display(0, e);
tm1637.display(1, f);
tm1637.display(2, g);
delay(500);
if(digitalRead(0)== LOW) h++;
if(h==10) h=0;
}
}
为了制做方便TM1637选用20脚直插集成块(如不考律显示屏大小可直接用带时钟LED的TM1637模块)。我是选用四个SM411501K 1。5英吋共阳数码管与TM1637组成显示屏。https://wenku.so.com/d/3b6c447649e4c20abc73228fdaf57cc3
若选用其它尺寸的数码管一定要保正其每段显示LED数不大于两个,否则TM1637无法显示。
页:
[1]