Merge branch 'WiFi-Manager' into refactor-BT/I2C

This commit is contained in:
sle118
2019-09-08 22:24:16 -04:00
committed by GitHub
53 changed files with 4272 additions and 383 deletions

View File

@@ -4,4 +4,5 @@ set(COMPONENT_SRCS "esp_app_main.c" "platform_esp32.c" "cmd_wifi.c" "console.c"
set(REQUIRES esp_common)
set(REQUIRES_COMPONENTS freertos squeezelite nvs_flash esp32 spi_flash newlib log console )
register_component()

View File

@@ -21,72 +21,6 @@ menu "Squeezelite-ESP32"
help
Set logging level info|debug|sdebug
endmenu
menu "Wifi Configuration"
config WIFI_SSID
string "WiFi SSID"
default "myssid"
help
SSID (network name) for the example to connect to.
config WIFI_PASSWORD
string "WiFi Password"
default "mypassword"
help
WiFi password (WPA or WPA2) for the example to use.
choice SCAN_METHOD
prompt "scan method"
default WIFI_FAST_SCAN
help
scan method for the esp32 to use
config WIFI_FAST_SCAN
bool "fast"
config WIFI_ALL_CHANNEL_SCAN
bool "all"
endchoice
choice SORT_METHOD
prompt "sort method"
default WIFI_CONNECT_AP_BY_SIGNAL
help
sort method for the esp32 to use
config WIFI_CONNECT_AP_BY_SIGNAL
bool "rssi"
config WIFI_CONNECT_AP_BY_SECURITY
bool "authmode"
endchoice
config FAST_SCAN_THRESHOLD
bool "fast scan threshold"
default y
help
wifi fast scan threshold
config FAST_SCAN_MINIMUM_SIGNAL
int "fast scan minimum rssi"
depends on FAST_SCAN_THRESHOLD
range -127 0
default -127
help
rssi is use to measure the signal
choice FAST_SCAN_WEAKEST_AUTHMODE
prompt "fast scan weakest authmode"
depends on FAST_SCAN_THRESHOLD
default EXAMPLE_OPEN
config EXAMPLE_OPEN
bool "open"
config EXAMPLE_WEP
bool "wep"
config EXAMPLE_WPA
bool "wpa"
config EXAMPLE_WPA2
bool "wpa2"
endchoice
endmenu
menu "Audio CODEC libraries"
config INCLUDE_FLAC
bool "FLAC"

View File

@@ -45,8 +45,9 @@ static void * squeezelite_thread(){
return NULL;
}
isRunning=true;
ESP_LOGI(TAG,"Waiting for WiFi.");
while(!wait_for_wifi()){usleep(100000);};
// Let's not wait on WiFi to allow squeezelite to run in bluetooth mode
// ESP_LOGI(TAG,"Waiting for WiFi.");
// while(!wait_for_wifi()){usleep(100000);};
ESP_LOGD(TAG ,"Number of args received: %u",thread_parms.argc );
ESP_LOGD(TAG ,"Values:");
for(int i = 0;i<thread_parms.argc; i++){

View File

@@ -7,183 +7,4 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "cmd_wifi.h"
#include <stdio.h>
#include <string.h>
#include "cmd_decl.h"
#include "esp_log.h"
#include "esp_console.h"
#include "argtable3/argtable3.h"
#include "freertos/FreeRTOS.h"
#include "freertos/event_groups.h"
#include "esp_wifi.h"
#include "tcpip_adapter.h"
#include "esp_event.h"
#include "led.h"
#define JOIN_TIMEOUT_MS (10000)
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
static const char * TAG = "cmd_wifi";
/** Arguments used by 'join' function */
static struct {
struct arg_int *timeout;
struct arg_str *ssid;
struct arg_str *password;
struct arg_end *end;
} join_args;
///** Arguments used by 'join' function */
//static struct {
// struct arg_int *autoconnect;
// struct arg_end *end;
//} auto_connect_args;
static void event_handler(void* arg, esp_event_base_t event_base,
int32_t event_id, void* event_data)
{
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_DISCONNECTED) {
led_blink_pushed(LED_GREEN, 250, 250);
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
led_unpush(LED_GREEN);
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
}
}
bool wait_for_wifi(){
bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
if(!connected){
ESP_LOGD(TAG,"Waiting for WiFi...");
connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
if(!connected){
ESP_LOGD(TAG,"wifi timeout.");
}
else
{
ESP_LOGI(TAG,"WiFi Connected!");
}
}
return connected;
}
static void initialise_wifi(void)
{
static bool initialized = false;
if (initialized) {
return;
}
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_event_loop_create_default());
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_event_handler_register(WIFI_EVENT, WIFI_EVENT_STA_DISCONNECTED, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_event_handler_register(IP_EVENT, IP_EVENT_STA_GOT_IP, &event_handler, NULL) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_NULL) );
ESP_ERROR_CHECK( esp_wifi_start() );
initialized = true;
led_blink(LED_GREEN, 250, 250);
}
static bool wifi_join(const char *ssid, const char *pass, int timeout_ms)
{
initialise_wifi();
wifi_config_t wifi_config = { 0 };
strncpy((char *) wifi_config.sta.ssid, ssid, sizeof(wifi_config.sta.ssid));
if (pass) {
strncpy((char *) wifi_config.sta.password, pass, sizeof(wifi_config.sta.password));
}
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
ESP_ERROR_CHECK( esp_wifi_connect() );
int bits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
pdFALSE, pdTRUE, timeout_ms / portTICK_PERIOD_MS);
return (bits & CONNECTED_BIT) != 0;
}
static int set_auto_connect(int argc, char **argv)
{
// int nerrors = arg_parse(argc, argv, (void **) &join_args);
// if (nerrors != 0) {
// arg_print_errors(stderr, join_args.end, argv[0]);
// return 1;
// }
// ESP_LOGI(__func__, "Connecting to '%s'",
// join_args.ssid->sval[0]);
//
// /* set default value*/
// if (join_args.timeout->count == 0) {
// join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
// }
//
// bool connected = wifi_join(join_args.ssid->sval[0],
// join_args.password->sval[0],
// join_args.timeout->ival[0]);
// if (!connected) {
// ESP_LOGW(__func__, "Connection timed out");
// return 1;
// }
// ESP_LOGI(__func__, "Connected");
return 0;
}
static int connect(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void **) &join_args);
if (nerrors != 0) {
arg_print_errors(stderr, join_args.end, argv[0]);
return 1;
}
ESP_LOGI(__func__, "Connecting to '%s'",
join_args.ssid->sval[0]);
/* set default value*/
if (join_args.timeout->count == 0) {
join_args.timeout->ival[0] = JOIN_TIMEOUT_MS;
}
bool connected = wifi_join(join_args.ssid->sval[0],
join_args.password->sval[0],
join_args.timeout->ival[0]);
if (!connected) {
ESP_LOGW(__func__, "Connection timed out");
return 1;
}
ESP_LOGI(__func__, "Connected");
return 0;
}
void register_wifi_join()
{
join_args.timeout = arg_int0(NULL, "timeout", "<t>", "Connection timeout, ms");
join_args.ssid = arg_str1(NULL, NULL, "<ssid>", "SSID of AP");
join_args.password = arg_str0(NULL, NULL, "<pass>", "PSK of AP");
join_args.end = arg_end(2);
const esp_console_cmd_t join_cmd = {
.command = "join",
.help = "Join WiFi AP as a station",
.hint = NULL,
.func = &connect,
.argtable = &join_args
};
ESP_ERROR_CHECK( esp_console_cmd_register(&join_cmd) );
}
void register_wifi()
{
register_wifi_join();
initialise_wifi();
}
// cmd_wifi has been replaced by wifi-manager

