当前位置:网站首页>ESP8266_ SNTP(Simple Network Time Protocol)
ESP8266_ SNTP(Simple Network Time Protocol)
2022-06-11 09:44:00 【Little tiger_ IOT】
1、SNTP Simple network time protocol
SNTP The agreement adopts client / The server How it works , With UDP Means of communication ,SNTP The server receives GPS Signal or built-in atomic clock As the time benchmark of the system .
There are many in the network SNTP The server , The client will select several SNTP Server usage . If one SNTP The server lost the external time source during operation , here SNTP The server will tell SNTP client “ I lost outside time ”. When SNTP When the client receives this message , Will choose other SNTP The server .
- In unicast mode ,SNTP Clients regularly access SNTP The server obtains accurate time information , Used to adjust the time of the client's own system , To synchronize the time .
- In broadcast mode ,SNTP The server periodically sends messages to the specified IP Broadcast address perhaps IP Multicast address .SNTP The client obtains time information by listening to these addresses .
2、 Reference resources SDK Programming manual SNTP routine
- Can make SNTP
- Set up SNTP The server , Up to 3 individual SNTP The server , You can use the domain name or IP Address settings ;
- initialization SNTP,sntp_init();
- Set the timer , Check SNTP Time stamp , routine 100ms Stop the timer if there is a correct time , I use it 1s loop check;
- Timer callback
- If the timestamp is not 0, Get the current time ;

3、 Code 、 result
The default is time format :“ week month Japan Time year ”, such as “Mon Jun 25 23:13:15 2018”, Not in line with habits , Reformat to “ year month Japan week Time ”, Without this requirement, you can print directly like the above routine ;
//SNTP---------------BEGIN------------------------
// ----------------------------------------------- -----------------------------------------------
// | month | English abbreviation | English full name | | week X | English abbreviation | English full name |
// ----------------------------------------------- -----------------------------------------------
// | January | Jan | January | | Monday | Mon | Monday |
// ----------------------------------------------- -----------------------------------------------
// | February | Feb | February | | Tuesday | Tue | Tuesday |
// ----------------------------------------------- -----------------------------------------------
// | March | Mar | March | | Wednesday | Wed | Wednesday |
// ----------------------------------------------- -----------------------------------------------
// | April | Apr | April | | Thursday | Thu | Thursday |
// ----------------------------------------------- -----------------------------------------------
// | May | May | May | | Friday | Fri | Friday |
// ----------------------------------------------- -----------------------------------------------
// | June | June | June | | Saturday | Sat | Saturday |
// ----------------------------------------------- -----------------------------------------------
// | July | July | July | | Sunday | Sun | Sunday |
// ----------------------------------------------- -----------------------------------------------
// | August | Aug | Aguest |
// -----------------------------------------------
// | September | Sept | September |
// -----------------------------------------------
// | October | Oct | October |
// -----------------------------------------------
// | November | Nov | November |
// -----------------------------------------------
// | December | Dec | December |
// -----------------------------------------------
os_timer_t timer_SNTP_check; // testing SNTP Timer for
void ICACHE_FLASH_ATTR timer_SNTP_check_callback(void* arg)
{
uint32 TimeStamp; // Time stamp
char * Str_RealTime; // String of actual time
char * Str_Head_Week; // 【" week "】 String first address
char * Str_Head_Month; // 【" month "】 String first address
char * Str_Head_Day; // 【" days "】 String first address
char * Str_Head_Clock; // 【" The clock "】 String first address
char * Str_Head_Year; // 【" year "】 String first address
char A_Str_Data[30] = {0}; // Adjust the string after the date
char *T_A_Str_Data = A_Str_Data; // Cache array pointer
// Query the current distance from the reference time (1970.01.01 00:00:00 GMT+8) The timestamp ( Company : second )
TimeStamp = sntp_get_current_timestamp();
if(TimeStamp == 0){ // Determine whether the offset time is obtained
// os_timer_arm(&timer_SNTP_check,100,0);
}else{
// os_timer_disarm(&timer_SNTP_check);
// Query actual time (GMT+8): East eight ( Beijing time. )
Str_RealTime = sntp_get_real_time(TimeStamp);
// 【 The actual time 】 character string == " Zhou month Japan when : branch : second year "
os_printf("SNTP_TimeStamp = %d\n",TimeStamp); // Time stamp
os_printf("SNTP_InternetTime = %s\n",Str_RealTime); // The actual time
// Only the number of abbreviated characters of the month is uncertain
Str_Head_Week = Str_RealTime; // " week " The first address of the string
Str_Head_Month = strstr(Str_Head_Week, " ") + 1; // " month " The first address of the string
Str_Head_Day = strstr(Str_Head_Month, " ") + 1; // " days " The first address of the string
Str_Head_Clock = strstr(Str_Head_Day, " ") + 1; // " The clock " The first address of the string
Str_Head_Year = Str_Head_Clock + 9; // year
os_memcpy(T_A_Str_Data, Str_Head_Year, 4);
A_Str_Data[4] = ' ';
T_A_Str_Data += 5;
if(Str_Head_Day - Str_Head_Month == 4){ // Month is 3 byte
os_memcpy(T_A_Str_Data, Str_Head_Month, 4); // Much of the 1 Bytes are their own spaces
T_A_Str_Data += 4;
}else{
os_memcpy(T_A_Str_Data, Str_Head_Month, 5); // Much of the 1 Bytes are their own spaces
T_A_Str_Data += 5;
}
if(Str_Head_Clock - Str_Head_Day == 2){ //day yes 1 byte
os_memcpy(T_A_Str_Data, Str_Head_Day, 2); // Much of the 1 Bytes are their own spaces
T_A_Str_Data += 2;
}else{ //day yes 2 byte
os_memcpy(T_A_Str_Data, Str_Head_Day, 3); // Much of the 1 Bytes are their own spaces
T_A_Str_Data += 3;
}
os_memcpy(T_A_Str_Data, Str_Head_Week, 4); //
T_A_Str_Data += 4;
os_memcpy(T_A_Str_Data, Str_Head_Clock, 8); //00:00:00
T_A_Str_Data += 8;
*T_A_Str_Data = '\0'; // 【" date "】 Add... After the string '\0'
os_printf("data = %s\n",A_Str_Data);
oled_show_string(0,2,A_Str_Data,FONT_8x16);
}
}
// initialization SNTP
void ICACHE_FLASH_ATTR SNTP_initial(void)
{
ip_addr_t * addr = (ip_addr_t *)os_zalloc(sizeof(ip_addr_t));
// sntp_setservername(0, "us.pool.ntp.org"); // The server _0【 domain name 】
sntp_setservername(0, "0.cn.pool.ntp.org"); // The server _0【 domain name 】
sntp_setservername(1, "ntp.sjtu.edu.cn"); // The server _1【 domain name 】
ipaddr_aton("210.72.145.44", addr); // dotted decimal => 32 Bit binary
sntp_setserver(2, addr); // The server _2【IP Address 】
os_free(addr); // Release addr
sntp_init(); // SNTP initialization API
os_timer_disarm(&timer_SNTP_check);
os_timer_setfn(&timer_SNTP_check,timer_SNTP_check_callback,NULL);
os_timer_arm(&timer_SNTP_check,1000,1); //1000ms
}
//SNTP-------------------END--------------------------------
Reference link :
边栏推荐
- [TiO websocket] III. The TiO websocket server can send messages to users anywhere
- Document object
- Slice of go language foundation
- Opencv image basic operation (IV) -- image feature extraction (corner detection)
- The first TOF related data set available for deep learning: deep learning for confidence information in stereo and TOF data fusion (iccv 2017)
- Where is it safer to open an account for soda ash futures? How much can soda ash futures do now?
- js基础--关于DOM
- ESP8266_通过MQTT协议连接阿里云
- Flask (II) - route
- ESP8266_MQTT协议
猜你喜欢

Troubleshooting the error ora-12545 reported by scanip in Oracle RAC

Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)

ESP8266_ SmartConfig

How to determine whether two time periods overlap?
![[ROS] noedic moveit installation and UR5 model import](/img/bc/865c752021a4ee68e963c09f5f632d.png)
[ROS] noedic moveit installation and UR5 model import

A summary of the problem type and method for proving the limit of sequence in postgraduate entrance examination

Understanding of the keyword this in JS

Package details

FPGA基础架构【参考ug998】

Opencv oak-d-w wide angle camera test
随机推荐
js基础--Date对象
Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods
Flask (VII) - static file
面试常问:rem布局,flex布局等
全局池化–Pytorch
关于CI框架批量导出至压缩文件
ORACLE DG物理备库使用别名数据文件改变路径到OMF路径
Flask (I) - quick start
Mysql比较
2022 must have Chrome extension - browser plug-in to double your Internet efficiency
[image denoising] image denoising based on mean + median + Gauss low pass + various improved wavelet transform, including Matlab source code
MySQL:Got a packet bigger than ‘max_ allowed_ packet‘ bytes
Flask (VI) - template
js基础--Array对象
Revisiting Self-Training for Few-Shot Learning of Language Model,EMNLP2021
Rebuilding Oracle XdB components
Opencv CEO teaches you to use oak (V): anti deception face recognition system based on oak-d and depthai
Bucket sort
Flask (III) -- variable rules
ESP8266_ SmartConfig