New to the esp chip. I have been trying to get the esp32 to work as an acess point. However, I cant get it to work correctly.
The esp will broadcast the network, but when devices try connect they fail most of the time with an error saying that they could not retrieve an IP-adress. I have tried to enable the DHCP server with tcpip_adapter_start.
What am I missing?
Code: Select all
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "lwip/ip_addr.h"
#include "common.h"
#include "esp_event.h"
#include "driver/gpio.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include <esp_event.h>
#include <esp_event_loop.h>
#include <esp_log.h>
#include <esp_system.h>
#include <esp_wifi.h>
#include <freertos/FreeRTOS.h>
#include <nvs_flash.h>
/**
* An ESP32 WiFi event handler.
* The types of events that can be received here are:
*
* SYSTEM_EVENT_AP_PROBEREQRECVED
* SYSTEM_EVENT_AP_STACONNECTED
* SYSTEM_EVENT_AP_STADISCONNECTED
* SYSTEM_EVENT_AP_START
* SYSTEM_EVENT_AP_STOP
* SYSTEM_EVENT_SCAN_DONE
* SYSTEM_EVENT_STA_AUTHMODE_CHANGE
* SYSTEM_EVENT_STA_CONNECTED
* SYSTEM_EVENT_STA_DISCONNECTED
* SYSTEM_EVENT_STA_GOT_IP
* SYSTEM_EVENT_STA_START
* SYSTEM_EVENT_STA_STOP
* SYSTEM_EVENT_WIFI_READY
*/
static esp_err_t esp32_wifi_eventHandler(void *ctx, system_event_t *event)
{
return ESP_OK;
}
// Main application
void app_main()
{
printf("Starting up!");
fflush(stdout);
tcpip_adapter_init();
tcpip_adapter_ip_info_t ipInfo;
IP4_ADDR(&ipInfo.ip, 192,168,1,99);
IP4_ADDR(&ipInfo.gw, 192,168,1,1);
IP4_ADDR(&ipInfo.netmask, 255,255,255,0);
unsigned char mac = 0;
tcpip_adapter_start(TCPIP_ADAPTER_IF_AP, &mac, &ipInfo);
esp_event_loop_init(esp32_wifi_eventHandler, NULL);
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
esp_wifi_init(&cfg);
esp_wifi_set_storage(WIFI_STORAGE_RAM);
esp_wifi_set_mode(WIFI_MODE_AP);
wifi_config_t apConfig = {
.ap = {
.ssid="testing",
.ssid_len=0,
.password="1234",
.channel=0,
.authmode=WIFI_AUTH_WPA2_PSK,
.ssid_hidden=0,
.max_connection=4,
.beacon_interval=100
}
};
}