string to char

I'm trying to get this little project working.

I'm using the D1 Mini WiFi module along with a SD card reader.

Basically, I am trying to read a text file from the SD reader that contains two lines:
First line is the SSID for WiFi
Second line is the password for WiFi.

I can read the file and get the two line separately and they become:
ssid1
password1

That all works but they read in a string using:
ssid1 = (myFile.readStringUntil('\n'));
and same for password1.

But the wiFi connection seems to want a char variable:

WiFi.mode(WIFI_STA);
WiFi.begin(ssid1,password1);
Serial.println("");
Serial.println("Connecting to WiFi");

Is there a way of converting either the WiFi.Begin or the string that is created from the SD read?

This is what works if not reading from SD card:
const char* ssid = "MySSID";
const char* password = "1234567";

Thanks all for any help.

readStringUntil reads characters from the file into a String object. Wifi.begin expects to be passed plain old C strings. You can get a C string from a String, but better to read your SSID and password directly into a C string from the start.

Thanks for the reply.

Is that reading from the SD straight into a Char?

I've put in a demo of code below of what I'm trying to achieve.

#include <SD.h>             // SD access
#define SD_CARD_CD_DIO D1       // DIO pin used to control the modules CS pin 
#include <ESP8266WiFi.h>

String ssid;
String password;
boolean state = true;
File myFile;

//#ifndef STASSID
// #define STASSID "your-ssid"
// #define STAPSK  "your-password"
//#endif

// const char* ssid     = STASSID;
// const char* password = STAPSK;

const char* host = "djxmmx.net";
const uint16_t port = 17;

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

 //Read from SD Card
    if (!SD.begin(SD_CARD_CD_DIO)) {
       Serial.println("No SD Card inserted!");
       return;
    }
    Serial.println("initialization done.");

    // open the file for reading:
    myFile = SD.open("router.txt");
    if (myFile) {
       Serial.println("router.txt:");
 
       // read from the file until there's nothing else in it:
       while (myFile.available()) {
             ssid = (myFile.readStringUntil('\n')); 
           
             while (myFile.available()) {
                 password = (myFile.readStringUntil('\n')); 
              
                 Serial.print("SSID = "); Serial.println(ssid);         // just to check I have the first line
                 Serial.print("PWD = "); Serial.println(password);      // just to check I have the second line      
              } 
       }
       // close the file:
       myFile.close();
    } else {
       // if the file didn't open, print an error:
       Serial.println("error opening test.txt");
    }
//End SD Read
    
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  boolean state = true;
  int i = 0;
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }

  // This will send a string to the server
  Serial.println("sending data to server");
  if (client.connected()) {
    client.println("hello from ESP8266");
  }

  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  delay(300000); // execute once every 5 minutes, don't flood remote service
}

Take a look at readBytesUntil. Similar to what you're doing but lets you read your data into an array of char instead of a String.

Ok thanks, I will look into that and update.

Thanks for the help.
I have changed over to using readBytesUntil and I can Serial.print the results ssid & password just before the WiFi.begin and it correctly displays the ssid and password.

I have no idea why it isn't connecting.

HELP!!

#include <SD.h>             // SD access
#define SD_CARD_CD_DIO D1       // DIO pin used to control the modules CS pin 
#include <ESP8266WiFi.h>

char ssid[33];
char password[33];

File myFile;

//#ifndef STASSID
// #define STASSID "your-ssid"
// #define STAPSK  "your-password"
//#endif

// const char* ssid     = STASSID;
// const char* password = STAPSK;

const char* host = "djxmmx.net";
const uint16_t port = 17;

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

 //Read from SD Card
    if (!SD.begin(SD_CARD_CD_DIO)) {
       Serial.println("No SD Card inserted!");
       return;
    }
    Serial.println("initialization done.");

    // open the file for reading:
    myFile = SD.open("router.txt");
    if (myFile) {
       Serial.println("router.txt:");
 
       // read from the file until there's nothing else in it:
       while (myFile.available()) {
     //        ssid = (myFile.readStringUntil('\n')); 
            int l = myFile.readBytesUntil('\n', ssid, sizeof(ssid));
            if (l > 0 && ssid[l-1] == '\r') {
                l--;
            }
            ssid[l] = 0;
                 Serial.print("SSID = "); Serial.print(l); Serial.print(" / "); Serial.println(ssid);         // just to check I have the first line

             while (myFile.available()) {
      //           password = (myFile.readStringUntil('\n')); 
                  int l = myFile.readBytesUntil('\n', password, sizeof(password));
                  if (l > 0 && password[l-1] == '\r') {
                    l--;
                  }
                  password[l] = 0;

                 Serial.print("PWD = "); Serial.print(l); Serial.print(" / "); Serial.println(password);      // just to check I have the second line      
              } 
       }
       // close the file:
       myFile.close();
    } else {
       // if the file didn't open, print an error:
       Serial.println("error opening test.txt");
    }
