当前位置:网站首页>Experiment the Arduino code of NTP network timing alarm clock with esp32+tm1638
Experiment the Arduino code of NTP network timing alarm clock with esp32+tm1638
2022-07-25 13:50:00 【Wuyu】
Resources to address : ESP32+TM1638 Network timing alarm clock ARDUINO Code
I bought a piece before TM1638 Driven LED Digital tube display module , Plus, I have a piece on hand ESP32 Minimum system board , Find a five core wire to connect , Take advantage of the hot weather these days , Stay at home and have nothing to do , Get a tape NTP Simple alarm clock with network timing function , Just take it as nothing to practice .
One 、 hardware configuration .
Let's start with the picture above , The left picture shows the hardware connection and experimental results , In addition to the power supply ,TM1638 Three signal lines STB、CLK、DIO Separate connection ESP32 Modular GPIO14、GPIO26、GPIO32. The right figure shows the functional design scheme .

Two 、 Functional design .
1、 First , The most important thing in this experiment is the clock timing function . It is required that the timing can be normal when the network is disconnected , Free adjustment . In the case of network connection , Can pass NTP Time server for calibration .
2、 make the best of TM1638 Display the resources on the module , Provide date 、 Time 、 Week display function and time 、 Alarm clock setting function .
The top LED Indicate Sunday to Saturday respectively , The last one ( On the right )LED As an alarm clock indication ;8 Only the nixie tube can display the date and time in time ; The bottom 8 Keys are function keys .
Press “ Pattern ” key ,8 Only the digital tube displays the date or time respectively ; Press “ Set up ” key ( And “ Pattern ” Bond fit ), You can adjust the time separately “ when ”、“ branch ”、“ second ” Or adjust the date “ year ”、“ month ”、“ Japan ”( The adjustment item will flash );“ Minus the key ” and “ Add the key ” Add or subtract the adjusted items respectively , And support long press quick adjustment mode .“ Calm down ” Press the key to set the current time / Cancel the alarm clock , and “ Qingnao ” Press the key to clear all alarm settings . Press “ Distribution network ” key ,ESP32 On board LED Will flash , At this time, you need to open wechat official account “ Anxinko Technology ” Distribution network ,WIFI Successful connection ,ESP32 On board LED Will always be bright ( Failure will extinguish ). Press “ Sync ” key , Will automatically connect NTP Server time calibration .
3、 About the alarm clock , It is a subsidiary function of this experiment , Because it's only for experimental purposes , The function is relatively simple . The program uses a string to save the alarm data ( There is no limit on the number of fixed alarms ), But there is no power-off saving function . The alarm data is only saved “ Hours ” and “ Minutes ” data , Do not save “ Second ” data , Therefore, the output time of each alarm clock is one minute , And the output is simply through a LED Display , There is no further music or electrical control design , To avoid system complexity .
3、 ... and 、 Software environment .
This experiment uses ARDUINO Integrated development environment , Related software and ESP32 Please compile the environment by yourself . Here is a practical resource :ARDUINO compile ESP32 Development environment files ( Note that only the development environment , barring ARDUINO Software , It mainly solves the problem of being unable to download ).

