1982 views|4 replies

1w

Posts

204

Resources
The OP
 

Cheer for the Olympic athletes - Get the Tokyo Olympic medal table using STM32 [Copy link]

Author: wcc149

Source: https://mp.weixin.qq.com/s/gtuy3ad_iutFqlhmIeNjuQ

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:

Desktop effect 1
Desktop Effect 2

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 :

Image: 2020.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:

cntv_api

The data returned by this interface is 11KB, and after formatting, it is 24KB. The JSON data format (partial):

cntv_json

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:

Mobile phone interface 2

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:

mi_api

The data returned by this interface is 21KB, and 33KB after formatting. The JSON data format (partial):

mi_json

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:

CCTV API interface for Olympic medal table:

TYPE:"TCP"
PORT:"80"
IP:"111.206.176.78"
API:"http://api.cntv.cn/olympic/getOlyMedals?serviceId=pcocean&itemcode=GEN-------------------------------"

Olympic Medal Table Xiaomi Mobile Phone API Interface

TYPE:"TCP"
PORT:"80"
IP:"111.206.101.253"
API:"http://act.e.mi.com/olympic/medal_rank"

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:

mi_json

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:

mi_json_deal

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:

Define a structure:

typedefstructmedal{
charrank[5];
charcountryname[50];
charcount[5];
chargold[5];
charsilver[5];
charbronze[5];
charupdate_time[50];
charcountryid[10];
}medalObj;

Parsing function, only read the medal data of the top 7:

uint8_tparse_mi_data(void)
{
cJSON*root,*data_obj,*list_obj;
char*str;
chardest[USART2_MAX_RECV_LEN];
char*loc;
chargbk[50];
charutf8[50];

medalObj*pobj;
medalObjobj;
intidx;

pobj=&obj;

str=(char*)USART2_RX_BUF;
memset(dest,'\0',USART2_MAX_RECV_LEN);
loc=strrchr(str,'}');
strncpy(dest,str,loc-str+1);
strcat(dest,"]}");
printf("jsondatasize:%dbytes\r\n",strlen(dest));

root=cJSON_Parse((constchar*)dest);

if(root!=0)
{
printf("JSONformatok,startparse!!!\r\n");
data_obj=cJSON_GetObjectItem(root,"data");
if(data_obj->type==cJSON_Array)
{
intsize=cJSON_GetArraySize(data_obj);
for(idx=0;idx<size;idx++)
{
if(size>=7&&idx<=7)
{
list_obj=cJSON_GetArrayItem(data_obj,idx);

strcpy(obj.bronze,cJSON_GetObjectItem(list_obj,"medal_bronze_count")->valuestring);
strcpy(obj.rank,cJSON_GetObjectItem(list_obj,"rank")->valuestring);
strcpy(obj.count,cJSON_GetObjectItem(list_obj,"medal_sum_count")->valuestring);
strcpy(obj.silver,cJSON_GetObjectItem(list_obj,"medal_silver_count")->valuestring);
//utf8->gbk
memset(utf8,'\0',sizeof(utf8));
memset(gbk,'\0',sizeof(gbk));
strcpy(utf8,cJSON_GetObjectItem(list_obj,"country_name")->valuestring);
SwitchToGbk(utf8,gbk);

strcpy(obj.countryname,gbk);

strcpy(obj.gold,cJSON_GetObjectItem(list_obj,"medal_gold_count")->valuestring);
strcpy(obj.update_time,cJSON_GetObjectItem(list_obj,"update_time")->valuestring);
printf("%s:%s%10s%s:%s-%s-%s\r\n",
pobj->rank,pobj->update_time,pobj->countryname,
pobj->count,pobj->gold,pobj->silver,pobj->bronze);
gui_show_data(48+idx*20,pobj);
}
}
Show_Str_Mid(200,225,(u8*)pobj->update_time,12,120);
}
}
else
{
printf("JSONformaterror:%s\r\n",cJSON_GetErrorPtr());//输出json格式错误信息
}

USART2_RX_STA=0;
memset(USART2_RX_BUF,0,sizeof(USART2_RX_BUF));

cJSON_Delete(root);

return0;
}

If parsing fails, you may need to adjust the stack size in your startup file:

//startup_stm32f10x_hd.s

Stack_SizeEQU0x00000C00

Heap_SizeEQU0x00000200

Final result

Final result:

BMP display effect
Desktop Effect 3
Desktop Effect 5

The data is exactly the same as the mobile phone data:

Mobile phone interface 1

Open source address

My development board has been pre-written with Chinese fonts, and development boards without fonts may not be suitable.

All codes have been open sourced on Gitee Code Cloud:

https://gitee.com/whik/stm32_olympic_medals

Or reply [ Olympic Games ] in the background to obtain the download link of the project compressed package.

Electronic circuit development learning

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.

Desktop Effect 4

This post is from DIY/Open Source Hardware
Add and join groups EEWorld service account EEWorld subscription account Automotive development circle

Latest reply

Quite interesting, those word art models are pretty good!   Details Published on 2021-8-11 16:48
Personal signature

玩板看这里:

http://en.eeworld.com/bbs/elecplay.html

EEWorld测评频道众多好板等你来玩,还可以来频道许愿树许愿说说你想要玩的板子,我们都在努力为大家实现!

 

4

Posts

0

Resources
2
 

add oil

This post is from DIY/Open Source Hardware
 
 

1942

Posts

2

Resources
3
 

Not bad.

This post is from DIY/Open Source Hardware
 
 
 

1368

Posts

6

Resources
4
 

Excellent

This post is from DIY/Open Source Hardware
Personal signature专注智能产品的研究与开发,专注于电子电路的生产与制造……QQ:2912615383,电子爱好者群: void
 
 
 

7422

Posts

2

Resources
5
 

Quite interesting, those word art models are pretty good!

This post is from DIY/Open Source Hardware
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list