[Resolved] Ethernet Rev3 Board - Periodically is not ping-able on startup

Hello all, new to the forum and pretty new to Arduino. Working with the Ethernet Rev3 board and a handful of times the IP doesn't seem to 'stick' and the device is not ping-able. I have to cycle power a couple times and it will eventually be accessible. I'm not using DHCP I'm providing the IP, not sure what it could be my code is below.

Basically what is happening is I read an SD card to get the IP that I would like (it is a simple text file with just the IP inside). Then I store that in the ip variable, which has a value as a fallback incase the SD card doesn't read.

Then in the loop it listens for various messages to turn the mag sensors on and off, but when I can't ping it, no messages get to the board of course :wink:

Any help would be greatly appreciated!

#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Wire.h>
#include <Ethernet.h>
#include <EthernetUdp.h>         // UDP library from: bjoern@cs.stanford.edu 12/30/2008
#include <SD.h>          // SD Card Library

//Ethernet Setup.
PROGMEM byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0xE0, 0x50 };
IPAddress ip(10, 201, 1, 39);
IPAddress remoteAddress(10, 201, 1, 24);
unsigned int localPort = 8888;      // local port to listen on
boolean receivedStart = false;
char packetBuffer[5]; //buffer to hold incoming packet,

// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;

struct demo_output {
  uint8_t		magx_h_0;
  uint8_t		magx_l_0;
  uint8_t		magy_h_0;
  uint8_t		magy_l_0;
  uint8_t		magz_h_0;
  uint8_t		magz_l_0;

  uint8_t		magx_h_1;
  uint8_t		magx_l_1;
  uint8_t		magy_h_1;
  uint8_t		magy_l_1;
  uint8_t		magz_h_1;
  uint8_t		magz_l_1;

  uint8_t		magx_h_2;
  uint8_t		magx_l_2;
  uint8_t		magy_h_2;
  uint8_t		magy_l_2;
  uint8_t		magz_h_2;
  uint8_t		magz_l_2;

  uint8_t		magx_h_3;
  uint8_t		magx_l_3;
  uint8_t		magy_h_3;
  uint8_t		magy_l_3;
  uint8_t		magz_h_3;
  uint8_t		magz_l_3;
} 
my_demo;

void setup()
{
  Serial.begin(9600);          // start serial communication at 9600bps
  Wire.begin();                // join i2c bus as master.
  SDSetup();                   //Setup SD Card. and set IP
  Ethernet.begin(mac, ip);     // start the Ethernet:
  Udp.begin(localPort);        //Start UDP
}

void SDSetup()
{
  File myFile;
  String IPstr = "";
  
  pinMode(10, OUTPUT);
  if (SD.begin(4)) {
     myFile = SD.open("IPData.txt");
 
    if (myFile) {
      // read from the file until there's nothing else in it:
      while (myFile.available()) {
          IPstr = IPstr + (char)myFile.read();
      }
      // close the file:
      myFile.close();
      
      Serial.println(IPstr);
      
      char* p;
      IPstr.toCharArray(p,IPstr.length()+1);
      char* str;
      int i = 0;
      while ((str = strtok_r(p, ".", &p)) != NULL) // delimiter is the semicolon
      {
        ip[i] = atoi(str);
        i++;
      }
    } else {
      // if the file didn't open, print an error:
      Serial.println("error opening IPData.txt");
    }
  }
}