Here are all the code resources of this experiment :ESP32+TM1638 Network timing alarm clock ARDUINO Code .
Four 、 Program description .
This program uses ARDUINO Software programming , Modular design , There are four files :TM1638.C For display driver module , Provide LED Lamp beads 、 Nixie tube 、 Key processing function .TIMERS.C It is a clock processing module , For setting and reading ESP32 Inside RTC Clock source , And time zone settings ,NTP Timing function .ESP32OBJ.C Then provide distribution network and WIFI Connection function . The main program NTP_TIME.INO Integrate all modules , To initialize , And be responsible for realizing all design functions .
The main program is posted below NTP_TIME.INO Code :
#include <Arduino.h>
#include <WiFi.h>
#include "ESP32OBJ.C"
#include "TM1638.C"
#include "TIMERS.C"
ESP32OBJ esp32;
TM1638 tm1638;
USER_TIMERS timers;
uint8_t days[12]={31,28,31,30,31,30,31,31,30,31,30,31};
String dingShi="";// Form like :"0000,1200,1530,2300";/* Four numeric characters , The first two expressions are , The last two represent points , Time intervals are separated by commas , There is no timing by default */
struct tm info;/* Time structure */
uint32_t keyUpTime=0;/* Key release time */
uint32_t keyDownTime=0;/* Key press time */
uint8_t dispMode=0;/* display mode (0-- Display time ;1-- Show date ;2-- Show week )*/
uint8_t setMode=0;/* Adjustment mode (0-- The normal state ;1-- Low adjustment state ;2-- Median adjustment state ;3-- High adjustment state )*/
void setup ()
{
Serial.begin(115200);
esp32.begin();
timers.begin();
tm1638.begin(14,26,32);/* Display module pin settings */
}
void loop(){
uint8_t key;uint8_t nowKey;
if(millis()-keyUpTime>10000){dispMode=0;setMode=0;}/*10 Seconds without operation, it will automatically turn into the time display state , And cancel the adjustment status .*/
key=tm1638.key();
if(key!=8){keyDownTime=millis();}/* If you press the key, record the system time when you press the key */
if(dispMode!=0||setMode!=1){info=timers.getTime();}/* Get time information ( Do not refresh when checking seconds )*/
keySet(key);
ScreenDisplay(false);
while(key!=8){
keyUpTime=millis();
nowKey=tm1638.key();
if(nowKey==8&&(key==1||setMode>0&&key>1&&key<4)){timers.setTime(info);break;}else{key=nowKey;}/* Update time when the key related to adjustment is released , write in RTC*/
if(keyUpTime-keyDownTime>1500){delay(100);if(key==2||key==3){keySet(key);}}/* Key 1.5 Seconds not released , Then press the long key ( Only support " Add the key " and " Minus the key ") Continuous processing )*/
if(setMode==0){info=timers.getTime();}/* In the non calibration state, the time will be refreshed even if the key is not released */
ScreenDisplay(true);/* If the key is not released, it will not flash */
;
}/* The key is blocked */
}
void keySet(uint8_t key){
/* Key handling functions */
switch(key){
case 0:/* mode key */
dispMode=(dispMode+1)%2;
setMode=0;
break;
case 1:/* Adjustment key */
if(dispMode<2){setMode=(setMode+1)%4;}
break;
case 2:/* Minus key */
switch(setMode){
case 1:/* Adjust the low position */
switch(dispMode){
case 0:/* One second minus one */
if(info.tm_sec>0){info.tm_sec--;}else{info.tm_sec=59;}break;
case 1:/* One less per day */
if(info.tm_mday>1){info.tm_mday--;}else{info.tm_mday=days[info.tm_mon-1];if(info.tm_mon==2&&info.tm_year%4==0){info.tm_mday++;}}break;
}break;
case 2:/* Adjust the median */
switch(dispMode){
case 0:/* Points minus one */
if(info.tm_min>0){info.tm_min--;}else{info.tm_min=59;}break;
case 1:/* Subtract one month */
if(info.tm_mon>1){info.tm_mon--;}else{info.tm_mon=12;}if(info.tm_mday>days[info.tm_mon-1]){info.tm_mday=days[info.tm_mon-1];if(info.tm_mon==2&&info.tm_year%4==0){info.tm_mday++;}}break;
}break;
case 3:/* Adjust the high position */
switch(dispMode){
case 0:/* Time minus one */
if(info.tm_hour>0){info.tm_hour--;}else{info.tm_hour=23;}break;
case 1:/* One year less */
if(info.tm_year>1970){info.tm_year--;if(info.tm_year%4!=0&&info.tm_mon==2&&info.tm_mday>days[info.tm_mon-1]){info.tm_mday=days[info.tm_mon-1];}}break;
}break;
}
break;
case 3:/* Plus key */
switch(setMode){
case 1:/* Adjust the low position */
switch(dispMode){
case 0:/* Second plus 1*/
info.tm_sec=(info.tm_sec+1)%60;break;
case 1:/* Riga 1*/
info.tm_mday++;if(info.tm_mday>days[info.tm_mon-1]&&(info.tm_mon!=2||info.tm_year%4!=0||info.tm_mday>29)){info.tm_mday=1;}break;
}break;
case 2:/* Adjust the median */
switch(dispMode){
case 0:/* Points plus one */
info.tm_min=(info.tm_min+1)%60;break;
case 1:/* Month plus one */
info.tm_mon=info.tm_mon%12+1;if(info.tm_mday>days[info.tm_mon-1]){info.tm_mday=days[info.tm_mon-1];if(info.tm_mon==2&&info.tm_year%4==0){info.tm_mday++;}}break;
}break;
case 3:/* Adjust the high position */
switch(dispMode){
case 0:/* Time plus one */
info.tm_hour=(info.tm_hour+1)%24;break;
case 1:/* Add one year */
if(info.tm_year<2037){info.tm_year++;}if(info.tm_mon==2&&info.tm_year%4!=0&&info.tm_mday>days[info.tm_mon-1]){info.tm_mday=days[info.tm_mon-1];}break;
}break;
}
break;
case 4:/* Ring settings */alarmClock(info.tm_hour,info.tm_min,1);break;
case 5:/* Ring clear */dingShi="";break;
case 6:/* Distribution key */esp32.smartConfig();break;
case 7:/* Timing key */timers.ntpUpdate();dispMode=0;setMode=0;break;
default:/* Default operation */break;
}
}
void ScreenDisplay(boolean mode){
/* Display handler */
uint16_t tem;uint8_t i;uint16_t temtime;
temtime=millis()%1000;
switch(dispMode){
case 1:/* Show date */
tem=info.tm_year;
for(i=0;i<4;i++){
if(setMode!=3||temtime<500||mode){tm1638.display(3-i,tem%10);tem=tem/10;}else{tm1638.display(3-i,-1);tem=tem/10;}
}
tem=info.tm_mon;
for(i=0;i<2;i++){
if(setMode!=2||temtime<500||mode){tm1638.display(5-i,tem%10);tem=tem/10;}else{tm1638.display(5-i,-1);tem=tem/10;}
}
tem=info.tm_mday;
for(i=0;i<2;i++){
if(setMode!=1||temtime<500||mode){ tm1638.display(7-i,tem%10);tem=tem/10;}else{tm1638.display(7-i,-1);tem=tem/10;}
}
break;
default:/* Display time */
tm1638.display(2,16);tm1638.display(5,16);/* Show horizontal lines .*/
tem=info.tm_hour;
if(setMode!=3||temtime<500||mode){tm1638.display(0,tem/10);tm1638.display(1,tem%10);}else{tm1638.display(0,-1);tm1638.display(1,-1);}
tem=info.tm_min;
if(setMode!=2||temtime<500||mode){tm1638.display(3,tem/10);tm1638.display(4,tem%10);}else{tm1638.display(3,-1);tm1638.display(4,-1);}
tem=info.tm_sec;
if(setMode!=1||temtime<500||mode){tm1638.display(6,tem/10);tm1638.display(7,tem%10);}else{tm1638.display(6,-1);tm1638.display(7,-1);}
break;
}
/* Use independent LED Prompt the day of the week , first LED Means Sunday */
for(i=0;i<7;i++){
if(info.tm_wday==i){tm1638.led(i,1);}else{tm1638.led(i,0);}
}
alarmClock(info.tm_hour,info.tm_min,0);
}
void alarmClock(uint16_t in_hour,uint16_t in_min,uint8_t in_mode){
// Judge whether to turn on the alarm according to the hours and minutes ( Ring the bell for one minute each time ).
// Due to the use of strings to save timing data , The actual use scenario basically does not need to consider the quantity limitation of timing control ( have access to 24*60=1440 It's a time , The string can be up to 1440*5=7200 Bytes .).
// Because flash memory is not used to save , Therefore, the timing data will be lost after power failure or restart .
String temStr="";int i;
temStr=temStr+char(in_hour/10+48)+char(in_hour%10+48)+char(in_min/10+48)+char(in_min%10+48);/* Four characters represent a fixed time */
switch(in_mode){
case 0:/* Ring to judge */
if(dingShi.indexOf(temStr)==-1){tm1638.led(7,0);}else{tm1638.led(7,1);}/* Here you can change the timing control mode , The example takes the last LED Light off as a sign */
break;
case 1:/* Ring settings */
i=dingShi.indexOf(temStr);
if(i!=-1){
/* If this timer already exists, delete */
if(i==0){
if(dingShi==temStr){
dingShi="";
}else{
dingShi=dingShi.substring(5);
}
}else{
if(i==dingShi.length()-4){
dingShi=dingShi.substring(0,dingShi.length()-5);
}else{
dingShi=dingShi.substring(0,i-1)+dingShi.substring(i+4);
}
}
}else{
/* If there is no such timing, add */
if(dingShi==""){dingShi=temStr;}else{dingShi=dingShi+","+temStr;}
}
break;
}
}The main positions are annotated in Chinese , Therefore, no further explanation will be given to the procedure , Please download the resource experiment . Here I would like to remind you that ,TIMERS.C A correction has been added to the date data in , Year number +1900 Automatically adjust to the usual form , And the month is also added 1, The time zone correction uses configTime function , The first parameter 8*3600 Indicates that it is revised to Beijing time ( East eight ). The whole program only depends on ESP32 Base library , Third party library references are not involved . All modules are ARDUINO standard C Language program , It can be modified at will , Add and delete functions . Everything is under your control .
边栏推荐
- Serious [main] org.apache.catalina.util.lifecyclebase Handlesubclassexception initialization component
- 6.27 uniapp project history
- What is your revenue rank among global developers in 2022?
- [configure hifive1 revb] the device manager does not recognize the port, and can not connect to j-link via USB
- Canvas判断内容为空
- Gym installation, invocation and registration
- Day1:三种语言暴刷牛客130题
- G027-op-ins-rhel-04 RedHat openstack creates a customized qcow2 format image
- Azure Devops (XIV) use azure's private nuget warehouse
- Nodejs link MySQL error: Er_ NOT_ SUPPORTED_ AUTH_ MODEError: ER_ NOT_ SUPPORTED_ AUTH_ MODE
猜你喜欢

