当前位置:网站首页>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 :
边栏推荐
- Day39 content summary
- 一万字彻底学会堆和二叉树
- Video review pulsar summit Asia 2021, cases, operation and maintenance, ecological dry goods
- An error will be reported when the RAC modifies the scanip to different network segments
- Document object
- keyboard entry.
- ESP8266_ Connect to Alibaba cloud through mqtt protocol
- OpenSSL usage
- How do online app stores of laundry chain stores do?
- 不同CV任务的标注类型
猜你喜欢

整型提升例题

How do online app stores of laundry chain stores do?

卸载grid时运行脚本报错Can‘t locate Env.pm in @INC

Oracle DG physical standby database uses alias data file to change path to OMF path

New feature in ES6 -- arrow function

Bowen dry goods | Apache inlong uses Apache pulsar to create data warehousing

Rebuilding Oracle XdB components

Opencv image basic operation (IV) -- image feature extraction (corner detection)

Don't use redis list to implement message queue. Stream is designed for queues

jedisLock—redis分布式锁实现
随机推荐
JS foundation -- Date object
Tissu. JS définit dynamiquement la taille de la police
ESP8266_ Access to the core suite of Baidu Internet of things and use mqtt protocol for communication
How do we connect to WiFi?
JS foundation - array object
Use of MSF evaluation module
ESP8266_SmartConfig
CVPR 2021: learning continuous image representation with local implicit image function
Telecommuting best practices and Strategies
JS foundation -- Operator
Sed explanation of shell script (SED command, sed -e, sed s/ new / old /...)
流式计算知识
考研數學 【數列極限證明題】題型方法總結
Sword finger offer II 041 Average value of sliding window
ORACLE RAC中连接ScanIP报错ORA-12545的问题解决
P1169 "chessboard making"
Events in JS
Mysql比较
Understanding of the keyword this in JS
Suffix Array