mirror of
https://github.com/esp8266/Arduino.git
synced 2025-04-21 10:26:06 +03:00
add WiFi scan Async mode
This commit is contained in:
parent
7a9563d6c6
commit
9a1ff7f70d
@ -40,7 +40,9 @@ extern "C" void esp_schedule();
|
||||
extern "C" void esp_yield();
|
||||
|
||||
ESP8266WiFiClass::ESP8266WiFiClass()
|
||||
: _useApMode(false)
|
||||
: _smartConfigStarted(false)
|
||||
, _smartConfigDone(false)
|
||||
, _useApMode(false)
|
||||
, _useClientMode(false)
|
||||
, _useStaticIp(false)
|
||||
{
|
||||
@ -121,7 +123,7 @@ uint8_t ESP8266WiFiClass::waitForConnectResult(){
|
||||
}
|
||||
|
||||
|
||||
// You will have to set the DNS-Server manually later since this will not enable DHCP
|
||||
// You will have to set the DNS-Server manually later since this will not enable DHCP2
|
||||
void ESP8266WiFiClass::config(IPAddress local_ip, IPAddress gateway, IPAddress subnet)
|
||||
{
|
||||
struct ip_info info;
|
||||
@ -357,12 +359,47 @@ void ESP8266WiFiClass::_scanDone(void* result, int status)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
ESP8266WiFiClass::_scanStarted = false;
|
||||
ESP8266WiFiClass::_scanComplete = true;
|
||||
|
||||
if(!ESP8266WiFiClass::_scanAsync) {
|
||||
esp_schedule();
|
||||
}
|
||||
}
|
||||
|
||||
int8_t ESP8266WiFiClass::scanComplete() {
|
||||
|
||||
int8_t ESP8266WiFiClass::scanNetworks()
|
||||
if(_scanStarted) {
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
|
||||
if(_scanComplete) {
|
||||
return ESP8266WiFiClass::_scanCount;
|
||||
}
|
||||
|
||||
return WIFI_SCAN_FAILD;
|
||||
}
|
||||
|
||||
void ESP8266WiFiClass::scanDelete()
|
||||
{
|
||||
if (ESP8266WiFiClass::_scanResult)
|
||||
{
|
||||
delete[] reinterpret_cast<bss_info*>(ESP8266WiFiClass::_scanResult);
|
||||
ESP8266WiFiClass::_scanResult = 0;
|
||||
ESP8266WiFiClass::_scanCount = 0;
|
||||
}
|
||||
_scanComplete = false;
|
||||
}
|
||||
|
||||
int8_t ESP8266WiFiClass::scanNetworks(bool async)
|
||||
{
|
||||
if(ESP8266WiFiClass::_scanStarted) {
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
|
||||
ESP8266WiFiClass::_scanAsync = async;
|
||||
|
||||
if(_useApMode) {
|
||||
// turn on AP+STA mode
|
||||
mode(WIFI_AP_STA);
|
||||
@ -377,21 +414,28 @@ int8_t ESP8266WiFiClass::scanNetworks()
|
||||
disconnect();
|
||||
}
|
||||
|
||||
if (ESP8266WiFiClass::_scanResult)
|
||||
{
|
||||
delete[] reinterpret_cast<bss_info*>(ESP8266WiFiClass::_scanResult);
|
||||
ESP8266WiFiClass::_scanResult = 0;
|
||||
ESP8266WiFiClass::_scanCount = 0;
|
||||
}
|
||||
scanDelete();
|
||||
|
||||
struct scan_config config;
|
||||
config.ssid = 0;
|
||||
config.bssid = 0;
|
||||
config.channel = 0;
|
||||
config.show_hidden = 0;
|
||||
wifi_station_scan(&config, reinterpret_cast<scan_done_cb_t>(&ESP8266WiFiClass::_scanDone));
|
||||
if(wifi_station_scan(&config, reinterpret_cast<scan_done_cb_t>(&ESP8266WiFiClass::_scanDone))) {
|
||||
ESP8266WiFiClass::_scanComplete = false;
|
||||
ESP8266WiFiClass::_scanStarted = true;
|
||||
|
||||
if(ESP8266WiFiClass::_scanAsync) {
|
||||
delay(0); // time for the OS to trigger the scan
|
||||
return WIFI_SCAN_RUNNING;
|
||||
}
|
||||
|
||||
esp_yield();
|
||||
return ESP8266WiFiClass::_scanCount;
|
||||
} else {
|
||||
return WIFI_SCAN_FAILD;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void * ESP8266WiFiClass::_getScanInfoByIndex(int i)
|
||||
@ -644,6 +688,10 @@ void ESP8266WiFiClass::printDiag(Print& p)
|
||||
|
||||
}
|
||||
|
||||
bool ESP8266WiFiClass::_scanAsync = false;
|
||||
bool ESP8266WiFiClass::_scanStarted = false;
|
||||
bool ESP8266WiFiClass::_scanComplete = false;
|
||||
|
||||
size_t ESP8266WiFiClass::_scanCount = 0;
|
||||
void* ESP8266WiFiClass::_scanResult = 0;
|
||||
|
||||
|
@ -32,6 +32,9 @@ extern "C" {
|
||||
#include "WiFiClient.h"
|
||||
#include "WiFiServer.h"
|
||||
|
||||
#define WIFI_SCAN_RUNNING (-1)
|
||||
#define WIFI_SCAN_FAILD (-2)
|
||||
|
||||
enum WiFiMode { WIFI_OFF = 0, WIFI_STA = 1, WIFI_AP = 2, WIFI_AP_STA = 3 };
|
||||
|
||||
class ESP8266WiFiClass
|
||||
@ -189,12 +192,26 @@ public:
|
||||
|
||||
int32_t RSSI();
|
||||
|
||||
|
||||
/*
|
||||
* called to get the scan state in Async mode
|
||||
*
|
||||
* return -1 if scan not fin
|
||||
* return -2 if scan not triggered
|
||||
*/
|
||||
int8_t scanComplete();
|
||||
|
||||
/*
|
||||
* delete last scan result from RAM
|
||||
*/
|
||||
void scanDelete();
|
||||
|
||||
/*
|
||||
* Start scan WiFi networks available
|
||||
*
|
||||
* return: Number of discovered networks
|
||||
*/
|
||||
int8_t scanNetworks();
|
||||
int8_t scanNetworks(bool async = false);
|
||||
|
||||
/*
|
||||
* Return the SSID discovered during the network scan.
|
||||
@ -314,13 +331,17 @@ protected:
|
||||
void * _getScanInfoByIndex(int i);
|
||||
static void _smartConfigCallback(uint32_t status, void* result);
|
||||
static void _eventCallback(void *event);
|
||||
bool _smartConfigStarted = false;
|
||||
bool _smartConfigDone = false;
|
||||
bool _smartConfigStarted;
|
||||
bool _smartConfigDone;
|
||||
|
||||
bool _useApMode;
|
||||
bool _useClientMode;
|
||||
bool _useStaticIp;
|
||||
|
||||
static bool _scanAsync;
|
||||
static bool _scanStarted;
|
||||
static bool _scanComplete;
|
||||
|
||||
static size_t _scanCount;
|
||||
static void* _scanResult;
|
||||
|
||||
|
@ -40,29 +40,29 @@ bool ESP8266WiFiMulti::addAP(const char* ssid, const char *passphrase) {
|
||||
|
||||
wl_status_t ESP8266WiFiMulti::run(void) {
|
||||
|
||||
int8_t scanResult;
|
||||
wl_status_t status = WiFi.status();
|
||||
if(status == WL_DISCONNECTED || status == WL_NO_SSID_AVAIL || status == WL_IDLE_STATUS || status == WL_CONNECT_FAILED) {
|
||||
|
||||
scanResult = WiFi.scanComplete();
|
||||
if(scanResult == WIFI_SCAN_RUNNING) {
|
||||
// scan is running
|
||||
return WL_NO_SSID_AVAIL;
|
||||
} else if(scanResult > 0) {
|
||||
// scan done analyze
|
||||
WifiAPlist_t bestNetwork { NULL, NULL };
|
||||
int bestNetworkDb = INT_MIN;
|
||||
uint8 bestBSSID[6];
|
||||
int32_t bestChannel;
|
||||
|
||||
DEBUG_WIFI_MULTI("[WIFI] delete old wifi config...\n");
|
||||
WiFi.disconnect();
|
||||
|
||||
DEBUG_WIFI_MULTI("[WIFI] start scan\n");
|
||||
// WiFi.scanNetworks will return the number of networks found
|
||||
int8_t n = WiFi.scanNetworks();
|
||||
|
||||
DEBUG_WIFI_MULTI("[WIFI] scan done\n");
|
||||
delay(0);
|
||||
|
||||
if(n <= 0) {
|
||||
if(scanResult <= 0) {
|
||||
DEBUG_WIFI_MULTI("[WIFI] no networks found\n");
|
||||
} else {
|
||||
DEBUG_WIFI_MULTI("[WIFI] %d networks found\n", n);
|
||||
for(int8_t i = 0; i < n; ++i) {
|
||||
DEBUG_WIFI_MULTI("[WIFI] %d networks found\n", scanResult);
|
||||
for(int8_t i = 0; i < scanResult; ++i) {
|
||||
|
||||
String ssid_scan;
|
||||
int32_t rssi_scan;
|
||||
@ -102,6 +102,9 @@ wl_status_t ESP8266WiFiMulti::run(void) {
|
||||
}
|
||||
}
|
||||
|
||||
// clean up ram
|
||||
WiFi.scanDelete();
|
||||
|
||||
DEBUG_WIFI_MULTI("\n\n");
|
||||
delay(0);
|
||||
|
||||
@ -142,6 +145,15 @@ wl_status_t ESP8266WiFiMulti::run(void) {
|
||||
} else {
|
||||
DEBUG_WIFI_MULTI("[WIFI] no matching wifi found!\n");
|
||||
}
|
||||
} else {
|
||||
// start scan
|
||||
DEBUG_WIFI_MULTI("[WIFI] delete old wifi config...\n");
|
||||
WiFi.disconnect();
|
||||
|
||||
DEBUG_WIFI_MULTI("[WIFI] start scan\n");
|
||||
// scan wifi async mode
|
||||
WiFi.scanNetworks(true);
|
||||
}
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user