You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

221 lines
5.2 KiB

3 years ago
#include "wifi.h"
#include "string.h"
#include "delay.h"
///////////////<EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>//////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
///////////////////////////////////////////////
/************* <EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> **************/
char Uart3_Buf[Buf3_Max];//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD>ջ<EFBFBD><EFBFBD><EFBFBD>
u8 First_Int = 0;
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
void USART3_init(u32 bound)
{
//GPIO<EFBFBD>˿<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //ʹ<EFBFBD><EFBFBD>USART3<EFBFBD><EFBFBD>GPIOBʱ<EFBFBD><EFBFBD>
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); //ʹ<EFBFBD><EFBFBD>USART3<EFBFBD><EFBFBD>GPIOBʱ<EFBFBD><EFBFBD>
//USART3_TX GPIOB10
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB10
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB, &GPIO_InitStructure);//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GPIOB
//USART3_RX GPIOB11<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;//PB11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
GPIO_Init(GPIOB, &GPIO_InitStructure);//<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>GPIOB
//Usart3 NVIC <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;//<EFBFBD><EFBFBD>ռ<EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD>3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQͨ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD>
NVIC_Init(&NVIC_InitStructure); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD>VIC<EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
//USART3 <EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_BaudRate = bound;//<EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//<EFBFBD>ֳ<EFBFBD>Ϊ8λ<EFBFBD><EFBFBD><EFBFBD>ݸ<EFBFBD>ʽ
USART_InitStructure.USART_StopBits = USART_StopBits_1;//һ<EFBFBD><EFBFBD>ֹͣλ
USART_InitStructure.USART_Parity = USART_Parity_No;//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>żУ<EFBFBD><EFBFBD>λ
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//<EFBFBD><EFBFBD>Ӳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //<EFBFBD>շ<EFBFBD>ģʽ
USART_Init(USART3, &USART_InitStructure); //<EFBFBD><EFBFBD>ʼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>3
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڽ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ж<EFBFBD>
USART_Cmd(USART3, ENABLE); //ʹ<EFBFBD>ܴ<EFBFBD><EFBFBD><EFBFBD>3
}
void USART3_IRQHandler(void) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>3<EFBFBD>жϷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
u8 Res=0;
Res = USART_ReceiveData(USART3);
Uart3_Buf[First_Int] = Res; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>յ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
First_Int++; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
if(First_Int >= Buf3_Max) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>,<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>ָ<EFBFBD>򻺴<EFBFBD><EFBFBD><EFBFBD><EFBFBD>׵<EFBFBD>ַ
{
First_Int = 0;
}
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void CLR_Buf(void)
{
u16 k;
for(k=0;k<Buf3_Max;k++) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
Uart3_Buf[k] = 0x00;
}
First_Int = 0; //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD>洢λ<EFBFBD><EFBFBD>
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
void UART3_SendString(char* s)
{
while(*s)//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
{
while(USART_GetFlagStatus(USART3, USART_FLAG_TC)==RESET);
USART_SendData(USART3 ,*s++);//<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD>ǰ<EFBFBD>ַ<EFBFBD>
}
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
void UART3_Send_Command(char* s)
{
CLR_Buf(); //<EFBFBD><EFBFBD><EFBFBD>ս<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>buffer
UART3_SendString(s); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
UART3_SendString("\r"); //<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> \r\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD>
}
//Ѱ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
//<EFBFBD><EFBFBD><EFBFBD>أ<EFBFBD>1 <EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD> 0 δ<EFBFBD>ҵ<EFBFBD>
u8 Find(char *a)
{
if(strstr(Uart3_Buf, a)!=NULL)
{
return 1;
}
else
{
return 0;
}
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ATָ<EFBFBD><EFBFBD>
//*b<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
//*a<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ񷵻ص<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
//wait_time<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>͵Ĵ<EFBFBD><EFBFBD><EFBFBD>
//interval_time:ÿ<EFBFBD>εȴ<EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>
u8 UART3_Send_AT_Command(char *b,char *a,u8 wait_time,u32 interval_time)
{
u8 i;
i = 0;
while(i < wait_time) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>û<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD> <EFBFBD>ͼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٷ<EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD> <EFBFBD>ٽ<EFBFBD><EFBFBD>в<EFBFBD><EFBFBD><EFBFBD>Ŀ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
{
UART3_Send_Command(b);//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>2<EFBFBD><EFBFBD><EFBFBD><EFBFBD> b <EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\r\n <EFBFBD><EFBFBD>ڷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD>
delay_ms(interval_time); //<EFBFBD>ȴ<EFBFBD>һ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD> <EFBFBD><EFBFBD>50<EFBFBD>Ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 50*20ms = 1<EFBFBD><EFBFBD>
if(Find(a)) //<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҪӦ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD> a
{
return 1;
}
i++;
}
return 0;
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD>WIFIģ<EFBFBD><EFBFBD>ģʽ
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> AP <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> WPA2_PSK
signed ESP8266_wifi_Init(void)
{
u8 ret;
ret = UART3_Send_AT_Command("AT\r\n","OK",3,100); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
delay_ms(600);
if(ret == 0)
{
return -1;
}
ret = UART3_Send_AT_Command("AT+CWMODE=2\r\n","OK",3,100); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>ΪAPģʽ
delay_ms(600);
if(ret == 0)
{
return -2;
}
ret = UART3_Send_AT_Command("AT+CWSAP=\"ZH\",\"123456789\",5,3\r\n","OK",3,100); //<EFBFBD><EFBFBD><EFBFBD>ý<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <EFBFBD><EFBFBD><EFBFBD><EFBFBD> ͨ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> WPA2_PSK
delay_ms(600);
if(ret == 0)
{
return -3;
}
ret = UART3_Send_AT_Command("AT+CIPMUX=1\r\n","OK",3,100); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
delay_ms(600);
if(ret == 0)
{
return -4;
}
ret = UART3_Send_AT_Command("AT+CIPSERVER=1,5000\r\n","OK",3,100); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ģ<EFBFBD><EFBFBD>ΪTCP<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿ں<EFBFBD>Ϊ5000
delay_ms(600);
if(ret == 0)
{
return -5;
}
ret = UART3_Send_AT_Command("AT+CIPSTO=0\r\n","OK",3,100); //<EFBFBD><EFBFBD><EFBFBD>ó<EFBFBD>ʱʱ<EFBFBD><EFBFBD>Ϊ0
delay_ms(600);
if(ret == 0)
{
return -6;
}
return 0;
}
//<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
//*buf<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
void ESP8266_send_data(char *buf)
{
UART3_Send_AT_Command("AT+CIPSEND=0,14\r\n",">",3,50); //<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͨ<EFBFBD><EFBFBD><EFBFBD>ţ<EFBFBD><EFBFBD>Լ<EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>͵<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
delay_ms(200);
UART3_SendString(buf);
delay_ms(100);
}