Skip to content

Commit

Permalink
fixed Wifi scan during first configuration (client connected to AP of…
Browse files Browse the repository at this point in the history
… Ahoy) #611
  • Loading branch information
lumapu committed Jan 28, 2023
1 parent 6721c8b commit f8d4b4f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
4 changes: 1 addition & 3 deletions src/web/RestApi.h
Original file line number Diff line number Diff line change
Expand Up @@ -600,10 +600,8 @@ class RestApi {
}

bool setSetup(JsonObject jsonIn, JsonObject jsonOut) {
if(F("scan_wifi") == jsonIn[F("cmd")]) {
DPRINTLN(DBG_INFO, F("rqst scan"));
if(F("scan_wifi") == jsonIn[F("cmd")])
mApp->scanAvailNetworks();
}
else if(F("set_time") == jsonIn[F("cmd")])
mApp->setTimestamp(jsonIn[F("val")]);
else if(F("sync_ntp") == jsonIn[F("cmd")])
Expand Down
26 changes: 16 additions & 10 deletions src/wifi/ahoywifi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ void ahoywifi::setup(settings_t *config, uint32_t *utcTimestamp, appWifiCb cb) {
mCnt = 0;
mScanActive = false;
mLastApClients = 0;
mScanCnt = 0;

#if defined(ESP8266)
wifiConnectHandler = WiFi.onStationModeConnected(std::bind(&ahoywifi::onConnect, this, std::placeholders::_1));
Expand Down Expand Up @@ -66,15 +67,21 @@ void ahoywifi::tickWifiLoop() {
if(mStaConn != GOT_IP) {
if (WiFi.softAPgetStationNum() > 0) { // do not reconnect if any AP connection exists
if(WIFI_AP_STA == WiFi.getMode()) {
// first time switch to AP Mode
if(mScanActive && (mLastApClients != WiFi.softAPgetStationNum()))
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()) {
mLastApClients = WiFi.softAPgetStationNum();
WiFi.scanDelete();
WiFi.mode(WIFI_AP);
//mDns.start(53, "*", mApIp);
mAppWifiCb(true);
mAppWifiCb(false);
DBGPRINTLN(F("AP client connected"));
welcome(mApIp.toString());
}
Expand All @@ -94,6 +101,7 @@ void ahoywifi::tickWifiLoop() {
if(!mScanActive && mBSSIDList.empty()) { // start scanning APs with the given SSID
DBGPRINT(F("scanning APs with SSID "));
DBGPRINTLN(String(mConfig->sys.stationSsid));
mScanCnt = 0;
mScanActive = true;
#if defined(ESP8266)
WiFi.scanNetworks(true, false, 0U, (uint8_t *)mConfig->sys.stationSsid);
Expand Down Expand Up @@ -259,21 +267,17 @@ void ahoywifi::sortRSSI(int *sort, int n) {

//-----------------------------------------------------------------------------
void ahoywifi::scanAvailNetworks(void) {
DPRINTLN(DBG_INFO, "start scan");
if(-2 == WiFi.scanComplete()) {
mScanActive = true;
if(WIFI_AP == WiFi.getMode())
WiFi.mode(WIFI_AP_STA);
WiFi.scanNetworks(true);
}
else
DPRINTLN(DBG_INFO, "complete!");
}

//-----------------------------------------------------------------------------
void ahoywifi::getAvailNetworks(JsonObject obj) {
JsonArray nets = obj.createNestedArray("networks");
DPRINTLN(DBG_INFO, "getAvailNetworks");

int n = WiFi.scanComplete();
if (n < 0)
Expand All @@ -282,7 +286,6 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
int sort[n];
sortRSSI(&sort[0], n);
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]["rssi"] = WiFi.RSSI(sort[i]);
}
Expand All @@ -294,8 +297,11 @@ void ahoywifi::getAvailNetworks(JsonObject obj) {
//-----------------------------------------------------------------------------
void ahoywifi::getBSSIDs() {
int n = WiFi.scanComplete();
if (n < 0)
return;
if (n < 0){
mScanCnt++;
if (mScanCnt < 20)
return;
}
if(n > 0) {
mBSSIDList.clear();
int sort[n];
Expand Down
1 change: 1 addition & 0 deletions src/wifi/ahoywifi.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ class ahoywifi {
uint8_t mLoopCnt;
bool mScanActive;
uint8_t mLastApClients;
uint8_t mScanCnt;

std::list<uint8_t> mBSSIDList;
};
Expand Down

0 comments on commit f8d4b4f

Please sign in to comment.