ESP32 Wi-Fi Connection problem

Hi everyone,

I want to be able to select the Wi-Fis ssid from Wi-Fi scan and just type the password but for some reasons i can select some Wi-Fis(goes trough Serial 1 and 2) and others get lost somewhere(goes trough Serial 1 only). Is there some kind of ssid symbols that are not allowed or something?

Thanks in advance,
DG_tao

//includes Display, NTP(time&date) and Wi-Fi library
#include <Adafruit_SSD1306.h>
#include <WiFi.h>
#include <NTPClient.h>
#include <WiFiUdp.h>

//defines Display width, height and at what pins are the buttons connected
#define OLED_WIDTH  128
#define OLED_HEIGHT  64
#define buttonOne    34
#define buttonTwo    35
#define buttonThree  32
#define buttonFour   33

//adds verables for time&date, ssid, password and keyboard
char* ssid         = "";
char* password     = "";
String ssid_s      = "";
String password_s  = "";
String writeBuffer = "";
String sendBuffer  = "";
String formattedDate;
String dayStamp;
String timeStamp;

//adds verables for Caps Lock, Login and buttons if pressed
int setWiFi             = 0;
int capsLock           = 0;
int buttonState        = 0;
int buttonOnePressed   = 0;
int buttonTwoPressed   = 0;
int buttonThreePressed = 0;
int buttonFourPressed  = 0;

//sets the Display width and height
Adafruit_SSD1306 display(OLED_WIDTH, OLED_HEIGHT);

// define NTP Client to get time
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP);

void setup()
{
  //starts Serial and Display
  Serial.begin(115200);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);

  //defines the modes of the buttons
  pinMode(buttonOne, INPUT);
  pinMode(buttonTwo, INPUT);
  pinMode(buttonThree, INPUT);
  pinMode(buttonFour, INPUT);

  WiFiSet();

  // initialize the NTPClient to get time and sets time GMT(GMT +3 = 10800)
  timeClient.begin();
  timeClient.setTimeOffset(10800);
}

void loop()
{
  timedate();
}

void WiFiSet()
{
  //calls the function WiFiScan
  WiFiScan();
  
  //uses keyboard to select ssid
  while (setWiFi != 1)
  {
    while (buttonFourPressed != 1)
    {
      keyboard();
    }
   buttonFourPressed = 0;
   ssid_s = sendBuffer;
   int ssid_i = ssid_s.toInt();
   char charBuf[50];
   ssid_s.toCharArray(charBuf, 50);
   if (isDigit(charBuf[0]))
   {
     setWiFi = 1;
     String wifi_s = WiFi.SSID(ssid_i - 1);
     ssid = const_cast<char*>(wifi_s.c_str());
     sendBuffer = "";
     display.clearDisplay();
     display.display();
     Serial.print("1: ");
     Serial.println(ssid);
   }
   else
   {
    display.setCursor(0, 0);
    display.clearDisplay();
    display.print("Input is not a No.");
    display.display();
    sendBuffer = "";
    delay(2000);
   }
   Serial.print("2: ");
   Serial.println(ssid);
  }
  delay(200);

  //uses keyboaed to type password
  while (buttonFourPressed != 1)
  {
    keyboard();
  } 
  buttonFourPressed = 0;
  setWiFi = 2;
  password_s = sendBuffer;
  password = const_cast<char*>(password_s.c_str());
  sendBuffer = "";
  display.clearDisplay();
  display.display();
  delay(200);

  //connects to Wi-Fi
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Connecting to:");
  display.println(ssid);
  display.display();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    display.print(".");
    display.display();
  }
  display.println("");
  display.println("WiFi connected.");
  display.println("IP address: ");
  display.print(WiFi.localIP());
  display.display();
  delay(5000);
  display.clearDisplay();
  display.display();
}

void WiFiScan()
{
  //disconnects from Wi-Fi
  WiFi.disconnect();
  delay(100);
  
  //searches and display if there were networks found or not
  display.clearDisplay();
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.println("Scaning for networks");
  display.display();
  int n = WiFi.scanNetworks();
  display.println("Scan done");
  if (n == 0)
  {
    display.println("No networks found");
    display.display();
  }
  else
  {
    display.print(n);
    display.println(" Networks found");
    display.display();
    delay(1000);
    display.clearDisplay();
    display.setCursor(0, 0);
    for (int i = 0; i < n; ++i)
    {
      display.print(i + 1);
      display.print(": ");
      display.print(WiFi.SSID(i));
      display.print(" ");
      display.println((WiFi.encryptionType(i) == WIFI_AUTH_OPEN)?" ":"*");
      display.display();
      delay(10);
    }
  }
  delay(1000);
}

