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.
1 Answer 1
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.
-
1Interesting.
WiFi.begin(ssid.c_str(), password.c_str());
You [and @Juraj] are also assuming (possibly correctly in this case) that thebegin
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– 6v6gt09/08/2024 05:38:37Commented Sep 8, 2024 at 5:38 -
trim()
onssid
andpassword
. I am not sure if you would see a solo \r in Serial Monitor