Recently I have been trying to setup softAP + captive portal. I am using Cornelis' example on Github - https://github.com/cornelis-61/esp32_Captdns
The hardware is a Sparkfun ESP32 thing.
The debug feedback on serial port is telling me that the softAP has been started, however I can't "see" the AP on my phone or laptop. I would really appreciate if someone could look at my code and debug output and see if they can spot something obviously wrong.
Code:
Code: Select all
* ----------------------------------------------------------------------------
* "THE BEER-WARE LICENSE" (Revision 42): Idea of Jeroen Domburg <jeroen@spritesmods.com>
*
* Cornelis wrote this file. As long as you retain
* this notice you can do whatever you want with this stuff. If we meet some day,
* and you think this stuff is worth it, you can buy me a beer in return.
* ----------------------------------------------------------------------------
*/
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_wifi_types.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "freertos/portmacro.h"
#include "freertos/event_groups.h"
#include "esp_log.h"
#include "tcpip_adapter.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/api.h"
#include "lwip/err.h"
#include "string.h"
#include "cJSON.h"
#include "lwip/dns.h"
#include "captdns.h"
#define LED_GPIO_PIN GPIO_NUM_4
#define LED_BUILTIN 16
#define delay(ms) (vTaskDelay(ms/portTICK_RATE_MS))
#define WIDIM_SSID "ESP32_TestAP"
#define WIDIM_PASS "testing123"
#define EXAMPLE_MAX_STA_CONN 4
static esp_err_t event_handler(void *ctx, system_event_t *event);
static void wifi_init_softap(void);
static EventGroupHandle_t wifi_event_group;
//static wifi_country_t wifi_country = {.cc="AU", .schan=1, .nchan=13, .policy=WIFI_COUNTRY_POLICY_AUTO};
static const char *TAG = "wifi softAP";
const int CONNECTED_BIT = BIT0;
char* json_unformatted;
const static char http_html_hdr[] =
"HTTP/1.1 200 OK\r\nContent-type: text/html\r\n\r\n";
const static char http_index_hml[] = "<!DOCTYPE html>"
"<html>\n"
"<head>\n"
" <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">\n"
" <style type=\"text/css\">\n"
" html, body, iframe { margin: 0; padding: 0; height: 100%; }\n"
" iframe { display: block; width: 100%; border: none; }\n"
" </style>\n"
"<title>HELLO ESP32</title>\n"
"</head>\n"
"<body>\n"
"<h1>Hello World, from ESP32!</h1>\n"
"</body>\n"
"</html>\n";
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_AP_START:
ESP_LOGI(TAG, "SYSTEM_EVENT_AP_START");
break;
case SYSTEM_EVENT_AP_STOP:
ESP_LOGI(TAG, "SYSTEM_EVENT_AP_STOP");
break;
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station:"MACSTR" join, AID=%d",
MAC2STR(event->event_info.sta_connected.mac),
event->event_info.sta_connected.aid);
break;
case SYSTEM_EVENT_AP_STADISCONNECTED:
ESP_LOGI(TAG, "station:"MACSTR"leave, AID=%d",
MAC2STR(event->event_info.sta_disconnected.mac),
event->event_info.sta_disconnected.aid);
break;
default:
break;
}
return ESP_OK;
}
static void
http_server_netconn_serve(struct netconn *conn)
{
struct netbuf *inbuf;
char *buf;
u16_t buflen;
err_t err;
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */
err = netconn_recv(conn, &inbuf);
if (err == ERR_OK) {
netbuf_data(inbuf, (void**)&buf, &buflen);
// strncpy(_mBuffer, buf, buflen);
/* Is this an HTTP GET command? (only check the first 5 chars, since
there are other formats for GET, and we're keeping it very simple )*/
printf("buffer = %s \n", buf);
if (buflen>=5 &&
buf[0]=='G' &&
buf[1]=='E' &&
buf[2]=='T' &&
buf[3]==' ' &&
buf[4]=='/' ) {
printf("buf[5] = %c\n", buf[5]);
/* Send the HTML header
* subtract 1 from the size, since we dont send the \0 in the string
* NETCONN_NOCOPY: our data is const static, so no need to copy it
*/
netconn_write(conn, http_html_hdr, sizeof(http_html_hdr)-1, NETCONN_NOCOPY);
if(buf[5]=='h') {
gpio_set_level(LED_BUILTIN, 0);
/* Send our HTML page */
netconn_write(conn, http_index_hml, sizeof(http_index_hml)-1, NETCONN_NOCOPY);
}
else if(buf[5]=='l') {
gpio_set_level(LED_BUILTIN, 1);
/* Send our HTML page */
netconn_write(conn, http_index_hml, sizeof(http_index_hml)-1, NETCONN_NOCOPY);
}
else if(buf[5]=='j') {
netconn_write(conn, json_unformatted, strlen(json_unformatted), NETCONN_NOCOPY);
}
else {
netconn_write(conn, http_index_hml, sizeof(http_index_hml)-1, NETCONN_NOCOPY);
}
}
}
/* Close the connection (server closes in HTTP) */
netconn_close(conn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
}
static void http_server(void *pvParameters)
{
struct netconn *conn, *newconn;
err_t err;
conn = netconn_new(NETCONN_TCP);
netconn_bind(conn, NULL, 80);
netconn_listen(conn);
do {
err = netconn_accept(conn, &newconn);
if (err == ERR_OK) {
http_server_netconn_serve(newconn);
netconn_delete(newconn);
}
} while(err == ERR_OK);
netconn_close(conn);
netconn_delete(conn);
}
int app_main(void)
{
//Initialize NVS
esp_err_t ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
ret = nvs_flash_init();
}
ESP_ERROR_CHECK(ret);
ESP_LOGI(TAG, "ESP_WIFI_MODE_AP");
wifi_init_softap();
// ip_addr_t dns_addr;
// IP_ADDR4(&dns_addr, 192,168,4,100);
// dns_setserver(0, &dns_addr);
// dns_init();
captdnsInit();
xTaskCreate(&http_server, "http_server", 2048, NULL, 5, NULL);
return 0;
}
void wifi_init_softap()
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL));
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.ssid = WIDIM_SSID,
.ssid_len = strlen(WIDIM_SSID),
.password = WIDIM_PASS,
.channel = 6,
.ssid_hidden = 0,
.max_connection = EXAMPLE_MAX_STA_CONN,
.authmode = WIFI_AUTH_WPA_WPA2_PSK,
.beacon_interval = 100
},
};
if (strlen(WIDIM_PASS) == 0) {
wifi_config.ap.authmode = WIFI_AUTH_OPEN;
}
ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_AP, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_LOGI(TAG, "wifi_init_softap finished.SSID:%s password:%s",
WIDIM_SSID, WIDIM_PASS);
//printf("SSID: %s pass: %s",WIDIM_SSID, EXAMPLE_ESP_WIFI_PASS);
}
Code: Select all
I (27) boot: ESP-IDF v3.2-dev-1385-g129d32772 2nd stage bootloader
I (27) boot: compile time 11:06:11
I (34) boot: Enabling RNG early entropy source...
I (34) boot: SPI Speed : 80MHz
I (37) boot: SPI Mode : DIO
I (41) boot: SPI Flash Size : 4MB
I (45) boot: Partition Table:
I (49) boot: ## Label Usage Type ST Offset Length
I (56) boot: 0 nvs WiFi data 01 02 00009000 00006000
I (64) boot: 1 phy_init RF data 01 01 0000f000 00001000
I (71) boot: 2 factory factory app 00 00 00010000 00100000
I (79) boot: End of partition table
I (83) esp_image: segment 0: paddr=0x00010020 vaddr=0x3f400020 size=0x1657c ( 91516) map
I (118) esp_image: segment 1: paddr=0x000265a4 vaddr=0x3ff80000 size=0x00000 ( 0) load
I (119) esp_image: segment 2: paddr=0x000265ac vaddr=0x3ff80000 size=0x00000 ( 0) load
I (125) esp_image: segment 3: paddr=0x000265b4 vaddr=0x3ffb0000 size=0x030f0 ( 12528) load
I (138) esp_image: segment 4: paddr=0x000296ac vaddr=0x3ffb30f0 size=0x00000 ( 0) load
I (143) esp_image: segment 5: paddr=0x000296b4 vaddr=0x40080000 size=0x00400 ( 1024) load
I (152) esp_image: segment 6: paddr=0x00029abc vaddr=0x40080400 size=0x06554 ( 25940) load
I (170) esp_image: segment 7: paddr=0x00030018 vaddr=0x400d0018 size=0x63d58 (408920) map
I (289) esp_image: segment 8: paddr=0x00093d78 vaddr=0x40086954 size=0x0ab6c ( 43884) load
I (305) esp_image: segment 9: paddr=0x0009e8ec vaddr=0x400c0000 size=0x00000 ( 0) load
I (306) esp_image: segment 10: paddr=0x0009e8f4 vaddr=0x50000000 size=0x00000 ( 0) load
I (312) esp_image: segment 11: paddr=0x0009e8fc vaddr=0x50000000 size=0x00000 ( 0) load
I (331) boot: Loaded app from partition at offset 0x10000
I (331) boot: Disabling RNG early entropy source...
I (333) cpu_start: Pro cpu up.
I (336) cpu_start: Starting app cpu, entry point is 0x40081020
I (0) cpu_start: App cpu up.
I (347) heap_init: Initializing. RAM available for dynamic allocation:
I (354) heap_init: At 3FFAE6E0 len 00001920 (6 KiB): DRAM
I (360) heap_init: At 3FFB9038 len 00026FC8 (155 KiB): DRAM
I (366) heap_init: At 3FFE0440 len 00003BC0 (14 KiB): D/IRAM
I (372) heap_init: At 3FFE4350 len 0001BCB0 (111 KiB): D/IRAM
I (379) heap_init: At 400914C0 len 0000EB40 (58 KiB): IRAM
I (385) cpu_start: Pro cpu start user code
I (67) cpu_start: Starting scheduler on PRO CPU.
I (0) cpu_start: Starting scheduler on APP CPU.
I (134) wifi softAP: ESP_WIFI_MODE_AP
I (134) wifi: wifi driver task: 3ffc0d0c, prio:23, stack:3584, core=0
I (134) wifi: wifi firmware version: 3f23198
I (144) wifi: config NVS flash: enabled
I (144) wifi: config nano formating: disabled
I (144) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (154) system_api: Base MAC address is not set, read default base MAC address from BLK0 of EFUSE
I (194) wifi: Init dynamic tx buffer num: 32
I (194) wifi: Init data frame dynamic rx buffer num: 32
I (194) wifi: Init management frame dynamic rx buffer num: 32
I (194) wifi: Init static rx buffer size: 1600
I (204) wifi: Init static rx buffer num: 10
I (204) wifi: Init dynamic rx buffer num: 32
I (1284) phy: phy_version: 4000, b6198fa, Sep 3 2018, 15:11:06, 0, 0
I (1284) wifi: mode : softAP (24:0a:c4:00:8f:09)
I (1284) wifi: Init max length of beacon: 752/752
I (1284) wifi: Init max length of beacon: 752/752
I (1294) wifi softAP: wifi_init_softap finished.SSID:ESP32_TestAP password:testing123
I (1294) wifi softAP: SYSTEM_EVENT_AP_START
CaptDNS inited.