The Tokyo Olympics can be said to be the most special one in the history of the Olympics, postponed for one year, without spectators, environmentally friendly Olympics , etc. 16 days have passed since the opening on July 23. As of 17:00 on the afternoon of August 7, China ranked first with 38 golds, 29 silvers and 17 bronzes , followed by the United States and Japan.
Tomorrow (August 8) will be the last day of this Olympic Games. I hope China can maintain its first position. Cheer for the Olympic athletes!
In order to keep an eye on the medal table in real time and to publish an article, I used STM32+ESP8266 to make a small desktop ornament of the medal table of the Tokyo Olympics at home on the weekend . The final effect is as follows:
Maybe many friends started to follow me from the two projects of STM32 epidemic monitoring and STM32 vaccine monitoring. In fact, the principle of obtaining the medal list is the same as these two. Both use STM32 to drive ESP8266 to connect to the network, and then GET the interface to read the JSON data returned by the interface, and then perform JSON parsing and LCD display. Using different interfaces means different functions.
The difference from the previous one is that the data returned by the above two API interfaces are very short, only a few hundred bytes, while the data returned by the medal table interface is as much as 20KB. STM32 cannot process such a large amount of data at one time. This article adopts a simple method to intercept the data to reduce the amount of data.
What are the contents?
API interface acquisition
JSON data preprocessing
JSON data parsing
Display Effect
Open source address
API interface acquisition
Before development, we must first find an API interface. The request method is preferably GET, and the returned data format is JSON. Search the Internet first and find that many netizens have implemented the function of obtaining medal table data, mostly using: Python, Java, PHP. The language used does not matter. What matters is whether the API interface is what we want. Finally, we found two API interfaces.
The first interface is the official Tokyo Olympics special page of CCTV.com :
The URL is as follows:
http://2020.cctv.com/medal_list/index.shtml
F12 opens the developer mode and you can find the API address requested by the page:
The data returned by this interface is 11KB, and after formatting, it is 24KB. The JSON data format (partial):
You can see that the data is quite complete, including ranking, total number of gold, silver, bronze and total medals, country ID, and country name is encoded in UTF-16BE, while the font library on the development board is encoded in GBK, which is difficult to handle. See if there are other interfaces.
The second interface is the Olympic medal table displayed on the negative first screen of my Xiaomi mobile phone, which is similar to this:
You can jump to the browser to open:
http://act.e.mi.com/olympic/index.html
Press F12 to open the developer mode, and you can find the requested API address:
The data returned by this interface is 21KB, and 33KB after formatting. The JSON data format (partial):
This interface returns rich data, which is also larger than that of CCTV.com. In addition to information such as medal ranking, it also includes the address of each country's flag image, update time, etc. The country name is encoded in UTF-8, and it needs to be converted from UTF8 to GBK to be used on our development board.
In summary, we have obtained information about two API interfaces:
In order to display the country name directly on the development board, we use the API interface of Xiaomi mobile phone.
JSON data preprocessing
By analyzing the data returned by the API interface, it includes the medal data of the top 90, and the data length is 21KB:
But we only need the data of the first 10. The serial port buffer length is set to 2500 bytes, that is, the returned 21KB byte data only receives the first 2500 bytes, and then processes it into the JSON standard format.
The processing method is to find the position of the last }number in reverse order, {discard the subsequent data, and then add it ]}, so that the intercepted 2500 bytes of data are modified into standard JSON format data:
JSON data parsing
As can be seen from the above figure, the JSON format is relatively simple and can be easily parsed using cJSON. For usage methods, see the following article:
Proficient in various MCUs, DSPs, FPGAs to realize running lights, program erasing and downloading, IDE environment installation and uninstallation, non-famous company engineer, bug maker. Share articles related to open source projects, board evaluation, and study notes. It may not be updated frequently, but each article is carefully written.
115 original content
Public Account
Summarize
On the evening of August 8, 2021, Beijing time, the 17-day 2020 Tokyo Olympics will come to an end, and the closing ceremony will be held as scheduled at 19:00. The concept of the closing ceremony of this Olympic Games is " Worlds we share ", which means "remembering the emotions shared with people of different personalities and cultural backgrounds, and working together to create the future", and also contains the meaning of human solidarity in fighting the new crown epidemic. Although the Tokyo Olympics will say goodbye to the audience, the upcoming 2022 Beijing Winter Olympics will continue to ignite the enthusiasm of sports fans.