赞
踩
根据手头要实现的需求,我需要通过手机端向32端发送指令,32端进行判断执行,所以采用esp8266的AP模式,将esp8266模块本身作为热点服务器,手机端作为客户端,连接热点WiFi发送数据。
STM32rct6板、esp8266(ESP-01S)、手机端网络助手app
wifi模块直接与TTL转串口模块相连即可,RX TX VCC GND,四根线连接对应即可。
AT
AT+CWMODE=2
AT+RST
AT+CWSAP="ESP8266","123456",11,3
AT+CIPMUX=1
AT+CIPSERVER=1,a (为端口号,默认333,最好不加,我改别的报错,不知道为啥)
指令还会返回一些相关信息,这里没贴出来,因为我的乱码了,不过返回ok就没问题
完成以上指令后,基本上就设置完成了,当用手机app连接时,app随便下一个网络调试助手就行,选择TCP客户端,连接时需要8266模块的ip,和之前设置的端口,端口为333,ip可以通过指令查询:
AT+CIFSR
点击连接,串口返回如下:
若与与手机端在一定时间内不通信,则模块会断开此连接,默认为3分钟。
完成连接后进行通信测试:
串口接收如下:
AT+CIPSEND=0,6//0为id号,6为要发送的数据长度
上面指令发送完成后直接继续发送数据即可
手机接收:
#include "timer5.h"
extern u8 start3;//串口中断接收完成标志
//定时器5中断服务程序
void TIM5_IRQHandler(void)
{
if (TIM_GetITStatus(TIM5, TIM_IT_Update) != RESET)//是更新中断
{
start3=1; //标记串口数据接收完成
TIM_ClearITPendingBit(TIM5, TIM_IT_Update ); //清除TIM5更新中断标志
TIM_Cmd(TIM5, DISABLE); //关闭TIM5
}
}
//通用定时器中断初始化
//这里始终选择为APB1的2倍,而APB1为36M
//arr:自动重装值。
//psc:时钟预分频数
void TIM5_Int_Init(u16 arr,u16 psc)
{
NVIC_InitTypeDef NVIC_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);//TIM7时钟使能
//定时器TIM7初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //根据指定的参数初始化TIMx的时间基数单位
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE ); //使能指定的TIM5中断,允许更新中断
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //子优先级1
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
}
//初始化IO 串口3
//bound:波特率
void usart3_init(u32 bound)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure; //声明一个结构体变量,用来初始化GPIO
//使能串口的RCC时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE); //使能UART3所在GPIOB的时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
//串口使用的GPIO口配置
// Configure USART3 Tx (PB.10) as alternate function push-pull
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
// Configure USART3 Rx (PB.11) as input floating
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//串口中断配置
//Configure the NVIC Preemption Priority Bits
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
// Enable the USART3 Interrupt
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//配置串口
USART_InitStructure.USART_BaudRate = bound;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
// Configure USART3
USART_Init(USART3, &USART_InitStructure);//配置串口3
// Enable USART3 Receive interrupts 使能串口接收中断
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
// Enable the USART3
USART_Cmd(USART3, ENABLE);//使能串口3
USART_ClearFlag(USART3, USART_FLAG_TC);
TIM5_Int_Init(1000-1,8400-1); //100ms中断
TIM_Cmd(TIM5, DISABLE); //关闭定时器7
}
//定义接收数组,接收缓冲,最大USART3_MAX_RECV_LEN个字节,宏定义为400
unsigned char USART3_RX_BUF[USART3_MAX_RECV_LEN];
u16 USART3_RX_STA=0; //数组标志位
u8 start3=0; //接收状态标志位
void USART3_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART3, USART_IT_RXNE) != RESET)//接收到数据
{
res =USART_ReceiveData(USART3);
TIM_SetCounter(TIM5,0);//计数器清空
TIM_Cmd(TIM5, ENABLE); //使能定时器5
USART3_RX_BUF[USART3_RX_STA]=res; //记录接收到的值
USART3_RX_STA++;
}
}
//串口3,printf 函数
//确保一次发送数据不超过USART3_MAX_SEND_LEN字节
void u3_printf(char* fmt,...)
{
u16 i,j;
va_list ap;
va_start(ap,fmt);
vsprintf((char*)USART3_TX_BUF,fmt,ap);
va_end(ap);
i=strlen((const char*)USART3_TX_BUF);//此次发送数据的长度
for(j=0;j<i;j++)//循环发送数据
{
while(USART_GetFlagStatus(USART3,USART_FLAG_TC)==RESET); //等待上次传输完成
USART_SendData(USART3,(uint8_t)USART3_TX_BUF[j]); //发送数据到串口3
}
}
//清空每次中断接收完成后的数组
void Clear_Buffer(void)//清空缓存
{
u8 i;
for(i=0;i<=USART3_RX_STA;i++)
USART3_RX_BUF[i]=0;//缓存
USART3_RX_STA=0;
Delay_ms(100);
}
//模块初始化
void esp8266_start_trans(void)
{
esp8266_send_cmd("AT+CWMODE=2","OK",50);
Clear_Buffer();
//Wifi模块重启
esp8266_send_cmd("AT+RST","OK",20);
Delay_ms(1000); //延时3S等待重启成功
Delay_ms(1000);
Delay_ms(1000);
//AP模式
esp8266_send_cmd("AT+CWSAP=\"想学ESP8266吗\",\"12345678\",11,3","OK",200);
Clear_Buffer();
esp8266_send_cmd("AT+CIPMUX=1","OK",20);
Clear_Buffer();
esp8266_send_cmd("AT+CIPSERVER=1","OK",200);
Clear_Buffer();
}
u8 esp8266_send_cmd(u8 *cmd,u8 *ack,u16 waittime)
{
u8 res=0;
USART3_RX_STA=0;
u3_printf("%s\r\n",cmd); //发送命令
printf("%s\r\n",cmd);
Delay_ms(waittime);
if(strstr((const char*)USART3_RX_BUF,"OK"))
{
Uart1_SendStr((char*)USART3_RX_BUF);
}
return res;
}
extern u8 start3;
extern unsigned char USART3_RX_BUF[USART3_MAX_RECV_LEN];
int main ( void )
{
/* 初始化 */
USART1_Config ();
usart3_init(115200);
CPU_TS_TmrInit();
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3);
esp8266_start_trans();
while ( 1 )
{
if(start3==1)//接收中断完成标志位
{
//判断接收的数据是否为密码数据,自己设置就行
if(strstr((const char*)USART3_RX_BUF,"12345678"))
{
printf("开门成功\r\n");
}
if(!strstr((const char*)USART3_RX_BUF,"12345678"))
{
printf("密码错误\r\n");
}
Clear_Buffer();
start3=0;
}
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。