4178 views|1 replies

6587

Posts

0

Resources
The OP
 

8266 WiFi module obtains time and weather information through the network [Copy link]

Currently, all smart clocks and smart ornaments on the market need the function of obtaining time and weather information. Many customers and friends have asked about this before, so I wrote this article to introduce how to obtain time and weather. I hope it will be helpful to everyone.

Get time
Computers use the ntp protocol to get network time. The ntp protocol is a UDP-based network timing protocol. The principle is that the client sends a request to the ntp server, and the server returns the request to the client. The client calculates the difference between its own time and the server time based on the time points of sending and returning the request, and then adjusts its own time to be consistent with the server time based on this difference.

Assuming that the current client time may be inaccurate, but the server time is accurate, the process of client calibrating time through the NTP protocol is as follows:

At time T1, the client initiates a request, and the content of the data packet is the client's current time T1

At time T2, the server receives the request and adds the server's current time T2 to the data packet.

At time T3, the server replies to the request and adds the server's current time T3 to the data packet.

At time T4, the client receives the reply and adds the client's current time T4 to the data packet.

At time T4, the client already has enough information to calculate the difference between its own time and the server time:

The average network delay between the client and the server is:

Delay = ((T4-T1)-(T3-T2)) / 2


The time difference between the client and the server is:

Offset =(((T2 -Delay)-T1)+((T3+Delay)- T4)) / 2

After sorting, we can get:

Offset =((T2 - T1)+(T3 - T4)) / 2

The above calculation is based on the assumption that the network delay for the client to send a request is equal to the network delay for the server to reply to the data. In actual applications, these two delay times are not necessarily equal, so this is why NTP time synchronization is inaccurate.

The above protocols and calculations are a bit complicated and difficult to implement. Fortunately, the ntp protocol stack has been implemented in ESP_IDF. When we use it, we only need to set the ntp server. The commonly used ntp servers in China are as follows:

cn.ntp.org.cn
edu.ntp.org.cn
us.ntp.org.cn
time.buptnet.edu.cn //Beijing University of Posts and Telecommunications
s1b.time.edu.cn //Tsinghua University
s1c.time.edu.cn //Peking University
s1d.time.edu.cn //Southeast University
From the above analysis of the ntp principle, we can see that the accuracy of time synchronization depends on the stability of network delay. Therefore, in order to obtain more accurate time, we should choose a nearby ntp server to reduce network delay interference.

Get weather information
Search "weather api" on Baidu and you will find many weather service providers, most of which are paid. I found a few free ones to share with you:

https://www.heweather.net //Partially free
https://www.tianqiapi.com
http://www.weather.com.cn/data/cityinfo/101010100.html
http://wthrcdn.etouch.cn/WeatherApi?city=北京
The first two require https to obtain weather data, and registration is required to use them. You can use the current network IP to obtain the weather information of the current city, and there is a limit on the number of visits. The latter two are accessed through http, with no limit on the number of visits. You need to specify the city name or city number to obtain the relevant city weather information.

The following takes Hefeng Weather as an example to introduce how to obtain weather information:

Access the Hefeng Weather API interface through the browser:
https://free-api.heweather.net/s6/weather/now?location=auto_ip&key=f9e338e363254fb4b7ee272e62200cae

The returned data format is as follows:
{
"HeWeather6": [{
"basic": {
"cid": "CN101280601",
"location": "深圳",
"parent_city": "深圳",
"admin_area": "广东",
"cnty": "中国",
"lat": "22.54700089",
"lon": "114.08594513",
"tz": "+8.00"
},
"update": {
"loc": "2019-10-19 14:56",
"utc": "2019-10-19 06:56"
},
"status": "ok",
"now": {
"cloud": "91",
"cond_code": "101",
"cond_txt": "Cloudy",
"fl": "30",
"hum": "43",
"pcpn": "0.0",
"pres": "1011",
"tmp": "29",
"vis": "16",
"wind_deg": "82",
"wind_dir": "东风",
"wind_sc": "1",
"wind_spd": "5"
}
}]
}

Weather information can be obtained by parsing the received data.

The Wifi device simulates the above process, first establishes an encrypted TCP connection with the server, then sends an http request, waits for the server to return data, and then parses the data through cJSON. I will not explain the knowledge related to the HTTP protocol here. If you are interested, you can refer to my other article: HTTP Protocol Analysis.

Effect demonstration
Connect to Hefeng Weather via ESP8266 to obtain the weather information of the current city. The running effect is as follows:

Shenzhen Mon Oct 21 11:31:57 2019
Sunny East Wind Level 2
Temperature: 24 Feeling: 25
Air Pressure: 1005 Relative Humidity: 69

This post is from RF/Wirelessly

Latest reply

It seems that I still need to do some basic and hardware articles.   Details Published on 2019-10-29 23:30
 

4817

Posts

4

Resources
2
 

It seems that I still need to do some basic and hardware articles.

This post is from RF/Wirelessly
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>
快速回复 返回顶部 Return list