[RVB2601 Creative Application Development] 4. Get weather information with cJSON
[Copy link]
1. Overview
In the previous post, we have completed the RVB2601 development board as a TCP client to send and receive data with the TCP Server (PC).
[RVB2601 Creative Application Development] 3. WiFi network communication
The purpose of this article is to enable RVB2601 to obtain network data (weather information) from the website, and then parse the required weather information through cJSON.
2. Preparation
If you want to get weather information from a website, you first need to find a website that can provide weather services. Of course, it is best if it is free.
According to my investigation, there are still many websites that provide weather services. As far as I know, the most commonly used one is Xinzhi Weather ( https://www.seniverse.com/ );
There are also Yaya Weather ( http://www.yytianqi.com/api.html ) and NOWAPI ( https://www.nowapi.com/api/weather.future ).
At first, I chose Xinzhi Weather because many people use it and I am familiar with it. But for some reason, I kept getting no response information a few days ago, so I simply switched to YY Weather.
Personally, I think YY Weather is easier to use. However, its simplicity comes at a price. It cannot directly return the English version of the city and weather. It only supports city codes and Chinese characters (unicode codes).
That is to say, if you want to use Chinese/English, you must convert them both, which is quite troublesome.
Let’s first take a look at the request format and return message of YY Weather.
Request format:
It should be noted that, similar to Xinzhi Weather, you need to specify the city code and private key when sending HTTP requests. The private key will be automatically assigned after successful registration, and you can use this private key in the future.
Return result, JSON format:
3. Join cJSON library
After YY Weather is ready, you can edit the code.
In fact, before the whole code, we should use PC to simulate TCP client to test YY Weather to see if we can get the return information. This step is too simple and will be omitted for the time being.
It is really easy to transplant the cJSON library to the RVB2601 development board, because there is no need to transplant it at all, just use it directly
cJSON download address: https://sourceforge.net/projects/cjson/
After decompression, you only need to add the following three files to the project. No other files are needed.
To test whether the cJSON library can be used normally, you can call the function cjson_test in test.c for testing.
Test results:
4. Get weather data
After the cJSON test passes, you can start changing the code to obtain and parse weather information.
1. Create a new task to handle weather request and analysis. (Use it as LED task in early testing)
static weather_task_entry(void *arg)
{
uled_init();
while(1)
{
aos_msleep(1000);
led_flash();
//cmd_at_client_handler();
}
}
void create_weather_task(void)
{
aos_task_new_ext(&weather_task, "weather", weather_task_entry, NULL,
3*1024, AOS_DEFAULT_APP_PRI);
}
2.cJSON data parsing:
There are two functions involved, one for cJSON to parse the required content, and the other for converting it into a string:
t_weather_type weather_data;
static void weathrer_get_info(const cJSON *item, uint8_t val[])
{
switch(item ->type)
{
case cJSON_Number:
if(item ->valuedouble - item->valueint > 0.001)
{
sprintf(val,"%.1f",item->valuedouble);
}
else
{
if(item->valueint <= 99999)
{
sprintf(val,"%d",item->valueint);
}
}
break;
case cJSON_String:
sprintf(val,"%.8s",item->valuestring);
break;
}
}
//天气数据解析
void weather_data_analysis(void *data, size_t len)
{
uint8_t *buf;
cJSON *json;
cJSON *counts;
cJSON *wdata;
cJSON *cityid;
buf = (uint8_t *)data;
if(len == 0)
{
return;
}
printf("%s\n",buf);
json = cJSON_Parse(buf);//转换为json数据
if(!json)//
{
printf("Error1 before: [%s]\n",cJSON_GetErrorPtr());
}
else
{
counts = cJSON_GetObjectItem(json,"counts");
if(!counts)
{
printf("Error2 before: [%s]\n",cJSON_GetErrorPtr());
}
else
{
if(counts->valueint > 0)
{
wdata = cJSON_GetObjectItem(json,"data");
if(!data)
{
printf("Error3 before: [%s]\n",cJSON_GetErrorPtr());
}
else
{
cityid = cJSON_GetObjectItem(wdata,"cityId");
weathrer_get_info(cityid, weather_data.cityid);
printf("cityid = %s\n",weather_data.cityid);
cityid = cJSON_GetObjectItem(wdata,"qw");
weathrer_get_info(cityid, weather_data.air_temperature);
printf("qw = %s\n",weather_data.air_temperature);
}
}
}
}
cJSON_Delete(json);
}
5. Test Results
This time, only the city code and temperature were parsed. The test results are as follows:
The city code CH010100 represents Beijing
qw = 13 means the current temperature is 13℃
The test is now complete and will be further improved.
|