Basic knowledge of ESP8266, how to obtain network synchronization time (Beijing time)

This article requires readers to have a little basic knowledge of ESP8266, such as ESP8266 mode settings, AP connection, networking.

1. Website that provides network time

In order to get the network time, you must first have a website that provides network time, and there are many websites that provide free network time.

Here is an example of http://api.k780.com to get network time.

2. The working mode of ESP8266

The working mode of ESP8266 is set to: STA. The transmission protocol selects TCP and is configured as a client.

The way to work is to connect the ESP8266 to a router that can connect to the Internet and connect to the Internet through this router.

Visit http://api.k780.com for web time.

3. Extract time from data obtained from http://api.k780.com

Let's look at the data provided by this website to ESP8266.

400 Bad Request

400 Bad Request


Nginx

Apr 2018 09:11:18 GMT

Content-Type: text/html

Content-Length: 166

Connection: close

From these data we can see that the red font is the data we want. And this time is the time in the central time zone.

To convert to Beijing time requires +8. Take the time above as an example for 09:11:18 and Beijing time for 17:11:18.

The extraction method is to use the strstr function to find the position of the GMT, then adjust the pointer to the tens digit of time, followed by a

The time-acquisition code is then converted into a number and converted into Beijing time.

The method for obtaining website data is: After the transparent transmission mode is enabled, "http://api.k780.com:88/?app=life.time&appkey=10003

&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json".

Code implementation on the M4 platform

Main function

Int main()
{
SDErrEnum_Typ sderr;
U32 t = 0;
U16 i = 0;
//u16 *pgbk = (u16*)g_sdcard_rcv_send_buff_5k;
U8 *ssid = (u8*)"SP3-20171119FAU-72970";//here is your router's SSID (name)
U8 *password = (u8*)"xxxxxx";//here is your router password
U8 cmdbuff[100];
U8 *p = cmdbuff;
NetWorkTime_TypeDef nwt;//Structure to save network time
FATFS fs;
FRESULT fres;
Sderr = sderr;
SystickInit(21);
LedInit();
KeyInit();
UartInit (115200);
UART3Init(115200);
t = QuitTrans();
Printf("%d",t);
EspSendCmd((u8*)"AT",(u8*)"OK",50);
EspSendCmd((u8*)"AT+CWMODE=1",(u8*)"OK",50);
EspSendCmd((u8*)"AT+RST",(u8*)"OK",50); //Reset esp8266
Delay_ms(3000);
EspSendCmd((u8*)"AT+CWMODE?",(u8*)"OK",50);
Sprintf((char*)p,"AT+CWJAP=\"%s\",\"%s\"",ssid,password);//Connect to the router
//sprintf((char*)p,"AT+CWLAP");
While(EspSendCmd(p,(u8*)"WIFI GOT IP",300))
{
;
}
While(EspSendCmd((u8*)"AT+CWJAP?",(u8*)"OK",50))
{
;
}
EspSendCmd((u8*)"AT+CIPMUX?",(u8*)"OK",50);
While(EspSendCmd((u8*)"AT+CIPSTART=?",(u8*)"OK",50))
{
;
}
GetNetWorkTime(&nwt,(u8*)"GMT",200);//Get network time
Printf("%d:%d:%d",nwt.hour,nwt.min,nwt.sec); //Print the obtained network time
LCDInit();
LCDFill(0,0,100,100,0);
While(1)
{
t++;
If(t%50 == 0)
{
LED1 = !LED1;
}
Key_delay();
}
}

Get network time function

U8 QuitTrans()
{
Delay_ms(200);
UART3Printf("+++");
Delay_ms(200);//must be delayed or it cannot exit transparent pass
Return EspSendCmd((u8*)"AT",(u8*)"OK",50);//Test whether or not the transparent pass was exited (effective if you exit the AT command)
}
U8 GetNetWorkTime(NetWorkTime_TypeDef *nwt,u8 *resp,u16 overtime_10ms)
{
U8 res = 0xFF;
U8 temp[100] = {0};
U8 *p = temp;
U8 *pend;
Sprintf((char*)p,"AT+CIPSTART=\"%s\",\"%s\",80","TCP",\
"api.k780.com");
While(EspSendCmd((u8*)p,(u8*)"OK",50)) //Connect to website
{
;
}
EspSendCmd((u8*)"AT+CIPMODE=1",(u8*)"OK",50); //Open passthrough mode
EspSendCmd((u8*)"AT+CIPSEND",(u8*)"OK",50);
UART3Printf("http://api.k780.com:88/?app=life.time&appkey=10003&sign=b59bc3ef6191eb9f747dd4e83c99f2a4&format=json");
If(resp && overtime_10ms)
{
While(overtime_10ms--)
{
Delay_ms(10);
If(g_uart3_rx_sta & 0x8000)
{
G_uart3_rx_buff[g_uart3_rx_sta & 0x7ff] = 0;
Printf("%s",g_uart3_rx_buff);
If(strstr((char*)g_uart3_rx_buff,(char*)resp)) //Time conversion
{
Pend = (u8*)strstr((char*)g_uart3_rx_buff,(char*)resp);
p = pend - 9;
Nwt->hour = ((*p - 48)*10 + (*(p+1) - 48) + 8) % 24;
Nwt->min = ((*(p+3) - 48)*10 + (*(p+4) - 48)) % 60;
Nwt->sec = ((*(p+6) - 48)*10 + (*(p+7) - 48)) % 60;
Res = 0;
G_uart3_rx_sta = 0;
Break;
}
G_uart3_rx_sta = 0;
}
}
If(overtime_10ms == 0)
{
Printf("send cmd ot"); //Test
}
}
QuitTrans();
EspSendCmd((u8*)"AT+CIPMODE=0",(u8*)"OK",50);//Exit passthrough mode back to normal mode
EspSendCmd((u8*)"AT+CIPCLOSE",(u8*)"OK",50);//Close the TCP connection
Return res;
}

Successful website connection

Obtained network time

Cement Resistor

Cement resistance: is the resistance wire wound on the alkali heat-resistant porcelain, coupled with heat resistant, resistant to wet outside fixed protection and corrosion resistance of the materials and the winding resistance into the square porcelain box body, using special incombustible cement packing seal.The outside of cement resistance is mainly made of ceramic materials (generally divided into high alumina porcelain and feldspar porcelain).

Cement Resistor,Thermal Cement Resistor,Thin Film Cement Resistor,Winding Cement Resistor,Fusing Cement Resistor

YANGZHOU POSITIONING TECH CO., LTD , https://www.yzpstcc.com