NodeMCU自动加入开放wifi

NodeMCU auto-join open wifi

我已经为这个问题寻找了很长时间的解决方案,但没有成功。

我希望 NodeMCU 寻找一个开放的 wifi 网络并连接到它。只要连接可用就使用该连接 - 当连接断开时开始寻找新的开放网络。

我住在芬兰,我们几乎每个角落都有免费的开放式 WiFi。我正计划创建一些东西 wearable/mobile 在可用时使用 WiFi。

我也只是开始编程、C 基础知识和使用 Arduino IDE,所以这里没有 Lua 语言经验。

我知道 WiFi.scanNetworks() 可以区分安全和不安全的 SSID,但我还没有找到如何在 Arduino 中利用它来发挥我的优势 IDE。

您也可以在 STA 模式下扫描网络。

你需要的方法是在WiFi.scanNetworks()之后WiFi.encryptionType()判断一个网络是否加密。

我正在与您分享我之前为类似项目制作的草图。

Sketch 搜索 WiFi 网络,按 RSSI 顺序对它们进行排序,并在强度最高的非加密网络上执行连接。

到了,祝你好运:

#include <ESP8266WiFi.h>

/* Serial Baud Rate */
#define SERIAL_BAUD       9600
/* Delay paramter for connection. */
#define WIFI_DELAY        500
/* Max SSID octets. */
#define MAX_SSID_LEN      32
/* Wait this much until device gets IP. */
#define MAX_CONNECT_TIME  30000

/* SSID that to be stored to connect. */
char ssid[MAX_SSID_LEN] = "";

/* Scan available networks and sort them in order to their signal strength. */
void scanAndSort() {
  memset(ssid, 0, MAX_SSID_LEN);
  int n = WiFi.scanNetworks();
  Serial.println("Scan done!");
  if (n == 0) {
    Serial.println("No networks found!");
  } else {
    Serial.print(n);
    Serial.println(" networks found.");
    int indices[n];
    for (int i = 0; i < n; i++) {
      indices[i] = i;
    }
    for (int i = 0; i < n; i++) {
      for (int j = i + 1; j < n; j++) {
        if (WiFi.RSSI(indices[j]) > WiFi.RSSI(indices[i])) {
          std::swap(indices[i], indices[j]);
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      Serial.print(WiFi.SSID(indices[i]));
      Serial.print(" ");
      Serial.print(WiFi.RSSI(indices[i]));
      Serial.print(" ");
      Serial.print(WiFi.encryptionType(indices[i]));
      Serial.println();
      if(WiFi.encryptionType(indices[i]) == ENC_TYPE_NONE) {
        Serial.println("Found non-encrypted network. Store it and exit to connect.");
        memset(ssid, 0, MAX_SSID_LEN);
        strncpy(ssid, WiFi.SSID(indices[i]).c_str(), MAX_SSID_LEN);
        break;
      }
    }
  }
}

void setup() {
  Serial.begin(SERIAL_BAUD);
  Serial.println("Started.");
}

void loop() {
  if(WiFi.status() != WL_CONNECTED) {
    /* Clear previous modes. */
    WiFi.softAPdisconnect();
    WiFi.disconnect();
    WiFi.mode(WIFI_STA);
    delay(WIFI_DELAY);
    /* Scan for networks to find open guy. */
    scanAndSort();
    delay(WIFI_DELAY);
    /* Global ssid param need to be filled to connect. */
    if(strlen(ssid) > 0) {
      Serial.print("Going to connect for : ");
      Serial.println(ssid);
      /* No pass for WiFi. We are looking for non-encrypteds. */
      WiFi.begin(ssid);
      unsigned short try_cnt = 0;
      /* Wait until WiFi connection but do not exceed MAX_CONNECT_TIME */
      while (WiFi.status() != WL_CONNECTED && try_cnt < MAX_CONNECT_TIME / WIFI_DELAY) {
        delay(WIFI_DELAY);
        Serial.print(".");
        try_cnt++;
      }
      if(WiFi.status() == WL_CONNECTED) {
        Serial.println("");
        Serial.println("WiFi connected");
        Serial.println("IP address: ");
        Serial.println(WiFi.localIP());
      } else {
        Serial.println("Cannot established connection on given time.");
      }
    } else {
      Serial.println("No non-encrypted WiFi found.");  
    }
  }
}