提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言
因为心知天气下发的数据是json格式,使用串口提取时使用cJSON库提取
一、下载cJSON库
这个链接是一个大佬的https://blog.csdn.net/weixin_45488643/article/details/107132066,
只需要上面这些包,text.c是测试源码,可要可不要。
二、代码
1.代码展示
心知天气获取的json数据
{"results":[
{
"location":{
"id":"WM6N2PM3WY2K",
"name":"Chengdu",
"country":"CN",
"path":"Chengdu,Chengdu,Sichuan,China",
"timezone":"Asia/Shanghai",
"timezone_offset":"+08:00"
},
"daily":[
{
"date":"2022-07-28",
"text_day":"Cloudy",
"code_day":"4",
"text_night":"Cloudy",
"code_night":"4",
"high":"35",
"low":"23",
"rainfall":"0.00",
"precip":"0.00",
"wind_direction":"CLM",
"wind_direction_degree":"",
"wind_speed":"8.4",
"wind_scale":"2",
"humidity":"90"
},
{
"date":"2022-07-29",
"text_day":"Cloudy",
"code_day":"4",
"text_night":"Shower",
"code_night":"10",
"high":"33",
"low":"24",
"rainfall":"5.40",
"precip":"0.52",
"wind_direction":"CLM",
"wind_direction_degree":"",
"wind_speed":"8.4",
"wind_scale":"2",
"humidity":"87"
},
{"date":"2022-07-30",
"text_day":"Shower",
"code_day":"10",
"text_night":"Light rain",
"code_night":"13","high":"32",
"low":"22",
"rainfall":"5.40",
"precip":"0.52",
"wind_direction":"CLM",
"wind_direction_degree":"",
"wind_speed":"3.0",
"wind_scale":"1",
"humidity":"92"
}
],
"last_update":"2022-07-28T08:00:00+08:00"
}
]}
main.c
#include "main.h"
#include "gpio.h"
#include "USART.h"
#include "Delay.h"
#include "i2c.h"
#include "AHT10_S.h"
#include "lcd.h"
#include "stdio.h"
#include "touch.h"
#include "tim.h"
#include "lvgl.h"
#include "lv_port_disp.h"
#include "lv_port_indev.h"
#include "ADC.h"
#include "WIFI.h"
#include "usart2.h"
#include "cJSON.h"
#include "lvgl_text.h"
uint8_t Weather_analysis(u8* buff,u8 *Weather_stat,u8 *data);
uint8_t dat;
uint8_t CP_Rxbuff[1024];
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
Delay_init(180);
ADC_Config();
uart_init(115200);
USART_config();
MX_TIM8_Init();
LCD_Init(); //ԵʼۯLCD FSMCޓࠚ
//ԥľǁԵʼۯ
AT_WIFIMODE(STA);
printf("\n");
AT_CONNECT_WIFI(ssid,pass);
printf("\n");
AT_Connect_TCP();
printf("\n");
AT_SET_TCPMODE(PT);
printf("\n");
AT_SEND_TCP();
//printf("\n");
AT_SEND_MES();
AT_BAKE_SEND();
//AT_SET_TIME();
//AT_GET_TIME();
cJSON *json;
//tp_dev.init();
//lv_init();
//lv_port_disp_init();
//lv_port_indev_init();
//lvgl_dome();
while (1)
{
Delay_ms(3000);
for(int j=0;j<strlen(Rxbuff) * sizeof(char);j++){
CP_Rxbuff[j]=Rxbuff[j];
} //这个是将串口2回调的函数打包给CP_Rxbuff
//printf("\r\n%s\r\n",Rxbuff);
printf("\r\n%s\r\n",CP_Rxbuff);
//***********************************JSON解析数据
cJSON *root =cJSON_Parse((char*)CP_Rxbuff);//读取心知天气回调包
cJSON *array =cJSON_GetObjectItem(root,"results");//读取关键字
cJSON *results0=cJSON_GetArrayItem(array,0);//判断位置
cJSON *location =cJSON_GetObjectItem(results0,"location");//读取关键字
cJSON *daily =cJSON_GetObjectItem(results0,"daily");//读取关键字
printf("name:%s\r\n",cJSON_GetObjectItem(location,"name")->valuestring);//串口打印location里面的name,
if(daily->type==cJSON_Array){
for(int i=0;i<3;i++){ //循环三次,因为我们是读取心知天气的未来三天的数据
cJSON *dailys=cJSON_GetArrayItem(daily,i);//读取的位置
cJSON *daily_dat=cJSON_GetObjectItem(dailys,"humidity");//读取daily里面的humi
printf("date[%d]:%s%%\r\n",i,daily_dat->valuestring);
}
}
cJSON_Delete(root);//释放json
//tp_dev.scan(0);
//lv_task_handler();
}
}
2.成果展示
set_1_LCD ID:9488
AT+CWMODE=1
AT+CWJAP="CMCC-6w7C","5xwjiw2g"CLOSEDWSCONNECTWIFI CONNECTEDWIFI GOT IP
AT+CIPSTART="TCP","api.seniverse.com",80CONNECTOK
AT+CIPMODE=1
AT+CIPSEND
{"results":[{"location":{"id":"WM7V66VSE5DV","name":"Guangan","country":"CN","path":"Guangan,Guangan,Sichuan,China","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"daily":[{"date":"2022-07-28","text_day":"Sunny","code_day":"0","text_night":"Clear","code_night":"1","high":"34","low":"24","rainfall":"0.00","precip":"0.00","wind_direction":"SW","wind_direction_degree":"225","wind_speed":"8.4","wind_scale":"2","humidity":"87"},{"date":"2022-07-29","text_day":"Sunny","code_day":"0","text_night":"Cloudy","code_night":"4","high":"38","low":"25","rainfall":"0.00","precip":"0.00","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"8.4","wind_scale":"2","humidity":"81"},{"date":"2022-07-30","text_day":"Cloudy","code_day":"4","text_night":"Cloudy","code_night":"4","high":"37","low":"25","rainfall":"0.00","precip":"0.00","wind_direction":"CLM","wind_direction_degree":"","wind_speed":"3.0","wind_scale":"1","humidity":"78"}],"last_update":"2022-07-28T08:00:00+08:00"}]}
name:Guangan
date[0]:87%
date[1]:81%
date[2]:78%
总结
路漫漫其修远兮,吾将上下而求索。