I'm trying to read network parameters from a "config.txt" file on a sd card.
It reads properly and the values are being stored into variables.
The problem is that it converts the first byte of each array to zero.
The first set of values are my var netBuffer read from txt, the second set are the values stored in the variables.
mac 12:34:56:78:90:AB
ip 192.168.2.2
netmask 255.255.255.0
gateway 192.168.2.1
dns 192.168.2.1
mac 0:34:56:78:90:AB
ip 0.168.2.2
netmask 0.255.255.0
gateway 0.168.2.1
dns 192.168.2.1
The content of the txt file is:
12:34:56:78:90:AB
192.168.2.2
255.255.255.0
192.168.2.1
192.168.2.1
The code is:
#include <SPI.h>
#include <SD.h>
byte myMac[7];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];
const int chipSelect = 4;
void setup() {
Serial.begin(115200);
while (!Serial) {
;
}
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
while (1);
}
File dataFile = SD.open("config.txt");
char netBuffer[32];
int chPos = 0;
int lineNo = 0;
if (dataFile) {
while (dataFile.available()) {
char ch = dataFile.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;
}
}
dataFile.close();
}
int x;
Serial.println("mac ");
for (x = 0; x < 6; x++) {
Serial.print(myMac[x], HEX);
if (x < 5) Serial.print(":");
}
Serial.println("ip ");
for (x = 0; x < 4; x++) {
Serial.print(myIP[x], DEC);
if (x < 3) Serial.print(".");
}
Serial.println("netmask ");
for (x = 0; x < 4; x++) {
Serial.print(myNM[x], DEC);
if (x < 3) Serial.print(".");
}
Serial.print("gateway ");
for (x = 0; x < 4; x++) {
Serial.print(myGW[x], DEC);
if (x < 3) Serial.print(".");
}
Serial.print("dns ");
for (x = 0; x < 4; x++) {
Serial.print(myDNS[x], DEC);
if (x < 3) Serial.print(".");
}
}
void loop() {
}
Can anyone please help me figure out what am I doing wrong?