//End SD Read
    
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  Serial.println(password);            // just to show 
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }

  // This will send a string to the server
  Serial.println("sending data to server");
  if (client.connected()) {
    client.println("hello from ESP8266");
  }

  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  delay(300000); // execute once every 5 minutes, don't flood remote service
}

What's the resulting output from the Monitor?

I have found the issue...

Not a programming fault but a human one.

The SSID on the SD card had a uppercase letter instead of a lowercase.

All working!!!!

Thanks for all the help.

Code below if anyone wants to connect to WiFi using SD Card with SSID and password on it.

txtfile layout is just:
ssid_name
password

#include <SD.h>             // SD access
#define SD_CARD_CD_DIO D1       // DIO pin used to control the modules CS pin 
#include <ESP8266WiFi.h>

char ssid[33];
char password[33];

File myFile;


const char* host = "djxmmx.net";
const uint16_t port = 17;

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

 //Read from SD Card
    if (!SD.begin(SD_CARD_CD_DIO)) {
       Serial.println("No SD Card inserted!");
       return;
    }
    Serial.println("initialization done.");

    // open the file for reading:
    myFile = SD.open("router.txt");
    if (myFile) {
       Serial.println("router.txt:");
 
       // read from the file until there's nothing else in it:
       while (myFile.available()) {
     //        ssid = (myFile.readStringUntil('\n')); 
            int l = myFile.readBytesUntil('\n', ssid, sizeof(ssid));
            if (l > 0 && ssid[l-1] == '\r') {
                l--;
            }
            ssid[l] = 0;
                 Serial.print("SSID = "); Serial.print(l); Serial.print(" / "); Serial.println(ssid);         // just to check I have the first line

             while (myFile.available()) {
      //           password = (myFile.readStringUntil('\n')); 
                  int l = myFile.readBytesUntil('\n', password, sizeof(password));
                  if (l > 0 && password[l-1] == '\r') {
                    l--;
                  }
                  password[l] = 0;

                 Serial.print("PWD = "); Serial.print(l); Serial.print(" / "); Serial.println(password);      // just to check I have the second line      
              } 
       }
       // close the file:
       myFile.close();
    } else {
       // if the file didn't open, print an error:
       Serial.println("error opening test.txt");
    }
//End SD Read
    
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  Serial.print("connecting to ");
  Serial.print(host);
  Serial.print(':');
  Serial.println(port);

  // Use WiFiClient class to create TCP connections
  WiFiClient client;
  if (!client.connect(host, port)) {
    Serial.println("connection failed");
    delay(5000);
    return;
  }

  // This will send a string to the server
  Serial.println("sending data to server");
  if (client.connected()) {
    client.println("hello from ESP8266");
  }

  // wait for data to be available
  unsigned long timeout = millis();
  while (client.available() == 0) {
    if (millis() - timeout > 5000) {
      Serial.println(">>> Client Timeout !");
      client.stop();
      delay(60000);
      return;
    }
  }

  // Read all the lines of the reply from server and print them to Serial
  Serial.println("receiving from remote server");
  // not testing 'client.connected()' since we do not need to send data here
  while (client.available()) {
    char ch = static_cast<char>(client.read());
    Serial.print(ch);
  }

  // Close the connection
  Serial.println();
  Serial.println("closing connection");
  client.stop();

  delay(300000); // execute once every 5 minutes, don't flood remote service
}

Hi ashtonm,

Thx for your code, took me too long to find something working :slight_smile:

Just wondering what I should change/add if I want 3 lines read from the file?

Tried adding a third while inside the main while loop but it messes everything up.

You need to implement a line counter, increment it after reading each line, and check it at the beginning of the while, like

const int numberOfLines = 3;
lineCount = 0;
while (myFile.available() and lineCount < numberOfLines) {