stm32天气预报基本功能,连接上心知天气网,并解释cJSON格式数据,可以再TFTlcd屏幕显示
单片机源程序如下:
- #include "config.h"
- Results results[] = {{0}}; //存储json数据的结构体
- u8 get_current_weather(char *JSON, Results *results);
- int main()
- {
- //初始化变量
- u8 i = 0;
- u8 DTH11_Refresh = 0;
- u8 len, t, j, k;
- u8 key = 0;
- u8 duoji = 100;
- u32 sd_size;
- u8 sd_buf[6];
- u32 free,total, sdCapacity;
- u8 Read_ok_flag = 0;
-
- FIL file; //文件对象
- FATFS fatfs; //逻辑驱动器的工作区
- static FRESULT res; //FRESULT函数公共结果代码
- char write_buffer[1024]; //写文件缓冲区
- char read_buffer[1024]; //读文件缓冲区
- UINT counter;
-
- uint8_t json_buff[512];//接受json数据的缓存数组
-
- //char message[1024] = "{ \"results\":[{\"location\":{\"id\":\"wwww4d\",\"name\":\"beijng\",\"country\":\"CN\",\"path\":\"bejing,shanghai,guangzhou\",\"timezone\": \"Asia/Shanghai\",\"timezone_offset\": \"+08:00\"},\"now\":{\"text\":\"12\",\"code\": \"9\",\"temperature\": \"17\"},\"last_update\": \"2021-05-31T10:45:00+08:00\"}]}";
- /*-------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*-------------------------------------------------------------------------------------------------------------------------------------------------*/
-
- //外设初始化
- SysTick_Init(72); //节拍定时器频率设置 72M
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级,用于串口助手有数据发送时的中断
- USATR1_Init(115200); //串口1初始化为115200
- USART3_Init(9600); //串口3 树莓派通信 / 语音模块
- TIM3_Init(2000-1, 720-1); //周期是20ms PWM输出
- TIM4_Init(6000-1,36000-1); //定时3s
- LED_Init(); //LED灯初始化
- TFTLCD_Init(); //TFTLCD屏幕初始化
- KEY_Init(); //按键初始化
- ESP8266_Init(115200); //ESP8266初始化,里面包含串口2的初始化
- RF433M_Init(); //433M射频模块
- EN25QXX_Init(); //初始化falsh
- my_mem_init(SRAMIN); //初始化内部内存池
- FATFS_Init(); //为fatfs相关变量申请内存 没有这个申请内存,fatfs文件系统就不能用
- RTC_Init(); //RTC时钟
-
- /*-------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*-------------------------------------------------------------------------------------------------------------------------------------------------*/
- FRONT_COLOR=BLACK;//设置字体为黑色 12号字体,X轴可以0-220 Y轴可以0-380
- LCD_ShowFont12Char(60,0,"智能家居控制");
- LCD_DrawLine(0, 16, 240, 16); //画线
- LCD_DrawLine(0, 17, 240, 17);
- LCD_DrawLine(0, 130, 240, 130); //画线
- LCD_DrawLine(0, 131, 240, 131);
- LCD_DrawLine(105, 16,105, 130); //竖线
-
- FRONT_COLOR=RED; //设置字体为红色, 打印出错时的字体
- //SD卡
- while(SD_Init()!=0)
- {
- LCD_ShowFont12Char(10,30,"SD Card Error!");
- }
- f_mount(fs[0],"0:",1); //挂载SD卡 外设 如果为1那么就会强制的进行加载操作
- f_mount(fs[1],"1:",1); //挂载FLASH. 外设
- //FATF文件系统
- while(FATFS_GetFree("0", &sdCapacity, &free) != 0)
- {
- LCD_ShowFont12Char(10, 50, "SD Card FAT ERROR!");
- }
- // //温湿度模块
- // while(DHT11_Init())
- // {
- // LCD_ShowString(130,150,tftlcd_data.width,tftlcd_data.height,16,"DHT11 Error ");
- // delay_ms(500);
- // }
- FRONT_COLOR=BLACK; //恢复字体为黑色
- /*-------------------------------------------------------------------------------------------------------------------------------------------------*/
- /*-------------------------------------------------------------------------------------------------------------------------------------------------*/
- //温湿度模块数据显示
- LCD_DrawLine(0, 378, 240, 378); //画线
- LCD_DrawLine(0, 379, 240, 379);
- LCD_ShowString(0,380,tftlcd_data.width,tftlcd_data.height,16,"Temp: C");
- LCD_ShowString(100,380,tftlcd_data.width,tftlcd_data.height,16,"Humi: %RH");
- LCD_ShowFont12Char(110,20,"城市:");
- LCD_ShowFont12Char(110,60,"天气:");
- LCD_ShowFont12Char(110,100,"RTC时钟:");
- ESP8266_STA_TCPClient_Test();//连接热点+连接服务器
- while(1){
-
- // LCD_Clear_one(WHITE,0,150,240, 150);
- get_current_weather((char *)json_buff, results);
- Clean_buff1();
- printf("qingchuhou\n%s\n",ESP8266_Fram_Record_Struct .Data_RX_BUF);
- LCD_ShowString(155,20,175,20,16,results[0].location.name);
- LCD_ShowString(155,60,175,60,16,results[0].now.text);
- //
-
- if(strcmp(results[0].now.text, "Sunny") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_shower_rain);
- else if(strcmp(results[0].now.text, "Fair") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_fair);
- else if(strcmp(results[0].now.text, "Cloudy") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_cloudy);
- else if(strcmp(results[0].now.text, "Mostly_cloudy") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_mostly_cloudy);
- else if(strcmp(results[0].now.text, "partly_cloudy") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_partly_cloudy);
- else if(strcmp(results[0].now.text, "moon_cloudy") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_moon_cloudy);
- else if(strcmp(results[0].now.text, "overcast") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_overcast);
- else if(strcmp(results[0].now.text, "shower_rain") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_shower_rain);
- else if(strcmp(results[0].now.text, "thunder_shower") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_thunder_shower);
- else if(strcmp(results[0].now.text, "hail") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_hail);
- else if(strcmp(results[0].now.text, "light_rain") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_light_rain);
- else if(strcmp(results[0].now.text, "moderate_rain") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_moderate_rain);
- else if(strcmp(results[0].now.text, "heavy_rain") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_heavy_rain);
- else if(strcmp(results[0].now.text, "storm") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_storm);
- else if(strcmp(results[0].now.text, "heavy_storm") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_heavy_storm);
- else if(strcmp(results[0].now.text, "severe_storm") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_severe_storm);
- else if(strcmp(results[0].now.text, "freezing_rain") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_freezing_rain);
- else if(strcmp(results[0].now.text, "snow_flurry") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_snow_flurry);
- else if(strcmp(results[0].now.text, "light_snow") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_light_snow);
- else if(strcmp(results[0].now.text, "moderate_snow") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_moderate_snow);
- else if(strcmp(results[0].now.text, "heavy_snow") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_heavy_snow);
- else if(strcmp(results[0].now.text, "snow_storm") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_snow_storm);
- else if(strcmp(results[0].now.text, "dust") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_dust);
- else if(strcmp(results[0].now.text, "sand") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_sand);
- else if(strcmp(results[0].now.text, "sand_storm") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_sand_storm);
- else if(strcmp(results[0].now.text, "dust_storm") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_dust_storm);
- else if(strcmp(results[0].now.text, "foggy") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_foggy);
- else if(strcmp(results[0].now.text, "haze") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_haze);
- else if(strcmp(results[0].now.text, "windy") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_windy);
- else if(strcmp(results[0].now.text, "gale") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_gale);
- else if(strcmp(results[0].now.text, "hurricane") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_hurricane);
- else if(strcmp(results[0].now.text, "cold") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_cold);
- else if(strcmp(results[0].now.text, "hot") == 0) LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_hot);
- else{
- LCD_Draw_Picture(0,20,100,120,(u8 *)gImage_unknown);
- }
-
-
-
-
- }
- return 0;
-
- }
- //获取一次实时天气
- //返回:0---获取成功,1---获取失败
- u8 get_current_weather(char *JSON, Results *results)
- {
- while(!ESP8266_UnvarnishSend());//返回1:配置成功 0:配置失败
- //printf("清除前%s\n",ESP8266_Fram_Record_Struct .Data_RX_BUF);
- Clean_buff();
- //printf("清除后%s\n",ESP8266_Fram_Record_Struct .Data_RX_BUF);
- ESP8266_SendString(ENABLE, "GET api.seniverse点com/v3/weather/now.json?key=S1EB-JUvy4icX5_CO&location=Wuxi&language=en&unit=c\r\n", 0, Single_ID_0 );
- delay_ms(1000);
- strcpy(JSON,(char *)ESP8266_Fram_Record_Struct .Data_RX_BUF);
- cJSON_WeatherParse( JSON, results);
- //Clean_buff1();
- ESP8266_ExitUnvarnishSend(); //退出透传
- //获取三天的天气(包括今日)
- //ESP8266_SendString(ENABLE, "GET api.seniverse点com/v3/weather/daily.json?key=50alegxkcaehcge6&location=wuxi&language=en&unit=c&start=0&days=5\r\n", 0, Single_ID_0 );
- return 0;
- }
复制代码
所有资料51hei提供下载:
smartHouse.7z
(451.21 KB, 下载次数: 40)
|