Uncaught SyntaxError: Octal literals are not allowed in strict mode.

Applet sharing function

redis集群的三种方式

What are the ranking strategies for mobile websites, independent apps and websites?

ES6数组去重 new Set()

Package management apt, dpkg
![[server data recovery] HP EVA server storage raid information power loss data recovery](/img/ee/8f36ef1b5842f1778c0dd695401b40.jpg)
[server data recovery] HP EVA server storage raid information power loss data recovery

Brush questions - luogu-p1089 Jinjin savings plan

【力扣】645.错误的集合

Internal error of LabVIEW
随机推荐
Okaleido launched the fusion mining mode, which is the only way for Oka to verify the current output
redis集群的三种方式
How to refactor embedded code?
在线问题反馈模块实战(十三):实现多参数分页查询列表
Hcip day 8 notes
Brush questions - Luogu -p1047 trees outside the school gate
刷题-洛谷-P1075 质因数分解
@wrap 装饰器
Brush questions - Luogu -p1035 series summation
AQS of concurrent programming
刷题-洛谷-P1161 开灯
Leetcode202 --- Happy number
I2C can also be powered on by bus!
命名空间与库
NoSQL, relational database, row and column database comparison and analogy
Hcip day 10 notes
Business visualization - make your flowchart'run'(3. Branch selection & cross language distributed operation node)
Azure Devops (XIV) use azure's private nuget warehouse
hcip第十天笔记
DNS resolution error during windows unbutu20 lts apt, WGet installation