Pages: [1] 2   Go Down
Author Topic: Can SD Card be used to store ip address/MAC configs?  (Read 4067 times)
0 Members and 1 Guest are viewing this topic.
Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'd like to use the SD Card on the Ethernet Sheild so that  a file on it can be edited with a PC to set the Arduino's static IP and MAC address.  Has anyone done this?
 
My need for this is I'd like to be able to send some co-workers an Arduino preloaded with my code.  Using a PC, I'd like them to be able to set the board to work on their LAN without me having to set the IP ahead of time or them having to recompile in IDE.   The Arduino would load the local ip settings read from the file on boot up.

I thought I might try working this out myself, but if someone has already done it, I sure would like some help.

Thanks
Logged

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 14
Posts: 307
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A very short answer is, totally absolutely without any shadow of a doubt yes you can ..... now I just need to find the code to show you how ...... I shall pack my back back and go a searching for it

hop hop hop ........
Logged


Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Great.  I wasn't sure if the SD card would interfere with the Ethernet setup. Now I know it's possible, I'll start to write my own code.   If you  finish 'hopping' before I can figure it out, please post some code for me.  Thanks very much for your help, and all of the great input on this forum from other posters.  It really helps a newbie like me get up and running.
Logged

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 14
Posts: 307
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I found parts of the code buried deep in a few routines, so I pulled  it out and re-assembled it to resemble something you might be able to use.
Note, the original code was developed by someone other than myself.

You will need to make a text file named params.txt.
The format is name1:value1,name2:value2,
Where the names for your purposes will be ip0 ip1 ip2 ans ip3
The values can be what you want.
So for example for an ip of 192.168.1.10, the text in the file will need to be ip0:192,ip1:168,ip2:1,ip3:10,
Remembering to have the coma at the end as well as this is needed to check that we have come to the end.

Make sure you have your libraries that you will be needing.

I haven't tested this example below, but I am hopeful you will be able to work it out.

It's a shame the code wrapper throws out the nice tab spacing, oh well, what can one do....

Paul

Code:
#include "SD.h"
#include "Ethernet.h"


uint8_t SD_CS = 4; // Chip Select line for SD card:
uint8_t mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
uint8_t ip[] = {192,168,1,10};

void setup() {
Serial.begin(9600);

readFile(); // read parameters from SD card:
Ethernet.begin(mac, ip); // configure Ethernet:

}
//------------------------------------------------------------------------
// void readFile()
// read file whose format is name1:value1,name2:value2,
// last value must end with a comma

void readFile() {
File file;
SD.begin(SD_CS);
file=SD.open("params.txt");
if (file) {
int i = 0; // array pointer:
char chtr; // variable to hold value from file:
char name[4]; // array for name data:
char value[8]; // array for value data:

while (file.available()) { // read the settings file:
i = 0;
boolean paramEnd = false;
boolean valueEnd = false;
memset(name, 0, 4);
memset(value, 0, 8);

while (!paramEnd) { // while not at the end of the parameter keep reading:
chtr = file.read();
if (chtr != 58) { // if the char from file is not a':' read it in:
name[i] = chtr;
i++;
} else {
paramEnd = true; // otherwise we have come to the end of our parmeter name:
}
}
i = 0; // reset our array pointer:
while (!valueEnd) {
chtr = file.read();
if (chtr != 44) { // if the char from file is not a ',' read it in:
value[i] = chtr;
i++;
} else {
valueEnd = true; // otherwise we have come to the end of our value:
}
}

if (strcmp(name, "ip0")) {
ip[0] = atoi(value); // assign value to ip0 first ip octet:
}
if (strcmp(name, "ip1")) {
ip[1] = atoi(value); // assign value to ip1 first ip octet:
}
if (strcmp(name, "ip2")) {
ip[2] = atoi(value); // assign value to ip2 first ip octet:
}
if (strcmp(name, "ip3")) {
ip[3] = atoi(value); // assign value to ip4 first ip octet:
}
}
}
}
Logged


Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I added this code to my sketch and I guess I was hoping it would work "right out of the gate".  I kept plugging away at it until it would compile with no errors. While it's been a great learning experience, I haven't got it working yet. 

