Go Down

Topic: Program compiles, but breaks setup() (Read 4238 times) previous topic - next topic

wimdh

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!

Code: [Select]

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


Coding Badly


[font=Courier New]void toByte (char data[], byte *target) {
    /*
     * Converteer een char array met een ip addres,
     * geef een array van 4 integers terug.
     */
    char tmp[ 4 ];
[/font]

pekkaa

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

Code: [Select]
Serial.begin(9600);
Serial.print("Server address:");


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

mkwired

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

wimdh

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


Cheers!
--

wimdh

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!

mkwired


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.

Code: [Select]

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

wimdh

mkwired,

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


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

dxw00d


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


Erm... an IP address has 4 numbers in it, not 3 :P  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:
Quote
the tmp variable should contain the 3 digits (0-255) of each ip octet

majenko

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

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

Go Up