Skip to content
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

Default credentials and Hostname #154

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
12 changes: 12 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,16 @@ config DEFAULT_AP_BEACON_INTERVAL
help
100ms is the recommended default.

config DEFAULT_STA_SSID
string "Station SSID"
default ""
help
SSID (network name) the esp32 will connect to by default (leave empty for AP mode first).

config DEFAULT_STA_PASSWORD
string "Station Password"
default ""
help
Password used for the WiFi connection (leave empty for AP mode first).

endmenu
17 changes: 16 additions & 1 deletion src/wifi_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ static esp_netif_t* esp_netif_sta = NULL;
/* @brief netif object for the ACCESS POINT */
static esp_netif_t* esp_netif_ap = NULL;

static char* wifi_manager_hostname = NULL;

/**
* The actual WiFi settings in use
*/
Expand Down Expand Up @@ -141,7 +143,9 @@ const int WIFI_MANAGER_SCAN_BIT = BIT7;
/* @brief When set, means user requested for a disconnect */
const int WIFI_MANAGER_REQUEST_DISCONNECT_BIT = BIT8;


void wifi_manager_set_hostname(char* hostname) {
wifi_manager_hostname = hostname;
}

void wifi_manager_timer_retry_cb( TimerHandle_t xTimer ){

Expand Down Expand Up @@ -319,6 +323,16 @@ bool wifi_manager_fetch_wifi_sta_config(){

esp_err = nvs_open(wifi_manager_nvs_namespace, NVS_READONLY, &handle);

if (esp_err == ESP_ERR_NVS_NOT_FOUND && CONFIG_DEFAULT_STA_SSID[0] != '\0' && CONFIG_DEFAULT_STA_PASSWORD[0] != '\0') { // Initial use
ESP_LOGE(TAG, "Using defaults");
if(wifi_manager_config_sta == NULL) wifi_manager_config_sta = (wifi_config_t*)malloc(sizeof(wifi_config_t));
memset(wifi_manager_config_sta, 0x00, sizeof(wifi_config_t));
strncpy((char*) wifi_manager_config_sta->sta.ssid, CONFIG_DEFAULT_STA_SSID, 32);
strncpy((char*) wifi_manager_config_sta->sta.password, CONFIG_DEFAULT_STA_PASSWORD, 64);
nvs_sync_unlock();
return true;
}

if(esp_err != ESP_OK){
nvs_sync_unlock();
return false;
Expand Down Expand Up @@ -960,6 +974,7 @@ void wifi_manager( void * pvParameters ){

/* by default the mode is STA because wifi_manager will not start the access point unless it has to! */
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
if (wifi_manager_hostname != NULL) ESP_ERROR_CHECK(esp_netif_set_hostname(esp_netif_sta, wifi_manager_hostname));
ESP_ERROR_CHECK(esp_wifi_start());

/* start http server */
Expand Down
2 changes: 2 additions & 0 deletions src/wifi_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ esp_netif_t* wifi_manager_get_esp_netif_sta();
esp_netif_t* wifi_manager_get_esp_netif_ap();


void wifi_manager_set_hostname(char* hostname);

/**
* Allocate heap memory for the wifi manager and start the wifi_manager RTOS task
*/
Expand Down