I found the blow code from SurferTim, and though it works asis, it didn't in my project, it changed the netmask and gateway so they start wit 0 (zero), I found out that it was related to the order i had defined my variables. mine are like this.
byte myMac[6];
byte myIP[4];
byte myDNS[4];
byte myGW[4];
byte myNM[4];
I am not an educated programmer, but there's have to an other explanation than the order??
Hope someone would take the time to clear this out for me......
I'am using Arduion 1.0.6 on a Mega and a WiFi shield W5100
To use the code this needs to be on the SD card in file "CONFIG1.DAT"
DE:AD:BE:EF:FE:EE
192.168.100.178
255.255.255.0
192.168.100.1
192.168.100.1
#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(115200);
pinMode(10,OUTPUT);
digitalWrite(10,HIGH);
if(!SD.begin(4)) Serial.println("SD fail");
else Serial.println("SD ok");
File fh = SD.open("CONFIG1.DAT",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() {
}