当前位置:网站首页>Esp8266----- SNTP get network time
Esp8266----- SNTP get network time
2022-07-27 01:29:00 【Paradise_ Violet】
Catalog
Preface
It introduces 8266 How to get network time
user_init
First of all, will 8266 Set to sta Pattern
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200,115200); // Initialize the serial port baud rate
os_delay_us(10000); // Wait for the serial port to stabilize
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");
// OLED Display initialization
//----------------------------------------------------------------
OLED_Init(); // OLED initialization
OLED_ShowString(0,0," "); // Internet Time
OLED_ShowString(0,2,"Clock = "); // Clock: The clock
OLED_ShowString(0,4,"Temp = "); // Temperature: temperature
OLED_ShowString(0,6,"Humid = "); // Humidity: humidity
//----------------------------------------------------------------
LED_Init_JX(); // LED initialization
ESP8266_STA_Init_JX(); // ESP8266_STA initialization
OS_Timer_IP_Init_JX(1000,1); // 1 Second repetition timing ( obtain IP Address )
}IP Timed callback function
If you succeed in obtaining ip Address , So called ESP8266_SNTP_Init_JX(); To initialize the SNTP
// IP Timed callback function
//=========================================================================================================
void ICACHE_FLASH_ATTR OS_Timer_IP_cb(void)
{
u8 C_LED_Flash = 0; // LED Scintillation meter
struct ip_info ST_ESP8266_IP; // ESP8266 Of IP Information
u8 ESP8266_IP[4]; // ESP8266 Of IP Address
// Successful access to WIFI【STA In mode , If open DHCP( Default ), be ESO8266 Of IP Address by WIFI Router automatic allocation 】
//-------------------------------------------------------------------------------------
if( wifi_station_get_connect_status() == STATION_GOT_IP ) // Determine whether to obtain IP
{
wifi_get_ip_info(STATION_IF,&ST_ESP8266_IP); // obtain STA Of IP Information
ESP8266_IP[0] = ST_ESP8266_IP.ip.addr; // IP Eight digits higher than the address == addr Lower eight
ESP8266_IP[1] = ST_ESP8266_IP.ip.addr>>8; // IP Next highest eight digits of address == addr Lower eight
ESP8266_IP[2] = ST_ESP8266_IP.ip.addr>>16; // IP Address lower eight digits == addr Second highest eight
ESP8266_IP[3] = ST_ESP8266_IP.ip.addr>>24; // IP Lower eight digits of address == addr Top eight digit
// Show ESP8266 Of IP Address
//------------------------------------------------------------------------------------------------
//os_printf("\nESP8266_IP = %d.%d.%d.%d\n",ESP8266_IP[0],ESP8266_IP[1],ESP8266_IP[2],ESP8266_IP[3]);
//OLED_ShowIP(24,2,ESP8266_IP); // OLED Show ESP8266 Of IP Address
//------------------------------------------------------------------------------------------------
// Access WIFI After success ,LED Flash 3 Time
//----------------------------------------------------
for(; C_LED_Flash<=5; C_LED_Flash++)
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),(C_LED_Flash%2));
delay_ms(100);
}
os_timer_disarm(&OS_Timer_IP); // off timer
ESP8266_SNTP_Init_JX(); // initialization SNTP
}
}initialization SNTP
8266 It supports setting up to three SNTP The server , among SNTP0 Is the primary server , The remaining two are standby servers
We can call these two API To set up SNTP The server , Parameter one is SNTP The priority of the server
sntp_setservername(0, "us.pool.ntp.org"); // The server _0【 domain name 】 The second parameter is SNTP Domain name of
sntp_setserver(2, addr); // The server _2【IP Address 】 The second parameter is SNTP Server's 32 Bit binary IP AddressSo it's using sntp_setserver(2, addr); Use before ipaddr_aton("210.72.145.44", addr); To divide dots into decimal strings ip Address conversion to 32 Binary of bit ip Address , As sntp_setserver(2, addr); Parameters of
Then call sntp_init(); SNTP initialization API
To set up OS_Timer_SNTP_Init_JX(1000,1); 1 Second repetition timing (SNTP)
// initialization SNTP
//=============================================================================
void ICACHE_FLASH_ATTR ESP8266_SNTP_Init_JX(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(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_SNTP_Init_JX(1000,1); // 1 Second repetition timing (SNTP)
}SNTP Timing callback function
call sntp_get_current_timestamp(); To query the current distance from the reference time (1970.01.01 00:00:00 GMT+8) The timestamp ( Company : second ) The base time is 1970 year 1 month 1 Number 0 when 0 branch 0 Second East 8 District
If TimeStamp This timestamp is not equal to 0, Just explain SNTP Get network time successfully .
call sntp_get_real_time(TimeStamp); Its parameter is the timestamp from the reference time , To get the current real time .
Then the serial port prints the timestamp , The actual time of serial port printing
According to the obtained time, we can calculate the time we need , date , The value of time is oled Show , also oled Display the read temperature and humidity data
// SNTP Timing callback function
//===================================================================================================
void ICACHE_FLASH_ATTR OS_Timer_SNTP_cb(void * arg)
{
// String collation Related variables
//------------------------------------------------------
u8 C_Str = 0; // String byte count
char A_Str_Data[20] = {0}; // 【" date "】 Array of strings
char *T_A_Str_Data = A_Str_Data; // Cache array pointer
char A_Str_Clock[10] = {0}; // 【" Time "】 Array of strings
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
//------------------------------------------------------
uint32 TimeStamp; // Time stamp
char * Str_RealTime; // String of actual time
// 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) // Determine whether the offset time is obtained
{
//os_timer_disarm(&OS_Timer_SNTP); // close SNTP Timer
// 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("\r\n----------------------------------------------------\r\n");
os_printf("SNTP_TimeStamp = %d\r\n",TimeStamp); // Time stamp
os_printf("\r\nSNTP_InternetTime = %s",Str_RealTime); // The actual time
os_printf("--------------------------------------------------------\r\n");
// Time string collation ,OLED Show 【" date "】、【" Time "】 character string
//…………………………………………………………………………………………………………………
// 【" year " + ' '】 Fill in the date array
//---------------------------------------------------------------------------------
Str_Head_Year = Str_RealTime; // Set the starting address
while( *Str_Head_Year ) // find 【" The actual time "】 The end character of the string '\0'
Str_Head_Year ++ ;
// 【 notes :API The actual time string returned , Finally, there is a line break , So here -5】
//-----------------------------------------------------------------
Str_Head_Year -= 5 ; // obtain 【" year "】 The first address of the string
T_A_Str_Data[4] = ' ' ;
os_memcpy(T_A_Str_Data, Str_Head_Year, 4); // 【" year " + ' '】 Fill in the date array
T_A_Str_Data += 5; // Point to 【" year " + ' '】 The address after the string
//---------------------------------------------------------------------------------
// obtain 【 date 】 The first address of the string
//---------------------------------------------------------------------------------
Str_Head_Week = Str_RealTime; // " week " The first address of the string
Str_Head_Month = os_strstr(Str_Head_Week, " ") + 1; // " month " The first address of the string
Str_Head_Day = os_strstr(Str_Head_Month, " ") + 1; // " days " The first address of the string
Str_Head_Clock = os_strstr(Str_Head_Day, " ") + 1; // " The clock " The first address of the string
// 【" month " + ' '】 Fill in the date array
//---------------------------------------------------------------------------------
C_Str = Str_Head_Day - Str_Head_Month; // 【" month " + ' '】 Bytes of
os_memcpy(T_A_Str_Data, Str_Head_Month, C_Str); // 【" month " + ' '】 Fill in the date array
T_A_Str_Data += C_Str; // Point to 【" month " + ' '】 The address after the string
// 【" days " + ' '】 Fill in the date array
//---------------------------------------------------------------------------------
C_Str = Str_Head_Clock - Str_Head_Day; // 【" days " + ' '】 Bytes of
os_memcpy(T_A_Str_Data, Str_Head_Day, C_Str); // 【" days " + ' '】 Fill in the date array
T_A_Str_Data += C_Str; // Point to 【" days " + ' '】 The address after the string
// 【" week " + ' '】 Fill in the date array
//---------------------------------------------------------------------------------
C_Str = Str_Head_Month - Str_Head_Week - 1; // 【" week "】 Bytes of
os_memcpy(T_A_Str_Data, Str_Head_Week, C_Str); // 【" week "】 Fill in the date array
T_A_Str_Data += C_Str; // Point to 【" week "】 The address after the string
// OLED Show 【" date "】、【" The clock "】 character string
//---------------------------------------------------------------------------------
*T_A_Str_Data = '\0'; // 【" date "】 Add... After the string '\0'
OLED_ShowString(0,0,A_Str_Data); // OLED Show date
os_memcpy(A_Str_Clock, Str_Head_Clock, 8); // 【" The clock "】 String into the clock array
A_Str_Clock[8] = '\0';
OLED_ShowString(64,2,A_Str_Clock); // OLED Display time
//…………………………………………………………………………………………………………………
}
// Every time 5 second , Read / Display temperature and humidity data
//-----------------------------------------------------------------------------------------
C_Read_DHT11 ++ ; // Read DHT11 timing
if(C_Read_DHT11>=5) // 5 Second timing
{
C_Read_DHT11 = 0; // timing =0
if(DHT11_Read_Data_Complete() == 0) // Read DHT11 Temperature and humidity
{
DHT11_NUM_Char(); // DHT11 The data value is converted into a string
OLED_ShowString(64,4,DHT11_Data_Char[1]); // DHT11_Data_Char[0] == 【 Temperature string 】
OLED_ShowString(64,6,DHT11_Data_Char[0]); // DHT11_Data_Char[1] == 【 Humidity string 】
}
else
{
OLED_ShowString(64,4,"----"); // Temperature: temperature
OLED_ShowString(64,6,"----"); // Humidity: humidity
}
}
//-----------------------------------------------------------------------------------------
}

You can see 8266 The timestamp from the benchmark time has been successfully obtained , And according to this timestamp, the current actual time is successfully obtained Sun Jul 24 19:14:22 2022 It is consistent with the computer system time , This means that 8266 adopt SNTP Successfully obtained the network time , stay olde I've been walking for last second , It shows that it keeps getting accurate network time .
边栏推荐
- Li Hongyi machine learning (2021 Edition)_ P7-9: training skills
- Jenkins--基础--04--安装中文插件
- [unity] unity interface scene view [1]
- markdown
- Square root of X
- Li Hongyi machine learning (2017 Edition)_ P5: error
- C language to realize mine sweeping game:
- 【unity】Unity界面scene视图[1]
- Play guest cloud brush machine 5.9
- Navicat操作数据库2(微进阶)
猜你喜欢

Software Foundation of software test interview questions

c语言实现三子棋游戏
![【unity】Unity界面scene视图[1]](/img/5a/c34ff09ef1ddba4b65c7873775c251.png)
【unity】Unity界面scene视图[1]

c语言实现动态顺序表的增删查改

4. Root user login
#问题反馈 手机 app 同步失败:数据仓库外部变更损坏 ipad app 也是一打开就下载仓库,然后闪退

Li Hongyi machine learning (2017 Edition)_ P1-2: introduction to machine learning

Unity CharacterController

Unity Twitter登录接入

Next generation Internet: Video Networking
随机推荐
if 与 else if 的区别
Unity uses navmesh to realize simple rocker function
Unity CharacterController
Software Foundation of software test interview questions
Complexity OJ question
Markdown grammar learning summary
Deep understanding of pod objects: basic management
Longest common substring
Play guest cloud with zerotier nanny level teaching to ensure learning waste
最大公约数的求法
#问题反馈 手机 app 同步失败:数据仓库外部变更损坏 ipad app 也是一打开就下载仓库,然后闪退
Jenkins--基础--03--安装后设置向导
Play guest cloud brush machine 5.9
Scoring system based on 485 bus
Promoting practice with competition - Reflection on the 303th weekly match
Navicat operation database 2 (micro advanced)
c语言实现动态顺序表的增删查改
Jenkins--基础--5.1--系统配置--插件管理
Unity Line接入
报错信息 WARNING: IPv4 forwarding is disabled. Networking will not work.