Skip to content

[WIP] Add wifi manager for provisioning #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions arduino/libraries/WiFi/src/WiFi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -615,11 +615,19 @@ esp_err_t WiFiClass::systemEventHandler(void* ctx, system_event_t* event)
return ESP_OK;
}

#include "wifi_manager.h"

void WiFiClass::handleSystemEvent(system_event_t* event)
{
switch (event->event_id) {
case SYSTEM_EVENT_SCAN_DONE:
xEventGroupSetBits(_eventGroup, BIT2);

if (wifi_manager_event_group != NULL) {
xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_SCAN_BIT);
wifi_manager_send_message(EVENT_SCAN_DONE, NULL);
}

break;

case SYSTEM_EVENT_STA_START: {
Expand Down Expand Up @@ -664,9 +672,23 @@ void WiFiClass::handleSystemEvent(system_event_t* event)
case SYSTEM_EVENT_STA_GOT_IP:
memcpy(&_ipInfo, &event->event_info.got_ip.ip_info, sizeof(_ipInfo));
_status = WL_CONNECTED;

if (wifi_manager_event_group != NULL) {
xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_WIFI_CONNECTED_BIT);
wifi_manager_send_message(EVENT_STA_GOT_IP, (void*)event->event_info.got_ip.ip_info.ip.addr );
}

break;

case SYSTEM_EVENT_STA_DISCONNECTED: {

if (wifi_manager_event_group != NULL) {
/* if a DISCONNECT message is posted while a scan is in progress this scan will NEVER end, causing scan to never work again. For this reason SCAN_BIT is cleared too */
xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_WIFI_CONNECTED_BIT | WIFI_MANAGER_SCAN_BIT);
/* post disconnect event with reason code */
wifi_manager_send_message(EVENT_STA_DISCONNECTED, (void*)( (uint32_t)event->event_info.disconnected.reason) );
}

uint8_t reason = event->event_info.disconnected.reason;

_reasonCode = reason;
Expand Down Expand Up @@ -725,6 +747,10 @@ void WiFiClass::handleSystemEvent(system_event_t* event)

_status = WL_AP_LISTENING;
xEventGroupSetBits(_eventGroup, BIT1);
if (wifi_manager_event_group != NULL) {
xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_AP_STARTED_BIT);
}

break;
}

Expand All @@ -737,13 +763,21 @@ void WiFiClass::handleSystemEvent(system_event_t* event)

case SYSTEM_EVENT_AP_STACONNECTED:
_status = WL_AP_CONNECTED;
if (wifi_manager_event_group != NULL) {
xEventGroupSetBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT);
}

break;

case SYSTEM_EVENT_AP_STADISCONNECTED:
wifi_sta_list_t staList;

esp_wifi_ap_get_sta_list(&staList);

if (wifi_manager_event_group != NULL) {
xEventGroupClearBits(wifi_manager_event_group, WIFI_MANAGER_AP_STA_CONNECTED_BIT);
}

if (staList.num == 0) {
_status = WL_AP_LISTENING;
}
Expand Down
11 changes: 11 additions & 0 deletions components/wifi_manager/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
set(COMPONENT_SRCS
"main.c"
"dns_server.c"
"http_server.c"
"json.c"
"wifi_manager.c"
)
set(COMPONENT_ADD_INCLUDEDIRS "")
set(COMPONENT_EMBED_FILES "style.css jquery.gz code.js index.html")

register_component()
63 changes: 63 additions & 0 deletions components/wifi_manager/Kconfig.projbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
menu "Wifi Manager Configuration"

config WIFI_MANAGER_TASK_PRIORITY
int "RTOS Task Priority for the wifi_manager"
default 5
help
Tasks spawn by the manager will have a priority of WIFI_MANAGER_TASK_PRIORITY-1. For this particular reason, minimum recommended task priority is 2.

config WIFI_MANAGER_MAX_RETRY
int "Max Retry on failed connection"
default 2
help
Defines when a connection is lost/attempt to connect is made, how many retries should be made before giving up.

config DEFAULT_AP_SSID
string "Access Point SSID"
default "esp32"
help
SSID (network name) the the esp32 will broadcast.

config DEFAULT_AP_PASSWORD
string "Access Point Password"
default "esp32pwd"
help
Password used for the Access Point. Leave empty and set AUTH MODE to WIFI_AUTH_OPEN for no password.

config DEFAULT_AP_CHANNEL
int "Access Point WiFi Channel"
default 1
help
Be careful you might not see the access point if you use a channel not allowed in your country.

config DEFAULT_AP_IP
string "Access Point IP Address"
default "10.10.0.1"
help
This is used for the redirection to the captive portal. It is recommended to leave unchanged.

config DEFAULT_AP_GATEWAY
string "Access Point IP Gateway"
default "10.10.0.1"
help
This is used for the redirection to the captive portal. It is recommended to leave unchanged.

config DEFAULT_AP_NETMASK
string "Access Point Netmask"
default "255.255.255.0"
help
This is used for the redirection to the captive portal. It is recommended to leave unchanged.

config DEFAULT_AP_MAX_CONNECTIONS
int "Access Point Max Connections"
default 4
help
Max is 4.

config DEFAULT_AP_BEACON_INTERVAL
int "Access Point Beacon Interval (ms)"
default 100
help
100ms is the recommended default.

endmenu
12 changes: 12 additions & 0 deletions components/wifi_manager/ap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[
{"ssid":"Pantum-AP-A6D49F","chan":11,"rssi":-55,"auth":4},
{"ssid":"a0308","chan":1,"rssi":-56,"auth":3},
{"ssid":"dlink-D9D8","chan":11,"rssi":-82,"auth":4},
{"ssid":"Linksys06730","chan":7,"rssi":-85,"auth":3},
{"ssid":"SINGTEL-5171","chan":9,"rssi":-88,"auth":4},
{"ssid":"1126-1","chan":11,"rssi":-89,"auth":4},
{"ssid":"The Shah 5GHz-2","chan":1,"rssi":-90,"auth":3},
{"ssid":"SINGTEL-1D28 (2G)","chan":11,"rssi":-91,"auth":3},
{"ssid":"dlink-F864","chan":1,"rssi":-92,"auth":4},
{"ssid":"dlink-74F0","chan":1,"rssi":-93,"auth":4}
]
Loading