Hi Guys,
i want to store ip, netmask and gateway from cfg/network.cfg on SD card. I'm able to read these values, but from some reason value mask[0] is overwriten with 0.
I don't see where could be problem. Here is the code:
#include <SdFat.h>
#include <SPI.h>
#include <Ethernet.h>
byte mac[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[4] ={0};
byte gw_ip[4] = {0};
byte mask[4] = {0};
char cfg_file[]="cfg/network.cfg";
EthernetServer server(8080);
#define REQ_BUF_SZ 100
char HTTP_req[REQ_BUF_SZ] = {0}; // buffered HTTP req
SdFat sd;
#define error(s) sd.errorHalt_P(PSTR(s))
void setup() {
Serial.begin(9600);
pinMode(10, OUTPUT); // set the SS pin as an output (necessary!)
digitalWrite(10, HIGH); // but turn off the W5100 chip!
if (!sd.begin(4, SPI_HALF_SPEED)) sd.initErrorHalt();
read_cfg();
Ethernet.begin(mac,ip,gw_ip,gw_ip,mask);
Serial.println(Ethernet.localIP());
Serial.println(Ethernet.subnetMask());
Serial.println(Ethernet.gatewayIP());
Serial.println(Ethernet.dnsServerIP());
server.begin();
Serial.println("server begin");
delay(200);
}
void loop()
{
}
void read_cfg(){
char junk[10];
ifstream sdin(cfg_file);
if (!sdin.is_open()) error("open");
while (sdin.getline(HTTP_req, REQ_BUF_SZ, '\n'))
{
Serial.println(HTTP_req);
if(strncmp(HTTP_req, "ip",2) == 0)
{
sscanf(HTTP_req, "%2s=%u.%u.%u.%u", &junk, &ip[0], &ip[1], &ip[2], &ip[3]);
}
else if(strncmp(HTTP_req, "netmask", 7) == 0)
{
sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &mask[0], &mask[1], &mask[2], &mask[3]);
}
else if(strncmp(HTTP_req, "gateway", 7) == 0)
{
sscanf(HTTP_req, "%7s=%u.%u.%u.%u", &junk, &gw_ip[0], &gw_ip[1], &gw_ip[2], &gw_ip[3]);
}
// More record checking/parsing
}
}
cfg/network.cfg file I have:
ip=10.0.55.80
netmask=255.255.255.0
gateway=10.0.55.1
And when I run this program output is as following:
ip=10.0.55.80
netmask=255.255.255.0
gateway=10.0.55.1
10.0.55.80
0.255.255.0
10.0.55.1
10.0.55.1
server begin
As you can see, first part of netmask is 0.
Could you provide me some help? Thank you.