void timedate()
{
  //updates timeClient if update is available
  while(!timeClient.update())
  {
    timeClient.forceUpdate();
  }

  // extracts date and time
  formattedDate = timeClient.getFormattedDate();
  Serial.println(formattedDate);

  // displays date and time
  int splitT = formattedDate.indexOf("T");
  dayStamp = formattedDate.substring(0, splitT);
  display.setTextSize(1);
  display.setTextColor(WHITE);
  display.setCursor(0, 0);
  display.print("DATE: ");
  display.println(dayStamp);
  timeStamp = formattedDate.substring(splitT+1, formattedDate.length()-1);
  display.print("HOUR: ");
  display.println(timeStamp);
  display.display();
  delay(1000);
}

void keyboard()
{
  //reads the states of the buttons and writes them to variables
  buttonOnePressed = digitalRead(buttonOne);
  buttonTwoPressed = digitalRead(buttonTwo);
  buttonThreePressed = digitalRead(buttonThree);
  buttonFourPressed = digitalRead(buttonFour);

  //if button one is pressed subtracts one from "buttonState"
  if (buttonOnePressed == 1)
  {
    buttonState = buttonState - 1;
    delay(150);
    if(buttonState < -15)
    {
      buttonState = 26;
    }
  }
  
  //if button one is pressed adds one to "buttonState"
  if (buttonTwoPressed == 1)
  {
    buttonState = buttonState + 1;
    delay(150);
    if(buttonState > 26)
    {
      buttonState = -15;
    }
  }

  //defines all symbols in the keyboard
  switch (buttonState)
  {
    case -15:
      writeBuffer = "_";
      break;
    case -14:
      writeBuffer = "!";
      break;
    case -13:
      writeBuffer = "?";
      break;
    case -12:
      if (capsLock == 0)
      {
        writeBuffer = "Caps Lock";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "CAPS LOCK";
      }
      break;
    case -11:
      writeBuffer = "<-";
      break;
    case -10:
      writeBuffer = "SPACE";
      break;
    case -9:
      writeBuffer = "9";
      break;
    case -8:
      writeBuffer = "8";
      break;
    case -7:
      writeBuffer = "7";
      break;
    case -6:
      writeBuffer = "6";
      break;
    case -5:
      writeBuffer = "5";
      break;
    case -4:
      writeBuffer = "4";
      break;
    case -3:
      writeBuffer = "3";  
      break;
    case -2:
      writeBuffer = "2";
      break;
    case -1:
      writeBuffer = "1";
      break;
    case 0:
      writeBuffer = "0";
      break;
    case 1:
      if (capsLock == 0)
      {
        writeBuffer = "a";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "A";
      }
      break;
    case 2:
      if (capsLock == 0)
      {
        writeBuffer = "b";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "B";
      }
      break;
    case 3:
      if (capsLock == 0)
      {
        writeBuffer = "c";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "C";
      }
      break;
    case 4:
      if (capsLock == 0)
      {
        writeBuffer = "d";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "D";
      }
      break;
    case 5:
      if (capsLock == 0)
      {
        writeBuffer = "e";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "E";
      }
      break;
    case 6:
      if (capsLock == 0)
      {
        writeBuffer = "f";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "F";
      }
      break;
    case 7:
      if (capsLock == 0)
      {
        writeBuffer = "g";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "G";
      }
      break;
    case 8:
      if (capsLock == 0)
      {
        writeBuffer = "h";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "H";
      }
      break;
    case 9:
      if (capsLock == 0)
      {
        writeBuffer = "i";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "I";
      }
      break;
    case 10:
      if (capsLock == 0)
      {
        writeBuffer = "j";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "J";
      }
      break;
    case 11:
      if (capsLock == 0)
      {
        writeBuffer = "k";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "k";
      }
      break;
    case 12:
      if (capsLock == 0)
      {
        writeBuffer = "l";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "L";
      }
      break;
    case 13:
      if (capsLock == 0)
      {
        writeBuffer = "m";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "M";
      }
      break;
    case 14:
      if (capsLock == 0)
      {
        writeBuffer = "n";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "N";
      }
      break;
    case 15:
      if (capsLock == 0)
      {
        writeBuffer = "o";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "O";
      }
      break;
    case 16:
      if (capsLock == 0)
      {
        writeBuffer = "p";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "P";
      }
      break;
    case 17:
      if (capsLock == 0)
      {
        writeBuffer = "q";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "Q";
      }
      break;
    case 18:
      if (capsLock == 0)
      {
        writeBuffer = "r";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "R";
      }
      break;
    case 19:
      if (capsLock == 0)
      {
        writeBuffer = "s";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "S";
      }
      break;
    case 20:
      if (capsLock == 0)
      {
        writeBuffer = "t";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "T";
      }
      break;
    case 21:
      if (capsLock == 0)
      {
        writeBuffer = "u";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "U";
      }
      break;
    case 22:
      if (capsLock == 0)
      {
        writeBuffer = "v";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "V";
      }
      break;
    case 23:
      if (capsLock == 0)
      {
        writeBuffer = "w";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "W";
      }
      break;
    case 24:
      if (capsLock == 0)
      {
        writeBuffer = "x";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "X";
      }
      break;
    case 25:
      if (capsLock == 0)
      {
        writeBuffer = "y";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "Y";
      }
      break;
    case 26:
      if (capsLock == 0)
      {
        writeBuffer = "z";
      }
      else if (capsLock == 1)
      {
        writeBuffer = "Z";
      }
      break;
    default:
      
      break;
  }
  
  //makes Wi-Fi setting screen and displays selected sybbols/functions
  if (setWiFi == 0)
  {
    display.clearDisplay();
    display.drawRect(0, 53, 128, 11, WHITE);
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(2, 55);
    display.print(writeBuffer);
    display.setCursor(0, 9);
    display.print(sendBuffer);
    display.setCursor(0, 0);
    display.print("Connect to Wi-Fi No.:");
    display.display();
  }
  else if (setWiFi == 1)
  {
    display.clearDisplay();
    display.drawRect(0, 53, 128, 11, WHITE);
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(2, 55);
    display.print(writeBuffer);
    display.setCursor(0, 9);
    display.print(sendBuffer);
    display.setCursor(0, 0);
    display.print("Wi-Fi password:");
    display.display();
  }
  else if (setWiFi == 2)
  {
    display.clearDisplay();
    display.drawRect(0, 53, 128, 11, WHITE);
    display.setTextSize(1);
    display.setTextColor(WHITE);
    display.setCursor(2, 55);
    display.print(writeBuffer);
    display.setCursor(0, 0);
    display.print(sendBuffer);
    display.display();
  }
  
  //if button three is pressed the displayed symbol/function is written/performed
  if (buttonThreePressed == 1)
  {
    if (writeBuffer == "<-")
    {
      int L = sendBuffer.length();
      sendBuffer = sendBuffer.substring(0,L - 1);
      writeBuffer = "";
    }
    else if (writeBuffer == "SPACE")
    {
      writeBuffer = " ";
    }
    else if (writeBuffer == "Caps Lock")
    {
      capsLock = 1;
      writeBuffer = "";
    }
    else if (writeBuffer == "CAPS LOCK")
    {
      capsLock = 0;
      writeBuffer = "";
    }
    sendBuffer = sendBuffer + writeBuffer;
    writeBuffer = "";
    delay(150);
  }
}

I don't understand how the Serial 1 and 2 are involved here.

What kind of special characters (symbols) raised your suspicion?

I use serial to check if the Wi-Fi ssid is saved in the variable. It first checks right after the assignment of the name of the Wi-Fi to ssid and the second time i check is afther the if that assigns the ssid. Some network names can pass through both checks but some pass just trough the first and became lost afther. There might be a bug in my code or maybe some of the network names are not supported. The network I am trying to connect is ASUS_NET.

I'm quite sure that name is supported. It doesn't contain any special characters that may pose a problem.

Does that mean for that network you see the serial output of the "1: " lines but you don't see the "2: " lines?
That's easily explained: your "ssid" variable is globally declared. Inside the if-block you assign it a pointer to a variable you declared locally inside that block. At the end of the block the wifi_s variable goes out of scope and it's memory is released. Two lines later you provide that pointer to Serial.println() which probably throws an exception.

Thanks for the response! I will try to fix it and fill you in if it works. Have a great day!

Thanks it works now. I really appreciate the help!

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