天天看點

STM32-ESP8266解析天氣一:材料準備二:ESP8266前期準備三:stm32配置

一:材料準備

STM32F103ZET6一個,TFT彩屏一個,esp8266一個

二:ESP8266前期準備

  1. esp8266使用AT固件
  2. 提前使用序列槽助手配置8266為STA模式,連上無線網絡,網址參考https://www.cnblogs.com/lifan3a/articles/7070028.html

三:stm32配置

  1. 使用序列槽3(PB10,PB11)配置如下:
<usart3.c>

//序列槽接收緩存區 	
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<USART3_MAX_RECV_LEN)	//還可以接收資料
			{

				if(USART3_RX_STA==0) 			
				{

				}
				USART3_RX_BUF[USART3_RX_STA++]=res;	//記錄接收到的值	 
			}else 
			{
				USART3_RX_STA|=1<<15;				//強制标記接收完成
			} 
		}
	}  				 											 
}   

<usart3.h>
#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解析天氣一:材料準備二:ESP8266前期準備三:stm32配置

有疑問者請聯系:QQ:1735915513