Char Array manipulation.

Hi, hello, howdy, etc.

I'll quickly start by stating how much help I've had from this forum by just searching, but I've hit a wall with my understanding of C++ and dealing with characters.

I'm working on a summer project that uses an Arduino Mega and the Wifi Shield and I've had a great deal of success but I've hit a problem. I want to read SSID and password data from the SD card and connect to the network. This will allow the installation to be moved to different WiFi spots with a change to a text file, rather than a recompile of the sketch.

Using the standard example by stating...

char ssid[] = "yourNetwork"; // 
char pass[] = "secretPassword";

works fine with

status = WiFi.begin(ssid, pass);

But the problem I have is storing the characters from the SD card into a char array that WiFi.begin is happy with. At the moment I'm doing this...

char ssid[32]; //  setting up buffers for the incoming sd card data
char pass[32]; //

....

void setup(){

.... setting up the SD card....

myData = SD.open("myData.txt");
      int charCount=0; 
      while (myData.available()) {
        char cx = myData.read();
        if(cx==10){
          //Serial.println("You got it");
          switch(lineCount){
           case 0:
           ssid[charCount]='\0';
          break;
           case 1:
           pass[charCount]='\0';
          break;
        }
         lineCount++;
        charCount=0; 
        }else{
        switch(lineCount){
           case 0:
           ssid[charCount]=cx;
          break;
           case 1:
           pass[charCount]=cx;
          break;
        }
        charCount++;
        }
    }

Serial.println(ssid); // this prints out the ssid from the card
Serial.println(pass); // this prints out the password from the card
.....

status = WiFi.begin(ssid, pass); // just won't connect using this method.

Any pointers? Every example I've seen seems to be happy that you can print out the char array, but doesn't use it in the way I am wanting to.

EDIT Using 1.0.2 and I haven't upgraded the firmware on the Wifi. (I can't, it's not mine). Everything with the card is pretty much working OK, it's just the manipulation of the chars that is hindering me,

r3dstar:
Any pointers?

So the Serial.println() calls print the correct value but it's not connecting?

Post all of your code, not just snippets. If it's too big to fit on a single page, attach it with the additional options.

Windows text files end each line with CR,LF (13,10). It may be that you are including a carriage return in your ssid and password. You could put in a line to skip any CR you encounter:

        char cx = myData.read();
        if (cx == 13)
            cx = myData.read();

Well, I'll be... That was it.

3 hours... seriously, 3 hours that screwed with me...

Thank you so much.

johnwasser:
Windows text files end each line with CR,LF (13,10). It may be that you are including a carriage return in your ssid and password. You could put in a line to skip any CR you encounter:

        char cx = myData.read();

if (cx == 13)
           cx = myData.read();

Hi
I'm going crazy with this problem too.
I can not read properly the SSID and pass from txt files.
have you solved it?

Hi
I'm going crazy with this problem too.
I can not read properly the SSID and pass from txt files.
have you solved it?

I can. Too bad you can't.

If you posted your code, perhaps we could see what problems you are having.