Estou criando um dispositivo que requer interface web e é assim que configuro o WiFi (inspirado no meu plugue inteligente):
- Abra o AP para que os dispositivos se conectem
- torne-se 6.6.6.6 para que os clientes possam enviar solicitações para esse IP
- cliente faz uma solicitação
- dispositivo tenta conectar WiFi como STA
- dispositivo responde à solicitação do cliente
Tenho encontrado um problema em que a conexão é reiniciada (httpd_txrx: httpd_sock_err: erro no envio: 113) quando o STA se conecta com sucesso a um WiFi, se não, funciona normalmente. Existe alguma maneira de consertar isso? Código para armazenar e responder à solicitação:
esp_err_t connect_post_handler(httpd_req_t *req)
{
size_t size = req->content_len;
if (size > 1024) {
// too big
const char* response = "Content too massive, but you know what else is massive?";
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, response);
return ESP_OK;
}
char content[1024];
int bytes_read = httpd_req_recv(req, content, size);
if (bytes_read == 0) {
// connection closed
return ESP_ERR_HTTPD_INVALID_REQ;
}
cJSON* jsonData = cJSON_ParseWithLength(content, size);
if (jsonData == NULL) {
// json parsing failed, bad request
const char* response = "Bad JSON format";
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, response);
return ESP_OK;
}
char* ssid = cJSON_GetObjectItem(jsonData, "ssid")->valuestring;
char* password = cJSON_GetObjectItem(jsonData, "password")->valuestring;
reconnect_callback(ssid, password);
httpd_req_async_handler_begin(req, &request); // <--- this!!!
cJSON_Delete(jsonData);
return ESP_OK;
}
void wifi_setup_handle_response(bool boolean, const char * response)
{
cJSON* response_data = cJSON_CreateObject();
cJSON_AddBoolToObject(response_data, "success", boolean);
cJSON_AddStringToObject(response_data, "message", response);
char* response_string = cJSON_Print(response_data);
printf(response_string);
if (response_string == NULL) {
// failed
ESP_LOGE(LOG_TAG, "Could not write wifi connect response to string");
httpd_resp_send_500(request);
cJSON_Delete(response_data);
return;
}
httpd_resp_send(request, response_string, strlen(response_string));
httpd_req_async_handler_complete(request);
free(response_string);
cJSON_Delete(response_data);
}
httpd_req_t* request;
Existe alguma solução alternativa para isso ou essa é uma maneira ruim de configurar ou mesmo usar e eu deveria usar Bluetooth para configurar (e usar) em vez de WiFi?