0

I'm trying to move my hard-coded wifi SSID/password credentials to an SD card so they can be more easily changed, but when passing the SSID to the WiFi.begin() function, I'm getting a status code 1 - "WL_NO_SSID_AVAIL" error. I'm even double-checking to make sure there are no additional characters at the end of the SSID when I read it from the text file on the SD Card. The values appear to be exactly right, but I'm still getting the error. Can anyone see what I'm missing? The code I'm trying is here:

#include <SPI.h>
#include <SD.h>
#include <WiFi.h>
#include <esp_now.h>
#include <vector>  // Include vector library

// Define the pin for the SD card reader
#define SD_CS_PIN 5

// Variables to store network credentials and MAC addresses
String ssid;
String password;
std::vector<uint8_t*> macAddresses;  // Dynamic array to store MAC addresses

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

  // Initialize SD card
  if (!SD.begin(SD_CS_PIN)) {
    Serial.println("Card failed, or not present");
    return;
  }
  Serial.println("Card initialized.");

  // Open the config file
  File configFile = SD.open("/network.txt");

  if (!configFile) {
    Serial.println("Failed to open config file");
    return;
  }

  // Read the file line by line and parse the values
  while (configFile.available()) {
    String line = configFile.readStringUntil('\n');

    if (line.startsWith("SSID=")) {
      ssid = line.substring(5);
    } else if (line.startsWith("PASS=")) {
      password = line.substring(5);
    } 
  }

  configFile.close();
  delay(500); // Making sure the execution is complete before continuing

  // Debug: Print the SSID and Password to the Serial Monitor -- checking for trailing chars
  Serial.print("Connecting to SSID: '");
  Serial.print(ssid);
  Serial.println("'");

  Serial.print("Using Password: '");
  Serial.print(password);
  Serial.println("'");


  // Connect to Wi-Fi
  WiFi.disconnect(true); // Making sure no connections are currently established
  delay(1000); 
  WiFi.begin(ssid.c_str(), password.c_str());

  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.print("Current WiFi Status Code: ");
    Serial.println(WiFi.status());
    Serial.println("Connecting to WiFi...");
  }

  Serial.println("Connected to WiFi");

void loop() {

}

I've tried making sure the I/O function with the SD Card was complete by adding a delay after the close() command, but that didn't help. I also tried adding a WiFi.disconnect() before the WiFi.begin() with a delay just to make sure there weren't any other connections being made. Finally, I'm verifying the SSID and PASS are correct in the serial monitor, and don't have any whitespace characters at the end that I wasn't seeing in the text file. The values are correct as I see them, but I'm still getting the 1 error code, or "WL_NO_SSID_AVAIL" after each connection attempt. When I hardcode the values into the code directly, the I'm connecting without issue.

4
  • try trim() on ssid and password. I am not sure if you would see a solo \r in Serial Monitor
    – Juraj
    Commented Sep 7 at 17:11
  • That....worked. I can't believe the debug line to print the string followed immediately by a single quote didn't catch that. I thought for sure I'd at least see a space or something before the quote. Thank you very much!
    – radioloko
    Commented Sep 7 at 18:11
  • Dangers of using plain text file. Invisible characters can sneak in. Use JSON, for instance, to store settings.
    – dda
    Commented Sep 8 at 0:49
  • Even though you're printing the SSID and password to the Serial Monitor, it's possible that hidden characters (such as a carriage return \r or extra whitespace) are being appended to the SSID or password when reading from the SD card. You can manually trim the hidden characters by using trim() function.
    – liaifat85
    Commented Sep 9 at 15:53

1 Answer 1

2

Thanks to Juraj, this is solved. The method I was using allowed for a non-printable character at the end to sneak in. Adding 'ssid.trim();' after getting the SSID value, and adding 'password.trim();' after getting the password value fixed the issue.

2
  • 1
    Interesting. WiFi.begin(ssid.c_str(), password.c_str()); You [and @Juraj] are also assuming (possibly correctly in this case) that the begin method stores the character arrays and not just a pointer to them so that say subsequent calls, if necessary, of .begin() (without parameters) will work. Some of the ESP functions, for example configTime( , , char* NtpServerName ) require the user to provide static storage for the character array and this is not always obvious without drilling through the library. The character array provided by .c_str() doesn't last very long.
    – 6v6gt
    Commented Sep 8 at 5:38
  • @6v6gt I am not assuming. I know
    – Juraj
    Commented Sep 8 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.