STM32-ESP8266解析天气

一:材料准备

STM32F103ZET6一个,TFT彩屏一个,esp8266一个

二:ESP8266前期准备

  1. esp8266使用AT固件
  2. 提前使用串口助手配置8266为STA模式,连上无线网络,网址参考https://www.cnblogs.com/lifan3a/articles/7070028.html

三:stm32配置

  1. 使用串口3(PB10,PB11)配置如下:


//串口接收缓存区 	
u8 USART3_RX_BUF[USART3_MAX_RECV_LEN]; 				//接收缓冲,最大USART3_MAX_RECV_LEN个字节.
u8  USART3_TX_BUF[USART3_MAX_SEND_LEN]; 			//发送缓冲,最大USART3_MAX_SEND_LEN字节

vu16 USART3_RX_STA=0;   	

void USART3_IRQHandler(void)
{
	u8 res;	      
	if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//接收到数据
	{	 
		res =USART_ReceiveData(USART3);		 
		if((USART3_RX_STA&(1<<15))==0)//接收完的一批数据,还没有被处理,则不再接收其他数据
		{ 
			if(USART3_RX_STA
#define USART3_MAX_RECV_LEN		1500					//最大接收缓存字节数
#define USART3_MAX_SEND_LEN		600					//最大发送缓存字节数
#define USART3_RX_EN 			1					//0,不接收;1,接收.

extern u8  USART3_RX_BUF[USART3_MAX_RECV_LEN]; 		//接收缓冲,最大USART3_MAX_RECV_LEN字节
extern u8  USART3_TX_BUF[USART3_MAX_SEND_LEN]; 		//发送缓冲,最大USART3_MAX_SEND_LEN字节




使用的指令发送,代码如下

//天气连接端口号:80	
#define WEATHER_PORTNUM 	"80"
//天气服务器IP
#define WEATHER_SERVERIP 	"api.seniverse.com"

//时间端口号
#define TIME_PORTNUM			"80"
//时间服务器IP
#define TIME_SERVERIP			"www.beijing-time.org"


void get_time()
{	
	resp=mymalloc(SRAMIN,10);
	p_end=mymalloc(SRAMIN,40);
	p=mymalloc(SRAMIN,40);							//申请40字节内存
	sprintf((char*)p,"AT+CIPSTART=\"TCP\",\"%s\",%s",TIME_SERVERIP,TIME_PORTNUM);    //配置目标TCP服务器	
	if(ESP_sendCommand(p,"CONNECT",3000, 10)==Success); //连接TCP服务器   
	else ESP_errorLog(3);

	if(ESP_sendCommand("AT+CIPMODE=1\r\n","OK",3000, 10)==Success); //多连接模式
	else ESP_errorLog(3);
	
	if(ESP_sendCommand("AT+CIPSEND\r\n","OK",3000, 10)==Success); //进入透传模式  
	else ESP_errorLog(3);
	
	myfree(SRAMIN,p);        //释放内存	
	p=mymalloc(SRAMIN,40);
	USART3_RX_STA=0;

	u3_printf("GET /time15.asp HTTP/1.1\r\nHost:www.beijing-time.org\n\n");
	delay_ms(1000);
	//if(USART3_RX_STA&0X8000)		//此时再次接到一次数据,为天气的数据
	{ 
		USART3_RX_BUF[USART3_RX_STA&0X7FFF]=0;//添加结束符
		resp="Date";
		USART3_RX_BUF[USART3_RX_STA & 0x7ff] = 0;
		//printf("get_tim_srt:%s\r\n",USART3_RX_BUF);
		if(strstr((char*)USART3_RX_BUF,(char*)resp)) 
		{
			resp="GMT";
			p_end = (u8*)strstr((char*)USART3_RX_BUF,(char*)resp);
			p = p_end - 9; 
			//printf("get_net_str %s\r\n",p);
			nwt.hour = ((*p - 0x30)*10 + (*(p+1) - 0x30) + 8) % 24;  //GMT0-->GMT8						  
			nwt.min = ((*(p+3) - 0x30)*10 + (*(p+4) - 0x30)) % 60;						 
			nwt.sec = ((*(p+6) - 0x30)*10 + (*(p+7) - 0x30)) % 60;
			nwt.year = ((*(p-5) - 0x30)*1000 + (*(p-4) - 0x30)*100+ (*(p-3) - 0x30)*10+ (*(p-2) - 0x30)); 
			nwt.date = ((*(p-12) - 0x30)*10 + (*(p-11) - 0x30)); 			
			if        ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Jan")) nwt.month=1; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Feb")) nwt.month=2; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Mar")) nwt.month=3; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Apr")) nwt.month=4; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "May")) nwt.month=5; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Jun")) nwt.month=6; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Jul")) nwt.month=7; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Aug")) nwt.month=8; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Sep")) nwt.month=9; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Oct")) nwt.month=10; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Nov")) nwt.month=11; 
			else if   ((u8*)strstr((char*)USART3_RX_BUF,(char*) "Dec")) nwt.month=12;										 													     
			printf("nwt.year = %d\r\n",nwt.year);
			printf("nwt.month = %d\r\n",nwt.month);
			printf("nwt.date = %d\r\n",nwt.date);  			//获取data 28日					
			LCD_ShowxNum(78,50,nwt.year,4,16,0);		
			LCD_ShowChar(112,50,'-',16,0);						
			LCD_ShowxNum(120,50,nwt.month/10,1,16,0);
			LCD_ShowxNum(128,50,nwt.month%10,1,16,0);
			LCD_ShowChar(136,50,'-',16,0);
			LCD_ShowxNum(144,50,nwt.date/10,1,16,0);
			LCD_ShowxNum(152,50,nwt.date%10,1,16,0);		
			printf("nwt.hour = %d\r\n",nwt.hour);
			printf("nwt.min = %d\r\n",nwt.min);
			printf("nwt.sec = %d\r\n",nwt.sec);	
		if(nwt.hour >12)
		{
			nwt.hour = nwt.hour - 12;
			LCD_ShowString(112,70,200,16,16,"PM");
		}
		else
			LCD_ShowString(112,70,200,16,16,"AM");
			
		LCD_ShowxNum(88,90,nwt.hour/10,1,16,0);
		LCD_ShowxNum(96,90,nwt.hour%10,1,16,0);
		LCD_ShowChar(104,90,':',16,0);						
		LCD_ShowxNum(112,90,nwt.min/10,1,16,0);
		LCD_ShowxNum(120,90,nwt.min%10,1,16,0);
		LCD_ShowChar(128,90,':',16,0);
		LCD_ShowxNum(136,90,nwt.sec/10,1,16,0);
		LCD_ShowxNum(144,90,nwt.sec%10,1,16,0);
			//printf("uddate:nettime!!!");			
			myfree(SRAMIN,resp);
			myfree(SRAMIN,p_end);
		}	
	}
	USART3_RX_STA=0;
	USART3_RX_BUF[0]='\0';   //清空
	//printf("%s\r\n",USART3_RX_BUF);   //打印BUF数据
	u3_printf("+++");        //退出透传模式
	delay_ms(1000);
	myfree(SRAMIN,p);        //释放内存	
	printf("ALL   end ....................\r\n");	   //解析结束		
}
void get_weather()
{
	
	p=mymalloc(SRAMIN,40);
	//申请40字节内存
	sprintf((char*)p,"AT+CIPSTART=\"TCP\",\"%s\",%s",WEATHER_SERVERIP,WEATHER_PORTNUM);    //配置目标TCP服务器	
	
	if(ESP_sendCommand(p,"CONNECT",3000, 10)==Success); //连接TCP服务器   
	else ESP_errorLog(3);
	
	if(ESP_sendCommand("AT+CIPMODE=1\r\n","OK",3000, 10)==Success); //多连接模式
	else ESP_errorLog(3);
	
	if(ESP_sendCommand("AT+CIPSEND\r\n","OK",3000, 10)==Success); //进入透传模式  
	else ESP_errorLog(3);
	
	myfree(SRAMIN,p);        //释放内存	
	p=mymalloc(SRAMIN,40);
	USART3_RX_STA=0;
	u3_printf("GET https://api.seniverse.com/v3/weather/daily.json?key=pqe1fgv45lrdruq7&location=zhengzhou&language=zh-Hans&unit=c&start=0&days=5\n\n");
	delay_ms(1000);
	if(USART3_RX_STA&0X8000)		//此时再次接到一次数据,为天气的数据
	{ 
		USART3_RX_BUF[USART3_RX_STA&0X7FFF]=0;//添加结束符
	}
	parse_3days_weather();   //解析天气
	u3_printf("+++");        //退出透传模式
	//delay_ms(1000); 
	myfree(SRAMIN,p);        //释放内存	
}
  1. JSON数据解析是从网上参考大神的,读者可以系统学习一下。

  2. api参考链接:https://docs.seniverse.com/api/start/code.html

  3. 测试方法:打开浏览器,打开以下链接,查看反馈数据,json解析。

https://api.seniverse.com/v3/weather/daily.json?key=pqe1fgv45lrdruq7&location=zhengzhou&language=zh-Hans&unit=c&start=0&days=5

 

废话不多说上图:

STM32-ESP8266解析天气_第1张图片

 

 

有疑问者请联系:QQ:1735915513

你可能感兴趣的:(STM32,树莓派,PLC)