2017-01-08 2 views
4

J'ai cherché une solution pour ce problème pendant une longue période sans succès.NodeMCU auto-join ouverte wifi

Je souhaiterais que NodeMCU recherche un réseau wifi ouvert et s'y connecte. Tant que la connexion est disponible, utilisez cette connexion - et lorsque la connexion tombe, commencez à chercher un nouveau réseau ouvert.

Je vis en Finlande et nous avons un accès wifi gratuit presque à chaque coin de rue. Je prévois de créer quelque chose d'portable/mobile qui utiliserait le WiFi quand disponible.

Je commence aussi seulement sur la programmation, les bases en C et en utilisant l'IDE Arduino, donc pas d'expérience en langage Lua ici.

Je comprends que WiFi.scanNetworks() peut distinguer un SSID sécurisé d'un SSID non sécurisé, mais je n'ai pas découvert comment je pourrais utiliser cela à mon avantage dans Arduino IDE.

+1

un accroc qui vient à l'esprit: vous ne pouvez pas rechercher les points d'accès sur d'autres canaux en mode STA. vous pouvez scanner une liste, filtrer les points d'accès sécurisés, puis 'WiFi.begin()' sur un restant. – dandavis

+0

"Je voudrais NodeMCU ..." - indique que vous utilisez le firmware NodeMCU. "en utilisant l'IDE Arduino, donc pas d'expérience linguistique Lua ici" - maintenant quoi? NodeMCU/Lua firmware ou Arduino? –

+0

Arduino prend en charge la carte NodeMCU à partir de son gestionnaire de périphériques. Mais oui, ça ne fait rien d'autre que ESP8266 + Serial to USB. – cagdas

Répondre

3

Vous pouvez également rechercher des réseaux en mode STA.

La méthode dont vous avez besoin est WiFi.encryptionType() après WiFi.scanNetworks() pour déterminer si un réseau est crypté ou non. Je partage avec vous un croquis sur lequel je travaillais auparavant pour un projet similaire. Esquisse recherche les réseaux WiFi, les trie pour RSSI et effectue la connexion sur un réseau non crypté avec la plus grande force.

Ici, il est, bonne chance:

#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."); 
    } 
    } 
} 
+0

Des trucs géniaux! Voulez-vous le mettre à jour pour fonctionner sur ESP32? – pawisoon

+0

Mon mauvais! Pour ESP32, seul le changement est: ENC_TYPE_NONE à WIFI_AUTH_OPEN et l'en-tête d'importation: #include pawisoon

+0

En fait, je n'ai pas eu le temps de commencer à travailler avec 32.Mais heureux de voir que tu l'as bien fait. – cagdas