When I had no success with my code modification, I started playing with some of the Arduino SD card example sketchs.  I got the test program to write and read my SD card so now I have to incorporate some of the code from that example, your code and merge it with my sketch.

It may be that I haven't inittalized the arduino pin (pin 4 on the Arduino Ethernet Shield??) propperly.  I don't think your code makes mention of it either.

In your code  does it also try to map the MAC address from the SD card as well?  Your notes only seem to refer to ip0 thru ip3, while some of the 'lines' do seem be be referring to MAC entries.  I'm assuming I'll have to add similar lines for MAC0 thru MAC7.  Also I'll eventually have to add the Gateway and Subnet enteries from the card read.  One thing at a time I guess.

Again, thanks for the code.  It'll definately turbo charge my sketch writing efforts.

Mark
Logged

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 14
Posts: 307
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You can expand on it to also have the mac address pulled from the file as well.
I thought just keep it more simple with the ip address octets.
The mac address you see in the code is just a standard array initialise needed for the Ethernet.begin statement.

No, the Ethernet chip select is pin 10 and the SD card chip select is pin 4, each needs its own chip select and they need to be both set as outputs in your program.
e.g.
Code:
pinMode(4, OUTPUT);
pinMode(10, OUTPUT);

Something I should have added is that you may need to add a little extra code to disable the Ethernet chip while the SD.begin does it thing.
Try adding the following to your setup section;
Code:
void setup() {
    pinMode(4, OUTPUT);      // set SD card chip select as output:
    pinMode(10, OUTPUT);    // set Ethernet chip select as output:

    pinMode(10, HIGH);         // disable ethernet by pulling high its chip select:
    sdBegin():                         // initialise the SD card:
    readFile();                       // read parameters from SD card:

    delay(150);                      // allow some time for Ethernet processor to come out of reset on Arduino power up:
    Ethernet.begin(mac, ip);
}


look at thread re disabling Ethernet CS before SD.begin arduino.cc/forum/index.php/topic,125646.0.html

Paul
« Last Edit: October 26, 2012, 09:27:57 pm by rockwallaby » Logged


Dallas, TX
Offline Offline
Sr. Member
****
Karma: 10
Posts: 318
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

If you want the IP and MAC addresses to be associated with particular SD cards then you are on the right track. I like to tie the IP and MAC to individual Arduinos so I put them in the EEPROM. If you standardize the EEPROM addresses you use you can use the same procedure to extract them in all your programs.
Logged

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 14
Posts: 307
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yes, using eeprom is also a good idea PapaG, I prefer this concept even better I think.

In fact what you could do, is on startup, you could receive the ip address from say the serial port using a command and then store the received value in eeprom.
I like that idea better actually the ip is then store permanently in eeprom and there is no special need for the SD card.
You could simple give or email your co-workers the ip address they need to enter on their computer which is connected to the Arduino via simple serial/(USB) interface.

You can make the program so that it is a once only operation by checking the contents of eeprom location for valid values.
Or you cam make so that it can be change at any time.

There are many choices.

Paul
Logged


Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

This does sound like a good idea.  The only problem is I know less about this than I do about the SD card.  LOL there's more to learn everywhere I look.

