Go Down

Topic: Can SD Card be used to store ip address/MAC configs? (Read 4 times) previous topic - next topic

SurferTim

#26
Oct 31, 2012, 01:13 pm Last Edit: Oct 31, 2012, 01:31 pm by SurferTim Reason: 1
The reported size by the compiler is the program memory, not SRAM. The Uno has only 2K, and the Mega has 8K. The SD.open() function requires a 500 byte buffer, even if the file is only 20 bytes. That is a quarter of your SRAM to open that file.

There is a MemoryFree library function in the playground.
http://www.arduino.cc/playground/Code/AvailableMemory

edit:I think I used the one from github.
https://github.com/homecontrol/Arduino-MemoryFree

If you have questions about that, fat16lib hangs around the "Storage" section of the forum. He is the guy to ask.

mfriesen

I'm using an Uno.  I'm only trying to open the one file, "params.txt" or as in the later method, "Network.txt".  Neither is more than 1k. 

How can I tell how much SRAM I am using.  Is it the 'file' size reported at the load to the Uno from IDE?


SurferTim

#24
Oct 30, 2012, 02:36 pm Last Edit: Oct 30, 2012, 03:01 pm by SurferTim Reason: 1
Quote
While both yours and mine work 'stand alone'  as soon as I try to merge either one with my sketch which calls up some Telnet configurations, the SD Card read fails.

Are you using an Uno or a Mega? You may be running out of SRAM when you merge the sketches with that telnet code.

edit: According to fat16lib, you can open as many SD files as you want now, but at the cost of 500 bytes SRAM per open file.

mfriesen

This looks good.  While both yours and mine work 'stand alone'  as soon as I try to merge either one with my sketch which calls up some Telnet configurations, the SD Card read fails.  I 'fooled' around with different sketchs, but I'm having little to no progress with any of them.  I'm not the type of guy that quits, but I think I'll investigate the suggestion earlier on in this thread to set the variables in EEPROM.  Eventually I'll make this work, but I have to move on.

Thanks

SurferTim

#22
Oct 29, 2012, 09:48 pm Last Edit: Oct 29, 2012, 10:05 pm by SurferTim Reason: 1
Why don't you store all the network settings? I used this for the contents of a file named "network.txt". The mac must be the first line, followed by ip, netmask, gateway, and dns in that order. Each on their own line.
Code: [Select]
12:34:56:78:90:AB
192.168.2.2
255.255.255.0
192.168.2.1
192.168.2.1


Then I used this sketch:
Code: [Select]
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>

byte myMac[6];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];

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

 pinMode(10,OUTPUT);
 digitalWrite(10,HIGH);

 if(!SD.begin(4)) Serial.println("SD fail");
 else Serial.println("SD ok");

 File fh = SD.open("network.txt",FILE_READ);
 char netBuffer[32];
 
 if(!fh)
 {
   Serial.println("SD open fail");
   return;    
 }

 int chPos = 0;
 int lineNo = 0;
 
 while(fh.available())
 {
   char ch = fh.read();
   if(ch == '\r') {
     chPos = 0;

     switch(lineNo) {
       case 0:
//          Serial.print("mac ");        
 sscanf(netBuffer,"%2x:%2x:%2x:%2x:%2x:%2x",&myMac[0],&myMac[1],&myMac[2],&myMac[3],&myMac[4],&myMac[5]);  
       break;

       case 1:
//          Serial.print("ip ");        
 sscanf(netBuffer,"%u.%u.%u.%u",&myIP[0],&myIP[1],&myIP[2],&myIP[3]);  
       break;

       case 2:
//          Serial.print("netmask ");        
 sscanf(netBuffer,"%u.%u.%u.%u",&myNM[0],&myNM[1],&myNM[2],&myNM[3]);  
       break;

       case 3:
//          Serial.print("gateway ");        
 sscanf(netBuffer,"%u.%u.%u.%u",&myGW[0],&myGW[1],&myGW[2],&myGW[3]);  
       break;

       case 4:
//          Serial.print("dns ");        
 sscanf(netBuffer,"%u.%u.%u.%u",&myDNS[0],&myDNS[1],&myDNS[2],&myDNS[3]);  
       break;
     }

//      Serial.println(netBuffer);
     lineNo++;
   }
   else if(ch == '\n') {
      // do nothing
   }
   else if(chPos < 31) {
     netBuffer[chPos] = ch;
      chPos++;
     netBuffer[chPos] = 0;
   }
 }
 
 fh.close();

 int x;
 
 Serial.print("\r\nmac ");
 for(x=0;x<6;x++) {
   Serial.print(myMac[x],HEX);
   if(x<5) Serial.print(":");
 }

 Serial.print("\r\nip ");
 for(x=0;x<4;x++) {
   Serial.print(myIP[x],DEC);
   if(x<3) Serial.print(".");
 }

 Serial.print("\r\nnetmask ");
 for(x=0;x<4;x++) {
   Serial.print(myNM[x],DEC);
   if(x<3) Serial.print(".");
 }

 Serial.print("\r\ngateway ");
 for(x=0;x<4;x++) {
   Serial.print(myGW[x],DEC);
   if(x<3) Serial.print(".");
 }

 Serial.print("\r\ndns ");
 for(x=0;x<4;x++) {
   Serial.print(myDNS[x],DEC);
   if(x<3) Serial.print(".");
 }

 Serial.println("\r\nStarting ethernet");
 Ethernet.begin(myMac,myIP,myDNS,myGW,myNM);
 digitalWrite(10,HIGH);
 
 Serial.println(Ethernet.localIP());
}

void loop() {
}

I commented out some of the serial debugging stuff. You can use it to debug your text file.

edit: Removed the double post. My bad.

Go Up