/* ESP8266 WiFi Scanning and selection - By Chris R.*/
//#include <ESP8266WiFi.h> //ESP8266
#include <WiFi.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2); // SCL: 5 (D1) , SDA: 4 (D2) //ESP8266
//#define ENCODER_CLK 14 // D5 //ESP8266
//#define ENCODER_DT 12 // D6 //ESP8266
//#define ENCODER_SW 13 // D7 //ESP8266
#define ENCODER_CLK 0
#define ENCODER_DT 2
#define ENCODER_SW 4
#define SIZE 8
String wlan_data[SIZE] = { "wlan_1","$$admin1234!", "wlan_2","$$user1234!", "Wokwi-GUEST", "", "wlan_3","$$user1234!"};
String ssid= "";
String password= "";
int counter = 0;
int stage =0;
void setup() {
Serial.begin(9600);
WiFi.mode(WIFI_STA);
// Initialize encoder pins
pinMode(ENCODER_CLK, INPUT);
pinMode(ENCODER_DT, INPUT);
pinMode(ENCODER_SW, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(ENCODER_CLK), readEncoder, FALLING);
attachInterrupt(digitalPinToInterrupt(ENCODER_SW), readSwitch, FALLING);
LCD.init();
LCD.clear();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Setting WiFi...");
Serial.println("\n\nInitializing WiFi...");
WiFi.mode(WIFI_STA);
delay(2000);
Serial.println("Setup done!");
LCD.setCursor(0, 1);
LCD.print("Setup done. ");
delay(2000);
Serial.println("System will connect to last Wifi in 10 seconds.");
Serial.println("Press button to scan and select a Wifi manually");
LCD.setCursor(0, 0);
LCD.print("Wait 10 sec. for");
LCD.setCursor(0, 1);
LCD.print("last Wifi or ");
delay(1000);
LCD.setCursor(0, 0);
LCD.print("press button to");
LCD.setCursor(0, 1);
LCD.print("select a Wifi.");
delay(2000);
// WiFi_scanning(); // NO UTILIZADO YA AQUI.
}
void readEncoder() {
int dtValue = digitalRead(ENCODER_DT);
if (dtValue == HIGH) {
counter++; // Clockwise
}
if (dtValue == LOW) {
counter--; // Counterclockwise
}
}
void readSwitch(){
if (digitalRead(ENCODER_SW) == LOW) {
if (stage == 0) {
stage+=1;
Serial.println("Stage0-->Stage1 : Switch pulsado.");
}
}
}
void WiFi_scanning() {
Serial.print("Scanning...");
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Scanning... ");
// WiFi.scanNetworks will return the number of networks found
int n = WiFi.scanNetworks();
Serial.print("scan done! ");
LCD.setCursor(0, 1);
LCD.print("Scan done! ");
delay(2000);
if (n == 0) {
Serial.println("No networks found.");
LCD.setCursor(0, 1);
LCD.print("No networks. ");
delay(2000);
} else {
Serial.print(n);
Serial.println(" networks found\n");
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Networks:");
LCD.setCursor(10, 0);
LCD.print(n);
delay(3000);
for (int i = 0; i < n; ++i) {
// Print SSID and RSSI for each network found
Serial.print(i + 1);
Serial.print(": ");
Serial.print(WiFi.SSID(i));
Serial.print(" (");
Serial.print(WiFi.RSSI(i));
Serial.print(")");
Serial.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN) ? " " : "*"); // Wifi.h (ESP32)
// Serial.println(WiFi.encryptionType(i) == ENC_TYPE_NONE ? "open" : "");
delay(10);
}
}
Serial.println("");
}
void WiFi_pwd() {
Serial.print("PWD search for: ");
Serial.println(ssid);
for (int i=0; i<SIZE; i+=2) {
//Serial.print(wlan_data[i]);
//Serial.print(", iteration: ");
//Serial.println(i);
if(wlan_data[i] == ssid) {
password = wlan_data[i+1];
Serial.print("Password: ");
Serial.println(password);
LCD.setCursor(0, 0);
LCD.print("PWD found for:");
}
}
}
void WiFi_pwd_input(){
Serial.print("Type password: ");
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Type password: ");
password = Serial.readString();
delay(10000);
Serial.println(password);
LCD.setCursor(0, 1);
LCD.print(password);
}
void WiFi_select() {
while(stage==1){
delay(1000);
ssid= WiFi.SSID(counter);
LCD.setCursor(0, 0);
LCD.print("Selection: < > ");
LCD.setCursor(0, 1);
LCD.print(ssid);
Serial.print("Counter: ");
Serial.print(counter);
Serial.print(" / ");
Serial.print("Selected Wifi: ");
Serial.println(ssid);
if (digitalRead(ENCODER_SW) == LOW) {
LCD.setCursor(12, 0);
LCD.print("OK");
Serial.println("Let's go!");
delay(2000);
stage=2;
Serial.println("Stage1-->Stage2 : Switch pulsado.\n");
WiFi_connect();
}
}
}
void WiFi_connect() {
LCD.clear();
LCD.setCursor(0, 0);
LCD.print("Connecting... ");
LCD.setCursor(0, 1);
LCD.print(ssid);
delay(2000);
Serial.print("Connecting to WiFi: ");
Serial.println(ssid);
WiFi_pwd(); // Comprobamos si disponemos de la PWD para esa red.
delay(2000);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(WiFi.status());
Serial.print('.');
delay(2000);
if (WiFi.status() == WL_NO_SSID_AVAIL){ // usar WL_CONNECT_FAILED Si ha fallado la contraseña
Serial.println("Wrong password.");
LCD.setCursor(0, 0);
LCD.print("Wrong password. ");
delay(3000);
WiFi_pwd_input(); // Introducimos contraseña por serie
}
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); // Intentamos conexión.
delay(2000);
}
Serial.println("\nConnected!");
LCD.setCursor(0, 0);
LCD.print("Connected! ");
delay(2000);
LCD.setCursor(0, 0);
LCD.print("WiFi SSID: ");
LCD.setCursor(0, 1);
LCD.print(WiFi.SSID());
LCD.setCursor(11, 0);
LCD.print(WiFi.RSSI());
//Serial.println(WiFi.status());
Serial.println(WiFi.localIP());
Serial.print("RRSI: ");
Serial.println(WiFi.RSSI());
delay(3000);
stage=3;
LCD.clear();
LCD.noBacklight();
}
int seg=10;
unsigned long mseg = millis();
void loop() {
if (stage==1) {
WiFi_scanning();
WiFi_select();
}
if (((millis()-mseg)==1000) && seg >= 0){
mseg=millis();
Serial.print("Segundos: ");
Serial.println(seg);
seg--;
delay(10);
}
if (seg=0){
WiFi.begin(); // Intentamos conexión a la última Wifi.
while (WiFi.status() != WL_CONNECTED) {
Serial.print(WiFi.status());
Serial.print('.');
delay(2000);
}
}
}