2205 views|4 replies

539

Posts

3

Resources
The OP
 

[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.

Latest reply

What command do you use to send HTTP request?   Details Published on 2022-4-14 15:49
 
 

6841

Posts

11

Resources
2
 
This introduces the cJson library, which makes data parsing and packaging much more convenient. Are you ready to display it on OLED next?

Comments

Yes, the board is equipped with OLED, of course it can't be wasted  Details Published on 2022-4-14 12:25
 
 
 

539

Posts

3

Resources
3
 
lugl4313820 published on 2022-4-14 08:33 This library introduced cJson, which makes data parsing and packaging much more convenient. Are you ready to display it on OLED next?

Yes, the board is equipped with OLED, of course it can't be wasted

 
 
 

1w

Posts

16

Resources
4
 

What command do you use to send HTTP request?

Comments

GET command used  Details Published on 2022-4-14 20:38
Personal signaturehttp://shop34182318.taobao.com/
https://shop436095304.taobao.com/?spm=a230r.7195193.1997079397.37.69fe60dfT705yr
 
 
 

539

Posts

3

Resources
5
 
ddllxxrr posted on 2022-4-14 15:49 What command is used to send HTTP request? ? ? ?

GET command used

 
 
 

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