各位大lao,我用stm32和ESP8266连接阿里云,已经实现了向阿里云发送数据,但是接收一直实现不了,以下是我的代码:
我是用串口2发送数据,串口1接收数据,这是串口1配置代码:
void Uart1_init(u32 bound)
{
//GPIO端口设置
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE); //使能USART1,GPIOA时钟
//USART1_TX GPIOA.9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.9
//USART1_RX GPIOA.10初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);//初始化GPIOA.10
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
//USART 初始化设置
USART_InitStructure.USART_BaudRate = bound;//串口波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//字长为8位数据格式
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; //收发模式
USART_Init(USART1, &USART_InitStructure); //初始化串口1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//开启串口接收中断
USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);//开启串口空闲中断
USART_Cmd(USART1, ENABLE); //使能串口1
}
这是接收阿里云指令代码:
char MQTT_Head[]="+MQTTSUBRECV";
void USART1_IRQHandler(void) //串口1中断服务程序 PA10为输入
{
int i;
uint8_t uart1RecvFlag = 0; //接收完成标志位
static uint8_t uart1RecvLen = 0; //接收数据的长度
extern char MQTT_Receive_Data[176];
if(USART_GetITStatus(USART1, USART_IT_RXNE) == SET) //接收中断
{
USART_RX_BUF[uart1RecvLen] = USART1->DR;
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
uart1RecvLen++;
}
else if(USART_GetITStatus(USART1,USART_IT_IDLE) ==SET) //空闲中断
{
USART1->SR;
USART1->DR; //作用是清除IDLE标志位, 根据规格要求, 清除IDLE是要先读取SR后读DR, 因为中断是IDLE, 所以从DR读出来的是没意思的数据,
for(i=0;i<11;i++)
{
if( USART_RX_BUF[i] != MQTT_Head[i]) //接收到的数据不是云平台发来的
{
memset(USART_RX_BUF,0x00,sizeof(USART_RX_BUF));
uart1RecvFlag = 0;
break;
}
else uart1RecvFlag = 1; //接收到的数据是云平台发来的
}
if(uart1RecvFlag == 1 )
{
for(i = 0;i < uart1RecvLen;i++)
{
MQTT_Receive_Data[i] = USART_RX_BUF[i]; //将缓冲区数据传给另一个数组
}
}
uart1RecvLen = 0;
}
}
这是截取数据代码(截取我需要的数值):
char MQTT_Receive_Data[176];
char ESP01FStruct_MQTT_Param[6];
void Receive_Deal(void)
{
u8 ESP01FStruct_MQTT_Param_Value;
u8 ESP01FStruct_Led_State_flag;
u8 ESP01FStruct_Led_flag;
extern u8 key1;
int i = 0,index;
char MQTT_Power_State[]="Switch";//6个
char *p;
p = MQTT_Receive_Data; //指针p指向接受数组的第一个元素
for(i=0;i<sizeof(MQTT_Receive_Data);i++)
{
if(*(p+i)=='p' && *(p+i+1)=='a' && *(p+i+2) == 'r')
{
index = i+10;
break;
}
}
for(i=0;i<6;i++,index++)
{
ESP01FStruct_MQTT_Param[i] = MQTT_Receive_Data[index];
}
ESP01FStruct_MQTT_Param_Value = MQTT_Receive_Data[index+2];
for(i=0;i<6;i++)
{
if(ESP01FStruct_MQTT_Param[i] != MQTT_Power_State[i])
{
ESP01FStruct_Led_State_flag=0;
break;
}
else
ESP01FStruct_Led_State_flag=1;
}
if(ESP01FStruct_Led_State_flag==1)
{
switch (ESP01FStruct_MQTT_Param_Value)
{
case 0x30:
{
ESP01FStruct_Led_flag=0;
break;
}
case 0x31:
{
ESP01FStruct_Led_flag=1;
break;
}
}
}
if(ESP01FStruct_Led_State_flag==1 && ESP01FStruct_Led_flag==1)
{
BUZZxhb_ON();
key1 = 1;
}
if(ESP01FStruct_Led_State_flag==1 && ESP01FStruct_Led_flag==0)
{
BUZZxhb_OFF();
key1 = 0;
}
}
不知道哪里错了,能不能麻烦各位大lao帮我指正一下,谢谢!