access point and web server
Posted: Tue Nov 16, 2021 9:25 am
i am trying to create a simple web server using access point. but code doesnt work and i am stuck. is there any one who can tell me why this code doesnt work. i guess i have problem at root_get_handler section. but why i cant find anything working, i saw who wanna work with idf not arduino, most of them facing same problem with me and there is no any working example, just arduino always
Code: Select all
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sys.h"
#include <sys/param.h>
#include "nvs_flash.h"
#include "esp_netif.h"
#include "esp_eth.h"
#include <esp_http_server.h>
#include <stdbool.h>
#include <string.h>
#include "esp_event.h"
#include "sdkconfig.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_log.h"
#include "driver/timer.h"
#include "esp_event.h"
#define AP_SSID "EMRE"
#define AP_PASS "12345678"
#define AP_CHANNEL 0
#define AP_MAX_CONN 4
static const char *TAG1 = "AccessPoint";
static const char *TAG2 = "start_webserver";
static const char *TAG3 = "root_get_handler";
static const char *TAG4 = "connect_handler";
static const char *TAG5 = "app_main";
/* ----------------------NOT ENTERING THIS SECTION---------------------- */
static esp_err_t root_get_handler(httpd_req_t *req)
{
ESP_LOGI(TAG3, " is working");
esp_err_t error;
const char* resp_str = (const char*) req->user_ctx;
error = httpd_resp_send(req, resp_str, strlen(resp_str));
if (error != ESP_OK) {
ESP_LOGI(TAG3, "ERROR %d httpd_resp_send",error);
}
else ESP_LOGI(TAG3, "httpd_resp_send SUCCESS");
return ESP_OK;
}
/* ---------------------NOT ENTERING THIS SECTION---------------------- */
static const httpd_uri_t root = {
.uri = "/webserver",
.method = HTTP_GET,
.handler = root_get_handler,
.user_ctx = "EMRE"
};
static httpd_handle_t start_webserver(void)
{
httpd_handle_t server = NULL;
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.lru_purge_enable = true;
esp_err_t ret = httpd_start(&server, &config);
if (ESP_OK != ret) {
ESP_LOGI(TAG2, "Error httpd_start!");
}
else ESP_LOGI(TAG2, "httpd_start SUCCESS");
// Set URI handlers
ESP_LOGI(TAG2, "Registering URI handlers");
ret = httpd_register_uri_handler(server, &root);
if (ret != ESP_OK) {
ESP_LOGI(TAG2, "ERROR %d httpd_register_uri_handler",ret);
}
else ESP_LOGI(TAG2, "httpd_register_uri_handler SUCCESS");
return server;
}
static void connect_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
ESP_LOGI(TAG4, " is working");
httpd_handle_t* server = (httpd_handle_t*) arg;
if (*server == NULL) {
*server = start_webserver();
}
}
//------------------------------------------------------------------------------------------------------------
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
ESP_LOGI(TAG1, "station "MACSTR" join, AID=%d",
MAC2STR(event->mac), event->aid);
} else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
ESP_LOGI(TAG1, "station "MACSTR" leave, AID=%d",
MAC2STR(event->mac), event->aid);
}
}
void wifi_init_softap(void)
{
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_ap();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT,
ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
wifi_config_t ap_config = {
.ap = {
.ssid = AP_SSID,
.password = AP_PASS,
.channel = AP_CHANNEL,
.max_connection = AP_MAX_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.ssid_hidden = 0,
},
};
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
ESP_ERROR_CHECK(esp_wifi_start());
}
//------------------------------------------------------------------------------------------------------------
void app_main(void)
{
static httpd_handle_t server = NULL;
ESP_ERROR_CHECK(nvs_flash_init());
wifi_init_softap();
esp_err_t error;
error = esp_event_handler_register(IP_EVENT, IP_EVENT_AP_STAIPASSIGNED, &connect_handler, &server);
if (error != ESP_OK) {
ESP_LOGI(TAG5, "ERROR %d esp_event_handler_register",error);
}
else ESP_LOGI(TAG5, "esp_event_handler_register SUCCESS");
}