Program compiles, but breaks setup()

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);
    }
  }
}

void toByte (char data[], byte target) {
/

  • Converteer een char array met een ip addres,
  • geef een array van 4 integers terug.
    */
    char tmp[ 4 ];

Have you set the same serial speed in the serial monitor and in the sketch?

If that doesn't solve the problem, try to reduce the sketch by removing everything that is not related to the problem. For example, remove everything but

Serial.begin(9600); 
Serial.print("Server address:");

from setup() and check if it still behaves erratically.

What board and/or what shield are you using, and which version of the Arduino IDE are you using?

I'm using the Arduino Uno with an ethernet shield.
I'm running that latest version of Arduino: 1.0.1

Cheers!

Coding Badly, indeed it was the tmp variable XD

But I don't understand why :~ the tmp variable should contain the 3 digits (0-255) of each ip octet??? Why should I define it as tmp[4] then?

Thanks!

wimdh:
But I don't understand why :~ the tmp variable should contain the 3 digits (0-255) of each ip octet??? Why should I define it as tmp[4] then?

You need a null-terminator. Changing the size to 4 is still incorrect. The last character needs to be set to 0. You could just use the following code.

void toByte (char* data, byte *target) {
  for (int i=0; i!=4; ++i) {
    *(target++) = atoi(data);
    data = strchr(data, '.')+1;
  }
}

mkwired,

That's all? I look like a fool with my 500k lines of code here :slight_smile:
Well it'll get better when I'll be past the newbie stage!

Thanks a lot for the help (to all of you)!

majenko:

the tmp variable should contain the 3 digits (0-255) of each ip

Erm... an IP address has 4 numbers in it, not 3 :stuck_out_tongue: Also, they should be stored as "unsigned char", not "char". A char holds -128 to 127. An unsigned char holds 0 to 255.

You missed the important word:

the tmp variable should contain the 3 digits (0-255) of each ip octet

... I'll get my coat ...

Serves me right for not reading all the OP's code :stuck_out_tongue: