当前位置:网站首页>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 :
边栏推荐
- ESP8266_通过MQTT协议连接阿里云
- Analysis of Kube scheduler disk scheduling source code
- 【ROS】noedic-moveit安装与UR5模型导入
- Rebuilding Oracle XdB components
- An error can't locate env pm in @INC
- Opencv oak-d-w wide angle camera test
- 全局池化–Pytorch
- Opencv image basic operation (III) -- image feature extraction (corner detection)
- Before applying data warehouse ODBC, you need to understand these problems first
- CVPR 2021: learning continuous image representation with local implicit image function
猜你喜欢

Résumé de la méthode d'examen des mathématiques

CVPR 2021: learning continuous image representation with local implicit image function

Use of MSF evaluation module

Flask (VIII) - form processing

How do we connect to WiFi?

Identifier keyword literal data type base conversion character encoding variable data type explanation operator

Day41 process pool and thread pool

Tenthousand words thoroughly learn heap and binary tree

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

Opencv image basic operation (IV) -- image feature extraction (corner detection)
随机推荐
远程工作时代的物联网安全
Flask (VII) - static file
Oracle XDB組件的重建
Fabric. JS dynamically set font size
Oracle 11g RAC disk group has space and cannot add data files?
Analysis of high frequency interview questions in massive data processing
The difference and relation between machine learning and statistics
Day39 process object and other method mutexes
考研数学 【数列极限证明题】题型方法总结
Flask (III) -- variable rules
Suffix Array
Exclusive interview - dialogue on open source Zhai Jia: excellent open source projects should be seen by more people. I am honored to participate in them
Inductive bias的一些理解
Opencv CEO teaches you to use oak (V): anti deception face recognition system based on oak-d and depthai
Day41 process pool and thread pool
DOS command virtual environment
Leetcode brushing questions - hand tearing binary tree
1400. construct K palindrome strings
P4147 "jade toad Palace"
Document object