Search code examples
stringarduinocharwifi

Arduino - unable to connect to wifi


I have this (partial) code, that is used to read the ssid and password from a text file on a SD card. This data should then be used to connect to the local wifi. But it is unable to connect, and keeps saying "Establishing connection to WiFi..."

I also dont seem to be able to print what I have read from the SD card. Previously my readline function returned a string. That I was able to print properly and verified that the file is read as expected. Then, since wifi requires a Char* I changed the code to Char* and since then it didnt work anymore:

#include <SD.h>
#include <AccelStepper.h>
#include <MultiStepper.h>
#include <WiFi.h>
File myFile;
char* ssid;
char* password;


void setup() {
  // put your setup code here, to run once:

  pinMode(thetaPos, INPUT);
  pinMode(rhoPos, INPUT);

  Serial.begin(115200);
  Serial.print("Initializing SD card...");
  if (!SD.begin()) {
    Serial.println("initialization failed!");
    while (1);

  }
  Serial.println("initialization done.");
  myFile = SD.open("/config.txt");
  if (myFile) {
    // read from the file until there's nothing else in it:
    ssid = readLine();
    password = readLine();
    
      Serial.println(ssid);
      Serial.println(password);
  
  connectToNetwork();

    
    // close the file:
    myFile.close();
  } else {
    // file didn't open       
  }
 }
 
void loop() {
}


char* readLine() {
  char* received = "";
  char ch;
  while (myFile.available()) {
    ch = myFile.read();
    if (ch == '\n') {
      return received;
    } else {
      received += ch;
    }
  }
  return received;
}

void connectToNetwork() {
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(1000);
    Serial.println("Establishing connection to WiFi..");
  }
 
  Serial.println("Connected to network");
 
}

what am I doing wrong?

this is the serial output:

Initializing SD card...initialization done.
ore calibration nvs commit failed(0x%x)


wdt_config->intr_handle)
Establishing connection to WiFi..
Establishing connection to WiFi..
Establishing connection to WiFi..
Establishing connection to WiFi..

Solution

  • Well, where is the error? This won't work as you hope:

    char* readLine() {
      char* received = "";         // poiner to \0 somewhere... 
      char ch;
      while (myFile.available()) {
        ch = myFile.read();
        if (ch == '\n') {
          return received;
        } else {
          received += ch;          // move pointer to location ch bytes after...
        }
      }
      return received;
    }
    

    You are utilizing pointer arithmetic - you are moving pointer around the memory and you are not storing anything anywhere. It's not String class that overloads += char and allows you to concatenate character to the end of it.

    Basically you can use:

    String readLine() {
      String received;
      while (myFile.available()) {
        ch = myFile.read();
        if (ch == '\n') {
          return received;
        } else {
          received += ch;
        }
      }
      return received;
    }
    

    and it'll work (althrough it's not a good idea to append character by character to the String - heap memory gets fragmented really fast by this approach)

    To avoid other mistakes, usage is something like:

    String ssid = readLine();
    String pass = readLine();
    
    Wifi.begin(ssid.c_str(), pass.c_str());
    
    //// definitely do not do something like:
    // Wifi.begin(readLine().c_str(), readLine().c_str()); // !!! WRONG !!!
    //// as the order of executing parameters isn't specified (and in the GCC it's from the last to the first)