mirror of
https://github.com/lumapu/ahoy.git
synced 2025-05-25 23:06:11 +02:00
fixed Wifi scan during first configuration (client connected to AP of Ahoy) #611
This commit is contained in:
parent
6721c8bbc1
commit
f8d4b4f5ae
3 changed files with 18 additions and 13 deletions
|
@ -600,10 +600,8 @@ class RestApi {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool setSetup(JsonObject jsonIn, JsonObject jsonOut) {
|
bool setSetup(JsonObject jsonIn, JsonObject jsonOut) {
|
||||||
if(F("scan_wifi") == jsonIn[F("cmd")]) {
|
if(F("scan_wifi") == jsonIn[F("cmd")])
|
||||||
DPRINTLN(DBG_INFO, F("rqst scan"));
|
|
||||||
mApp->scanAvailNetworks();
|
mApp->scanAvailNetworks();
|
||||||
}
|
|
||||||
else if(F("set_time") == jsonIn[F("cmd")])
|
else if(F("set_time") == jsonIn[F("cmd")])
|
||||||
mApp->setTimestamp(jsonIn[F("val")]);
|
mApp->setTimestamp(jsonIn[F("val")]);
|
||||||
else if(F("sync_ntp") == jsonIn[F("cmd")])
|
else if(F("sync_ntp") == jsonIn[F("cmd")])
|
||||||
|
|
|
@ -26,6 +26,7 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) {
|
||||||
mCnt = 0;
|
mCnt = 0;
|
||||||
mScanActive = false;
|
mScanActive = false;
|
||||||
mLastApClients = 0;
|
mLastApClients = 0;
|
||||||
|
mScanCnt = 0;
|
||||||
|
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
|
||||||
|
@ -66,15 +67,21 @@ void ahoywifi::tickWifiLoop() {
|
||||||
if(mStaConn != GOT_IP) {
|
if(mStaConn != GOT_IP) {
|
||||||
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
|
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
|
||||||
if(WIFI_AP_STA == WiFi.getMode()) {
|
if(WIFI_AP_STA == WiFi.getMode()) {
|
||||||
|
// first time switch to AP Mode
|
||||||
if(mScanActive && (mLastApClients != WiFi.softAPgetStationNum()))
|
if(mScanActive && (mLastApClients != WiFi.softAPgetStationNum()))
|
||||||
mScanActive = false;
|
mScanActive = false;
|
||||||
|
|
||||||
|
// scan is finished
|
||||||
|
if(!mScanActive) {
|
||||||
|
WiFi.mode(WIFI_AP);
|
||||||
|
mDns.start(53, "*", mApIp);
|
||||||
|
}
|
||||||
|
|
||||||
|
// only once a client connects to AP
|
||||||
if(mLastApClients != WiFi.softAPgetStationNum()) {
|
if(mLastApClients != WiFi.softAPgetStationNum()) {
|
||||||
mLastApClients = WiFi.softAPgetStationNum();
|
mLastApClients = WiFi.softAPgetStationNum();
|
||||||
WiFi.scanDelete();
|
WiFi.scanDelete();
|
||||||
WiFi.mode(WIFI_AP);
|
mAppWifiCb(false);
|
||||||
//mDns.start(53, "*", mApIp);
|
|
||||||
mAppWifiCb(true);
|
|
||||||
DBGPRINTLN(F("AP client connected"));
|
DBGPRINTLN(F("AP client connected"));
|
||||||
welcome(mApIp.toString());
|
welcome(mApIp.toString());
|
||||||
}
|
}
|
||||||
|
@ -94,6 +101,7 @@ void ahoywifi::tickWifiLoop() {
|
||||||
if(!mScanActive && mBSSIDList.empty()) { // start scanning APs with the given SSID
|
if(!mScanActive && mBSSIDList.empty()) { // start scanning APs with the given SSID
|
||||||
DBGPRINT(F("scanning APs with SSID "));
|
DBGPRINT(F("scanning APs with SSID "));
|
||||||
DBGPRINTLN(String(mConfig->sys.stationSsid));
|
DBGPRINTLN(String(mConfig->sys.stationSsid));
|
||||||
|
mScanCnt = 0;
|
||||||
mScanActive = true;
|
mScanActive = true;
|
||||||
#if defined(ESP8266)
|
#if defined(ESP8266)
|
||||||
WiFi.scanNetworks(true, false, 0U, (uint8_t *)mConfig->sys.stationSsid);
|
WiFi.scanNetworks(true, false, 0U, (uint8_t *)mConfig->sys.stationSsid);
|
||||||
|
@ -259,21 +267,17 @@ void ahoywifi::sortRSSI(int *sort, int n) {
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::scanAvailNetworks(void) {
|
void ahoywifi::scanAvailNetworks(void) {
|
||||||
DPRINTLN(DBG_INFO, "start scan");
|
|
||||||
if(-2 == WiFi.scanComplete()) {
|
if(-2 == WiFi.scanComplete()) {
|
||||||
mScanActive = true;
|
mScanActive = true;
|
||||||
if(WIFI_AP == WiFi.getMode())
|
if(WIFI_AP == WiFi.getMode())
|
||||||
WiFi.mode(WIFI_AP_STA);
|
WiFi.mode(WIFI_AP_STA);
|
||||||
WiFi.scanNetworks(true);
|
WiFi.scanNetworks(true);
|
||||||
}
|
}
|
||||||
else
|
|
||||||
DPRINTLN(DBG_INFO, "complete!");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::getAvailNetworks(JsonObject obj) {
|
void ahoywifi::getAvailNetworks(JsonObject obj) {
|
||||||
JsonArray nets = obj.createNestedArray("networks");
|
JsonArray nets = obj.createNestedArray("networks");
|
||||||
DPRINTLN(DBG_INFO, "getAvailNetworks");
|
|
||||||
|
|
||||||
int n = WiFi.scanComplete();
|
int n = WiFi.scanComplete();
|
||||||
if (n < 0)
|
if (n < 0)
|
||||||
|
@ -282,7 +286,6 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
|
||||||
int sort[n];
|
int sort[n];
|
||||||
sortRSSI(&sort[0], n);
|
sortRSSI(&sort[0], n);
|
||||||
for (int i = 0; i < n; ++i) {
|
for (int i = 0; i < n; ++i) {
|
||||||
DPRINTLN(DBG_INFO, "found network: " + WiFi.SSID(sort[i]));
|
|
||||||
nets[i]["ssid"] = WiFi.SSID(sort[i]);
|
nets[i]["ssid"] = WiFi.SSID(sort[i]);
|
||||||
nets[i]["rssi"] = WiFi.RSSI(sort[i]);
|
nets[i]["rssi"] = WiFi.RSSI(sort[i]);
|
||||||
}
|
}
|
||||||
|
@ -294,8 +297,11 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
void ahoywifi::getBSSIDs() {
|
void ahoywifi::getBSSIDs() {
|
||||||
int n = WiFi.scanComplete();
|
int n = WiFi.scanComplete();
|
||||||
if (n < 0)
|
if (n < 0){
|
||||||
return;
|
mScanCnt++;
|
||||||
|
if (mScanCnt < 20)
|
||||||
|
return;
|
||||||
|
}
|
||||||
if(n > 0) {
|
if(n > 0) {
|
||||||
mBSSIDList.clear();
|
mBSSIDList.clear();
|
||||||
int sort[n];
|
int sort[n];
|
||||||
|
|
|
@ -70,6 +70,7 @@ class ahoywifi {
|
||||||
uint8_t mLoopCnt;
|
uint8_t mLoopCnt;
|
||||||
bool mScanActive;
|
bool mScanActive;
|
||||||
uint8_t mLastApClients;
|
uint8_t mLastApClients;
|
||||||
|
uint8_t mScanCnt;
|
||||||
|
|
||||||
std::list<uint8_t> mBSSIDList;
|
std::list<uint8_t> mBSSIDList;
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue