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>
#else
 #include <WiFi.h>
#endif
#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] = EEPROM.read(i);
    if(cSSID[i] == '\0'){
      break;
    }
    tSSID[i] = cSSID[i];
  }

  for(i=0; i<=50; i++){
    cPWD[i] = EEPROM.read(i+60);
    if(cPWD[i] == '\0'){
      break;
    }
    tPWD[i] = cPWD[i];
  }

  for(i=0; i<=50; i++){
    cIP[i] = EEPROM.read(i+120);
    if(cIP[i] == '\0'){
      break;
    }
  }

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

}

void setup() {
Serial.begin(9600);
EEPROM.begin(240);
pinMode (0, INPUT_PULLUP);

if (!digitalRead(0)){
getData();
writeEEPROM();
}

readEEPROM();

EEPROM.end();

nIP = 0;


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

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


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

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



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

WiFi.begin(cSSID, cPWD);
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

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

  mb.master();
}

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
        Serial.print(".");
    }
    if (!mb.writeCoil(remote, REG, res, COUNT, cb)) // Try to Write array of COUNT of Coils to Modbus Slave
        Serial.print("#");
    if (mb.writeCoil(remote, REG, res, COUNT, cb) || millis()>30000){
      ESP.deepSleep(0);
    }
    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

https://arduino-esp8266.readthedocs.io/en/latest/esp8266wifi/generic-class.html#persistent

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);

try

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

KASSIMSAMJI:
try

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

so I am not alone :slight_smile: 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.

Cheers.

Hi Jones,
Can You write solution???

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

Cheers.

bump

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
  Serial.print("SSID#");
  Serial.print(mySSID);

  
  lastChar = strlen(myPassword) - 1; // Get the position of the last char
  myPassword[lastChar] = '\0';            //replace it with a NULL
  Serial.print("#  myPassword#");
  Serial.print(myPassword);
  Serial.println("#");
  
  // 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

1 Like