当前位置:网站首页>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 :
边栏推荐
- Flask (VII) - static file
- 【Objective-C】动态创建控件
- Output image is bigger (1228800b) than maximum frame size specified in properties (1048576b)
- FPGA基础架构【参考ug998】
- Redis transaction details
- oracle 11g rac 磁盘组有空间无法增加数据文件?
- Before applying data warehouse ODBC, you need to understand these problems first
- Comparison and introduction of OpenCV oak cameras
- Day41 process pool and thread pool
- 【音视频】SEI简介
猜你喜欢

Oracle XDB组件的重建

document对象

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

jedisLock—redis分布式锁实现

关于原型及原型链

Version mismatch between installed deeply lib and the required one by the script

Day39 process object and other method mutexes

全局池化–Pytorch

PD chip ga670-10 for OTG while charging

Technical practice of dolphin dispatching in kubernetes system
随机推荐
ORA-00059 超过db_files限制
Opencv image basic operation (III) -- image feature extraction (corner detection)
Day41 process pool and thread pool
Mysql比较
The difference and relation between machine learning and statistics
[TiO websocket] III. The TiO websocket server can send messages to users anywhere
Leetcode brushing questions - hand tearing binary tree
Set MySQL as externally connectable
考研數學 【數列極限證明題】題型方法總結
js基础--Date对象
Error [error] input tesnor exceeded available data range [neuralnetwork (3)] [error] input tensor '0' (0)
Analysis of high frequency interview questions in massive data processing
Day44 database
ESP8266_SmartConfig
Day39 content summary
OpenSSL usage
Tissu. JS définit dynamiquement la taille de la police
ESP8266_GET请求天气预报、json解析
Identifier keyword literal data type base conversion character encoding variable data type explanation operator
[TiO websocket] v. TiO websocket server counts the number of online people