View File

@@ -12,8 +12,7 @@
extern "C" {
#endif
// Register WiFi functions
void register_wifi();
#ifdef __cplusplus
}

View File

@@ -145,12 +145,9 @@ void process_autoexec(){
{
ESP_LOGD(TAG,"No matching command found for name autoexec. Adding default entries");
uint8_t autoexec_dft=0;
char autoexec1_dft[64];
char autoexec2_dft[256]="squeezelite -o \"I2S\" -b 500:2000 -d all=info -M esp32";
snprintf(autoexec1_dft, 64, "join %s %s", CONFIG_WIFI_SSID, CONFIG_WIFI_PASSWORD);
char autoexec1_dft[256]="squeezelite -o I2S -b 500:2000 -d all=info -M esp32";
store_nvs_value(NVS_TYPE_U8,"autoexec",&autoexec_dft);
store_nvs_value(NVS_TYPE_STR,"autoexec1",autoexec1_dft);
store_nvs_value(NVS_TYPE_STR,"autoexec2",autoexec2_dft);
}
}
static void initialize_filesystem() {
@@ -237,7 +234,6 @@ void console_start() {
/* Register commands */
esp_console_register_help_command();
register_system();
register_wifi();
register_nvs();
register_squeezelite();
register_i2ctools();

View File

@@ -20,6 +20,33 @@
*/
#include "platform_esp32.h"
#include "led.h"
#include <stdio.h>
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include "driver/spi_master.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_spi_flash.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "esp_log.h"
#include "freertos/event_groups.h"
#include "mdns.h"
#include "lwip/api.h"
#include "lwip/err.h"
#include "lwip/netdb.h"
#include "http_server.h"
#include "wifi_manager.h"
static EventGroupHandle_t wifi_event_group;
const int CONNECTED_BIT = BIT0;
#define JOIN_TIMEOUT_MS (10000)
static const char TAG[] = "esp_app_main";
#ifdef CONFIG_SQUEEZEAMP
#define LED_GREEN_GPIO 12
@@ -29,10 +56,48 @@
#define LED_RED_GPIO 0
#endif
/* brief this is an exemple of a callback that you can setup in your own app to get notified of wifi manager event */
void cb_connection_got_ip(void *pvParameter){
ESP_LOGI(TAG, "I have a connection!");
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
led_unpush(LED_GREEN);
}
void cb_connection_sta_disconnected(void *pvParameter){
led_blink_pushed(LED_GREEN, 250, 250);
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
}
bool wait_for_wifi(){
bool connected=(xEventGroupGetBits(wifi_event_group) & CONNECTED_BIT)!=0;
if(!connected){
ESP_LOGD(TAG,"Waiting for WiFi...");
connected = (xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
pdFALSE, pdTRUE, JOIN_TIMEOUT_MS / portTICK_PERIOD_MS)& CONNECTED_BIT)!=0;
if(!connected){
ESP_LOGW(TAG,"wifi timeout.");
}
else
{
ESP_LOGI(TAG,"WiFi Connected!");
}
}
return connected;
}
void app_main()
{
led_config(LED_GREEN, LED_GREEN_GPIO, 0);
led_config(LED_RED, LED_RED_GPIO, 0);
wifi_event_group = xEventGroupCreate();
/* start the wifi manager */
led_blink(LED_GREEN, 250, 250);
wifi_manager_start();
wifi_manager_set_callback(EVENT_STA_GOT_IP, &cb_connection_got_ip);
wifi_manager_set_callback(WIFI_EVENT_STA_DISCONNECTED, &cb_connection_sta_disconnected);
console_start();
}