I've been making pretty good progress with the SD method.  I started adding code so that at various stages of the program status updates are sent to the serial monitor.  I added some of the code from the Arduino Reference for testing the SD card.  In that code it reads and reports to the serial monitor the FAT type and any files on the SD card. Then I added your code that reads and assigns the ip values from the params.txt file.  When I run the code it reports the FAT buts stops short of name the files on the SD card and then when your code runs it reports back to the serial monite that the "file" value is zero.  If I run the  SDcardinfo example from the Reference by itself, it reads the SD card and reports the presense of params.txt on the SD card.  For some reason when I added your code, the program can no longe read the file name on the SD card.  My code is way to long to paste it between the hash marks.  I'm not sure how to add a zip file of the whole code, so please forgive me when I just add the section of how I merged the code.
Drat I'm longer than allowed.  I'll post as a separate piece.
Logged

Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Here's the code:
Code:
/*
 Timer sketch using Telnet IP input
 */
 int pw_out = 8;      // moved from pin 12 since Ethernet Shield uses that pin.
 int by_pass = 9;     // By Pass line to select between Internal and Remote Torpey set to pin 9
 int SD_CS = 4;
 int ETH_CS = 10;     // By Pass line to select between Internal and Remote Torpey set to pin 9
 int secs = 0;        // Calculated Seconds for countdown output
 int mins = 0;        // Calculated Minutes for countdown output
 int hours = 0;       // Calculated Hours for countdown output
 int time2display = 0;
 unsigned long T =0;            // Sum of Inputted Time in Seconds
 unsigned long TR = 0;          // Time Run since count started
 unsigned long intmillis = 0;   // initial "millis" reading used to calculate TR
 int s = 0;      // units "seconds" display variable
 int S = 0;      // decade "seconds" display variable
 int m = 0;      // units "minute" display variable
 int M = 0;      // decade "minute" display variable
 int sip = 0;    // Units seconds user input variable
 int Sip = 0;    // Decade Seconds user input variable
 int mip = 0;    // Units minute user input variable
 int Mip = 0;    // Decade Minutes user input variable
 int hip = 0;    // hours users input variable
 int Mode = 1;   // Mode Variable, 0=countdown 1=static 2= countup
 int x = 0;      // Case variable to load one at a time h,M,m,S,s etc into generateRS422
 int p = 0;      // counter to throttle amount of Time Dispaly sent to Telnet Client.
 boolean t = 0;  // toggle IP print flag
 boolean BP = 0;      // Enable disable internal/external Timer.
 boolean SAZ = 0;     // Stop At Zero
 boolean minus = 0;   // sets Minus Flag on Display
  // Chip Select line for SD card:
 byte mac[6] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
 byte ip[4]    = {};
 byte    gateway[4] = {192, 168, 1, 1};  //gateway ip to be set before code is compiled;
 byte    subnet[4]  = {255, 255, 255, 0 };


#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>


Sd2Card card;
SdFile root;
SdVolume volume;


// Other global variables

#define textBuffSize 10 //length of longest command string plus two spaces for CR + LF
char textBuff[textBuffSize]; //someplace to put received text
int charsReceived = 0;

boolean connectFlag = 0; //we'll use a flag separate from client.connected
                         //so we can recognize when a new connection has been created
unsigned long timeOfLastActivity; //time in milliseconds of last activity
unsigned long allowedConnectTime = 300000; //five minutes

EthernetServer server(23); // Telnet listens on port 23
EthernetClient client = 0; // Client needs to have global scope so it can be called
                           // from functions outside of loop, but we don't know
                           // what client is yet, so creating an empty object


