#include "esp8266.h"
#include "string.h"
#include "stdio.h"
#include "delay.h"
#include "MQTTPacket.h"
#include "DHT11.h"
#include "led1.h"
#include "KQM6600.h"
#include "stdlib.h"
#include "usart.h"
#include "RTC.h"
#include "time.h"
#include "gui_guider.h"
#include "SPI.h"
#include "string.h"
struct esp wifi={0};
//wifi模块ESP8266初始化
void esp_init(void)
{
//1.开启时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//开启GPIOB口的时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE,ENABLE);//开启GPIOE口的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//开启USART3的时钟
//GPIO初始化
GPIO_InitTypeDef GPIO_InitStruct={0};
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOB,&GPIO_InitStruct);
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOE,&GPIO_InitStruct);
GPIO_SetBits(GPIOE,GPIO_Pin_6);//IO口电平拉高
//串口初始化
USART_InitTypeDef USART_InitStruct={0};
USART_InitStruct.USART_BaudRate=115200;//波特率
USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStruct.USART_WordLength=USART_WordLength_8b;//8位数据
USART_InitStruct.USART_StopBits=USART_StopBits_1;//停止位
USART_InitStruct.USART_Parity=USART_Parity_No;//没有奇偶校验
USART_InitStruct.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;//收发使能
USART_Init(USART3,&USART_InitStruct);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);//开启串口接收中断
USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);//开启串口空闲中断
USART_Cmd(USART3, ENABLE);//对USRT3使能
//NVIC初始化
NVIC_InitTypeDef NVIC_InitStruct={0};
NVIC_InitStruct.NVIC_IRQChannel=USART3_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=2;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStruct);
}
//发送函数
//unsigned char
void UART3_send(uint8_t data)
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC) == 0){}//检查发送移位寄存器是否为空,判断一次是否发送完成
USART_SendData(USART3,data);//发送数据(一个字节)
}
//发送不定长的字符串
void UART3_sendstr(uint8_t *str)
{
while(*str !='\0')//直到遇到'\0'停止
{
UART3_send(*str);
str++;
}
}
//发送定长字符串
void UART3_sendbuff(uint8_t *buff,uint8_t len)
{
for(uint8_t i=0;i<len;i++)
{
UART3_send(buff[i]);
}
}
//串口中断函数
void USART3_IRQHandler(void)
{
/*
1.判断哪个中断触发
2.清理中断标志
3.执行代码
*/
uint8_t recvdata=0;
if(USART_GetITStatus(USART3,USART_IT_RXNE)==1)
{
recvdata=USART_ReceiveData(USART3);
wifi.rxdata[wifi.rxlen]=recvdata;
wifi.rxlen++;
wifi.rxlen %= 1024;
//串口接收数据,没有办法判断数据是多少
//利用串口发送函数,将数据发送。
USART_SendData(USART1,recvdata);
}
if(USART_GetITStatus(USART3,USART_IT_IDLE)==1)//判断是否为串口空闲中断
{
//空闲中断的清理,不不能用清理中断标志的函数
//需要读取(接收)数据,即可清零
//官方的手册 给的操作是先读状态寄存器 再度数据寄存器
recvdata = USART3->SR;
recvdata = USART3->DR;
wifi.rxflag=1;//数据接收完成标志位置1;
}
}
//wifi命令发送函数
u8 send_check_ack(u8* AT,u8*ack,u16 outtime)
{
//1.发送指令
//2.检查返回值里面有没有ACK
memset(wifi.rxdata,0,1024);
wifi.rxlen=0;
wifi.rxflag=0;
//发送AT指令
UART3_sendstr(AT);
while(outtime)
{
if(wifi.rxflag == 1)
{
if(strstr((char *)wifi.rxdata,(char *)ack) !=NULL)
{
wifi.rxflag=0;
return 1;
}
else
{
wifi.rxflag=0;
}
}
outtime--;
Delay_nms(1);
}
return 0;
}
//wifi复位指令
u8 fuweiflag = 1;
void wifi_fuwei(void)
{
fuweiflag=0;
while(USART_GetFlagStatus(USART3,USART_FLAG_TC) == 0){}//检查发送移位寄存器是否为空,判断一次是否发送完成
UART3_sendstr((u8*)"+++");
Delay_nms(50);
if(send_check_ack((u8*)"AT+RST\r\n",(u8*)"OK",100))
{
Delay_nms(2000);
sFLASH_EraseSector(0x000000);//擦除指定扇区数据
sFLASH_EraseSector(0x001000);//擦除指定扇区数据
sFLASH_EraseSector(0x002000);//擦除指定扇区数据
Delay_nms(1000);
wififlag=0;
APwififlag=0;
}
}
//等待用户连接ESP8266散发的热点wifi
u8 AP_wifi_late(void)
{
memset(wifi.rxdata,0,1024);
wifi.rxlen=0;
wifi.rxflag=0;
u16 outtime=10000;
while(outtime)
{
if(wifi.rxflag == 1)
{
if(strstr((char *)wifi.rxdata+40,"192.168.4.2") !=NULL)
{
wifi.rxflag=0;
return 1;
}
else
{
wifi.rxflag=0;
}
}
outtime--;
Delay_nms(1);
}
return 0;
}
//wifi命令 AP模式
u8 wifiret=0;
u8 APwififlag = 0;
u8 wfbuff[128]={0};
u8 wifi_APcmd(void)
{
if(send_check_ack((u8*)"AT\r\n",(u8*)"OK",100))
{
if(send_check_ack((u8*)"AT+CWMODE=2\r\n",(u8*)"OK",100))//AP模式
{
if(send_check_ack((u8*)"AT+CWSAP=\"AYYPESP\",\"12345678\",6,4\r\n",(u8*)"OK",200))//设置ESPWIFI密码账号
{
if(send_check_ack((u8*)"AT+CIPMUX=1\r\n",(u8*)"OK",200))//开启多链接
{
if(send_check_ack((u8*)"AT+CIPSERVER=1,8080\r\n",(u8*)"OK",200))//设置端口
{
//判断flash中是否有值了
//memset(wfbuff,0,128);
sFLASH_ReadBuffer(wfbuff,0x002000,128);
if(strstr((char*)wfbuff,"wifiname")!=NULL)
{
APwififlag=1;
return 1;
}
//Delay_nms(10000);
u8 ret=AP_wifi_late();
if(ret==1)
{
if(strstr((char*)wifi.rxdata,"STA_CONNECTED")!=NULL)
{
memset(wifi.rxdata,0,1024);
wifi.rxlen=0;
wifi.rxflag=0;
while(wifi.rxflag==0){}
if(strstr((char*)wifi.rxdata+1,"CONNECT")!=NULL)
{
memset(wifi.rxdata,0,1024);
wifi.rxlen=0;
wifi.rxflag=0;
while(wifi.rxflag==0){}
if(strstr((char*)wifi.rxdata+10,"wifiname")!=NULL)
{
memset(wifi.wifiname,0,64);
memset(wifi.wifipd,0,64);
sFLASH_EraseSector(0x002000);//擦除指定扇区数据
sFLASH_WriteBuffer(wifi.rxdata+10,0x002000,1024);
char* wname1=strstr((char*)wifi.rxdata+10,"wifiname");
char* wname2=strstr((char*)wifi.rxdata+10,"password");
for(u8 i=0;i<wname2-wname1-10-4;i++)
{
wifi.wifiname[i]=wifi.rxdata[i+25];
}
char* wpassword=strstr((char*)wifi.rxdata+10,"mycity");
for(u8 i=0;i<wpassword-wname2-14;i++)
{
wifi.wifipd[i]=wifi.rxdata[i+42];
}
printf("wifiname:%s,wifipasswd:%s\r\n",wifi.wifiname,wifi.wifipd);
sFLASH_EraseSector(0x000000);//擦除指定扇区数据
sFLASH_EraseSector(0x001000);//擦除指定扇区数据
sFLASH_WriteBuffer(wifi.wifiname,0x000000,64);
sFLASH_WriteBuffer(wifi.wifipd,0x001000,64);
APwififlag=1;
fuweiflag=1;
return 1;
}
}
}
}
else{return 0;}
}
}
}
}
}
}
void Get_Weather(void)
{
//+IPD,0,90:{"wifiname":"YYP","password":"12345678911","mycity":"zhengzhou","alarm_h":"","alarm_m":""}
memset(wifi.location,0,128);
unsigned char buf[128] = {0};
unsigned char buf2[128] = {0};
sFLASH_ReadBuffer(buf,0x002000,128);
char* wname1=strstr((char*)buf,"mycity");
char* wname2=strstr((char*)buf,"alarm_h");
if(wname1==NULL || wname2==NULL)
{
return;
}
for(u8 i=0;i<wname2-wname1-12;i++)
{
wifi.location[i]=buf[i+55];
}
printf("location:%s\r\n",wifi.location);
if(send_check_ack((u8*)"AT+CIPSTART=\"TCP\",\"api.seniverse.com\",80\r\n",(u8*)"OK",5000))
{
if(send_check_ack((u8*)"AT+CIPMODE=1\r\n",(u8*)"OK",100))
{
if(send_check_ack((u8*)"AT+CIPSEND\r\n",(u8*)">",100))
{
sprintf((char*)buf2,"GET https://api.seniverse.com/v3/wea