void loop()
{   
    int packetSize = Udp.parsePacket();
    //Serial.println(packetSize);
    if(packetSize)
    {
      //Store IP as Remote IP
      remoteAddress = Udp.remoteIP();
      
      // read the packet into packetBufffer
      Udp.read(packetBuffer,5);
      
      //For Testing Only.
      Serial.println("Contents:");
      Serial.println(packetBuffer);
      
      if (strcmp(packetBuffer,"INIT") == 0)
      {
          //receivedIP = true;  
      } 
      else if (strcmp(packetBuffer,"GOGO") == 0)
      {
          receivedStart = true;
          //receivedIP = true;  
      }
      else if (strcmp(packetBuffer,"STOP") == 0)
      {
          receivedStart = false;
          //receivedIP = true;  
      }
    }
     
    if (receivedStart)
    {
      //Sensor 0:
      i2c_write(0x30, 0x07, 0x80); // Refill Charge Cap
      delayMicroseconds(55);
      i2c_write(0x30, 0x07, 0x20); //SET Instruction 
      i2c_write(0x30, 0x07, 0x40); //RESET Instruction
      i2c_write(0x30, 0x07, 0x01); // take measurement TM Flag == 1
      while ( (i2c_read(0x30, 0x06) & 0x01) != 1 ); // Wait until Measurement Done
      //Read 0
      my_demo.magx_h_0 = i2c_read(0x30, 0x01);
      my_demo.magx_l_0 = i2c_read(0x30, 0x00);
      my_demo.magy_h_0 = i2c_read(0x30, 0x03);
      my_demo.magy_l_0 = i2c_read(0x30, 0x02);		
      my_demo.magz_h_0 = i2c_read(0x30, 0x05);
      my_demo.magz_l_0 = i2c_read(0x30, 0x04);
      
      //Sensor 1:
      i2c_write(0x31, 0x07, 0x80); // Refill Charge Cap
      delayMicroseconds(55);
      i2c_write(0x31, 0x07, 0x20); //SET Instruction 
      i2c_write(0x31, 0x07, 0x40); //RESET Instruction
      i2c_write(0x31, 0x07, 0x01); // take measurement TM Flag == 1
      while ( (i2c_read(0x31, 0x06) & 0x01) != 1 ); // Wait until Measurement Done
      //Read 1
      my_demo.magx_h_1 = i2c_read(0x31, 0x01);
      my_demo.magx_l_1 = i2c_read(0x31, 0x00);
      my_demo.magy_h_1 = i2c_read(0x31, 0x03);
      my_demo.magy_l_1 = i2c_read(0x31, 0x02);		
      my_demo.magz_h_1 = i2c_read(0x31, 0x05);
      my_demo.magz_l_1 = i2c_read(0x31, 0x04);
      
      //Sensor 2:
      i2c_write(0x32, 0x07, 0x80); // Refill Charge Cap
      delayMicroseconds(55);
      i2c_write(0x32, 0x07, 0x20); //SET Instruction 
      i2c_write(0x32, 0x07, 0x40); //RESET Instruction
      i2c_write(0x32, 0x07, 0x01); // take measurement TM Flag == 1
      while ( (i2c_read(0x32, 0x06) & 0x01) != 1 ); // Wait until Measurement Done
      //Read 0
      my_demo.magx_h_2 = i2c_read(0x32, 0x01);
      my_demo.magx_l_2 = i2c_read(0x32, 0x00);
      my_demo.magy_h_2 = i2c_read(0x32, 0x03);
      my_demo.magy_l_2 = i2c_read(0x32, 0x02);		
      my_demo.magz_h_2 = i2c_read(0x32, 0x05);
      my_demo.magz_l_2 = i2c_read(0x32, 0x04);
      
      Udp.beginPacket(remoteAddress, 8888);
      Udp.write(convertObjectToJson());
      Udp.endPacket();
    }
}

void i2c_write(uint8_t device_address, uint8_t register_address, uint8_t data_write)
{
  Wire.beginTransmission(device_address);
  Wire.write(byte(register_address));
  Wire.write(byte(data_write));
  Wire.endTransmission(false);
}

uint8_t i2c_read(uint8_t device_address, uint8_t register_address)
{
  uint8_t result;

  Wire.beginTransmission(device_address);
  Wire.write(byte(register_address));
  Wire.endTransmission(false);

  Wire.requestFrom(int(device_address),1); //Read 1 byte back..
  int i = 0;
  while (Wire.available() < 1)
  {
    //Just Waiting...
    i++;
    if (i > 50)
    {
      break;
    }
  }
  
  result = Wire.read();

  return (result);
}

unsigned int CombineBits(uint8_t x_high, uint8_t x_low)
{
  unsigned int combined;          //We Use Unsigned Int due to 2 bit limitations.
  combined = x_high;              //send x_high to rightmost 8 bits
  combined = combined<<8;         //shift x_high over to leftmost 8 bits
  combined |= x_low;                 //logical OR keeps x_high intact in combined and fills in rightmost 8 bits  
  return combined;
}

char* convertObjectToJson()
{
  char charBuf[350];
  sprintf(charBuf,
    "{\"sensors\":[{\"id\":0,\"data\":{\"xMag\":%u,\"yMag\":%u,\"zMag\":%u}},{\"id\":1,\"data\":{\"xMag\":%u,\"yMag\":%u,\"zMag\":%u}},{\"id\":2,\"data\":{\"xMag\":%u,\"yMag\":%u,\"zMag\":%u}}]}"
    ,CombineBits(my_demo.magx_h_0,my_demo.magx_l_0),CombineBits(my_demo.magy_h_0,my_demo.magy_l_0),CombineBits(my_demo.magz_h_0,my_demo.magz_l_0),CombineBits(my_demo.magx_h_1,my_demo.magx_l_1),CombineBits(my_demo.magy_h_1,my_demo.magy_l_1),CombineBits(my_demo.magz_h_1,my_demo.magz_l_1),CombineBits(my_demo.magx_h_2,my_demo.magx_l_2),CombineBits(my_demo.magy_h_2,my_demo.magy_l_2),CombineBits(my_demo.magz_h_2,my_demo.magz_l_2));
  //Serial.println(charBuf);
  return charBuf;
}

I occasionally store my network setup on the SD card. I use a Mega, but it should work fine on the Arduino Ethernet as well. Here is a link to the code and file I use:
http://forum.arduino.cc/index.php?topic=128763.msg976118#msg976118

Feel free to borrow as much or little as you need from that.

Thank you that did the trick! My IP was being parsed incorrectly from the SD card. I switched to your method and all seems to be fixed. It seems your way used more memory so I had to optimize some things to keep everything running, but optimization is never bad thing right 8)

Thank You For Your Help!