void setup() {
  
Serial.begin(9600);
         pinMode(pw_out, OUTPUT);
         pinMode(by_pass, OUTPUT);
pinMode(SD_CS, OUTPUT);         // set SD card chip select as output:        
         //pinMode(ETH_CS, HIGH);          // disable ethernet by pulling high its chip select:
         pinMode(ETH_CS, OUTPUT);        // set ethernet chip select as output:
         digitalWrite(ETH_CS, HIGH);
        
         Serial.print("\nInitializing SD card...");
   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
   // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);     // change this to 53 on a mega
 

  // we'll use the initialization code from the utility libraries
   // since we're just testing if the card is working!
   if (!card.init(SPI_HALF_SPEED, SD_CS)) {
     Serial.println("initialization failed. Things to check:");
     Serial.println("* is a card is inserted?");
     Serial.println("* Is your wiring correct?");
     Serial.println("* did you change the chipSelect pin to match your shield or module?");
     return;
   } else {
    Serial.println("Wiring is correct and a card is present.");
  }
 
  // print the type of card
   Serial.print("\nCard type: ");
   switch(card.type()) {
     case SD_CARD_TYPE_SD1:
       Serial.println("SD1");
       break;
     case SD_CARD_TYPE_SD2:
       Serial.println("SD2");
       break;
     case SD_CARD_TYPE_SDHC:
       Serial.println("SDHC");
       break;
     default:
       Serial.println("Unknown");
   }
 
  // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
   if (!volume.init(card)) {
     Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
     return;
   }
 

  // print the type and size of the first FAT-type volume
   uint32_t volumesize;
   Serial.print("\nVolume type is FAT");
   Serial.println(volume.fatType(), DEC);
   Serial.println();
  
   volumesize = volume.blocksPerCluster();    // clusters are collections of blocks
   volumesize *= volume.clusterCount();       // we'll have a lot of clusters
   volumesize *= 512;                            // SD card blocks are always 512 bytes
   Serial.print("Volume size (bytes): ");
   Serial.println(volumesize);
   Serial.print("Volume size (Kbytes): ");
   volumesize /= 1024;
   Serial.println(volumesize);
   Serial.print("Volume size (Mbytes): ");
   volumesize /= 1024;
   Serial.println(volumesize);
 
  
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
   root.openRoot(volume);
  
  // list all files in the card with date and size
   root.ls(LS_R | LS_DATE | LS_SIZE);
 
        
        
File file;
SD.begin(SD_CS);
         Serial.println ("SD Start");
        
file=SD.open ("params.txt") ;
         Serial.println (file);
if (file) {
int i = 0; // array pointer:
char chtr; // variable to hold value from file:
char name[4]; // array for name data:
char value[8]; // array for value data:

while (file.available()) { // read the settings file:
i = 0;
boolean paramEnd = false;
boolean valueEnd = false;
memset(name, 0, 4);
memset(value, 0, 8);          

while (!paramEnd) {         // while not at the end of the parameter keep reading:
chtr = file.read();
if (chtr != 58) { // if the char from file is not a':' read it in:
name[i] = chtr;
i++;
} else {
paramEnd = true; // otherwise we have come to the end of our parmeter name:
}
}
i = 0; // reset our array pointer:
while (!valueEnd) {
chtr = file.read();
if (chtr != 44) { // if the char from file is not a ',' read it in:
value[i] = chtr;
                                  i++;
} else {
valueEnd = true; // otherwise we have come to the end of our value:
}
}

if (strcmp(name, "ip0")) {
ip[0] = atoi(value); // assign value to ip0 first ip octet:  String Compare "ASCII to int"
                            Serial.print (ip[0]);

}
if (strcmp(name, "ip1")) {
ip[1] = atoi(value);
                                 Serial.print(",");
                                 Serial.print (ip[1]); // assign value to ip1 first ip octet:
}
if (strcmp(name, "ip2")) {
ip[2] = atoi(value);
 Serial.print(",");
                                  Serial.print (ip[2]); // assign value to ip2 first ip octet:
}
if (strcmp(name, "ip3")) {
ip[3] = atoi(value);
                                  Serial.print(",");
                                  Serial.print (ip[3]);
                                  Serial.println ("done"); // assign value to ip4 first ip octet:
}
}
}
        

        
        
        
        
         //delay(150);                     // allow some time for Ethernet processor to come out of reset on Arduino power up:
         digitalWrite(ETH_CS, LOW);       // set ethernet chip select as output:
         Ethernet.begin(mac, ip, gateway, subnet);
         server.begin();
        
         Serial.println(Ethernet.localIP());
        

}

