Page 1 of 1

WIFI IN AP MODE IMPLEMENTATION ISSUE

Posted: Fri Jul 28, 2023 7:49 am
by nyameaama
Hi, so I am trying to write an implementation for Wifi in AP mode.
Her is the code

_broadcast.h
#ifndef BROADCASTED_SERVER_H
#define BROADCASTED_SERVER_H

#include <esp_http_server.h>

class BroadcastedServer {
public:
BroadcastedServer();
void begin();

private:
static esp_err_t testHandler(httpd_req_t *req);
httpd_handle_t server;
};

#endif

_broadcast.cpp
#include "_broadcast.h"
#include <string.h>
#include <esp_log.h>
#include "esp_wifi.h"
#include "esp_netif.h"
#include "nvs_flash.h" // Add this header for NVS related functions


BroadcastedServer::BroadcastedServer() : server(NULL) {}

void BroadcastedServer::begin() {
// Initialize the NVS
ESP_ERROR_CHECK(nvs_flash_erase());
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
// If NVS storage was not initialized yet, erase it and initialize again
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);

// Initialize the Wi-Fi module
ESP_ERROR_CHECK(esp_netif_init());
esp_netif_t *ap_netif = esp_netif_create_default_wifi_ap();
assert(ap_netif);
wifi_init_config_t wifi_cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&wifi_cfg));

// Configure the ESP32 as an AP
//esp_netif_create_default_wifi_ap(); // Create a default WiFi AP netif

ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_NULL));
ESP_ERROR_CHECK(esp_wifi_start());

wifi_config_t ap_config = {
.ap = {
.ssid = "HIVE",
.password = "LMHIVE2DRONE",
.authmode = WIFI_AUTH_OPEN,
.max_connection = 1, // Set the maximum number of connections allowed
},
};

ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &ap_config));
// Start the HTTP server
/*httpd_config_t config = HTTPD_DEFAULT_CONFIG();
if (httpd_start(&server, &config) == ESP_OK) {
httpd_uri_t testUri = {
.uri = "/test",
.method = HTTP_GET,
.handler = testHandler,
.user_ctx = NULL
};
httpd_register_uri_handler(server, &testUri);
}*/
}

esp_err_t BroadcastedServer::testHandler(httpd_req_t *req) {
const char resp[] = "<h1>Hello World</h1>";
httpd_resp_send(req, resp, HTTPD_RESP_USE_STRLEN);
return ESP_OK;
}

I am able to build and flash to the esp32 and the network is created and shows on other devices. However when I try to connect on a client it is stuck on loading and the output from the esp 32 is:

I (3011) wifi:mode : softAP (ec:62:60:76:2c:8d)
I (3011) wifi:Total power save buffer number: 16
I (3011) wifi:Init max length of beacon: 752/752
I (3011) wifi:Init max length of beacon: 752/752
E (3011) wifi:failed to post WiFi event=12 ret=259
E (3021) wifi:failed to post WiFi event=13 ret=259
I (3021) wifi:Total power save buffer number: 16
E (3031) wifi:failed to post WiFi event=12 ret=259
I (3031) main_task: Returned from app_main()
I (7211) wifi:new:<1,0>, old:<1,1>, ap:<1,1>, sta:<255,255>, prof:1
I (7211) wifi:station: c6:81:73:28:c4:5e join, AID=1, bgn, 20
E (7211) wifi:failed to post WiFi event=14 ret=259
I (7501) wifi:<ba-add>idx:2 (ifx:1, c6:81:73:28:c4:5e), tid:0, ssn:0, winSize:64

What am I doing wrong?. Thanks