diff --git a/Core/Src/esp_at.c b/Core/Src/esp_at.c
index 252af14..d7909d5 100644
--- a/Core/Src/esp_at.c
+++ b/Core/Src/esp_at.c
@@ -48,7 +48,6 @@ void ESP_ClearBuffer(void)
}
/*************************************************/
-
static bool ESP_WaitFor(const char *expect, uint32_t timeout)
{
uint32_t start = HAL_GetTick();
@@ -63,10 +62,12 @@ static bool ESP_WaitFor(const char *expect, uint32_t timeout)
if (strstr(esp_rx_buf, "busy"))
{
- HAL_Delay(100); // 🔥 ESP ocupado → espera
+ HAL_Delay(100);
ESP_ClearBuffer();
start = HAL_GetTick();
}
+
+ HAL_Delay(1);
}
return false;
@@ -167,31 +168,45 @@ bool ESP_CloseLink(int link_id)
}
/*************************************************/
-
void ESP_Init(void)
{
- HAL_Delay(1000);
- ESP_SendCmd("AT", "OK", 2000);
- ESP_SendCmd("ATE0", "OK", 2000);
- ESP_SendCmd("AT+CIPDINFO=1", "OK", 2000);
- ESP_SendCmd("AT+CWMODE=2", "OK", 2000);
- ESP_SendCmd("AT+CWSAP=\"STM32\",\"12345678\",1,3,4", "OK", 5000);
+ // 🔥 manda reset à bruta
+ ESP_SendData("AT+RST\r\n");
- HAL_Delay(3000); // 🔥 importante
+ dbg("RESET ESP...\r\n");
- ESP_SendCmd("AT+CIPSERVER=0", "OK", 2000); // mata server se existir
- // ESP_SendCmd("AT+CIPCLOSE=5", "OK", 2000); // fecha tudo (modo multi)
- HAL_Delay(500);
+ // 🔥 espera real (não confies no WaitFor)
+ HAL_Delay(6000);
+ ESP_ClearBuffer();
+
+ dbg("ESP PRONTO\r\n");
+
+ // 🔥 agora sim começa
+ if (!ESP_SendCmd("AT", "OK", 3000))
+ dbg("AT FAIL\r\n");
+
+ if (!ESP_SendCmd("ATE0", "OK", 3000))
+ dbg("ATE0 FAIL\r\n");
+
+ if (!ESP_SendCmd("AT+CIPDINFO=1", "OK", 3000))
+ dbg("CIPDINFO FAIL\r\n");
+
+ if (!ESP_SendCmd("AT+CWMODE=2", "OK", 3000))
+ dbg("CWMODE FAIL\r\n");
+
+ if (!ESP_SendCmd("AT+CWSAP=\"STM32\",\"12345678\",1,3,4", "OK", 8000))
+ dbg("CWSAP FAIL\r\n");
+
+ HAL_Delay(3000);
if (!ESP_SendCmd("AT+CIPMUX=1", "OK", 3000))
dbg("CIPMUX FAIL\r\n");
- if (!ESP_SendCmd("AT+CIPSERVER=1,80", "OK", 3000))
+ if (!ESP_SendCmd("AT+CIPSERVER=1,80", "OK", 5000))
dbg("CIPSERVER FAIL\r\n");
}
-
/*************************************************/
__attribute__((unused))
static int parse_ipd_link(const char *p)
@@ -204,40 +219,38 @@ static int parse_ipd_link(const char *p)
/*************************************************/
void ESP_Task(void)
{
- char *p = strstr(ESP_GetBuffer(), "+IPD,");
- if (p)
+ char *buf = ESP_GetBuffer();
+
+ if (!strstr(buf, "GET "))
+ return;
+
+ dbg("HTTP DETECTADO\r\n");
+
+ // 🔥 ESPERAR O RESTO DO HTTP CHEGAR
+ HAL_Delay(200);
+
+ int link_id = 0;
+
+ const char *resp =
+ "HTTP/1.1 200 OK\r\n"
+ "Content-Type: text/html\r\n"
+ "Connection: close\r\n"
+ "\r\n"
+ "
PORTAL XUPA FUNCIONA
";
+
+ char cmd[64];
+
+ snprintf(cmd, sizeof(cmd), "AT+CIPSEND=%d,%u",
+ link_id, (unsigned)strlen(resp));
+
+ if (ESP_SendCmd(cmd, ">", 3000))
{
- int link_id = -1;
- int len = 0;
-
- // extrair link_id e tamanho
- sscanf(p, "+IPD,%d,%d:", &link_id, &len);
-
- dbg("IPD DETECTADO\r\n");
-
- // 🔥 esperar fim do pacote
- HAL_Delay(50);
-
- const char *resp =
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/html\r\n"
- "Connection: close\r\n"
- "\r\n"
- "PORTAL XUPA FUNCIONA
";
-
- char cmd[64];
-
- snprintf(cmd, sizeof(cmd), "AT+CIPSEND=%d,%d", link_id, strlen(resp));
-
- if (ESP_SendCmd(cmd, ">", 2000))
- {
- ESP_SendData(resp);
- ESP_WaitFor("SEND OK", 3000);
- }
-
- snprintf(cmd, sizeof(cmd), "AT+CIPCLOSE=%d", link_id);
- ESP_SendCmd(cmd, "OK", 2000);
-
- ESP_ClearBuffer(); // 🔥 só aqui
+ ESP_SendData(resp);
+ ESP_WaitFor("SEND OK", 5000);
}
+
+ snprintf(cmd, sizeof(cmd), "AT+CIPCLOSE=%d", link_id);
+ ESP_SendCmd(cmd, "OK", 3000);
+
+ ESP_ClearBuffer();
}
diff --git a/Core/Src/main.c b/Core/Src/main.c
index 44a356a..bd4bd54 100644
--- a/Core/Src/main.c
+++ b/Core/Src/main.c
@@ -37,7 +37,7 @@
UART_HandleTypeDef huart1; // ESP
UART_HandleTypeDef huart3; // debug
-static uint8_t esp_rx_byte;
+uint8_t esp_rx_byte;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
@@ -91,99 +91,92 @@ volatile int esp_rx_idx = 0;
volatile uint8_t http_request_flag = 0;
volatile int http_link_id = -1;
+/* USER CODE BEGIN 0 */
+
+
+
+// 👇 METE AQUI
+static int parse_link_id(const char *buf)
+{
+ char *p = strstr(buf, "+IPD,");
+ if (!p) return -1;
+
+ int id = -1;
+ sscanf(p, "+IPD,%d,", &id);
+ return id;
+}
+
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if (huart->Instance == USART1)
{
- // guarda TUDO (sem filtros marados)
- if (esp_rx_idx < ESP_RX_BUF_SIZE - 1)
- {
- esp_rx_buf[esp_rx_idx++] = esp_rx_byte;
- esp_rx_buf[esp_rx_idx] = 0;
- }
- else
- {
- // buffer cheio → reset suave
- esp_rx_idx = 0;
- }
+ ESP_RxByte(esp_rx_byte);
HAL_UART_Receive_IT(&huart1, &esp_rx_byte, 1);
}
}
-
-
-
static void Portal_SendHello(int link_id)
{
- char cmd[32];
+ char cmd[64];
const char *resp =
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/html\r\n"
- "Connection: close\r\n"
- "\r\n"
- ""
- "WiFi Setup"
- ""
- "Configurar WiFi
"
- ""
- ""
- "";
+ "HTTP/1.1 200 OK\r\n"
+ "Content-Type: text/html\r\n"
+ "Connection: close\r\n"
+ "\r\n"
+ ""
+ "WiFi Setup"
+ ""
+ "Configurar WiFi
"
+ ""
+ ""
+ "";
int len = strlen(resp);
- // 🔥 limpar buffer ANTES
ESP_ClearBuffer();
esp_rx_idx = 0;
- // 🔥 pedir envio
snprintf(cmd, sizeof(cmd), "AT+CIPSEND=%d,%d\r\n", link_id, len);
ESP_SendData(cmd);
- // 🔥 esperar pelo '>'
uint32_t t0 = HAL_GetTick();
- while (1)
+ while ((HAL_GetTick() - t0) < 3000)
{
if (strstr(esp_rx_buf, ">"))
break;
if (strstr(esp_rx_buf, "ERROR"))
return;
-
- if (HAL_GetTick() - t0 > 2000)
- return;
}
- // 🔥 enviar HTML
+ ESP_ClearBuffer();
+ esp_rx_idx = 0;
+
ESP_SendData(resp);
- // 🔥 esperar SEND OK (opcional mas bom)
t0 = HAL_GetTick();
- while (1)
+ while ((HAL_GetTick() - t0) < 5000)
{
if (strstr(esp_rx_buf, "SEND OK"))
break;
if (strstr(esp_rx_buf, "ERROR"))
break;
-
- if (HAL_GetTick() - t0 > 2000)
- break;
}
- // 🔥 fechar ligação
snprintf(cmd, sizeof(cmd), "AT+CIPCLOSE=%d\r\n", link_id);
ESP_SendData(cmd);
- // 🔥 limpar para próxima
+ HAL_Delay(100);
+
ESP_ClearBuffer();
esp_rx_idx = 0;
- http_link_id = -1;
}
/* USER CODE END 0 */
@@ -240,62 +233,10 @@ int main(void)
while (1)
{
- static uint16_t last_idx = 0;
-
- // 🔍 DETECTAR HTTP
- if (!http_request_flag)
- {
- if (strstr(esp_rx_buf, "GET /"))
- {
- http_link_id = 0;
- http_request_flag = 1;
-
- HAL_UART_Transmit(&huart3,
- (uint8_t*)"HTTP DETECTADO\r\n",
- 17,
- 100);
-
- // 🔥 limpa logo
- esp_rx_idx = 0;
- memset(esp_rx_buf, 0, ESP_RX_BUF_SIZE);
- last_idx = 0;
- }
- }
-
- // 🖥 DEBUG (só quando muda)
- if (esp_rx_idx != last_idx)
- {
- HAL_UART_Transmit(&huart3,
- (uint8_t*)esp_rx_buf,
- esp_rx_idx,
- 1000);
-
- HAL_UART_Transmit(&huart3, (uint8_t*)"\r\n", 2, 100);
-
- last_idx = esp_rx_idx;
- }
-
- // 🌐 RESPONDER
- if (http_request_flag)
- {
- http_request_flag = 0;
-
- HAL_Delay(50); // 🔥 importantíssimo
-
- Portal_SendHello(http_link_id);
-
- // 🔥 limpa depois
- esp_rx_idx = 0;
- memset(esp_rx_buf, 0, ESP_RX_BUF_SIZE);
- last_idx = 0;
- }
+ ESP_Task(); // só tratar RX + parser HTTP
HAL_Delay(1);
}
-
-
-
-
/* USER CODE END 3 */
}