hello everybody,
I want to store the ip config in EEPROM and load it from there. To do this, I wrote this piece of code.
It compiles, but it seems to break the setup() routine.
Instead of printing "Server address: 192.3168.1.6" it prints "Se".
I'm sure some clever guys here can tell me what I'm doing wrong...
Thanks a lot!
#include <EEPROM.h>
#include <Ethernet.h>
#include <SPI.h>
#include <String.h>
#define CONFIG_START 0
#define CONFIG_VERSION "a01"
struct settings {
char ipAddress[16], netMask[16], gateway[16];
unsigned int tankSize, hysteresis, autoFill;
char settings_version[4];
}
// Default values.
settings = {
"192.168.1.6", "255.255.255.0", "192.168.1.1",
1000,10,0,
CONFIG_VERSION
};
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x29, 0x71};
byte ip[4];
byte nm[4];
byte gw[4];
unsigned int localPort = 23; // local port to listen on
EthernetServer server(localPort);
boolean alreadyConnected = false;
void toByte (char data[], byte *target) {
/*
* Converteer een char array met een ip addres,
* geef een array van 4 integers terug.
*/
char tmp[3];
int cnt = 0;
int idx = 0;
for (int i=0; i<16; i++) {
if (data[i] == '.' or data[i] == '\0') {
target[idx] = atoi(tmp);
if (data[i] == '\0') {
break;
} else {
idx++;
cnt = 0;
strcpy(tmp, " ");
}
} else {
tmp[cnt] = data[i];
cnt++;
}
}
}
void setup() {
toByte(settings.ipAddress, ip);
toByte(settings.netMask, nm);
toByte(settings.gateway, gw);
Ethernet.begin(mac, ip, gw, nm);
server.begin();
Serial.begin(9600);
Serial.print("Server address:");
Serial.println(Ethernet.localIP());
}
void loop() {
// wait for a new client:
EthernetClient client = server.available();
// when the client sends the first byte, say hello:
if (client) {
if (!alreadyConnected) {
// clean out the input buffer:
client.flush();
Serial.println("We have a new client");
client.println("Hello, client!");
alreadyConnected = true;
}
if (client.available() > 0) {
// read the bytes incoming from the client:
char thisChar = client.read();
server.write(thisChar);
Serial.write(thisChar);
}
}
}