Skip to content

Commit

Permalink
Improv wifi (CarlosDerSeher#85)
Browse files Browse the repository at this point in the history
* - add wifi credential reset
  o press reset button (nRESET pin) 3 times
    but wait about 1s between button presses
    the button press counter is reset 5s after boot
* add wifi provisioning service through improv wifi (fix CarlosDerSeher#75)

Signed-off-by: Karl Osterseher <[email protected]>
  • Loading branch information
CarlosDerSeher authored and Andriy Malyshenko committed Jun 14, 2024
1 parent e989248 commit 5c2cd7d
Show file tree
Hide file tree
Showing 9 changed files with 110 additions and 171 deletions.
31 changes: 9 additions & 22 deletions .project
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,26 @@
<link>
<name>build/ide/esp_idf_components/esp_partition/partition_target.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/esp32/spiram_psram.c</location>
<location>/home/karl/espressif/esp-idf-v5.1.1/components/esp_partition/partition_target.c</location>

</link>
<link>
<name>build/ide/esp_idf_components/esp32/system_api_esp32.c</name>
<name>build/ide/esp_idf_components/esp_psram/mmu_psram_flash.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/esp32/system_api_esp32.c</location>
</link>
<link>
<name>build/ide/esp_idf_components/esp_adc_cal/esp_adc_cal_common.c</name>
<name>build/ide/esp_idf_components/esp_ringbuf/ringbuf.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/esp_adc_cal/esp_adc_cal_common.c</location>
</link>
<link>
<name>build/ide/esp_idf_components/esp_event/default_event_loop.c</name>
<name>build/ide/esp_idf_components/esp_system/crosscore_int.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/esp_event/default_event_loop.c</location>
<location>/home/karl/espressif/esp-idf-v5.1.1/components/esp_pm/pm_trace.c</location>
</link>
<link>
<name>build/ide/esp_idf_components/esp_ringbuf/ringbuf.c</name>
<name>build/ide/esp_idf_components/esp_system/debug_stubs.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/esp_ringbuf/ringbuf.c</location>
</link>
Expand Down Expand Up @@ -311,7 +312,8 @@
<link>
<name>build/ide/esp_idf_components/esp_system/freertos_hooks.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/esp_system/panic.c</location>
<location>/home/karl/espressif/esp-idf-v5.1.1/components/esp_system/freertos_hooks.c</location>

</link>
<link>
<name>build/ide/esp_idf_components/esp_system/sleep_modes.c</name>
Expand Down Expand Up @@ -1638,21 +1640,6 @@
<type>1</type>
<location>/home/karl/espressif/esp-idf-v5.1.1/components/esp_phy/src/phy_override.c</location>
</link>
<link>
<name>build/ide/esp_idf_components/hal/esp32/brownout_hal.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/hal/esp32/brownout_hal.c</location>
</link>
<link>
<name>build/ide/esp_idf_components/hal/esp32/efuse_hal.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/hal/esp32/efuse_hal.c</location>
</link>
<link>
<name>build/ide/esp_idf_components/hal/esp32/emac_hal.c</name>
<type>1</type>
<location>/home/carlos/espressif/idf-v4.3.5/esp-idf-v4.3.5/components/hal/esp32/emac_hal.c</location>
</link>
<link>
<name>build/ide/esp_idf_components/hal/esp32/gpio_hal_workaround.c</name>
<type>1</type>
Expand Down
9 changes: 4 additions & 5 deletions components/audio_board/Kconfig.projbuild
Original file line number Diff line number Diff line change
Expand Up @@ -101,11 +101,10 @@ menu "Audio Board"

config DAC_TAS5805M
bool "Texas Instruments TAS5805M"

config DAC_PT8211
bool "Princeton Technology PT8211"
select I2S_USE_MSB_FORMAT


config DAC_PT8211
bool "Princeton Technology PT8211"
select I2S_USE_MSB_FORMAT
endchoice

menu "DAC I2C control interface"
Expand Down
3 changes: 3 additions & 0 deletions components/lightsnapcast/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -275,14 +275,17 @@ static esp_err_t player_setup_i2s(i2s_port_t i2sNum,
{
#if CONFIG_INVERT_MCLK_LEVEL
.mclk_inv = true,

#else
.mclk_inv = false,
#endif

#if CONFIG_INVERT_BCLK_LEVEL
.bclk_inv = true,
#else
.bclk_inv = false,
#endif

#if CONFIG_INVERT_WORD_SELECT_LEVEL
.ws_inv = true,
#else
Expand Down
3 changes: 2 additions & 1 deletion components/wifi_interface/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
idf_component_register(SRCS "wifi_interface.c"
INCLUDE_DIRS "include"
REQUIRES wifi_provisioning esp_event esp_wifi esp_hw_support nvs_flash)
REQUIRES wifi_provisioning esp_event esp_wifi esp_hw_support nvs_flash improv_wifi)

97 changes: 8 additions & 89 deletions components/wifi_interface/wifi_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,7 @@

#if ENABLE_WIFI_PROVISIONING
#include <string.h> // for memcpy
#include <wifi_provisioning/manager.h>
#include <wifi_provisioning/scheme_softap.h>

static const char *provPwd = CONFIG_WIFI_PROVISIONING_PASSWORD;
static const char *provSsid = CONFIG_WIFI_PROVISIONING_SSID;
#include "wifi_provisioning.h"
#endif

static const char *TAG = "WIFI";
Expand All @@ -36,8 +32,6 @@ static int s_retry_num = 0;
static esp_netif_t *esp_wifi_netif = NULL;

#if ENABLE_WIFI_PROVISIONING
static wifi_config_t wifi_config;

static esp_timer_handle_t resetReasonTimerHandle = NULL;
static const esp_timer_create_args_t resetReasonTimerArgs = {
.callback = &reset_reason_timer_counter_cb,
Expand Down Expand Up @@ -69,10 +63,6 @@ static void reset_reason_timer_counter_cb(void *args) {
}
#endif

/* FreeRTOS event group to signal when we are connected & ready to make a
* request */
// static EventGroupHandle_t wifi_event_group;

/* The event group allows multiple bits for each event,
but we only care about one event - are we connected
to the AP with an IP? */
Expand Down Expand Up @@ -115,10 +105,6 @@ void wifi_init(void) {
ESP_ERROR_CHECK(
esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
(esp_event_handler_t)&event_handler, NULL));
#if ENABLE_WIFI_PROVISIONING
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_PROV_EVENT, ESP_EVENT_ANY_ID,
&event_handler, NULL));
#endif

esp_wifi_netif = esp_netif_create_default_wifi_sta();

Expand All @@ -134,15 +120,6 @@ void wifi_init(void) {
// esp_wifi_set_ps(WIFI_PS_NONE);

#if ENABLE_WIFI_PROVISIONING
// Configuration for the provisioning manager
wifi_prov_mgr_config_t config = {
.scheme = wifi_prov_scheme_softap,
.scheme_event_handler = WIFI_PROV_EVENT_HANDLER_NONE};

// Initialize provisioning manager with the
// configuration parameters set above
ESP_ERROR_CHECK(wifi_prov_mgr_init(config));

esp_reset_reason_t resetReason = esp_reset_reason();
ESP_LOGI(TAG, "reset reason was: %d", resetReason);
esp_timer_create(&resetReasonTimerArgs, &resetReasonTimerHandle);
Expand All @@ -157,7 +134,7 @@ void wifi_init(void) {
ESP_LOGI(TAG, "get POR reset counter ...");
err |= nvs_get_u8(nvs_handle, "restart_counter", &resetReasonCounter);

ESP_LOGE(TAG, "counter %d", resetReasonCounter);
ESP_LOGI(TAG, "reset counter %d", resetReasonCounter);

resetReasonCounter++;

Expand All @@ -166,7 +143,12 @@ void wifi_init(void) {

resetReasonCounter = 0;

wifi_prov_mgr_reset_provisioning();
esp_wifi_restore();
// esp_wifi_set_bandwidth (WIFI_IF_STA, WIFI_BW_HT20);
esp_wifi_set_bandwidth(WIFI_IF_STA, WIFI_BW_HT40);
esp_wifi_set_protocol(WIFI_IF_STA, WIFI_PROTOCOL_11B |
WIFI_PROTOCOL_11G |
WIFI_PROTOCOL_11N);

esp_timer_stop(resetReasonTimerHandle);
esp_timer_delete(resetReasonTimerHandle);
Expand All @@ -180,69 +162,6 @@ void wifi_init(void) {
}
}

bool provisioned = false;
/* Let's find out if the device is provisioned */
ESP_ERROR_CHECK(wifi_prov_mgr_is_provisioned(&provisioned));

/* If device is not yet provisioned start provisioning service */
if (!provisioned) {
ESP_LOGI(TAG, "Starting provisioning");

// Wi-Fi SSID when scheme is wifi_prov_scheme_softap
char service_name[27];
get_device_service_name(service_name, sizeof(service_name));

/* What is the security level that we want (0 or 1):
* - WIFI_PROV_SECURITY_0 is simply plain text communication.
* - WIFI_PROV_SECURITY_1 is secure communication which consists of
* secure handshake using X25519 key exchange and proof of possession
* (pop) and AES-CTR for encryption/decryption of messages.
*/
wifi_prov_security_t security = WIFI_PROV_SECURITY_1;

/* Do we want a proof-of-possession (ignored if Security 0 is selected):
* - this should be a string with length > 0
* - NULL if not used
*/
const char *pop = NULL; //"abcd1234";

/* What is the service key (could be NULL)
* This translates to :
* - Wi-Fi password when scheme is wifi_prov_scheme_softap
* - simply ignored when scheme is wifi_prov_scheme_ble
*/
const char *service_key = provPwd;

/* An optional endpoint that applications can create if they expect to
* get some additional custom data during provisioning workflow.
* The endpoint name can be anything of your choice.
* This call must be made before starting the provisioning.
*/
// wifi_prov_mgr_endpoint_create("custom-data");
/* Start provisioning service */
ESP_ERROR_CHECK(wifi_prov_mgr_start_provisioning(
security, pop, service_name, service_key));

/* The handler for the optional endpoint created above.
* This call must be made after starting the provisioning, and only if
* the endpoint has already been created above.
*/
// wifi_prov_mgr_endpoint_register("custom-data",
// custom_prov_data_handler, NULL);

/* Uncomment the following to wait for the provisioning to finish and
* then release the resources of the manager. Since in this case
* de-initialization is triggered by the default event loop handler, we
* don't need to call the following */
wifi_prov_mgr_wait();
wifi_prov_mgr_deinit();
} else {
ESP_LOGI(TAG, "Already provisioned, starting Wi-Fi STA");

/* We don't need the manager as device is already provisioned,
* so let's release it's resources */
wifi_prov_mgr_deinit();

/* Start Wi-Fi station */
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

Expand Down
4 changes: 2 additions & 2 deletions main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
idf_component_register(SRCS "main.c"
INCLUDE_DIRS "."
PRIV_REQUIRES esp_timer esp_wifi eth_interface nvs_flash wifi_interface audio_board audio_hal audio_sal net_functions opus flac ota_server
ui_http_server
PRIV_REQUIRES esp_timer esp_wifi nvs_flash wifi_interface audio_board audio_hal audio_sal net_functions opus flac ota_server
ui_http_server improv_wifi
)
36 changes: 32 additions & 4 deletions main/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2752,10 +2752,12 @@ void app_main(void) {

// if enabled these cause a timer srv stack overflow
esp_log_level_set("HEADPHONE", ESP_LOG_NONE);
esp_log_level_set("gpio", ESP_LOG_NONE);
// esp_log_level_set("i2s_std", ESP_LOG_DEBUG);
// esp_log_level_set("i2s_common", ESP_LOG_DEBUG);

esp_log_level_set("gpio", ESP_LOG_WARN);
esp_log_level_set("uart", ESP_LOG_WARN);
// esp_log_level_set("i2s_std", ESP_LOG_DEBUG);
// esp_log_level_set("i2s_common", ESP_LOG_DEBUG);
esp_log_level_set("wifi", ESP_LOG_WARN);
esp_log_level_set("wifi_init", ESP_LOG_WARN);
esp_log_level_set("wifi", ESP_LOG_WARN);
esp_log_level_set("wifi_init", ESP_LOG_WARN);

Expand Down Expand Up @@ -2860,6 +2862,32 @@ void app_main(void) {

ESP_LOGI(TAG, "init player");
init_player();

// ensure there is no noise from DAC
{
board_i2s_pin_t pin_config0;
get_i2s_pins(I2S_NUM_0, &pin_config0);

gpio_config_t gpioCfg = {
.pin_bit_mask =
BIT64(pin_config0.mck_io_num) | BIT64(pin_config0.data_out_num) |
BIT64(pin_config0.bck_io_num) | BIT64(pin_config0.ws_io_num),
.mode = GPIO_MODE_OUTPUT,
.pull_up_en = GPIO_PULLUP_DISABLE,
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
gpio_config(&gpioCfg);
gpio_set_level(pin_config0.mck_io_num, 0);
gpio_set_level(pin_config0.data_out_num, 0);
gpio_set_level(pin_config0.bck_io_num, 0);
gpio_set_level(pin_config0.ws_io_num, 0);

gpioCfg.pin_bit_mask = BIT64(pin_config0.data_in_num);
gpioCfg.mode = GPIO_MODE_INPUT;
gpioCfg.pull_up_en = GPIO_PULLUP_ENABLE;
gpio_config(&gpioCfg);
}

// ensure there is no noise from DAC
{
Expand Down
49 changes: 25 additions & 24 deletions sdkconfig
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,7 @@ CONFIG_SNTP_SERVER="pool.ntp.org"
#
# Wifi Configuration
#
# CONFIG_ENABLE_WIFI_PROVISIONING is not set
CONFIG_WIFI_SSID="zuhause"
CONFIG_WIFI_PASSWORD="dErtischlEr"
CONFIG_ENABLE_WIFI_PROVISIONING=y
CONFIG_WIFI_MAXIMUM_RETRY=0
# end of Wifi Configuration

Expand Down Expand Up @@ -1117,14 +1115,17 @@ CONFIG_IEEE802154_PENDING_TABLE_SIZE=20
#
# Log output
#
# CONFIG_LOG_DEFAULT_LEVEL_NONE is not set
CONFIG_LOG_DEFAULT_LEVEL_NONE=y
# CONFIG_LOG_DEFAULT_LEVEL_ERROR is not set
# CONFIG_LOG_DEFAULT_LEVEL_WARN is not set
CONFIG_LOG_DEFAULT_LEVEL_INFO=y
# CONFIG_LOG_DEFAULT_LEVEL_INFO is not set
# CONFIG_LOG_DEFAULT_LEVEL_DEBUG is not set
# CONFIG_LOG_DEFAULT_LEVEL_VERBOSE is not set
CONFIG_LOG_DEFAULT_LEVEL=3
CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT=y
CONFIG_LOG_DEFAULT_LEVEL=0
# CONFIG_LOG_MAXIMUM_EQUALS_DEFAULT is not set
# CONFIG_LOG_MAXIMUM_LEVEL_ERROR is not set
# CONFIG_LOG_MAXIMUM_LEVEL_WARN is not set
CONFIG_LOG_MAXIMUM_LEVEL_INFO=y
# CONFIG_LOG_MAXIMUM_LEVEL_DEBUG is not set
# CONFIG_LOG_MAXIMUM_LEVEL_VERBOSE is not set
CONFIG_LOG_MAXIMUM_LEVEL=3
Expand Down Expand Up @@ -1656,23 +1657,6 @@ CONFIG_WEBSOCKET_SERVER_TASK_PRIORITY=5
# CONFIG_WEBSOCKET_SERVER_PINNED is not set
# end of WebSocket Server

#
# DSP Library
#
CONFIG_DSP_OPTIMIZATIONS_SUPPORTED=y
# CONFIG_DSP_ANSI is not set
CONFIG_DSP_OPTIMIZED=y
CONFIG_DSP_OPTIMIZATION=1
# CONFIG_DSP_MAX_FFT_SIZE_512 is not set
# CONFIG_DSP_MAX_FFT_SIZE_1024 is not set
# CONFIG_DSP_MAX_FFT_SIZE_2048 is not set
CONFIG_DSP_MAX_FFT_SIZE_4096=y
# CONFIG_DSP_MAX_FFT_SIZE_8192 is not set
# CONFIG_DSP_MAX_FFT_SIZE_16384 is not set
# CONFIG_DSP_MAX_FFT_SIZE_32768 is not set
CONFIG_DSP_MAX_FFT_SIZE=4096
# end of DSP Library

#
# mDNS
#
Expand Down Expand Up @@ -1702,6 +1686,23 @@ CONFIG_MDNS_PREDEF_NETIF_AP=y
CONFIG_MDNS_PREDEF_NETIF_ETH=y
# end of MDNS Predefined interfaces
# end of mDNS

#
# DSP Library
#
CONFIG_DSP_OPTIMIZATIONS_SUPPORTED=y
# CONFIG_DSP_ANSI is not set
CONFIG_DSP_OPTIMIZED=y
CONFIG_DSP_OPTIMIZATION=1
# CONFIG_DSP_MAX_FFT_SIZE_512 is not set
# CONFIG_DSP_MAX_FFT_SIZE_1024 is not set
# CONFIG_DSP_MAX_FFT_SIZE_2048 is not set
CONFIG_DSP_MAX_FFT_SIZE_4096=y
# CONFIG_DSP_MAX_FFT_SIZE_8192 is not set
# CONFIG_DSP_MAX_FFT_SIZE_16384 is not set
# CONFIG_DSP_MAX_FFT_SIZE_32768 is not set
CONFIG_DSP_MAX_FFT_SIZE=4096
# end of DSP Library
# end of Component config

# CONFIG_IDF_EXPERIMENTAL_FEATURES is not set
Expand Down
Loading

0 comments on commit 5c2cd7d

Please sign in to comment.