HTTP get and post method with esp32 | IoT procols
Pawan Meena
Founder - otamation | Consultant | Trainer | IoT & AI | Flutter | Industry 4.0 | Embedded Systems | Firmware & App Development | C | Python | CAN | ML | MQTT | Bluetooth | Microcontrollers | Circuit Design |
Connecting esp32 and other embedded systems with the internet is the base of IoT. Here I explained and provided some code to connect esp32 with esp32.
The HTTP protocol is used in many applications to set up local servers on many IoT devices locally.
The provided code sets up an HTTP server on the ESP32 that listens on port 80 and has two endpoints: "/getData" and "/post_endpoint". The function "hello_get_handler" is used to handle requests to both endpoints. It checks the URI of the request and runs different code depending on whether the request is for the "/getData" or "/post_endpoint" endpoint.
For the "/getData" endpoint, it sets the response status to "200 OK" and the response type to "text/plain" and sends an empty response to the client.
For the "/post_endpoint" endpoint, it receives data from the client and logs it to the console.
The function "setup_server()" is used to start the server and register the endpoints.
The function "startWiFi()" is used to connect the ESP32 to a wifi network using the provided SSID and password.
领英推荐
In the "app_main" function, it starts wifi and setup the server, it is the entry point for the application.
Note that you should replace "Your_SSID" and "Your_PASSWORD" with the actual values for your wifi network.
#include <esp_http_server.h
#include "esp_wifi.h"
#include "esp_log.h"
static const char *TAG = "HTTP Examples";
char received_data[1024];
sp_err_t hello_get_handler(httpd_req_t *req){
?
? // Receive data from the client?
? int received_len = httpd_req_recv(req, received_data, sizeof(received_data));?
? ESP_LOGI(TAG, "URL: %s", req->uri);?
? // Check if the request is for the "/getData" endpoint?
? if (strstr(req->uri, "/getData"))?
? {?
? ? ? // Run function on getData url calling?
? ? ? httpd_resp_set_status(req, "200 OK");?
? ? ? httpd_resp_set_type(req, "text/plain");?
? ? ? httpd_resp_send(req, NULL, 0); // Sends an empty response to the client?
? }?
? else if (strstr(req->uri, "/post_endpoint"))?
? {?
? ? ? ESP_LOGI(TAG, "Data received using post method");?
? ? ? char data_buffer[1024];?
? ? ? int ret = httpd_req_recv(req, data_buffer, sizeof(data_buffer));?
? ? ? ESP_LOGI(TAG, "DATA: %s", data_buffer);?
? }}
/
/ Register the "/getData" endpointh
ttpd_uri_t getData = {?
? .uri = "/getData",?
? .method = HTTP_GET,?
? .handler = hello_get_handler,?
? .user_ctx = NULL};/
/ Register the "/post_endpoint" endpointh
ttpd_uri_t post_uri = {?
? ? ? .uri = "/post_endpoint",?
? ? ? .method = HTTP_POST,?
? ? ? .handler = hello_get_handler, // Using the same handler function for both GET and POST requests?
? ? ? .user_ctx = NULL?
? };v
oid setup_server(){?
? // Initialize the HTTP server?
? httpd_handle_t server = NULL;?
? httpd_config_t config = HTTPD_DEFAULT_CONFIG();?
? config.server_port = 80;?
? config.ctrl_port = 32768;?
? httpd_start(&server, &config);?
? // Register the endpoints?
? httpd_register_uri_handler(server, &post_uri);?
? httpd_register_uri_handler(server, &getData);}
v
oid startWiFi(){
??
? wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();?
? esp_wifi_init(&wifi_init_config);?
? wifi_config_t wifi_config = {?
? ? ? .sta = {?
? ? ? ? ? .ssid = "Your_SSID",?
? ? ? ? ? .password = "Your_PASSWORD",?
? ? ? },?
? };?
? esp_wifi_set_mode(WIFI_MODE_STA);?
? esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config);?
? esp_wifi_start();}
v
oid app_main(void){s
etup_server();s
tartWiFi();}
ubscribe to this newsl etter for future updates.
In the next articles, Iiwill try to cover other protocols of IoT. Like MQTT and Websockets etc.