在线时间589 小时
UID3637937
注册时间2020-6-26
NXP金币2365
TA的每日心情 | 奋斗 昨天 13:46 |
---|
签到天数: 850 天 [LV.10]以坛为家III
金牌会员
- 积分
- 7000
- 最后登录
- 2024-5-7
|
看到RT1060evk板子有网络接口,本次就试试网络功能。搞一个获取天气信息例子试试效果。
插上网线
打开menuconfig配置。找到板上外设,打开网络和SDRAM。
然后选择WebClient软件包,这个是用于获取天气的IOT软件包。使用的是心知天气的API,通过HTTP协议或取返回数据,返回数据是Json结构.
再就是CJson软件包。
更新mdk工程。
对keil工程设置一下。
获取天气代码。
- #include <rtthread.h>
- #include <rtdevice.h>
- #include <board.h>
- #include <webclient.h> /* 使用 HTTP 协议与服务器通信需要包含此头文�?? */
- #include <sys/socket.h> /* 使用BSD socket,需要包含socket.h头文�?? */
- #include <netdb.h>
- #include <cJSON.h>
- #include <finsh.h>
- #if 1
- #define GET_HEADER_BUFSZ 1024 //头部大小
- #define GET_RESP_BUFSZ 512 //响应缓冲区大
- #define GET_URL_LEN_MAX 256 //网址最大长
- #define GET_URI "http://api.seniverse.com/v3/weather/now.json?key=%s&location=%s&language=zh-CN&unit=c" //心知天气 API
- #define AREA_NAME "guangzhou" //广州 ID
- #define XINZHI_API_KEY "********" //心知天气认证 pravite key,注册心知天气账号获取
- /* 天气数据解析 */
- void net_weather_data_parse(rt_uint8_t *data)
- {
- cJSON *root = RT_NULL, *object = RT_NULL, *item = RT_NULL;
- cJSON *arrayItem = RT_NULL,*subobject = RT_NULL;
-
- root = cJSON_Parse((const char *)data);
- if (!root)
- {
- rt_kprintf("\nNo memory for cJSON root!\n");
- return;
- }
- if((arrayItem = cJSON_GetObjectItem(root, "results")) != RT_NULL)
- {
- //int size = cJSON_GetArraySize(arrayItem); //获取数组中对象个 3
- if((object = cJSON_GetArrayItem(arrayItem,0)) != RT_NULL)//获取父对象内
- {
- /* 匹配子对1 */
- if((subobject = cJSON_GetObjectItem(object,"location")) != RT_NULL)
- {
- item = cJSON_GetObjectItem(subobject, "name");
- rt_kprintf("\nCityName:%s \n", item->valuestring);
- item = cJSON_GetObjectItem(subobject, "path");
- rt_kprintf("CityPath:%s \n", item->valuestring);
- }
- /* 匹配子对2 */
- if((subobject = cJSON_GetObjectItem(object,"now")) != RT_NULL)
- {
- item = cJSON_GetObjectItem(subobject, "text");
- rt_kprintf("weather:%s \n", item->valuestring);
- item = cJSON_GetObjectItem(subobject, "code");
- rt_kprintf("code:%s \n", item->valuestring);
- item = cJSON_GetObjectItem(subobject, "temperature");
- rt_kprintf("temperature:%s \n", item->valuestring);
- }
- /* 匹配子对3 */
- if((subobject = cJSON_GetObjectItem(object,"last_update")) != RT_NULL)
- {
- rt_kprintf("UpdateTime:%s \n", subobject->valuestring);
- }
- }
- }
- if (root != RT_NULL) cJSON_Delete(root);
- }
- void netweather(int argc, char **argv)
- {
- rt_uint8_t *buffer = RT_NULL;
- int resp_status;
- struct webclient_session *session = RT_NULL;
- char *weather_url = RT_NULL;
- int content_length = -1, bytes_read = 0;
- int content_pos = 0;
- /* weather_url 分配空间 */
- weather_url = rt_calloc(1, GET_URL_LEN_MAX);
- if (weather_url == RT_NULL)
- {
- rt_kprintf("No memory for weather_url!\n");
- goto __exit;
- }
- /* 拼接 GET 网址 */
- if(argc > 1)
- {
- rt_snprintf(weather_url, GET_URL_LEN_MAX, GET_URI,XINZHI_API_KEY, argv[1]);
- }else
- {
- rt_snprintf(weather_url, GET_URL_LEN_MAX, GET_URI,XINZHI_API_KEY, AREA_NAME);
- }
- /* 创建会话并且设置响应的大 */
- session = webclient_session_create(GET_HEADER_BUFSZ);
- if (session == RT_NULL)
- {
- rt_kprintf("No memory for get header!\n");
- goto __exit;
- }
- /* �?? GET 请求使用默认的头 */
- if ((resp_status = webclient_get(session, weather_url)) != 200)
- {
- rt_kprintf("webclient GET request failed, response(%d) error.\n", resp_status);
- goto __exit;
- }
- /* 分配用于存放接收数据的缓 */
- buffer = rt_calloc(1, GET_RESP_BUFSZ);
- if (buffer == RT_NULL)
- {
- rt_kprintf("No memory for data receive buffer!\n");
- goto __exit;
- }
- rt_memset(buffer,0,GET_RESP_BUFSZ);
- content_length = webclient_content_length_get(session);
- rt_kprintf("\r\ncontent_length = %d\r\n",content_length);
- if (content_length)
- {
- /* 返回的数据是分块传输. */
- do
- {
- bytes_read = webclient_read(session, buffer + content_pos,
- content_length - content_pos > GET_RESP_BUFSZ ?
- GET_RESP_BUFSZ : content_length - content_pos);
- rt_kprintf("bytes_read = %d\r\n",bytes_read);
- if (bytes_read <= 0)
- {
- break;
- }
- content_pos += bytes_read;
- }while (content_pos < content_length);
- }
-
- for(int i=0;i<content_length;i++)
- rt_kprintf("%c",buffer[i]);
- rt_kprintf("\r\n");
-
- /* 天气数据解析 */
- net_weather_data_parse(buffer);
-
- __exit:
- /* 释放网址空间 */
- if (weather_url != RT_NULL)
- rt_free(weather_url);
- /* 关闭会话 */
- if (session != RT_NULL)
- webclient_close(session);
- /* 释放缓冲区空 */
- if (buffer != RT_NULL)
- rt_free(buffer);
- }
- MSH_CMD_EXPORT(netweather, Get weather by webclient);
- #endif
复制代码 注意使用网络时不能操作led那个gpio。main中去掉点灯。
编译出现一个错误。找到对应头文件路径包含上。
加上头文件路径后编译成功
下载完成,复位,打开串口工具。可以看到输出信息了。
测试网络接口和SDRAM.
下面就是获取天气信息了。
可以看到获取的Json字符串了。Json解析有点问题。是SDRAM不稳定,分配堆内存在SDRAM中,出现错误了。
|
|