前面章节讲解了AP模式运用,这节来探索STA模式如何使用。何谓STA模式呢?通俗来讲就是我们的WIFI模块,连接到可用的无线网络(如手机发射出来的热点或者家里路由器的热点),连接上无线网络后,相当于模块也是可以上网了,就跟手机连路由器WIFI上网,这时我们就可以利用模块去访问某些服务器进行通信,来获取我们想要的信息。比如获取天气服务器的天气信息等等。
1.1资源环境:
ESP8266 WIFI模块一个
STM32开发板(本例程采用STM32F103ZET6开发板)
SD卡一张(没有也可以,此处用来获取显示天气状态的图标)
可上网的热点(ssid和密码)
1.2 ESP82266与开发板引脚连接说明:
ESP82266 开发板引脚
VCC ------ 5V
GND ------ GND
TXD ------ PB11
RXD ------ PB10
RST ------ PA4(可不接)
IO_0 ------ PA15(可不接)
在AP模式章节也讲到,每种模式又可以设置为三种不同子模式进行数据通信,STA模式也不例外。即模块可以配置为TCP服务器子模式、TCP客户端子模式、UDP子模式。下面分别说下配置成这三种模式的必要配置:
TCP服务器的配置:
TCP客户端的配置:
UDP模式配置:
首先我们要配置WIFI模块连上热点,这里我们采取配置为TCP客户端子模式,便于后面获取天气预报信息:
atk_8266_send_cmd("AT+CWMODE=1","OK",50); //配置STA模式
atk_8266_send_cmd("AT+RST","OK",20); //重启模块
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
delay_ms(1000);
//设置无线参数:ssid和密码
sprintf((char*)p,"AT+CWJAP=\"%s\",\"%s\"",wifista_ssid,wifista_password);
while(atk_8266_send_cmd(p,"WIFI GOT IP",300)); //连接目标路由器,获取ip
开发板上设置好要连接的ssid和密码:
当模块连接上热点后,就可以开始建立要访问的目标服务器的TCP连接了:这里以访问心知天气服务器为例
心知天气文档了解入口:https://docs.seniverse.com/api/start/start.html
获取一次实时天气信息如下:以获取广州天气信息为例,这里涉及到http相关知识和cjson格式数据解析
//心知天气端口号
#define WEATHER_PORTNUM "80"
//心知天气IP
#define WEATHER_SERVERIP "api.seniverse.com"
sprintf((char*)p,"AT+CIPSTART=\"TCP\",\"%s\",%s",WEATHER_SERVERIP,WEATHER_PORTNUM); //配置心知天气的ip和端口
res = atk_8266_send_cmd(p,"OK",200);//连接到目标tcp服务器
delay_ms(300);
atk_8266_send_cmd("AT+CIPMODE=1","OK",100); //设置传输模式为透传
atk_8266_send_cmd("AT+CIPSEND","OK",100); //开始透传
u3_printf("GET https://api.seniverse.com/v3/weather/now.json?key=x3owc7bndhbvi8oq&location=guangzhou&language=zh-Hans&unit=c\n\n");
当WIFI模块获取心知服务器一次实时天气成功后,会返回对应的cjson格式数据,可以通过串口打印出返回的信息如下:
{"results":[{"location":{"id":"WS0E9D8WN298","name":"骞垮窞","country":"CN","path":"骞垮窞,骞垮窞,骞夸笢,涓浗","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"now":{"text":"澶氫簯","code":"4","temperature":"29"},"last_update":"2020-04-19T13:49:00+08:00"}]}
对应部分的cjson格式数据解析如下:
pSub = cJSON_GetObjectItem(root,"results");
if(pSub != NULL)
{
arrayItem = cJSON_GetArrayItem(pSub,0);
pr = cJSON_Print(arrayItem);
pItem = cJSON_Parse(pr);
if(pItem != NULL)
{
pSubItem = cJSON_GetObjectItem(pItem,"now");
if(pSubItem != NULL)
{
pChildItem = cJSON_GetObjectItem(pSubItem,"code"); //获取气象代码
if(pChildItem != NULL)
{
gbkstr = pChildItem->valuestring;
curwer_buf[0]=str2int((u8 *)gbkstr);
}
pChildItem = cJSON_GetObjectItem(pSubItem,"temperature"); //获取实时温度值
if(pChildItem != NULL)
{
gbkstr = pChildItem->valuestring;
curwer_buf[1] = str2int((u8 *)gbkstr);
}
}
}
}
我们还可以继续获取今、明、后三天的天气信息,具体看你的心知服务器的api接口:对应的访问接口如下
u3_printf("GET https://api.seniverse.com/v3/weather/daily.json?key=x3owc7bndhbvi8oq&location=guangzhou&language=zh-Hans&unit=c&start=0&days=5\n\n");
返回的cjson格式数据如下:
"results":[{"location":{"id":"WS0E9D8WN298","name":"骞垮窞","country":"CN","path":"骞垮窞,骞垮窞,骞夸笢,涓浗","timezone":"Asia/Shanghai","timezone_offset":"+08:00"},"daily":[{"date":"2020-04-19","text_day":"澶氫簯","code_day":"4","text_night":"澶氫簯","code_night":"4","high":"29","low":"22","rainfall":"0.0","precip":"","wind_direction":"鏃犳寔缁鍚?","wind_direction_degree":"0","wind_speed":"16.20","wind_scale":"3","humidity":"78"},{"date":"2020-04-20","text_day":"澶氫簯","code_day":"4","text_night":"澶氫簯","code_night":"4","high":"29","low":"22","rainfall":"0.0","precip":"","wind_direction":"鍗?","wind_direction_degree":"198","wind_speed":"25.20","wind_scale":"4","humidity":"79"},{"date":"2020-04-21","text_day":"涓洦","code_day":"14","text_night":"澶ч洦","code_night":"15","high":"28","low":"20","rainfall":"10.0","precip":"","wind_direction":"鍗?","wind_direction_degree":"179","wind_speed":"25.20","wind_scale":"4","humidity":"89"}],"last_update":"2020-04-19T11:17:53+08:00"}]}
当获取成功后,就可以在我们的开发板上展示如下的天气信息:可知当前的风力、风速、和温度,以及今明后三天的最高温度和最低温度。
大家应该也观察到了,这里会显示出对应天气状态的小图标,是根据获取到气象代码,从SD卡里读取对应气象代码的图片,显示出来的。
SD卡里面的气象小图标如下所示:
具体实现如下:
void Weather_Icon_Show(void)
{
u8 i=0;
u8 res;
u16 temp;
pic_info.totpicnum=pic_get_tnum((u8 *)"0:/wc"); //得到总有效文件数
pic_info.picfileinfo.lfsize=30*2+1; //长文件名最大长度 , _MAX_LFN换成30
pic_info.picfileinfo.lfname=mymalloc(SRAMIN,pic_info.picfileinfo.lfsize); //为长文件缓存区分配内存
pic_info.pname=mymalloc(SRAMIN,pic_info.picfileinfo.lfsize); //为带路径的文件名分配内存
pic_info.picindextbl=mymalloc(SRAMIN,2*pic_info.totpicnum); //申请2*totpicnum个字节的内存,用于存放图片索引
res=f_opendir(&pic_info.picdir,"0:/wc"); //打开目录
if(res==FR_OK)
{
pic_info.curindex=0;//当前索引为0
while(1)//全部查询一遍
{
temp=pic_info.picdir.index; //记录当前index
res=f_readdir(&pic_info.picdir,&pic_info.picfileinfo); //读取目录下的一个文件
if(res!=FR_OK||pic_info.picfileinfo.fname[0]==0)break; //错误了/到末尾了,退出
pic_info.fn=(u8*)(*pic_info.picfileinfo.lfname?pic_info.picfileinfo.lfname:pic_info.picfileinfo.fname);
res=f_typetell(pic_info.fn);
if((res&0XF0)==0X50)//取高四位,看看是不是图片文件
{
pic_info.picindextbl[pic_info.curindex]=temp;//记录索引 ,若是分配的内存空间不够,就会导致索引乱序
pic_info.curindex++;
}
}
}
delay_ms(1500);
piclib_init(); //初始化画图
res=f_opendir(&pic_info.picdir,(const TCHAR*)"0:/wc"); //打开目录
if(res==FR_OK) pic_info.picstatus|=(1<<3);
if(pic_info.picstatus&0X08)//打开成功
{
for(i=0;i<4;i++)
{
dir_sdi(&pic_info.picdir,pic_info.picindextbl[iconbuf[i]]); //改变当前目录索引
f_readdir(&pic_info.picdir,&pic_info.picfileinfo); //读取目录下的一个文件
pic_info.fn=(u8*)(*pic_info.picfileinfo.lfname?pic_info.picfileinfo.lfname:pic_info.picfileinfo.fname);
strcpy((char*)pic_info.pname,"0:/wc/"); //复制路径(目录)
strcat((char*)pic_info.pname,(const char*)pic_info.fn); //将文件名接在后面
if(i<=2) ai_load_picfile(pic_info.pname,105+i*265,320,60,60,1);//显示图片 lcddev.width
else ai_load_picfile(pic_info.pname,565,90,60,60,1);//显示图片 lcddev.width
delay_ms(500);
}
}
if(pic_info.picfileinfo.lfname!=NULL ||pic_info.pname!=NULL || pic_info.picindextbl!=NULL)
{
myfree(SRAMIN,pic_info.picfileinfo.lfname); //释放内存
myfree(SRAMIN,pic_info.pname); //释放内存
myfree(SRAMIN,pic_info.picindextbl); //释放内存
}
}
通过对WIFI--ESP8266模块STA模式获取天气信息学习应用,不仅可以了解到STA模式原理,还可以加深对HTTP协议,CJSON格式数据解析使用。