issues with WiFi.begin() and Sting or char variables

Hey guys,

i currently have some issues with the WiFi.begin() function.
I want to send the SSID and the password via serial to the Wemos D1 mini Pro and then store them in the EEPROM. So far so good.
I get the correct char back from the eeprom but i didn´t get a connection to the network.
I tried it with char variables and with string variables but nothing worked.

Can somone please help me?

Here my code:

#include <EEPROM.h>;

#ifdef ESP8266
 #include <ESP8266WiFi.h>
 #include <WiFi.h>
#include <ModbusIP_ESP8266.h>

const int REG = 1001;                    // Modbus Coils Offset
const int COUNT = 1;  

char mySSID[50];
char myPWD[50];
char myIP[50];
char mymIP[50];

char cSSID[50];
char cPWD[50];
char tSSID[50];
char tPWD[50];
char cIP[50];
char cmIP[50];

char mIP1[3];
char mIP2[3];
char mIP3[3];
char mIP4[3];
int nIP = 0;

int IP1;
int IP2;
int IP3;
int IP4;

String sSSID;
String sPWD;
String sIP;
String smIP;
int i;
int lasti;

ModbusIP mb;  // ModbusIP object
IPAddress remote(IP1, IP2, IP3, IP4);

void getData(){
  Serial.println("SSID eingeben...");
  while(Serial.available() == 0){
  sSSID = Serial.readString();

  Serial.println("Passwort eingeben...");
  while(Serial.available() == 0){
  sPWD = Serial.readString();

  Serial.println("IP eingeben...");
  while(Serial.available() == 0){
  sIP = Serial.readString();

/*  Serial.println("Modul IP eingeben...");
  while(Serial.available() == 0){
  smIP = Serial.readString();*/

  for (i=0; i <= sSSID.length(); i++){
    mySSID[i] = sSSID[i];
    mySSID[i+1] = '\0';

  for (i=0; i <= sPWD.length(); i++){
    myPWD[i] = sPWD[i];
    myPWD[i+1] = '\0';

  for (i=0; i <= sIP.length(); i++){
    myIP[i] = sIP[i];
    myIP[i+1] = '\0';
  for (i=0; i <= smIP.length(); i++){
    mymIP[i] = smIP[i];
    mymIP[i+1] = '\0';

void writeEEPROM(){
  for(i=0; i<= sSSID.length(); i++){
    EEPROM.put(i, mySSID[i]);
  EEPROM.put(i+1, '\0');

  for(i=0; i<= sPWD.length(); i++){
    EEPROM.put(i+60, myPWD[i]);
  EEPROM.put(i+61, '\0');

  for(i=0; i<= sIP.length(); i++){
    EEPROM.put(i+120, myIP[i]);
  EEPROM.put(i+121, '\0');

void readEEPROM(){
  for(i=0; i<=50; i++){
    cSSID[i] =;
    if(cSSID[i] == '\0'){
    tSSID[i] = cSSID[i];

  for(i=0; i<=50; i++){
    cPWD[i] =;
    if(cPWD[i] == '\0'){
    tPWD[i] = cPWD[i];

  for(i=0; i<=50; i++){
    cIP[i] =;
    if(cIP[i] == '\0'){

  sSSID = String(cSSID);
  sPWD = String(cPWD);
  sIP = String(cIP);


void setup() {
pinMode (0, INPUT_PULLUP);

if (!digitalRead(0)){



nIP = 0;

for (i=0; i<= sIP.length(); i++){
  if(nIP == 0 && cIP[i] != '.'){
    mIP1[i] = cIP[i];
    nIP = nIP +1;
    lasti = i;
IP1 = atoi(mIP1);

for (i=0; i<= sIP.length(); i++){
  if(nIP == 1 && cIP[i+lasti] != '.'){
    mIP2[i] = cIP[i+lasti];
    nIP = nIP +1;
    lasti = lasti + i;
IP2 = atoi(mIP2);

for (i=0; i<= sIP.length(); i++){
  if(nIP == 2 && cIP[i + lasti] != '.'){
    mIP3[i] = cIP[i+lasti];
    nIP = nIP +1;
    lasti = lasti + i;
IP3 = atoi(mIP3);

for (i=0; i<= sIP.length(); i++){
  if(nIP == 3 && cIP[i + lasti] != '.'){
    mIP4[i] = cIP[i+lasti];
    nIP = nIP +1;
    lasti = lasti + i;
IP4 = atoi(mIP4);
IPAddress remote(IP1, IP2, IP3, IP4);    // Address of Modbus Slave 

Serial.print("Connecting to: ");
Serial.print("with PWD: ");

WiFi.begin(cSSID, cPWD);
  while (WiFi.status() != WL_CONNECTED) {

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


bool cb(Modbus::ResultCode event, uint16_t transactionId, void* data) { // Modbus Transaction callback
  if (event != Modbus::EX_SUCCESS)                  // If transaction got an error
    Serial.printf("Modbus result: %02X\n", event);  // Display Modbus error code
  if (event == Modbus::EX_TIMEOUT) {    // If Transaction timeout took place
    mb.disconnect(remote);              // Close connection to slave and
    mb.dropTransactions();              // Cancel all waiting transactions
  return true;

bool res[COUNT] = {true};
//bool res1[COUNT] = {false};

void loop() {
    if (!mb.isConnected(remote)) {   // Check if connection to Modbus Slave is established
        mb.connect(remote);           // Try to connect if no connection
    if (!mb.writeCoil(remote, REG, res, COUNT, cb)) // Try to Write array of COUNT of Coils to Modbus Slave
    if (mb.writeCoil(remote, REG, res, COUNT, cb) || millis()>30000){
    mb.task(); // Modbus task
    delay(50); // Pushing interval


Just as a quick sanity check, what happens if you replace this line:

WiFi.begin(cSSID, cPWD);

with the equivalent line with your actual SSID and password hardcoded in in place of the variables?

Then the Wemos connect properly like expect...

use cSSID.c_str()

but would you do is wrong esp8266 has only eeprom emulation esp8266 stores the ssid and password itself, you don't need to do that

I already tried this with the String Variable but it hasn´t worked. When I try it like you descriped it with the char array I get the following error massage:

request for member 'c_str' in 'cSSID', which is of non-class type 'char [50]'

WiFi.begin((char*) cSSID, (char*) cPWD);


WiFi.begin(String(cSSID).c_str(), String(cPWD).c_str());


WiFi.begin(String(cSSID).c_str(), String(cPWD).c_str());

so I am not alone :-) read the previous comments cSSID is a char array sSSID is String

I tried it with my String Variable sSSID.c_st() and my password. I think it's the same. Sorry guys I have no idea what to try else. Or do you think it makes a difference between my String Variable sSSID and String(cSSID)??

Thank's in advance.

Jones7: I tried it with my String Variable sSSID.c_st() and my password. I think it's the same. Sorry guys I have no idea what to try else. Or do you think it makes a difference between my String Variable sSSID and String(cSSID)??

Thank's in advance.

read my comment #5

No error massage but it still don´t connect.

Jones7: No error massage but it still don´t connect.

usually it is because the strings received over Serial contains the line end characters

Hey Juraj,

Thank you very much. That was the right tip. Got it running.


Hi Jones, Can You write solution???

How to use String for SSID/PASSWORD in WiFi.begin???



Thank you Bob for bumping,

if WLAN/Wifi credentials the SSID and the WLAN-key / password are read in into a string or an array of char from serial or flash or an SD-Card they might contain line-end-characters CR carriage return and / or LF line-feed.

These characters will not appear in the serial-monitor but they are still there.
If you pass variables

into Wifi.begin(mySSID, myPassword)

the variables contain the line-ending characters
then the SSID / the password is not equal to what the router expects.

So you have to remove them.

char mySSID[64];
char myPassword[64];

//if - and only if the last character read in is a line-ending character 
// a "CR" carriage return or a "LF"  line feed

  byte lastChar = strlen(mySSID) - 1; // Get the position of the last char
  mySSID[lastChar] = '\0';            //replace it with a NULL

  lastChar = strlen(myPassword) - 1; // Get the position of the last char
  myPassword[lastChar] = '\0';            //replace it with a NULL
  Serial.print("#  myPassword#");
  // I tested all three ways to connect to the router with an ESP8266 and Arduino -iDE 1.8.12
  //WiFi.begin(String(mySSID).c_str(), String(myPassword).c_str());
  //WiFi.begin((char*)mySSID, (char*)myPassword); // SSID, WLAN-Key
  WiFi.begin(mySSID, myPassword); // SSID, WLAN-Key

the most universal method would be to check tha last two chacters beeing a CR or lF decimal value 13 or 10 and then set the terminating zero to that position that contains the CR or LF

best regards Stefan