Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Missing feature of scanning and connecting to a local WiFi network #437

Open
hexs00si opened this issue Jun 19, 2024 · 1 comment
Open
Labels
feature New feature or request

Comments

@hexs00si
Copy link
Contributor

Is your feature request related to a problem? Please describe.
I am trying to connect the openBot to a local wifi network so that i can fetch parameters from different sensors , pins and send their respective values to a web app through http methods , but i am unable to do so as it is missing the firmware is missing the native feature or code to scan and connect to the local wifi network . It gets frustrating browsing multiple web pages to find the solution to include the wifi library and then code to get the desired result.

Describe the solution you'd like
A feature updated in the firmware to scan , connect and disconnect from a wifi network of choice , Additional features such as checking the connection status and getting the IP address of the openbot connected.

Describe alternatives you've considered
No major alternatives , apart from continuously and recursively browse multiple web pages in order to implement the feature

Additional context
This could help developers seamlessly integrate any web related feature and even create web servers to feed their web applications with data readings or other bi-directional communication needs. Someone who is not well wersed with Computer Networks could make use of this feature to seamlessly develop web apps

@hexs00si hexs00si added the feature New feature or request label Jun 19, 2024
@hexs00si
Copy link
Contributor Author

The following issue can be resolved using the library <WiFi.h> . The ESP32 board can act as Wi-Fi Station, Access Point or both.
We would use our board as a WiFi Station for the following implementation
This can be implemented in the following manner :

#include "WiFi.h"

const char* ssid = "REPLACE_WITH_YOUR_SSID"; // Preferred network SSID
char password[50]; // Buffer to store user input for password

void setup() {
  Serial.begin(115200);

  // Set WiFi to station mode and disconnect from an AP if it was previously connected.
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  Serial.println("Setup done");

  // Scan for networks and check for the preferred network
  scanAndConnect();
}

void scanAndConnect() {
  Serial.println("Scan start");

  // WiFi.scanNetworks will return the number of networks found.
  int n = WiFi.scanNetworks();
  Serial.println("Scan done");

  bool ssidFound = false;

  if (n == 0) {
    Serial.println("no networks found");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    Serial.println("Nr | SSID                             | RSSI | CH | Encryption");

    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.printf("%2d", i + 1);
      Serial.print(" | ");
      Serial.printf("%-32.32s", WiFi.SSID(i).c_str());
      Serial.print(" | ");
      Serial.printf("%4ld", WiFi.RSSI(i));
      Serial.print(" | ");
      Serial.printf("%2ld", WiFi.channel(i));
      Serial.print(" | ");
      switch (WiFi.encryptionType(i)) {
        case WIFI_AUTH_OPEN:            Serial.print("open"); break;
        case WIFI_AUTH_WEP:             Serial.print("WEP"); break;
        case WIFI_AUTH_WPA_PSK:         Serial.print("WPA"); break;
        case WIFI_AUTH_WPA2_PSK:        Serial.print("WPA2"); break;
        case WIFI_AUTH_WPA_WPA2_PSK:    Serial.print("WPA+WPA2"); break;
        case WIFI_AUTH_WPA2_ENTERPRISE: Serial.print("WPA2-EAP"); break;
        case WIFI_AUTH_WPA3_PSK:        Serial.print("WPA3"); break;
        case WIFI_AUTH_WPA2_WPA3_PSK:   Serial.print("WPA2+WPA3"); break;
        case WIFI_AUTH_WAPI_PSK:        Serial.print("WAPI"); break;
        default:                        Serial.print("unknown");
      }
      Serial.println();

      // Check if the preferred network is found
      if (WiFi.SSID(i) == ssid) {
        ssidFound = true;
      }

      delay(10);
    }
  }

  Serial.println("");

  if (ssidFound) {
    Serial.print("Preferred network ");
    Serial.print(ssid);
    Serial.println(" found. Please enter the password:");

    while (!Serial.available()) {
      // Wait for user input
    }
    Serial.readBytesUntil('\n', password, sizeof(password));

    // Connect to the network
    connectToWiFi();
  } else {
    Serial.println("Preferred network not found. Please try again.");
  }

  // Delete the scan result to free memory.
  WiFi.scanDelete();
}

void connectToWiFi() {
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  Serial.print("Connecting to WiFi ..");
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print('.');
    delay(1000);
  }
  Serial.println("Connected to WiFi");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // Your main code here
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New feature or request
Projects
Status: To do
Development

No branches or pull requests

1 participant