Sketch hangs with DHCP and XBee

I have a sketch that uses Xbee series 1 with an Ethernet R3 shield. It works fine with static IP, but hangs if I try to use dynamic. I tried it on an Uno and Mega 2560, same result with both.

I’m using Arduino IDE v1.0.1. Libraries are the standard Ethernet library and XBee library 0.3.

Anyone else have this problem? Any solutions?

My sketch is attached.

Pool_Monitor_Inside.ino (34.6 KB)

It works fine with static IP, but hangs if I try to use dynamic.

It hangs where? What is the XBee contributing to the process of getting a dynamic IP address?

Is there any difference if you (try to) get the dynamic IP address before initializing the xbee object?

What XBee shield are you using? Insure you do not have a memory card in the SD slots.

I was tweaking the program last night, basically shortening my strings going to twitter, shortening my Serial.print() strings and putting them in flash using Serial.print(F("put me in FLASH")). So now it works on the Mega, which seem weird because I had lots of extra memory (over 6K RAM), so I'm surprised that these changes made a difference. I'm using this code to display free RAM:

int freeRam(bool PrintRam)
{
  int freeSRAM;
  extern int __heap_start, *__brkval;
  int v;
  freeSRAM =  (int) &v - (__brkval == 0 ? (int) &__heap_start : (int) __brkval);
  if(PrintRam)
  {
  Serial.print(F("RAM "));
  Serial.println(freeSRAM);
  }
  return freeSRAM;
}

Now on the UNO, if I use static IP I have 359 bytes of ram at the end of setup(). Then it drops to to 292 right before I send a message to twitter, then it drops again to 192 bytes when I send to COSM and it stays at 192 bytes and everything works okay.

If I use DHCP, I have 259 at the end of setup, then it drops to 184 bytes right before I send a tweet, then it's 201 bytes right before I try to upload to COSM. Then the UNO reboots right after this code:

Serial.println((F("\n\nSend 2 COSM"));

Also in setup() function, I use Serial.println("Setup") but the first two letters are garbled when they print out.

SurferTim: What XBee shield are you using? Insure you do not have a memory card in the SD slots.

Xbee Series 1. No card in slot. In setup() have this code to disable microSD

  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);

Sounds like you ran out of SRAM on the Uno. What does your memory routine report on the Mega?

edit: Here is the link to the XBee series 1 radio. https://www.sparkfun.com/products/8664

The Sparkfun XBee shield is not Mega compatible.

SurferTim: Sounds like you ran out of SRAM on the Uno. What does your memory routine report on the Mega?

If I use DHCP: 5899 after setup() 5823 right before tweeting 5841 right before sending to cosm 5718 after sending to cosm - this is where is stays

If I use static IP 5999 after setup() 5931 right before tweeting 5949 right before sending to cosm 5826 after sending to cosm - this is where is stays

You are out of SRAM on the Uno. Only 2K on the Uno, and 8K on the Mega. You are down to less than 6K on the Mega.

SurferTim: You are out of SRAM on the Uno. Only 2K on the Uno, and 8K on the Mega. You are down to less than 6K on the Mega.

I agree, that's the problem. I'm kind of surprised using DHCP uses up more memory even after it's got an IP address. I can see how it needs extra memory when it's getting the IP address because it's doing more stuff then when using static IP, but why doesn't that memory get released after it has the IP?

I have not looked through the dhcp code to give you an answer on that, but if I get a chance, I will.

Normally, dhcp does require more memory than static because it must deal with an ip lease expiration and renewal.

edit: If you use dhcp and don't renew the lease (my routers issue a 2 day lease), the router may issue your ip to another device. :(

I had exactly the same problem and I solved it using the mega too.