#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include <esp_http_server.h>
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "esp_tls.h"
#include "nvs_flash.h"
#include "esp_system.h"
#include "freertos/event_groups.h"
static const char *TAG = "azure";
const int CONNECTED_BIT = BIT0;
#define EXAMPLE_WIFI_SSID "Wokwi-GUEST"
#define EXAMPLE_WIFI_PASS ""
EventGroupHandle_t wifi_event_group;
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
} else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
/* This is a workaround as ESP platform WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
}
}
static void initialise_wifi(void)
{
ESP_ERROR_CHECK( esp_netif_init() );
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK( esp_event_loop_create_default() );
esp_netif_t *sta_netif = esp_netif_create_default_wifi_sta();
assert(sta_netif);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
wifi_config_t wifi_config = {
.sta = {
.ssid = EXAMPLE_WIFI_SSID,
.password = EXAMPLE_WIFI_PASS,
},
};
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
#ifdef CONFIG_IDF_TARGET_ESP8266 || (ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(4, 0, 0))
#else
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
#endif
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
}
esp_err_t test_post_handler(httpd_req_t *req) {
char buf[100]; // Buffer to store received data
int ret, remaining = req->content_len; // Variables for data processing
if (remaining <= 0) {
// No data received
httpd_resp_send_err(req, HTTPD_400_BAD_REQUEST, "No data received");
return ESP_FAIL;
}
while (remaining > 0) {
// Read data from the request body into the buffer
if ((ret = httpd_req_recv(req, buf, sizeof(buf))) <= 0) {
if (ret == HTTPD_SOCK_ERR_TIMEOUT) {
// Retry receiving data if a timeout occurs
continue;
}
return ESP_FAIL; // Return error if data reception fails
}
remaining -= ret; // Update the remaining data length
buf[ret] = '\0'; // Null-terminate received data
// Process the received data (e.g., store it, parse it, etc.)
// In this example, let's print it to the console
printf("Received data: %s\n", buf);
}
// Send a response back to the client
httpd_resp_send(req, "Received POST request", HTTPD_RESP_USE_STRLEN);
return ESP_OK; // Return success
}
// Define the HTTP server setup function
void setup_http_server() {
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_handle_t server = NULL;
if (httpd_start(&server, &config) == ESP_OK) {
// Server setup successful, register URI handler
httpd_uri_t test_uri = {
.uri = "/test",
.method = HTTP_POST,
.handler = test_post_handler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &test_uri);
printf(server);
} else {
printf("Error starting HTTP server\n");
}
}
void app_main() {
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK( ret );
initialise_wifi();
printf("Hello, Wokwi!\n");
setup_http_server();
}