0

I'd like to use an SD card to provide the login credentials to an ESP32. I've already got the ESP32 reading the relative data from the SD card and storing it as a char array on the ESP, but when I use those arrays in the WiFi.begin function it never connects.

Is this something to do with the way arrays work or am I doing something else wrong?

Respective code:

if (dataFile) {
Serial.println("File is available");
while (dataFile.available()) {
  byte currbyte = dataFile.read();
  if (currbyte == 58) {
    reading = 1;    //Begin reading data
    i = 0;
  }
  while (reading == 1 and s == 0) {
    byte currbyte = dataFile.read();
    //Serial.print(currbyte);
    //Serial.println(char(currbyte));
    if (currbyte == 13) {
      s = 1;
      reading = 0 ;
    }
    ssid[i] = char(currbyte);
    i++;
    //Serial.println(ssid);
  }
  while (reading == 1 and s == 1) {
    byte currbyte = dataFile.read();
    //Serial.print(currbyte);
    //Serial.println(char(currbyte));
    if (currbyte == 13) {
      s = 2;
      reading = 0 ;
    }
    password[i] = char(currbyte);
    i++;
    //Serial.println(password);
  }
}
dataFile.close();
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED and count < 20) {
Serial.println("Connecting to WiFi..");
delay(1000);
count++;}
9
  • Hi I've got the data of the SD card without issues, howeverI can't connect to wifi with that data stored as arrays – Tanenthor Jun 16 '19 at 16:53
  • Alright, well I know that the code works if I manually enter the SSID and Password. A serial print of the arrays shows that they visually appear to be correct, and are being called correctly for the function – Tanenthor Jun 16 '19 at 17:19
  • that makes it clear ... one way to debug this issue is to write the data back to the SD card and examine the result (i am assuming that you used a mac or a pc to write the data to the SD card) .... perhaps use a separate filename for each data field .... also, update your question with the info in your last comment – jsotola Jun 16 '19 at 17:40
  • Writing the arrays for the SSID and password to a file on the SD card returns the correct results. So like I asked, is this something to do with arrays or is it something else I'm doing? – Tanenthor Jun 17 '19 at 5:18
  • do not write an array to the SD card ... write the content of the array .... the SSID and password strings ... check that there are no hidden characters in the strings – jsotola Jun 17 '19 at 6:28
1

you add the '\13' to the SSID and password string instead of the terminating zero, because ssid[i] = char(currbyte); runs for 13 too

use readBytesUntil()

  char ssid[33];
  int l = file.readBytesUntil('\n', ssid, sizeof(ssid));
  if (l > 0 && ssid[l-1] == '\r') {
    l--;
  }
  ssid[l] = 0;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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