I'm using two ESP32.
- ESP32-C3 behaves as a webserver and access point. If I make an HTTP get request (using a web browser or TestMace) I can read the html page content.
- ESP32 behaves as a client and it needs to connect itself to the ESP32-C3. I connects correctly to the ESP32-C3 and it makes a good HTTP get request (it returns status 200).
It is sure I'm making something wrong. However the example "esp_http_client" works well!
Piece of code on the server side:
Code: Select all
esp_err_t
file_server_init (const char * p_base_path)
{
esp_err_t ret = ESP_FAIL;
static file_server_data_t * p_server_data = NULL;
httpd_handle_t server = NULL;
// Per configurare correttamente la struttura di base, va sempre eseguito
// HTTPD_DEFAULT_CONFIG.
//
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
// Si deve fornire il percorso valido, il webserver supporta solo il
// percorso '/spiffs'.
//
if ((NULL == p_base_path) || (strcmp(p_base_path, "/spiffs") != 0))
{
ESP_LOGE(g_p_tag,
"File server presently supports only '/spiffs' as base path");
ret = ESP_ERR_INVALID_ARG;
}
// Verifica che il file server non sia già stato inizializzato.
//
else if (p_server_data != NULL)
{
ESP_LOGE(g_p_tag, "File server already started");
ret = ESP_ERR_INVALID_STATE;
}
else
{
// Alloca memoria per la struttura del file server.
//
p_server_data = calloc(1, sizeof(file_server_data_t));
if (NULL == p_server_data)
{
ESP_LOGE(g_p_tag, "Failed to allocate memory for server data");
ret = ESP_ERR_NO_MEM;
}
else
{
strlcpy(p_server_data->base_path, p_base_path,
sizeof(p_server_data->base_path));
// Aumento il numero massimo di handler URI registrabili.
//
config.max_uri_handlers = 20;
/* Use the URI wildcard matching function in order to
* allow the same handler to respond to multiple different
* target URIs which match the wildcard scheme */
config.uri_match_fn = httpd_uri_match_wildcard;
ESP_LOGI(g_p_tag, "Starting HTTP Server on port: '%d'",
config.server_port);
// Fa partire il server web creando una istanza HTTP e allocando
// memoria e risorse per esso in base alla configurazione
// specificata.
//
if (httpd_start(&server, &config) != ESP_OK)
{
ESP_LOGE(g_p_tag, "Failed to start file server!");
ret = ESP_FAIL;
}
// Impostazione dell'apertura della pagina principale, attraverso la
// definizione dell'URI, del metodo URI, della funzione legata a tale URI.
//
else
{
httpd_uri_t file_partition = {
.uri = "/index",
.method = HTTP_GET,
.handler = new_page_handler,
.user_ctx = p_server_data
};
ESP_ERROR_CHECK(httpd_register_uri_handler(server, &file_partition));
ret = ESP_OK;
}
}
}
return ret;
} /* file_server_init() */
static esp_err_t
new_page_handler (httpd_req_t * p_req)
{
int data_read = 0;
char buffer[100];
esp_err_t ret = ESP_FAIL;
ESP_LOGI(g_p_tag, "example.html");
// Apertura di example.txt dalla SPIFFS.
//
FILE * h_file_handler = fopen("/spiffs/example.html", "r");
if (NULL == h_file_handler)
{
ESP_LOGE(g_p_tag, "Failed to open example.html");
ret = ESP_FAIL;
}
else
{
// Riempio buf di zeri (quindi 64 zeri).
//
memset(buffer, 0, sizeof(buffer));
httpd_resp_sendstr_chunk(p_req, "<!DOCTYPE html>");
do
{
// Metto gli oggetti nel buffer.
data_read = fread(buffer, 1, sizeof(buffer), h_file_handler);
httpd_resp_sendstr_chunk(p_req, buffer);
// Display the read contents from the file
ESP_LOGI(g_p_tag, "Read from example.html: %s", buffer);
} while (100 == data_read);
httpd_resp_sendstr_chunk(p_req, NULL);
// Chiude il file example.html.
//
fclose(h_file_handler);
ret = ESP_OK;
}
return ret;
} /* new_page_handler() */
Code: Select all
void
https_client_init (void)
{
// Configurazione di default per il client.
//
esp_http_client_config_t config = {
.host = "192.168.1.1",
.path = "/index",
.query = "esp",
.event_handler = client_event_handler,
.user_data = g_local_response_buffer,
.disable_auto_redirect = true,
.transport_type = HTTP_TRANSPORT_OVER_TCP,
};
g_http_client = esp_http_client_init(&config);
return;
} /* https_client_init() */
esp_err_t
https_client_connection (const char * data_in)
{
int data_len = 0;
esp_err_t ret = ESP_OK;
(void) esp_http_client_set_header(g_http_client, HTTP_HEADER_NAME,
HTTP_HEADER_VALUE);
(void) esp_http_client_set_url(g_http_client, data_in);
esp_http_client_set_method(g_http_client, HTTP_METHOD_GET);
ret = esp_http_client_perform(g_http_client);
// Azione da compiere in base alla risposta della chiamata per l'esecuzione
// delle azioni del client.
//
if (ret == ESP_OK)
{
ESP_LOGI(g_p_tag, "Status = %d, content_length = %d",
esp_http_client_get_status_code(g_http_client),
esp_http_client_get_content_length(g_http_client));
if (true == esp_http_client_is_chunked_response(g_http_client))
{
(void) esp_http_client_get_chunk_length(g_http_client, &data_len);
if (data_len != 0)
{
ESP_LOGI(g_p_tag, "Dati chunk presenti");
}
else
{
ESP_LOGI(g_p_tag, "Dati chunk nulli");
}
}
ESP_LOG_BUFFER_HEX(g_p_tag, g_local_response_buffer,
strlen(g_local_response_buffer));
}
else
{
ESP_LOGE(g_p_tag, "Error perform http request %s",
esp_err_to_name(ret));
}
return ret;
} /* https_client_connection() */