STM32自动完成esp8266配置

上一篇是实现了将STM32从串口1收到的指令通过串口2发送给esp8266,在将从esp8266返回的通过串口1发送给电脑

如果只是使用电脑串口1发送数据给esp8266连接的TCP server的话,可以使用上一篇那样的手动配置,不过那样有很多问题,不是很方便,那么怎么让STM32自己完成连接AP,设置单连接,建立TCP,设置透传的步骤呢?

需要注意的是:

    esp8266需要获得带回车换行的AT指令才执行,所以需要 \r\n
    要判断一条指令是不是真的已经通过串口2发送给esp8266了
    如果上一个指令还没返回就很快的发下一个指令,可能会不执行

具体代码如下:

    if(HAL_UART_Transmit(&huart2, (uint8_t *)AT, sizeof(AT), 0xFFFF)==HAL_OK)
            {
                HAL_Delay(1000);
                printf("AT......\n");
                if(HAL_UART_Transmit(&huart2, (uint8_t *)AT_RST, sizeof(AT_RST), 0xFFFF)==HAL_OK)
                {
                    HAL_Delay(1000);
                    printf("AT_RST......\n");
                    if(HAL_UART_Transmit(&huart2, (uint8_t *)AT_CWJAP_CUR, sizeof(AT_CWJAP_CUR), 0xFFFF)==HAL_OK)
                    {
                        HAL_Delay(2000);
                        printf("AT_CWJAP_CUR......\n");
                        if(HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPMUX, sizeof(AT_CIPMUX), 0xFFFF)==HAL_OK)
                        {
                            HAL_Delay(2000);
                            printf("AT_CIPMUX......\n");
                            if(HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPSTART, sizeof(AT_CIPSTART), 0xFFFF)==HAL_OK)
                            {
                                HAL_Delay(5000);
                                    HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPSTART, sizeof(AT_CIPSTART), 0xFFFF);
                                    printf("AT_CIPSTART......\n");
                                    if(HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPMODE, sizeof(AT_CIPMODE), 0xFFFF)==HAL_OK)
                                    {
                                        HAL_Delay(2000);
                                        HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPMODE, sizeof(AT_CIPMODE), 0xFFFF);
                                        printf("AT_CIPMODE......\n");
                                        if(HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPSEND, sizeof(AT_CIPSEND), 0xFFFF)==HAL_OK)
                                        {
                                            HAL_Delay(2000);
                                            printf("AT_CIPSEND......\n");
                                            HAL_UART_Transmit(&huart1, (uint8_t *)finish, sizeof(finish), 0xFFFF);
                                        }
                                        else {
                                            do {
                                                printf("{");
                                                HAL_UART_Transmit(&huart1, (uint8_t *)AT_CIPSEND, sizeof(AT_CIPSEND), 0xFFFF);
                                                printf("}");
                                                HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPSEND, sizeof(AT_CIPSEND), 0xFFFF);
                                            } while (HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPSEND, sizeof(AT_CIPSEND), 0xFFFF)==HAL_OK);
                                        }

                                    }
                                    else {
                                        do {
                                            printf("{");
                                                HAL_UART_Transmit(&huart1, (uint8_t *)AT_CIPMODE, sizeof(AT_CIPMODE), 0xFFFF);
                                                printf("}");
                                            HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPMODE, sizeof(AT_CIPMODE), 0xFFFF);
                                        } while (HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPMODE, sizeof(AT_CIPMODE), 0xFFFF)==HAL_OK);
                                    }
                                }
                                else {
                                    do {
                                        printf("{");
                                                HAL_UART_Transmit(&huart1, (uint8_t *)AT_CIPSTART, sizeof(AT_CIPSTART), 0xFFFF);
                                                printf("}");
                                        HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPSTART, sizeof(AT_CIPSTART), 0xFFFF);
                                    }while(HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPSTART, sizeof(AT_CIPSTART), 0xFFFF)==HAL_OK);
                                }
                            }
                            else {
                                do {
                                    printf("{");
                                                HAL_UART_Transmit(&huart1, (uint8_t *)AT_CIPMUX, sizeof(AT_CIPMUX), 0xFFFF);
                                                printf("}");
                                    HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPMUX, sizeof(AT_CIPMUX), 0xFFFF);
                                } while (HAL_UART_Transmit(&huart2, (uint8_t *)AT_CIPMUX, sizeof(AT_CIPMUX), 0xFFFF)==HAL_OK);
                            }
                        }
                        else {
                            do {
                                printf("{");
                                                HAL_UART_Transmit(&huart1, (uint8_t *)AT_CWJAP_CUR, sizeof(AT_CWJAP_CUR), 0xFFFF);
                                                printf("}");
                                HAL_UART_Transmit(&huart2, (uint8_t *)AT_CWJAP_CUR, sizeof(AT_CWJAP_CUR), 0xFFFF);
                            } while (HAL_UART_Transmit(&huart2, (uint8_t *)AT_CWJAP_CUR, sizeof(AT_CWJAP_CUR), 0xFFFF)==HAL_OK);
                        }
                    }
                    else {
                        do {
                            printf("{");
                                                HAL_UART_Transmit(&huart1, (uint8_t *)AT_RST, sizeof(AT_RST), 0xFFFF);
                                                printf("}");
                            HAL_UART_Transmit(&huart2, (uint8_t *)AT_RST, sizeof(AT_RST), 0xFFFF);
                        }while(HAL_UART_Transmit(&huart2, (uint8_t *)AT_RST, sizeof(AT_RST), 0xFFFF)==HAL_OK);
                    }
                }
                else {
                    do {
                        printf("{");
                                                HAL_UART_Transmit(&huart1, (uint8_t *)AT, sizeof(AT), 0xFFFF);
                                                printf("}");
                        HAL_UART_Transmit(&huart2, (uint8_t *)AT, sizeof(AT), 0xFFFF);
                    }while(HAL_UART_Transmit(&huart2, (uint8_t *)AT, sizeof(AT), 0xFFFF)==HAL_OK);
                }

你可能感兴趣的:(STM32,物联网系统)