为什么代码里面写的at指令连接esp826601s就是失败,但是手动输入aT指令可以连接成功,代码哪里有问题啊

main.c:
#include "delay.h"
#include "sys.h"
#include "Serial.h"
#include "stdio.h"
#include "esp8266.h"
#include "timer.h"
// GPIO??????
void GPIO_Configuration(void);
// ????
int i = 0, j = 0;
char a[] = "LightSwitch";
int main(void)
{
delay_init(); // ???????
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // ??NVIC?????2
// ?????
Serial1_Init(); // ?????1,?????????????
Serial1_SendString("Serial1 initialized\r\n");
Serial2_Init(); // ?????2,???ESP8266??
Serial1_SendString("Serial2 initialized\r\n");
TIM3_Int_Init(10 - 1, 7200 - 1);
Serial1_SendString("TIM3 initialized\r\n");
Serial1_SendString("The program is running and trying to connect to WiFi and Aliyun002.\r\n");
// ???ESP8266
ESP8266_Init(); // ESP8266???
GPIO_Configuration();
while (1)
{
if (Time_1ms % 10 == 0)
{
ESP8266_Received(post_name);
if (Property_Data[0] == '1')
{
PCout(13) = 0; // ??LED
}
else if (Property_Data[0] == '0')
{
PCout(13) = 1; // ??LED
}
}
// ???2???????????1
if (Serial2_GetRxFlag())
{
char received_data = Serial2_GetRxData();
Serial1_SendByte(received_data);
}
}
}
// GPIO????
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
// LED -> PC13
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
//******************************************************************************************************
esp8266.c:
#include "esp8266.h"
#include "Serial.h"
#include "string.h"
#include "delay.h"
#include <stdio.h>
unsigned char Property_Data[2];
// ESP8266?????
void ESP8266_Init(void)
{
char wifi_cmd[64]; // WiFi????
char pub_cmd[128]; // MQTT????
// 1. ????AT??
Serial1_SendString("Sending basic AT commands...\r\n");
if (esp8266_send_cmd("AT", "OK", 50) == 0) {
Serial1_SendString("AT command successful.\r\n");
PCout(13) = 0; // ??LED????
} else {
Serial1_SendString("AT command failed.\r\n");
PCout(13) = 1; // ??LED????
return;
}
if (esp8266_send_cmd("AT+GMR", "OK", 50) == 0) {
Serial1_SendString("AT+GMR command successful.\r\n");
} else {
Serial1_SendString("AT+GMR command failed.\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 2. ?????ESP8266
Serial1_SendString("Resetting and restoring ESP8266...\r\n");
if (esp8266_send_cmd("AT+RST", "ready", 200) == 0) {
Serial1_SendString("AT+RST command successful.\r\n");
delay_ms(2000); // ??????
} else {
Serial1_SendString("AT+RST command failed.\r\n");
PCout(13) = 1; // ??LED????
return;
}
if (esp8266_send_cmd("AT+RESTORE", "OK", 100) == 0) {
Serial1_SendString("AT+RESTORE command successful.\r\n");
} else {
Serial1_SendString("AT+RESTORE command failed.\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 3. ??WiFi??
Serial1_SendString("Setting WiFi mode...\r\n");
if (esp8266_send_cmd("AT+CWMODE=1", "OK", 50) == 0) {
Serial1_SendString("WiFi mode set successfully.\r\n");
} else {
Serial1_SendString("Failed to set WiFi mode!\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 4. ??NTP
Serial1_SendString("Configuring NTP...\r\n");
if (esp8266_send_cmd("AT+CIPSNTPCFG=1,8,\"ntp1.aliyuncs.com\"", "OK", 100) == 0) {
Serial1_SendString("NTP configured successfully.\r\n");
} else {
Serial1_SendString("Failed to configure NTP!\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 5. ??WiFi
Serial1_SendString("Connecting to WiFi...\r\n");
sprintf(wifi_cmd, "AT+CWJAP=\"%s\",\"%s\"", WIFI_Name, WIFI_Pass);
if (esp8266_send_cmd(wifi_cmd, "GOT IP", 500) == 0) {
Serial1_SendString("Connected to WiFi successfully.\r\n");
} else {
Serial1_SendString("Failed to connect to WiFi!\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 6. ??MQTT????
Serial1_SendString("Configuring MQTT user settings...\r\n");
if (esp8266_send_cmd(MQTT_set, "OK", 100) == 0) {
Serial1_SendString("MQTT user configuration successful.\r\n");
} else {
Serial1_SendString("Failed to set MQTT user configuration!\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 7. ??MQTT???ID
Serial1_SendString("Setting MQTT Client ID...\r\n");
if (esp8266_send_cmd(MQTT_Client, "OK", 100) == 0) {
Serial1_SendString("MQTT client ID set successfully.\r\n");
} else {
Serial1_SendString("Failed to set MQTT client ID!\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 8. ??MQTT???
Serial1_SendString("Connecting to MQTT server...\r\n");
if (esp8266_send_cmd(MQTT_Pass, "OK", 300) == 0) {
Serial1_SendString("Connected to MQTT server successfully.\r\n");
} else {
Serial1_SendString("Failed to connect to MQTT server!\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 9. ??MQTT??
Serial1_SendString("Subscribing to MQTT topic...\r\n");
if (esp8266_send_cmd(MQTT_Sub, "OK", 100) == 0) {
Serial1_SendString("Subscribed to MQTT topic successfully.\r\n");
} else {
Serial1_SendString("Failed to subscribe to MQTT topic!\r\n");
PCout(13) = 1; // ??LED????
return;
}
// 10. ??????
Serial1_SendString("Publishing test data...\r\n");
sprintf(pub_cmd, "AT+MQTTPUB=0,\"%s\",\"{\\\"params\\\":{\\\"Temperature\\\":10.9}}\",1,0", MQTT_Pub_Topic);
if (esp8266_send_cmd(pub_cmd, "OK", 100) == 0) {
Serial1_SendString("Test data published successfully.\r\n");
} else {
Serial1_SendString("Failed to publish test data!\r\n");
PCout(13) = 1; // ??LED????
}
Serial1_SendString("ESP8266 initialization complete!\r\n");
}
// ??AT?????????
u8 esp8266_send_cmd(char *cmd, char *ack, u16 waittime)
{
u8 res = 0;
uint16_t timeout = waittime;
static char response_buffer[256];
uint16_t response_index = 0;
char debug_buf[20]; // ???????
Serial1_SendString("Sending command via Serial2: ");
Serial1_SendString(cmd);
Serial1_SendString("\r\n");
// ??????
Serial2_ClearRxFlag();
// ????
Serial2_SendString(cmd);
Serial2_SendByte('\r');
Serial2_SendByte('\n');
// ??????????????
delay_ms(200);
if (ack && waittime)
{
Serial1_SendString("Waiting for response: ");
Serial1_SendString(ack);
Serial1_SendString("\r\n");
while (--timeout)
{
delay_ms(10);
// ?????????
while (Serial2_GetRxFlag())
{
char data = Serial2_GetRxData();
if (response_index < sizeof(response_buffer) - 1)
{
response_buffer[response_index++] = data;
// ??????
sprintf(debug_buf, "%d", (uint16_t)data);
Serial1_SendString("Debug: Received character: ");
Serial1_SendByte(data);
Serial1_SendString(" (");
Serial1_SendString(debug_buf);
Serial1_SendString(")\r\n");
}
}
// ???????????
response_buffer[response_index] = '\0';
if (strstr(response_buffer, ack) != NULL)
{
Serial1_SendString("Command execution successful, response: ");
Serial1_SendString(response_buffer);
Serial1_SendString("\r\n");
return 0; // ??
}
}
// ????
Serial1_SendString("Timeout! Received data: ");
if (response_index > 0)
{
response_buffer[response_index] = '\0';
Serial1_SendString(response_buffer);
}
else
{
Serial1_SendString("No data received");
}
Serial1_SendString("\r\n");
Serial1_SendString("Timeout waiting for response: ");
Serial1_SendString(ack);
Serial1_SendString("\r\n");
res = 1; // ??
}
return res;
}
// ????????
u8 *esp8266_check_cmd(char *str)
{
char *strx = 0;
static char rx_buf[256];
static uint16_t rx_index = 0;
while (Serial2_GetRxFlag())
{
rx_buf[rx_index++] = Serial2_GetRxData();
if (rx_index >= sizeof(rx_buf) - 1)
{
rx_index = 0;
}
}
rx_buf[rx_index] = '\0';
strx = strstr(rx_buf, str);
return (u8 *)strx;
}
// ?ESP8266??????
void ESP8266_Send(char *property, int Data)
{
char cmd[128];
sprintf(cmd, "AT+MQTTPUB=0,\"%s\",\"{\\\"%s\\\":%d}\",1,0\r\n", post, property, Data);
Serial1_SendString("Sending data via Serial2: ");
Serial1_SendString(cmd);
Serial1_SendString("\r\n");
Serial2_SendString(cmd);
}
// ??ESP8266????
void ESP8266_Received(char *PRO)
{
unsigned char *ret = 0;
char *property = 0;
static char rx_buf[256];
static uint16_t rx_index = 0;
int i;
while (Serial2_GetRxFlag())
{
rx_buf[rx_index++] = Serial2_GetRxData();
if (rx_index >= sizeof(rx_buf) - 1)
{
rx_index = 0;
}
}
rx_buf[rx_index] = '\0';
if (PRO == NULL);
else
{
ret = (unsigned char *)rx_buf;
if (ret != 0)
{
property = strstr((const char *)ret, (const char *)PRO);
if (property != NULL)
{
for (i = 0; i < 2; i++) // ????
{
Property_Data[i] = *(property + 13 + i);
}
if (Property_Data[1] == '}') Property_Data[1] = 0;
}
}
}
rx_index = 0;
}//******************************************************************************************************
ep8266.h:
#ifndef __ESP8266_H
#define __ESP8266_H
#include "sys.h"
#define post "/k25hksEHYPH/Esp8266_01s_test1/user/Esp8266"
#define post_name "LightSwitch"
#define MQTT_Pub_Topic "/sys/k25hksEHYPH/Esp8266_01s_test1/thing/event/property/post"
#define MQTT_Sub "AT+MQTTSUB=0,\"/k25hksEHYPH/Esp8266_01s_test1/user/get\",1"
// MQTT??
#define MQTT_set "AT+MQTTUSERCFG=0,1,\"NULL\",\"Esp8266_01s_test1&k25hksEHYPH\",\"3f32fa17fff813571219b33d0108e22d8146debf999584b4950af4fbc5f8ad51\",0,0,\"\""
#define MQTT_Client "AT+MQTTCLIENTID=0,\"k25hksEHYPH.Esp8266_01s_test1|securemode=2\\,signmethod=hmacsha256\\,timestamp=1743766633388|\""
#define MQTT_Pass "AT+MQTTCONN=0,\"iot-06z00hc4k401f1h.mqtt.iothub.aliyuncs.com\",1883,1"
// WiFi??
#define WIFI_Name "OnePlus 8T"
#define WIFI_Pass "u8ufhfi8"
extern unsigned char Property_Data[]; // ???????
void ESP8266_Init(void);
u8 esp8266_send_cmd(char *cmd, char *ack, u16 waittime);
u8 *esp8266_check_cmd(char *str);
void ESP8266_Send(char *property, int Data);
void ESP8266_Received(char *PRO);
#endif
//*****************************************************************************************************
Serial.c:
#include "stm32f10x.h"
#include "Serial.h"
// 串口1相关变量
volatile uint8_t RxData1;
volatile uint8_t RxFlag1;
volatile uint8_t RxBuffer1[256];
volatile uint16_t RxIndex1 = 0;
// 串口2相关变量
volatile uint8_t RxData2;
volatile uint8_t RxFlag2;
volatile uint8_t RxBuffer2[256];
volatile uint16_t RxIndex2 = 0;
// 串口1初始化函数
void Serial1_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
// 使能时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |
RCC_APB2Periph_USART1 |
RCC_APB2Periph_AFIO, ENABLE);
// GPIO配置
// TX (PA9) 复用推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// RX (PA10) 浮空输入
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// USART参数配置
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStruct);
// 中断配置
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// 使能串口
USART_Cmd(USART1, ENABLE);
}
// 串口2初始化函数
void Serial2_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
NVIC_InitTypeDef NVIC_InitStruct;
// 使能时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
// GPIO配置
// TX (PA2) 复用推挽输出
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// RX (PA3) 浮空输入
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStruct);
// USART参数配置
USART_InitStruct.USART_BaudRate = 115200;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Parity = USART_Parity_No;
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2, &USART_InitStruct);
// 中断配置
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStruct);
// 使能串口
USART_Cmd(USART2, ENABLE);
}
// 串口1中断服务函数
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE))
{
RxData1 = USART_ReceiveData(USART1);
RxBuffer1[RxIndex1++] = RxData1;
if (RxIndex1 >= sizeof(RxBuffer1))
{
RxIndex1 = 0;
}
RxFlag1 = 1;
// 将接收到的数据发送到串口2
Serial2_SendByte(RxData1);
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
}
}
// 串口2中断服务函数
void USART2_IRQHandler(void)
{
if (USART_GetITStatus(USART2, USART_IT_RXNE))
{
RxData2 = USART_ReceiveData(USART2);
RxBuffer2[RxIndex2++] = RxData2;
if (RxIndex2 >= sizeof(RxBuffer2))
{
RxIndex2 = 0;
}
RxFlag2 = 1;
USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
}
// 串口1发送单字节函数
void Serial1_SendByte(uint8_t Byte)
{
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_SendData(USART1, Byte);
}
// 串口2发送单字节函数
void Serial2_SendByte(uint8_t Byte)
{
while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
USART_SendData(USART2, Byte);
}
// 串口1发送字符串函数
void Serial1_SendString(char *String)
{
while (*String)
{
Serial1_SendByte(*String++);
}
}
// 串口2发送字符串函数
void Serial2_SendString(char *String)
{
while (*String)
{
Serial2_SendByte(*String++);
}
}
// 串口1获取接收标志函数
uint8_t Serial1_GetRxFlag(void)
{
if (RxFlag1)
{
RxFlag1 = 0;
return 1;
}
return 0;
}
// 串口2获取接收标志函数
uint8_t Serial2_GetRxFlag(void)
{
if (RxFlag2)
{
RxFlag2 = 0;
return 1;
}
return 0;
}
// 串口1获取接收数据函数
uint8_t Serial1_GetRxData(void)
{
if (RxIndex1 > 0)
{
return RxBuffer1[--RxIndex1];
}
return 0;
}
// 串口2获取接收数据函数
uint8_t Serial2_GetRxData(void)
{
if (RxIndex2 > 0)
{
return RxBuffer2[--RxIndex2];
}
return 0;
}
// 串口1清除接收标志函数
void Serial1_ClearRxFlag(void)
{
RxFlag1 = 0;
RxIndex1 = 0;
}
// 串口2清除接收标志函数
void Serial2_ClearRxFlag(void)
{
RxFlag2 = 0;
RxIndex2 = 0;
}
//***************************************************************************************************
serial.h:
#ifndef __SERIAL_H
#define __SERIAL_H
#include "stm32f10x.h"
// ??1????
extern volatile uint8_t RxData1;
extern volatile uint8_t RxFlag1;
extern volatile uint8_t RxBuffer1[256];
extern volatile uint16_t RxIndex1;
// ??2????
extern volatile uint8_t RxData2;
extern volatile uint8_t RxFlag2;
extern volatile uint8_t RxBuffer2[256];
extern volatile uint16_t RxIndex2;
// ????
void Serial1_Init(void);
void Serial2_Init(void);
void USART1_IRQHandler(void);
void USART2_IRQHandler(void);
void Serial1_SendByte(uint8_t Byte);
void Serial2_SendByte(uint8_t Byte);
void Serial1_SendString(char *String);
void Serial2_SendString(char *String);
uint8_t Serial1_GetRxFlag(void);
uint8_t Serial2_GetRxFlag(void);
uint8_t Serial1_GetRxData(void);
uint8_t Serial2_GetRxData(void);
void Serial1_ClearRxFlag(void);
void Serial2_ClearRxFlag(void);
#endif