void loop(){
Logged

Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I should have added this.  It's  a cut and paste from the monitor output.  It loops continually this message:

Code:
Card type: SD2

Volume type is FAT16

Volume size (bytes): 2012872704
Volume size (Kbytes): 1965696
Volume size (Mbytes): 1919

Files found on the card (name, date and size in bytes):
SD Start
0
0.¡Ê+‹¥…±¥é¥¹SD card...Wiring is correct and a card is present.

Card type: SD2

Volume type is FAT16

Volume size (bytes): 2012872704
Volume size (Kbytes): 1965696
Volume size (Mbytes): 1919

Files found on the card (name, date and size in bytes):
SD Start
0
0.¡Ê+‹¥…±¥é¥¹SD card...Wiring is correct and a card is present.


I assume since it is looping the program is basically resetting when it trys to read the file.
Logged

Tasmania - Australia
Offline Offline
Sr. Member
****
Karma: 14
Posts: 307
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi,

A couple of things I notice on initial read of your code.

Firstly, you are setting the Ethernet chip select pin as an output twice
Code:
         pinMode(ETH_CS, OUTPUT);        // set ethernet chip select as output:
         digitalWrite(ETH_CS, HIGH); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< here as ETH_CD is declared as 10
         
         Serial.print("\nInitializing SD card...");
   // On the Ethernet Shield, CS is pin 4. It's set as an output by default.
   // Note that even if it's not used as the CS pin, the hardware SS pin
  // (10 on most Arduino boards, 53 on the Mega) must be left as an output
  // or the SD library functions will not work.
  pinMode(10, OUTPUT);     // change this to 53 on a mega <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< and here remove this line


It appears the you are not getting the very first line that you are requesting to print to the serial console;
Code:

         Serial.print("\nInitializing SD card...");
Did you change this from when you copied the console output?

It appears the program has an issue with the following code or you have no files on your card as it does not list any;
Code:
  Serial.println("\nFiles found on the card (name, date and size in bytes): ");
   root.openRoot(volume);
   
  // list all files in the card with date and size
   root.ls(LS_R | LS_DATE | LS_SIZE);

Remove the space after the word open, it's a function, no space there;
Code:
file=SD.open ("params.txt") ;

You also need to put the SD.begin() statement in the order like I originally proposed, like thus;
Code:
void setup() {
    pinMode(4, OUTPUT);      // set SD card chip select as output:
    pinMode(10, OUTPUT);    // set Ethernet chip select as output:

    pinMode(10, HIGH);         // disable ethernet by pulling high its chip select:
    sdBegin():                         // initialise the SD card:
    readFile();                       // read parameters from SD card:

    delay(150);                      // allow some time for Ethernet processor to come out of reset on Arduino power up:
    Ethernet.begin(mac, ip);
}

And then remove the SD.begin() from the following point, it needs to be done as stated above;
Code:
File file;
SD.begin(SD_CS);
         Serial.println ("SD Start");
Also, see above, again you have a space in the Serial.println, get rid of it, it is not correct, though it does appear to function.
Check the rest of your code for these spaces you seem to want to put in before the '(...)'

Where yo declare your ip array, you declare it empty, with no default values, I would leave it how I proposed initially, by having something in there, like thus;
Code:
uint8_t ip[] = {192,168,1,10}; <<<<<<<<<<<<<<<<<<<<<<<<<< have some default values in case of SD problems or such like.

Unless you really need it, I would be inclined to remove all that code which reports information about the card, you can see that your SD interface is working already.
It only makes is a little harder to debug and takes up resources.


You could use more Serial.print("debug point: doing what ever here") type statements to help you know what your program is doing and where it does get to.

Try these things and repost your code and any output then.

Paul
Logged


Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Paul,

I started a new sketch where I stripped out more or less everything that wasn't needed.  It's getting close, but now I have a problem with the memory array.  It's not populating  the IP correctly.  I put a 'debug' "Hello" print statement in the code so that I could see what was being loaded.  Here's the code followed by the Serial Monitor output:
Code:
#include <SD.h>
 #include <SPI.h>
 #include <Ethernet.h>
 
  byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
  byte ip[]    = {10, 0, 0, 59};
 // set up variables using the SD utility library functions:
 Sd2Card card;
 int SD_CS = 4;  


 void setup() {
  
   Serial.begin(9600);

   // disable w5100 SPI while setting up SD
  
   digitalWrite(10,HIGH);  // Disabled

   Serial.print("Starting SD...");
   pinMode(10,OUTPUT);
   if(!SD.begin(4)) Serial.println("SD failed to start");
   else Serial.println("ok");
   // SD.begin() returns with its SPI disabled. Good SD library!
   if (!card.init(SPI_HALF_SPEED, SD_CS)) {
     Serial.println("Initialization Failed.");
     return;
   } else {
    Serial.println("Wiring is correct and a card is present.");
  }
  
 
   delay (2000);

   Serial.print("Starting w5100...");
   Serial.println();
   Serial.print("Routine is setting IP to.... ");
  
   readFile();
      
   Ethernet.begin(mac, ip);
   Serial.println();
   Serial.print("Arduino reports back its IP as ");
   Serial.println(Ethernet.localIP());

    
   // rest of your setup
}

void loop(){}


void readFile() {
File file;
SD.begin(4);
file=SD.open("params.txt");
if (file) {
int i = 0; // array pointer:
char chtr;        // variable to hold value from file:
char name[4];        // array for name data:
char value[8]; // array for value data:

while (file.available()) { // read the settings file:
i = 0;
boolean paramEnd = false;
boolean valueEnd = false;
memset(name, 0, 4);
memset(value, 0, 8);

while (!paramEnd) { // while not at the end of the parameter keep reading:
chtr = file.read();
if (chtr != 58) { // if the char from file is not a':' read it in:
name[i] = chtr;
i++;
} else {
paramEnd = true;         // otherwise we have come to the end of our parmeter name:
                               Serial.print(" Hello ");
                               Serial.print(chtr);
}
}
i = 0; // reset our array pointer:
while (!valueEnd) {
chtr = file.read();
if (chtr != 44) { // if the char from file is not a ',' read it in:
value[i] = chtr;
i++;
} else {
valueEnd = true; // otherwise we have come to the end of our value:
}
}

if (strcmp(name, "ip0")) {
ip[0] = atoi(value); // assign value to ip0 first ip octet:
                         Serial.print (ip[0]);
}
if (strcmp(name, "ip1")) {
ip[1] = atoi(value); // assign value to ip1 first ip octet:
                         Serial.print (ip[1]);
}
if (strcmp(name, "ip2")) {
ip[2] = atoi(value); // assign value to ip2 first ip octet:
                         Serial.print (ip[2]);
}
if (strcmp(name, "ip3")) {
ip[3] = atoi(value); // assign value to ip4 first ip octet:
                         Serial.print (ip[3]);
}
}
}
}

Serial Monitor Output:

Code:
Starting SD...ok
Wiring is correct and a card is present.
Starting w5100...
Routine is setting IP to....  Hello :192192192 Hello :168168168 Hello :111 Hello :200200200
Arduino reports back its IP as 200.200.200.1

I tried figuring out why the array is being loaded 3 times per octet.  My head started to burst so I thought I would post my results and see if smarter minds can figure it out.
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48556
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
SD.begin(4);
This belongs in setup(), not in readFile.

Code:
while (file.available()) { // read the settings file:
i = 0;
boolean paramEnd = false;
boolean valueEnd = false;
memset(name, 0, 4);
memset(value, 0, 8);
You want the initialization of these variables to happen outside the read-from-file loop, not inside it.

Code:
if (chtr != 58) { // if the char from file is not a':' read it in:
If you had,
Code:
   if(chtr != ':')
the comment would not be needed.

Code:
if (chtr != 44) { // if the char from file is not a ',' read it in:
Same here, except using ',' in the code.

You don't the memset stuff. Each time you are done with the contents of name or value, simply set the 0th element to NULL.

Code:
                         Serial.print (ip[0]);
I really hate to see stuff like this. When a number appears in the Serial Monitor, what does it mean?

Code:
Serial.print("ip[0] = [");
Serial.print(ip[0]);
Serial.println("]");
leaves NO doubt about what the number means.

What, exactly is in your file? The print statements don't make much sense with knowing that.
Logged

Vancouver Canada
Offline Offline
Newbie
*
Karma: 0
Posts: 45
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hey another Paul! Thanks for your quick input.  I like your suggestions.  Replacing those ASCII numbers with the 'character' is why better.  

This sketch isn't meant to be a finished project on its own.  I started it as a method to figure out why the SD card wasn't reading the "params.txt" file in my previous attempts.  Most of the Serial print statements are just a means of debugging the sketch to see what's going wrong at certain steps.  As you can see from my Serial Monitor output I finally got the sketch to open and read the text file.  The only problem (besides the my poorly written sketch) is the array isn't populating correctly.

Anyway when you said
Quote
You don't the memset stuff. Each time you are done with the contents of name or value, simply set the 0th element to NULL.
I think you're probably onto my problem, but I really don't know enough to understand what you mean and where I am to place the NULL.  These loops were provided to me by the other Paul from this thread and I don't understand how they are supposed to work either.  Give me a few more days and I might figure it out.
Logged

Pages: [1] 2   Go Up
Jump to: