W801 实现获取天气情况

devtools/2024/10/15 5:28:47/

        看了小安派(AiPi-Eyes 天气站)的源码,感觉用W801也可以实现。

一、部分源码

main.c

#include "wm_include.h"
#include "Lcd_Driver.h"void UserMain(void)
{printf("\n user task \n");Lcd_Init();Lcd_Clear(RED);CreateWeatherTask();
}

 weather.c(该文件需保存为UTF8格式)

#include "Lcd_Driver.h"
#include "wm_include.h"
#include "LCD_calculate.h"
#include "cJSON.h"
#include "ctype.h"#define SSID		"SSID"
#define PASSWORD 	"PASSWORD"#define HTTPURL		"http://v1.yiketianqi.com/api?unescape=1&version=v61&appid=此处需替换为自己的KEY"#define    DEMO_TASK_SIZE      2048
static tls_os_queue_t 	*weather_q = NULL;
static OS_STK 			WeatherTaskStk[DEMO_TASK_SIZE];extern int demo_connect_net(void *, ...);
extern int http_get_demo(void *, ...);/*** @brief** @param weather_data
*/
void cjson_get_weather(char* weather_data)
{cJSON * item = NULL;//cjson对象cJSON* root = cJSON_Parse(weather_data );if (!root) {//printf("Error before: [%s]\n",cJSON_GetErrorPtr());}else{Gui_DrawFont_GBK16(5,95,WHITE,RED,"                           ");item = cJSON_GetObjectItem(root, "city");			//城市Gui_DrawFont_utf8_3bytes_16(5,15,WHITE,RED,item->valuestring);item = cJSON_GetObjectItem(root, "cityEn");			//城市英文Gui_DrawFont_utf8_3bytes_16(40,15,WHITE,RED,item->valuestring);item = cJSON_GetObjectItem(root, "wea");Gui_DrawFont_utf8_3bytes_16(5,35,WHITE,RED,"天气:");Gui_DrawFont_utf8_3bytes_16(60,35,WHITE,RED,item->valuestring);item = cJSON_GetObjectItem(root, "tem");			//温度Gui_DrawFont_utf8_3bytes_16(5,55,WHITE,RED,"温度:");Gui_DrawFont_GBK24(60,55,WHITE,RED,item->valuestring);Gui_DrawFont_utf8_3bytes_16(100,55,WHITE,RED,"℃");item = cJSON_GetObjectItem(root, "win");			//风Gui_DrawFont_utf8_3bytes_16(5,75,WHITE,RED,item->valuestring);item = cJSON_GetObjectItem(root, "air_level");		//空气质量Gui_DrawFont_utf8_3bytes_16(69,75,WHITE,RED,item->valuestring);}cJSON_free(item);cJSON_Delete(root);}void weather_task(void *sdata)
{Gui_DrawFont_GBK16(5,95,WHITE,RED,"Start......                     ");if(demo_connect_net(SSID,PASSWORD)==WM_SUCCESS){Gui_DrawFont_GBK16(5,95,WHITE,RED,"Connected                    ");tls_os_time_delay(3000);http_get_demo(HTTPURL);}for(;;){tls_os_time_delay(100);}	
}void CreateWeatherTask(void)
{tls_os_queue_create(&weather_q, DEMO_QUEUE_SIZE);tls_os_task_create(NULL, NULL,weather_task,NULL,(void *)WeatherTaskStk,          /* task's stack start address */DEMO_TASK_SIZE * sizeof(u32), /* task's stack size, unit:byte */DEMO_TASK_PRIO,0);
}

 Gui_DrawFont_utf8_3bytes_16函数,用来显示3字节UTF8编码的汉字

void Gui_DrawFont_utf8_3bytes_16(unsigned int x, unsigned int y, unsigned int fc, unsigned int bc, char *s)
{unsigned char i,j;unsigned short k,x0;x0=x;while(*s) {	for (k=0;k<sizeof(UTF8_hz16)/sizeof(UTF8_hz16[0]);k++) {if ((UTF8_hz16[k].Index[0]==*s)&&(UTF8_hz16[k].Index[1]==*(s+1))&&(UTF8_hz16[k].Index[2]==*(s+2))){ for(i=0;i<16;i++){for(j=0;j<8;j++) {if(UTF8_hz16[k].Msk[i*2]&(0x80>>j))	Gui_DrawPoint(x+j,y+i,fc);else {if (fc!=bc) Gui_DrawPoint(x+j,y+i,bc);}}for(j=0;j<8;j++) {if(UTF8_hz16[k].Msk[i*2+1]&(0x80>>j))	Gui_DrawPoint(x+j+8,y+i,fc);else {if (fc!=bc) Gui_DrawPoint(x+j+8,y+i,bc);}}}break;}}if(k!=sizeof(UTF8_hz16)/sizeof(UTF8_hz16[0])){s+=3;x+=16;}else		//ASC字符{k=*s;if (k>32) k-=32; else k=0;for(i=0;i<16;i++)for(j=0;j<8;j++) {if(asc16[k*16+i]&(0x80>>j))	Gui_DrawPoint(x+j,y+i,fc);else {if (fc!=bc) Gui_DrawPoint(x+j,y+i,bc);}}s++;x+=8;}}
}

 字模文件(UTF8格式文件)

#ifndef __FONTUTF8_H__
#define __FONTUTF8_H__struct typFNT_UrlCode
{unsigned char Index[3];	char Msk[32];
};
//阴码、逐行、顺向
const struct typFNT_UrlCode UTF8_hz16[] = {"城",0x20,0x28,0x20,0x24,0x20,0x20,0x27,0xFE,0x24,0x20,0xFC,0x20,0x24,0x24,0x27,0xA4,0x24,0xA4,0x24,0xA8,0x24,0xA8,0x3C,0x90,0xE6,0x92,0x49,0x2A,0x08,0x46,0x10,0x82,/*"城",0*/
"市",0x02,0x00,0x01,0x00,0x00,0x00,0x7F,0xFC,0x01,0x00,0x01,0x00,0x01,0x00,0x3F,0xF8,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x08,0x21,0x28,0x21,0x10,0x01,0x00,0x01,0x00,/*"市",1*/
"京",0x02,0x00,0x01,0x00,0xFF,0xFE,0x00,0x00,0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x01,0x00,0x11,0x10,0x11,0x08,0x21,0x04,0x45,0x04,0x02,0x00,/*"京",1*/}
#endif

在wm_sdk_w80x_20211115\demo\wm_http_demo.c中http_snd_req函数,增加一行:cjson_get_weather(Buffer);用于处理返回的json信息。


u32 http_snd_req(HTTPParameters ClientParams, HTTP_VERB verb, char *pSndData, u8 parseXmlJson)
{int                   nRetCode;u32                   nSize, nTotal = 0;char                 *Buffer = NULL;HTTP_SESSION_HANDLE   pHTTP;u32                   nSndDataLen ;do{Buffer = (char *)tls_mem_alloc(HTTP_CLIENT_BUFFER_SIZE);if(Buffer == NULL){return HTTP_CLIENT_ERROR_NO_MEMORY;}memset(Buffer, 0, HTTP_CLIENT_BUFFER_SIZE);printf("HTTP Client v1.0\r\n");nSndDataLen = (pSndData == NULL ? 0 : strlen(pSndData));// Open the HTTP request handlepHTTP = HTTPClientOpenRequest(0);if(!pHTTP){nRetCode =  HTTP_CLIENT_ERROR_INVALID_HANDLE;break;}// Set the VerbnRetCode = HTTPClientSetVerb(pHTTP, verb);if(nRetCode != HTTP_CLIENT_SUCCESS){break;}
#if TLS_CONFIG_HTTP_CLIENT_AUTH// Set authenticationif(ClientParams.AuthType != AuthSchemaNone){if((nRetCode = HTTPClientSetAuth(pHTTP, ClientParams.AuthType, NULL)) != HTTP_CLIENT_SUCCESS){break;}// Set authenticationif((nRetCode = HTTPClientSetCredentials(pHTTP, ClientParams.UserName, ClientParams.Password)) != HTTP_CLIENT_SUCCESS){break;}}
#endif //TLS_CONFIG_HTTP_CLIENT_AUTH
#if TLS_CONFIG_HTTP_CLIENT_PROXY// Use Proxy serverif(ClientParams.UseProxy == TRUE){if((nRetCode = HTTPClientSetProxy(pHTTP, ClientParams.ProxyHost, ClientParams.ProxyPort, NULL, NULL)) != HTTP_CLIENT_SUCCESS){break;}}
#endif //TLS_CONFIG_HTTP_CLIENT_PROXYif((nRetCode = HTTPClientSendRequest(pHTTP, ClientParams.Uri, pSndData, nSndDataLen, verb == VerbPost || verb == VerbPut, 0, 0)) != HTTP_CLIENT_SUCCESS){break;}// Retrieve the the headers and analyze themif((nRetCode = HTTPClientRecvResponse(pHTTP, 30)) != HTTP_CLIENT_SUCCESS){break;}printf("Start to receive data from remote server...\r\n");// Get the data until we get an error or end of stream codewhile(nRetCode == HTTP_CLIENT_SUCCESS || nRetCode != HTTP_CLIENT_EOS){// Set the size of our buffernSize = HTTP_CLIENT_BUFFER_SIZE;// Get the datanRetCode = HTTPClientReadData(pHTTP, Buffer, nSize, 300, &nSize);if(nRetCode != HTTP_CLIENT_SUCCESS && nRetCode != HTTP_CLIENT_EOS)break;cjson_get_weather(Buffer);        //增加了这一句nTotal += nSize;}}while(0);   // Run only oncetls_mem_free(Buffer);if(pHTTP)HTTPClientCloseRequest(&pHTTP);if(ClientParams.Verbose == TRUE){printf("\n\nHTTP Client terminated %d (got %d b)\n\n", nRetCode, nTotal);}return nRetCode;
}

二、说明 

1、W801的SDK中DEMO程序有2个函数可以直接用:

extern int demo_connect_net(void *, ...);    //用于连上wifi
extern int http_get_demo(void *, ...);       //用于打开网页获得数据

本质也是lwip

2、W801的实时操作系统底层还是freertos,只是SDK对freertos进行了封装。。

3、SDK中自带了cjson,但是版本太低,最好替换为最新的。

4、一般显示汉字的部分都是GB2312编码,但是从yiketianqi.com/获取的json格式是UTF8的,所以修改了所有相关函数、字模为UTF8编码。

5、程序修改自SDK 的DEMO,还在整理中。

6、程序烧写及调试信息:


http://www.ppmy.cn/devtools/42692.html

相关文章

React hooks - forwardRef+useImperativeHandle

forwardRefuseImperativeHandle React.forwardRef用法useImperativeHandle用法第三个参数的用法 React.forwardRef与useImperativeHandle配合使用注意事项 React.forwardRef用法 1.创建一个 能够接受到ref属性的React 组件。 ref 用来获取实例&#xff0c;但函数组件不存在实例…

Java中关于infinity的解释

在Java中&#xff0c;Infinity 不是一个直接的关键字或常量&#xff0c;但它是浮点数&#xff08;float 和 double&#xff09;可以表示的一个特殊值。具体来说&#xff0c;Infinity 通常与两个特殊的浮点值相关联&#xff1a;正无穷大 (POSITIVE_INFINITY) 和负无穷大 (NEGATI…

后端技术常用网站

技术说明官网SpringBootMVC框架https://spring.io/projects/spring-bootSpringCloud微服务框架https://spring.io/projects/spring-cloud/MyBatis-PlusORM框架https://mp.baomidou.com/Swagger-UI文档生产工具https://github.com/swagger-api/swagger-uiKibana分析和可视化平台…

【Linux】:进程优先级

朋友们、伙计们&#xff0c;我们又见面了&#xff0c;本期来给大家解读一下有关Linux进程优先级的知识点&#xff0c;如果看完之后对你有一定的启发&#xff0c;那么请留下你的三连&#xff0c;祝大家心想事成&#xff01; C 语 言 专 栏&#xff1a;C语言&#xff1a;从入门到…

PHP生成二维码+二维码包含logo图片展示

composer require chillerlan/php-qrcode 用到的扩展自己安装&#xff08;注&#xff1a;只生成二维码只要开gd扩展就行&#xff09; 仅生成二维码看这个&#xff1a; use chillerlan\QRCode\QRCode;public function QRCode(){$qrcode new QRCode();$url "http://ww…

蓝桥杯嵌入式国赛笔记(2):拓展板按键程序设计

目录 1、前言 2、电路原理 3、代码编写 3.1 读取Btn电压 3.2 检索按键 3.3 main文件编写 3.3.1 进行变量定义 3.3.2 AD_Key函数 3.3.3 LCD函数 3.3.4 main函数 3.3.5 完整代码 4、测试 5、总结 1、前言 本文进行拓展板按键程序设计&#xff0c;拓展板的按键是通…

Android android.os.DeadObjectException aidl通信异常分析及解决

问题描述 做一款音乐播放应用&#xff0c;播放服务是通过AIDL形式对外暴露&#xff0c;允许跨进程调用且多个App同时操作音乐播放&#xff0c;偶现android.os.DeadObjectException问题 12-15 09:28:12.371: W/System.err(5412): android.os.DeadObjectException 12-15 09:28:…