概述
一、硬件连接:
ESP8266芯片,ESP-01S或者ESP12F模块,连接GND VCC(3V3) RXD TXD即可,RST可不连。
二、软件连接路由器步骤:
uint8_t cMqtt_Steps = 0; //Mqtt连接阶段
uint16_t iMqtt_Delay = 0; //Mqtt需要的延时
//MQTT初始化函数
void ES8266_MQTT_Init(void)
{
switch(cMqtt_Steps)
{
case 0:
ESP8266_ExitUnvarnishedTrans(); //退出透传
iMqtt_Delay = 50; //Mqtt需要的延时
cMqtt_Steps ++;
break;
case 1:
ESP8266_ExitUnvarnishedTrans(); //退出透传
iMqtt_Delay = 500; //Mqtt需要的延时
cMqtt_Steps ++;
break;
//发送AT复位指令
case 2:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)"AT+RSTrn", 8); //发送AT复位指令
bDMA_Tx_Busy = 1;
iMqtt_Delay = 800; //Mqtt需要的延时
cMqtt_Code_Count = 0;
cMqtt_Steps ++;
break;
//发送AT握手指令
case 3:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)"ATrn", 4); //发送AT握手指令
bDMA_Tx_Busy = 1;
iMqtt_Delay = 2000; //Mqtt需要的延时
cMqtt_Code_Count ++;
if(cMqtt_Code_Count >= 5)
{
user_main_info("ESP8266复位失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
}
break;
//关闭回显
case 4:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)"ATE0rn", 6);//关闭回显
bDMA_Tx_Busy = 1;
iMqtt_Delay = 5000; //Mqtt需要的延时
cMqtt_Steps ++;
break;
case 5:
user_main_info("ESP8266设置失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
break;
//设置为STATION模式
case 6:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)"AT+CWMODE_CUR=1rn", 17); //设置为STATION模式
bDMA_Tx_Busy = 1;
iMqtt_Delay = 10000; //Mqtt需要的延时
cMqtt_Steps ++;
break;
case 7:
user_main_info("ESP8266设置STATION失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
break;
//连接目标AP
case 8:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
memset(Usart1_TxBuff, 0, USART1_RXBUFF_SIZE);
sprintf((char*)Usart1_TxBuff,"AT+CWJAP_CUR="%s","%s"rn",WIFI_NAME,WIFI_PASSWD);
HAL_UART_Transmit_DMA(&huart1, Usart1_TxBuff, strlen((const char*)Usart1_TxBuff));
//连接目标AP
bDMA_Tx_Busy = 1;
iMqtt_Delay = 10000; //Mqtt需要的延时
cMqtt_Steps ++;
break;
case 9:
user_main_info("ESP8266连接目标AP失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
break;
//多次连接需退出透传
case 10:
ESP8266_ExitUnvarnishedTrans(); //退出透传
iMqtt_Delay = 50; //Mqtt需要的延时
cMqtt_Steps ++;
break;
case 11:
ESP8266_ExitUnvarnishedTrans(); //退出透传
iMqtt_Delay = 500; //Mqtt需要的延时
cMqtt_Steps ++;
break;
//========================================================================
//连接阿里云IOT服务器
case 12:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
memset(Usart1_TxBuff, 0, USART1_RXBUFF_SIZE);
sprintf((char*)Usart1_TxBuff,"AT+CIPSTART="%s","%s",%drn",
"TCP", MQTT_BROKERADDRESS, 1883);
HAL_UART_Transmit_DMA(&huart1, Usart1_TxBuff, strlen((const char*)Usart1_TxBuff));
//连接阿里云IOT服务器
bDMA_Tx_Busy = 1;
iMqtt_Delay = 10000; //Mqtt需要的延时
cMqtt_Steps ++;
break;
//时间到,连接阿里云IOT服务器失败
case 13:
user_main_info("连接服务器失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
break;
//设置透传模式
case 14:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)"AT+CIPMODE=1rn", 14); //设置透传模式
bDMA_Tx_Busy = 1;
iMqtt_Delay = 2000; //Mqtt需要的延时
cMqtt_Code_Count ++;
if(cMqtt_Code_Count > 2)
{
user_main_info("ESP8266设置透传模式失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
}
break;
case 15:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
HAL_UART_Transmit_DMA(&huart1, (uint8_t *)"AT+CIPSENDrn", 12);//开始处于透传发送状态
bDMA_Tx_Busy = 1;
iMqtt_Delay = 2000; //Mqtt需要的延时
cMqtt_Code_Count ++;
if(cMqtt_Code_Count > 2)
{
user_main_info("ESP8266开启发送状态失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
}
break;
//========================================================================
//登陆MQTT connet
case 16:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
memset(Usart1_TxBuff, 0, USART1_RXBUFF_SIZE);
Mqtt_Pack(MQTT_CLIENTID, MQTT_USARNAME, MQTT_PASSWD);
HAL_UART_Transmit_DMA(&huart1, Usart1_TxBuff, Usart1_Txlen);
bDMA_Tx_Busy = 1;
for(DataLen = 0; DataLen < Usart1_Txlen; DataLen ++)
printf("%X ",Usart1_TxBuff[DataLen]);
iMqtt_Delay = 3000; //Mqtt需要的延时
cMqtt_Code_Count ++;
if(cMqtt_Code_Count > 2)
{
user_main_info("MQTT连接失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
}
break;
//订阅主题
case 17:
memset(Usart1_RxBuff, 0, USART1_RXBUFF_SIZE);
memset(Usart1_TxBuff, 0, USART1_RXBUFF_SIZE);
MQTT_SubscribeTopic_Pack(MQTT_SUBSCRIBE_TOPIC,0,1);
HAL_UART_Transmit_DMA(&huart1, Usart1_TxBuff, Usart1_Txlen);
bDMA_Tx_Busy = 1;
for(DataLen = 0; DataLen < Usart1_Txlen; DataLen ++)
printf("%X ",Usart1_TxBuff[DataLen]);
iMqtt_Delay = 3000; //Mqtt需要的延时
cMqtt_Code_Count ++;
if(cMqtt_Code_Count > 2)
{
user_main_info("订阅失败n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
}
break;
default:;
}
}
//MQTT连接过程中串口接收数据处理
void ES8266_MQTT_Rx_Deal(void)
{
switch(cMqtt_Steps)
{
case 3:
if(strstr((const char *)WiFi_RX_BUF, "OK"))
{
user_main_info("ESP8266复位成功n");
iMqtt_Delay =0;
cMqtt_Steps ++;
}
break;
case 5:
if(strstr((const char *)WiFi_RX_BUF, "OK"))
{
user_main_info("ESP8266设置成功n");
iMqtt_Delay =0;
cMqtt_Steps ++;
}
break;
case 7:
if(strstr((const char *)WiFi_RX_BUF, "OK"))
{
user_main_info("ESP8266设置STATION成功n");
iMqtt_Delay = 1000;
cMqtt_Steps ++;
}
break;
case 9:
if(strstr((const char *)WiFi_RX_BUF, "OK"))
{
user_main_info("ESP8266连接AP成功n");
iMqtt_Delay = 1000;
cMqtt_Steps ++;
}
break;
case 13:
if(strstr((const char *)WiFi_RX_BUF, "CONNECT"))
{
user_main_info("连接服务器成功n");
iMqtt_Delay = 1000;
cMqtt_Steps ++;
cMqtt_Code_Count = 0;
}
break;
case 14:
if(strstr((const char *)WiFi_RX_BUF, "OK"))
{
user_main_info("ESP8266设置透传模式成功n");
iMqtt_Delay =0;
cMqtt_Steps ++;
}
break;
case 15:
if(strstr((const char *)WiFi_RX_BUF, ">"))
{
user_main_info("ESP8266开启发送状态成功n");
iMqtt_Delay = 0;
cMqtt_Code_Count = 0;
cMqtt_Steps ++;
}
break;
case 16:
if(WiFi_RX_BUF[0] == parket_connetAck[0] && WiFi_RX_BUF[1]== parket_connetAck[1]
&& WiFi_RX_BUF[2]== parket_connetAck[2])
{
if(WiFi_RX_BUF[3]== parket_connetAck[3])
{
user_main_info("MQTT连接成功n");
iMqtt_Delay = 0;
cMqtt_Code_Count= 0;
cMqtt_Steps ++;
}
else if(WiFi_RX_BUF[3]== 0x04)
{
user_main_info("MQTT连接失败,无效的用户名或密码n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
}
else
{
user_main_info("其他错误n");
cMqtt_Steps = 0;
mqtt_connect_fail_count ++;
}
}
break;
case 17:
if(WiFi_RX_BUF[0] == parket_subAck[0] && WiFi_RX_BUF[1]== parket_subAck[1]
&& WiFi_RX_BUF[2]== 0x00 && WiFi_RX_BUF[3]== 0x01)
{
user_main_info("订阅成功n");
iMqtt_Delay = 0;
cMqtt_Code_Count= 0;
cMqtt_Steps ++;
bMqtt_Status = 1;
mqtt_connect_fail_count = 0;
}
break;
default:;
}
}
最后
以上就是娇气老鼠为你收集整理的ESP8266连接阿里云物联网流程备忘的全部内容,希望文章能够帮你解决ESP8266连接阿里云物联网流程备忘所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复