changed wifi to round-robin after rssi priority fails
This commit is contained in:
parent
99c1b35226
commit
bab3aeefb0
@ -86,9 +86,9 @@ void Settings::setDefaults()
|
|||||||
m_data.led.brightness = 25; // 25 %
|
m_data.led.brightness = 25; // 25 %
|
||||||
|
|
||||||
strcpy(m_data.wifi.entry[0].ssid, "gumball");
|
strcpy(m_data.wifi.entry[0].ssid, "gumball");
|
||||||
strcpy(m_data.wifi.entry[0].pwd, "mikelememb");
|
strcpy(m_data.wifi.entry[0].pwd, "mikelemembee");
|
||||||
strcpy(m_data.wifi.entry[1].ssid, "miro");
|
strcpy(m_data.wifi.entry[1].ssid, "miro");
|
||||||
strcpy(m_data.wifi.entry[1].pwd, "mikelemembe");
|
strcpy(m_data.wifi.entry[1].pwd, "mikelemembee");
|
||||||
|
|
||||||
m_data.wifi.num = 2;
|
m_data.wifi.num = 2;
|
||||||
m_data.wifi.selected = 0xff;
|
m_data.wifi.selected = 0xff;
|
||||||
|
|||||||
133
main/Wifi.cpp
133
main/Wifi.cpp
@ -8,7 +8,7 @@
|
|||||||
#include "utilities.h"
|
#include "utilities.h"
|
||||||
|
|
||||||
static const char *TAG = "Wifi";
|
static const char *TAG = "Wifi";
|
||||||
#define IGNORE_SSID_MINS 10
|
#define IGNORE_SSID_MINS 20
|
||||||
|
|
||||||
using namespace std::placeholders;
|
using namespace std::placeholders;
|
||||||
|
|
||||||
@ -18,29 +18,30 @@ Wifi::Wifi()
|
|||||||
esp_log_level_set("wifi", ESP_LOG_WARN);
|
esp_log_level_set("wifi", ESP_LOG_WARN);
|
||||||
esp_log_level_set("wifi_init", ESP_LOG_INFO);
|
esp_log_level_set("wifi_init", ESP_LOG_INFO);
|
||||||
|
|
||||||
memset(m_ignored, 0, SETTINGS_NUM_WIFI_ENTRIES);
|
// memset(m_ignored, 0, SETTINGS_NUM_WIFI_ENTRIES);
|
||||||
WiFi.onEvent(std::bind(&Wifi::wifi_event, this, _1, _2));
|
WiFi.onEvent(std::bind(&Wifi::wifi_event, this, _1, _2));
|
||||||
|
|
||||||
esp_timer_create_args_t timer;
|
// esp_timer_create_args_t timer;
|
||||||
timer.arg = this;
|
// timer.arg = this;
|
||||||
timer.callback = &timerCallback;
|
// timer.callback = &timerCallback;
|
||||||
timer.dispatch_method = ESP_TIMER_TASK;
|
// timer.dispatch_method = ESP_TIMER_TASK;
|
||||||
timer.skip_unhandled_events = true;
|
// timer.skip_unhandled_events = true;
|
||||||
timer.name = "wifi countdown timer";
|
// timer.name = "wifi countdown timer";
|
||||||
|
|
||||||
ESP_ERROR_CHECK(esp_timer_create(&timer, &m_timer));
|
// ESP_ERROR_CHECK(esp_timer_create(&timer, &m_timer));
|
||||||
ESP_ERROR_CHECK(esp_timer_start_periodic(m_timer, 60000000)); // 1 min
|
// ESP_ERROR_CHECK(esp_timer_start_periodic(m_timer, 60000000)); // 1 min
|
||||||
|
|
||||||
|
// m_task = TASK_FACTORY.createTask(std::bind(&Wifi::wifiTask, this), "wifi task", 2048, 5, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Wifi::timerCallback(void* arg)
|
// void Wifi::timerCallback(void* arg)
|
||||||
{
|
// {
|
||||||
for(int n = 0; n < SETTINGS_NUM_WIFI_ENTRIES; n++)
|
// for(int n = 0; n < SETTINGS_NUM_WIFI_ENTRIES; n++)
|
||||||
{
|
// {
|
||||||
if(((Wifi*)arg)->m_ignored[n] > 0)
|
// if(((Wifi*)arg)->m_ignored[n] > 0)
|
||||||
((Wifi*)arg)->m_ignored[n]--;
|
// ((Wifi*)arg)->m_ignored[n]--;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
void Wifi::wifi_event(arduino_event_id_t event, arduino_event_info_t info)
|
void Wifi::wifi_event(arduino_event_id_t event, arduino_event_info_t info)
|
||||||
{
|
{
|
||||||
@ -52,6 +53,35 @@ void Wifi::wifi_event(arduino_event_id_t event, arduino_event_info_t info)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// void Wifi::wifiTask()
|
||||||
|
// {
|
||||||
|
// while(true)
|
||||||
|
// {
|
||||||
|
// delay(1000);
|
||||||
|
// ESP_LOGI(TAG, "in wifi task!");
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
|
||||||
|
/// @brief Connect to provisioned wifi
|
||||||
|
/// @param index index of wifi in settings
|
||||||
|
/// @return ok/not connected
|
||||||
|
uint32_t Wifi::connectTo(int index)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Connecting to %s...", SETTINGS.wifi.entry[index].ssid);
|
||||||
|
WiFi.begin(SETTINGS.wifi.entry[index].ssid, SETTINGS.wifi.entry[index].pwd);
|
||||||
|
int status = WiFi.waitForConnectResult(10000);
|
||||||
|
|
||||||
|
if(status == WL_CONNECTED)
|
||||||
|
{
|
||||||
|
ESP_LOGI(TAG, "Connected");
|
||||||
|
return WH_OK;
|
||||||
|
}
|
||||||
|
WiFi.disconnect();
|
||||||
|
ESP_LOGW(TAG, "Failed to connect");
|
||||||
|
return WH_ERR_WIFI_NOT_CONNECTED;
|
||||||
|
}
|
||||||
|
|
||||||
uint32_t Wifi::connect()
|
uint32_t Wifi::connect()
|
||||||
{
|
{
|
||||||
if(SETTINGS.wifi.num == 0)
|
if(SETTINGS.wifi.num == 0)
|
||||||
@ -63,34 +93,71 @@ uint32_t Wifi::connect()
|
|||||||
if(WiFi.isConnected())
|
if(WiFi.isConnected())
|
||||||
return WH_OK;
|
return WH_OK;
|
||||||
|
|
||||||
|
if(SETTINGS.wifi.num == 1)
|
||||||
|
{
|
||||||
|
// only one wifi set up
|
||||||
|
//ssid_ix = 0;
|
||||||
|
SETTINGS.wifi.selected = 0;
|
||||||
|
for(int n = 0; n < 3; n++)
|
||||||
|
{
|
||||||
|
if(connectTo(SETTINGS.wifi.selected) == WH_OK)
|
||||||
|
return WH_OK;
|
||||||
|
|
||||||
|
WiFi.disconnect();
|
||||||
|
}
|
||||||
|
|
||||||
|
ESP_LOGW(TAG, "Cannot connect, need provisioning");
|
||||||
|
return WH_ERR_WIFI_NOT_PROVISIONED;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
memset(m_ignored, 0, SETTINGS_NUM_WIFI_ENTRIES);
|
||||||
int ssid_ix = SETTINGS.wifi.selected;
|
int ssid_ix = SETTINGS.wifi.selected;
|
||||||
|
|
||||||
if((SETTINGS.wifi.always_scan_before_connect && SETTINGS.wifi.num > 1) || SETTINGS.wifi.selected >= SETTINGS_NUM_WIFI_ENTRIES)
|
if(SETTINGS.wifi.always_scan_before_connect || SETTINGS.wifi.selected >= SETTINGS_NUM_WIFI_ENTRIES)
|
||||||
ssid_ix = scan();
|
ssid_ix = scan();
|
||||||
|
|
||||||
if(ssid_ix < 0)
|
if(ssid_ix < 0)
|
||||||
|
return WH_ERR_WIFI_NOT_PROVISIONED;
|
||||||
|
|
||||||
|
if(connectTo(ssid_ix) == WH_OK)
|
||||||
|
return WH_OK;
|
||||||
|
|
||||||
|
m_ignored[ssid_ix] = IGNORE_SSID_MINS;
|
||||||
|
|
||||||
|
// try to connect to highest rssi
|
||||||
|
do
|
||||||
{
|
{
|
||||||
// ESP_LOGE(TAG, "")
|
ssid_ix = scan();
|
||||||
|
if(ssid_ix >= 0)
|
||||||
|
{
|
||||||
|
if(connectTo(ssid_ix) == WH_OK)
|
||||||
|
return WH_OK;
|
||||||
|
else
|
||||||
|
m_ignored[ssid_ix] = IGNORE_SSID_MINS;
|
||||||
}
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
|
||||||
ESP_LOGI(TAG, "Connecting to %s...", SETTINGS.wifi.entry[ssid_ix].ssid);
|
} while(true);
|
||||||
WiFi.begin(SETTINGS.wifi.entry[ssid_ix].ssid, SETTINGS.wifi.entry[ssid_ix].pwd);
|
|
||||||
int status = WiFi.waitForConnectResult(15000);
|
|
||||||
|
|
||||||
if(status == WL_CONNECTED)
|
// tried all provisioned ssid's, try round robin few times
|
||||||
|
|
||||||
|
for(int m = 0; m < 3; m++)
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Connected");
|
for(int n = 0; n < SETTINGS.wifi.num; n++)
|
||||||
|
{
|
||||||
|
if(connectTo(n) == WH_OK)
|
||||||
return WH_OK;
|
return WH_OK;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
WiFi.disconnect();
|
ESP_LOGW(TAG, "Cannot connect, need provisioning");
|
||||||
m_ignored[ssid_ix] = IGNORE_SSID_MINS;
|
return WH_ERR_WIFI_NOT_PROVISIONED;
|
||||||
SETTINGS.wifi.selected = 0xff;
|
|
||||||
|
|
||||||
ESP_LOGE(TAG, "connect %d", status);
|
|
||||||
return WH_ERR_WIFI_NOT_CONNECTED;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// @brief Scans all visible ssid's and picks known ssid with highest rssi that is not ignored.
|
||||||
|
/// @return wifi index in Settings
|
||||||
int Wifi::scan()
|
int Wifi::scan()
|
||||||
{
|
{
|
||||||
ESP_LOGI(TAG, "Scanning wifi networks...");
|
ESP_LOGI(TAG, "Scanning wifi networks...");
|
||||||
@ -118,7 +185,11 @@ int Wifi::scan()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(selected_index >= 0)
|
||||||
ESP_LOGI(TAG, "Chosen network: '%s' based on RSSI", SETTINGS.wifi.entry[selected_index].ssid);
|
ESP_LOGI(TAG, "Chosen network: '%s' based on RSSI", SETTINGS.wifi.entry[selected_index].ssid);
|
||||||
|
else
|
||||||
|
ESP_LOGI(TAG, "No suitable networks found at this time");
|
||||||
|
|
||||||
return selected_index;
|
return selected_index;
|
||||||
}
|
}
|
||||||
10
main/Wifi.h
10
main/Wifi.h
@ -6,14 +6,18 @@
|
|||||||
#include <WiFi.h>
|
#include <WiFi.h>
|
||||||
#include <esp_timer.h>
|
#include <esp_timer.h>
|
||||||
#include "Settings.h"
|
#include "Settings.h"
|
||||||
|
#include "TaskFactory.h"
|
||||||
|
|
||||||
class Wifi
|
class Wifi
|
||||||
{
|
{
|
||||||
protected:
|
protected:
|
||||||
uint8_t m_ignored[SETTINGS_NUM_WIFI_ENTRIES];
|
uint8_t m_ignored[SETTINGS_NUM_WIFI_ENTRIES];
|
||||||
esp_timer_handle_t m_timer;
|
// esp_timer_handle_t m_timer;
|
||||||
static void timerCallback(void* arg);
|
// static void timerCallback(void* arg);
|
||||||
|
// TaskHandle_t m_task = nullptr;
|
||||||
|
// void wifiTask();
|
||||||
|
|
||||||
|
uint32_t connectTo(int index);
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Wifi();
|
Wifi();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user