Problem reading data from SD card and use it to connect on WiFi


hey guys i've been struggling problem , hope can me out.

i created text file on sd card containing 2 strings ssid , pass connect on wifi network using arduino wifi shield, , i've been trying read them file , use them connect but, everytime try error led on. i've tried declaring strings in code directly , works, reading them file can't connection, thou debugged while , strings file printing intended on serial output.

any appreciated.
obs: structed code read file being first line of file ssid , second line pass.
using 1.02 ide

edit: sorry guys found out problem, not checking carriage return ('\r').
i managed working, dont need reply! anyway ;)

code:

code: [select]

#include <spi.h>
#include <wifi.h>
#include <sd.h>

file myfile;
const char conf[]  = "conf.txt";
int status = wl_idle_status;
const int i1 = 1;
const int i0 = 0;
int = i0;
const int tendelay = 10000;


void setup(){

 serial.begin(9600);
 initialize();
}

void loop(){

}

void initialize(){
 loadconf();


}

void connectwifi(char ssid[], char pass[]){
 // check presence of shield:
 if (wifi.status() == wl_no_shield) {
   serial.println(f("wifi shield not present"));
   while(true);        // don't continue
 }

 // attempt connect wifi network:
 while (status != wl_connected) {
   serial.print(f("attempting connect network named: "));
   serial.println(ssid);                   // print network name (ssid);

   // connect wpa/wpa2 network. change line if using open or wep network:    
   status = wifi.begin(ssid, pass);
   // wait 10 seconds connection:
   delay(tendelay);
 }
}

void loadconf(){

 if (!sd.begin(4)) {
   serial.println(f("initialization sd failed!"));
   return;
 }
 serial.println(f("iniialization sd done."));

 int ssidlength = 0;
 int passlength = 0;
 // open file measure string's length
 myfile = sd.open(conf);
 if (myfile) {
   i = i0;
   while (myfile.available()) {
     char c = myfile.read();
     if(i==i0){
       if(c == '\n'){
         i = i1;        
       }
       else{
         ssidlength++;
       }
     }
     else{
       if(c != '\r'){
         passlength++;
       }
     }
   }
   // close file:
   myfile.close();
 }

 char ssid[ssidlength+1];
 char pass[passlength+1];

 // re-open file reading strings
 myfile = sd.open(conf);
 if (myfile) {
   i = i0;
   int j = i0;

   while (myfile.available()) {
     char c = myfile.read();
     if(i==i0){
       if(c == '\n'){
         i = i1;
         //closing string
         ssid[j++] = '\0';
         j = i0;          
       }
       else{
         ssid[j++] = c;
       }
     }
     else{
       if(c != '\r'){
         pass[j++] = c;
       }
     }
   }
   //closing string
   pass[j++] = '\0';
   // close file:
   myfile.close();
 }
 else {
   // if file didn't open, print error:
   serial.println(f("error opening file."));
 }

 serial.println(ssid);
 serial.println(pass);

 connectwifi(ssid, pass);
}



Arduino Forum > Using Arduino > Programming Questions > Problem reading data from SD card and use it to connect on WiFi


arduino

Comments

Popular posts from this blog

Connecting Raspberry Pi 2 to P10(1R)-V706 LED Dot Matrix - Raspberry Pi Forums

TypeError: <unknown> is not a numpy array - Raspberry Pi Forums

datso and removing imagetitle - Joomla! Forum - community, help and support