Use best WiFi channel?

Hi there. I've got a Mkr Wifi 1010, with a built-in ESP WiFi chip.

I think I can tell that my problem is that my device is not selecting the strongest WiFi channel available. I have two access points in my house, one on channel 6 and the second on channel 1. Channel 6 is in the same room as my Arduino, channel 1 is upstairs through a bunch of walls.

From the Arduino via Serial Monitor:

18:09:46.424 -> ** Scan Networks **
18:09:51.028 -> number of available networks:2
18:09:51.028 -> 0) triangle Signal: -51 dBm Channel: 6 Encryption: WPA2
18:09:51.028 -> 1) triangle Signal: -53 dBm Channel: 1 Encryption: WPA2

When I unplug the upstairs AP (channel 1) the Arduino has no trouble connecting to the channel 6 AP. But with them both plugged in, I intermittently get failures to connect:

WiFi not connected. Reason code==2

I suspect that if I could just get the WiFiNina library to choose the stronger signal, it would work sufficiently reliably. Is there a way to make this happen?

Also, is the Reason code a signal of any sort?

Here's my code:

#include <SPI.h>
#include <WiFiNINA.h>

#include "wifi_creds.h"

void setup() {
    Serial.begin(9600);
    Serial.println("---- Board startup ----");

    WiFi.noLowPowerMode();
}

void loop() {
    if (WiFi.status() != WL_CONNECTED) {
      WiFi.begin(ssid, password);

      if (WiFi.status() != WL_CONNECTED) {
        Serial.print("WiFi not connected.  Reason code==");
        Serial.print(WiFi.reasonCode());
        Serial.println();

        listNetworks2();
      }
    } else {
      Serial.print("WiFi connected, RSSI == ");
      Serial.print(WiFi.RSSI());
      Serial.println();
    }
    delay(1000);
}

void listNetworks2() {
  // scan for nearby networks:
  Serial.println("** Scan Networks **");
  int numSsid = WiFi.scanNetworks();
  if (numSsid == -1) {
    Serial.println("Couldn't get a wifi connection.");
    while (true);
  }

  // print the list of networks seen:
  Serial.print("number of available networks:");
  Serial.println(numSsid);

  // print the network number and name for each network found:
  for (int thisNet = 0; thisNet < numSsid; thisNet++) {
    Serial.print(thisNet);
    Serial.print(") ");
    Serial.print(WiFi.SSID(thisNet));
    Serial.print("\tSignal: ");
    Serial.print(WiFi.RSSI(thisNet));
    Serial.print(" dBm");
    Serial.print("\tChannel: ");
    Serial.print(WiFi.channel(thisNet));
    Serial.print("\tEncryption: ");
    printEncryptionType2(WiFi.encryptionType(thisNet));
  }
}

void printEncryptionType2(int thisType) {
  // read the encryption type and print out the name:
  switch (thisType) {
    case ENC_TYPE_WEP:
      Serial.println("WEP");
      break;
    case ENC_TYPE_TKIP:
      Serial.println("WPA");
      break;
    case ENC_TYPE_CCMP:
      Serial.println("WPA2");
      break;
    case ENC_TYPE_NONE:
      Serial.println("None");
      break;
    case ENC_TYPE_AUTO:
      Serial.println("Auto");
      break;
  }
}

else needed for second if statment

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.