No tienes acceso a esta clase

¡Continúa aprendiendo! Únete y comienza a potenciar tu carrera

Servidor HTTP básico con ESP32

27/30
Recursos

Aportes 8

Preguntas 2

Ordenar por:

¿Quieres ver más aportes, preguntas y respuestas de la comunidad?

no olvidar agregar al cmakelist de este nuevo proyecto set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)

Hola 😃

Aquí mi resultado

No voy a mentir, me emocioné un montón al ver ese Hola Mundo. Llevo muchos años desarrollando web pero hacer un servidor en algo tan pequeño, algo iot me generó mucha felicidad.

Gran clase!

Este es mi ejercicio
.
.

.
.

************************************************************
IP       : 192.168.197.252
 Net mask : 255.255.255.0
 Gateway  : 192.168.197.222
 I (10944) main_task: Returned from app_main()
I (11054) wifi:<ba-add>idx:1 (ifx:0, 36:4f:fc:11:3f:28), tid:6, ssn:2, winSize:64


.
Este es el codigo del programa:
.

#include <stdio.h>
#include "string.h"
#include "esp_wifi.h"
#include "nvs_flash.h"
#include "protocol_examples_common.h"
#include "esp_http_server.h"
#include "pagina.h"

esp_netif_ip_info_t ip_info;
esp_netif_t* netif = NULL;

static esp_err_t home_get_handler(httpd_req_t *req)
{
    httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
    return ESP_OK;
};

static const httpd_uri_t home = {
    .uri = "/",
    .method = HTTP_GET,
    .handler = home_get_handler
};

void web_server_init()
{
    httpd_handle_t server = NULL;
    httpd_config_t config = HTTPD_DEFAULT_CONFIG();
    
    if (httpd_start(&server, &config) == ESP_OK)
    {
        httpd_register_uri_handler(server, &home);
        return;
    } 
    else 
    {
        printf("Error al iniciar el servidor\n");
    };

};



void app_main(void)
{
    nvs_flash_init();

    esp_netif_init();

    esp_event_loop_create_default();
 
    example_connect();

    netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF");

    if (netif == NULL)
    {
        printf("No hay interfaz\n");
    }
    else 
    {
        esp_netif_get_ip_info(netif, &ip_info);
        printf("************************************************************\n");
        printf("IP       : %d.%d.%d.%d\n ", IP2STR(&ip_info.ip));
        printf("Net mask : %d.%d.%d.%d\n ", IP2STR(&ip_info.netmask));
        printf("Gateway  : %d.%d.%d.%d\n ", IP2STR(&ip_info.gw));

        web_server_init();
    };
     
}

Hay que hacer dos cosas para este nuevo proyecto
.

  1. agregar en el CMakeLists.txt del directorio raiz de proyecto:

set(EXTRA_COMPONENT_DIRS $ENV{IDF_PATH}/examples/common_components/protocol_examples_common)
.
2. correr el comando

idf.py menuconfig 

y modificar en Example Connection Configuration el valor del SSID y el de password.

**Aquí les comparto mi código y una imagen de la respuesta del web server:** ![](<\\\</a\>>)![](https://i.imgur.com/kSNm4d9l.jpg) ```js //incluimos las librerias necesarias #include <stdio.h> #include <string.h> #include "esp_wifi.h" #include "nvs_flash.h" #include "protocol_examples_common.h" #include "esp_http_server.h" #include "esp_log.h" //prototipos de funciones static void esp_logs(esp_err_t* err, char* msg); static void web_server_init(void); static esp_err_t api_get_handler(httpd_req_t* req); //declaramos las variables globales static const char* TAG = "ESP32"; esp_err_t err; static const httpd_uri_t api = { .uri = "/api", .method = HTTP_GET, .handler = api_get_handler }; void app_main(void) { ESP_LOGI(TAG, "Start Application!"); //inicializamos el driver de NVS err = nvs_flash_init(); esp_logs(&err, "NVS driver initialization"); esp_netif_init(); esp_event_loop_create_default(); example_connect(); esp_netif_ip_info_t ip_info; esp_netif_t* netif = NULL; netif = esp_netif_get_handle_from_ifkey("WIFI_STA_DEF"); if (netif != NULL) { esp_netif_get_ip_info(netif, &ip_info); ESP_LOGI(TAG, "IP info: %d.%d.%d.%d", IP2STR(&ip_info.ip)); web_server_init(); } else { ESP_LOGE(TAG, "No exist WiFi interface"); } } //------------------------- static void esp_logs(esp_err_t* err, char* msg) { if (*err == ESP_OK) ESP_LOGI(TAG, "%s done.", msg); else ESP_LOGE(TAG, "%s fail!!", msg); } //------------------------- static void web_server_init(void) { httpd_handle_t server = NULL; httpd_config_t config = HTTPD_DEFAULT_CONFIG(); //inicializamos el servidor err = httpd_start(&server, &config); esp_logs(&err, "HTTP Server start"); if (err == ESP_OK) { httpd_register_uri_handler(server, &api); } } //------------------------- static esp_err_t api_get_handler(httpd_req_t* req) { char resp[100] = ""; sprintf(resp, "%s%s", "

CURSO PLATZI IOT CON ESP32

", "

Hola Mundo desde ESP32 Server

"); httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN); return ESP_OK; } //------------------------- ```
pregunta es sobre servidores y mi cuestión era donde usar http y donde usar mqtt lo que pasa que estuve viendo sobre el protocolo mqtt para la domótica y de verdad no entiendo en que casos usar cada uno

.
.
este es el codigo que implementa el html de la pagina web.
.
utilizo para incluir el html en una variable tipo char algo llamado raw string:
.
.

Basically a raw string literal is a string in which the escape characters (like \n \t or \" ) of C++ are not processed. A raw string literal which starts with R"( and ends in )" ,introduced in C++11

.
aqui esta el codigo de pagina
.

utilizo el SVG para incluir el logo de platzi