Mega 2560 and Ethernet shield


I have a problem with a project of mine. I use a Mega2560 to read temperature and light sensors with 1 second frequency. The readings are sent on the network to a server where is processed. My problem is that, after a while (couple minutes) the arduino no longer respond to the server requests. If I activate the serial monitor from the laptop (now the board is powered and connected on USB) I see that the setup() method is called again and the board start responding again. But only for couple of minutes. Probably the board is entering in sleep mode but I didn't find a method to disable this permanently (I need the board to work always because he needs to read sensors and communicate with the server continuously). I need some help with this, any suggestion would be appreciated. Thank you.

You should post your code. Here is my ethernet client code:

It should not be entering a sleep mode. It is probably running out of sockets on the w5100.

//timer 1Hz
  if (connectedCounterCheck>waitPeriodBeforeDisconnected){
    digitalWrite(DISCONNECTED_PIN, LOW);
    isConnected = 0;
  } else { 
    temp1 = readTemperature(temperatureSensor1);
    temp2 = readTemperature(temperatureSensor2);
    lightSensor = String(analogRead(LIGHT_SENSOR_PIN));
    String val = "<root>";
    val += "<temp><t1>"+temp1+"</t1><t2>"+temp2+"</t2></temp><light>"+lightSensor+"</light>";
    val += "</root>";

void setup() {
  //22...35 digital out - 13 out
  pinMode(23, OUTPUT);
  pinMode(24, OUTPUT);
  pinMode(25, OUTPUT);
  pinMode(26, OUTPUT);
  pinMode(27, OUTPUT);
  pinMode(28, OUTPUT);
  pinMode(29, OUTPUT);
  pinMode(30, OUTPUT);
  pinMode(31, OUTPUT);
  pinMode(32, OUTPUT);
  pinMode(33, OUTPUT);
  pinMode(34, OUTPUT);
  pinMode(35, OUTPUT);
  pinMode(36, INPUT);
  pinMode(37, INPUT);
  pinMode(38, INPUT);
  pinMode(39, INPUT);
  pinMode(40, INPUT);
  pinMode(41, INPUT);
  pinMode(42, INPUT);

  pinMode(43, INPUT);
  pinMode(44, INPUT);
  pinMode(45, INPUT);
  pinMode(46, INPUT);

  pinMode(47, INPUT);
  pinMode(48, INPUT);
  pinMode(49, INPUT);
  pinMode(50, INPUT);
  digitalWrite(LIGHT_SENSOR_PIN, HIGH);
  noInterrupts();           // disable all interrupts
  TCCR3A = 0;
  TCCR3B = 0;
  TCNT3  = 0;

  OCR3A = 62500;            // compare match register 16MHz/512/1Hz
  TCCR3B |= (1 << WGM32);   // CTC mode
  TCCR3B |= (1 << CS32);    // 256 prescaler 
  TIMSK3 |= (1 << OCIE3A);  // enable timer compare interrupt

  Serial.println("intialization complete");

void loop()
  int packetSize = Udp.parsePacket();
  int test = 0;
    String val = "";
    IPAddress remote = Udp.remoteIP();,UDP_TX_PACKET_MAX_SIZE);
    String request = String(packetBuffer);
    for(int i=0;i<UDP_TX_PACKET_MAX_SIZE;i++) packetBuffer[i] = 0;
    String cmd = getCommand(request);
    Serial.println("Received: "+request);
    if (cmd == "s") {  //ping
      int check = cal.mustSyncTime();
      if (check == 1) {
       //sync time from server info
    } else if (cmd == "w") {  //write settings

    } else if (cmd == "c") {  //change setting
      String option = getOption(request);
      String value = getOptionValue(request);
      int pin = option.toInt();
      uint8_t pinShort = uint8_t(pin);
      pinMode(pinShort, OUTPUT);
      if (value == "off"){
        digitalWrite(pinShort, LOW);
      } else {
        digitalWrite(pinShort, HIGH);
  int button = 0;
  int checkIfMustSend = 0;
  for (int i=0; i<12; i++){
    int pin = 36+i;
    button = digitalRead(pin);
    if (input[i] != button) {
      checkIfMustSend = 1;
      input[i] = button;
  if (checkIfMustSend == 1){
      String val = "<root><button>";
      for (int i=0; i<12; i++){
        val += "<b"+String(i)+">"+input[i]+"</b"+String(i)+">";
      val += "</button></root>";

This is the code. I already try to disable the sleep mode but it seems that is not working. I am new on Arduino programming.

That is not all the code. Where is this function?


Are you using UDP or TCP? Either way, calling an ethernet function from an interrupt is not a good idea. Many functions count on the time for delays, and it isn't working right in an interrupt.

UDP. I need to call this from the interrupt because I need to send the status of the sensors with at 1s interval.

There are other ways to do that without an interrupt. You can use a delay(1000) or use millis() to determine a second.

void loop()
    IPAddress udpIP(1,2,3,4);
    int udpPort = 8888;

    char msg[] = "blablabla";
    Udp.beginPacket(udpIP, udpPort);

I cannot put a delay because I also need (in loop) to read the status of some inputs (sensors and command buttons) but it seems that, if I move the send to server logic from the interrupt and added to the loop method works. I set a variable on the interrupt method when it's called and then, in loop, if this is set, the I read the sensors and send to server (also reset the variable). Seems that this works ok. Thank you very much for your help.

You may have memory problems. Try setting all your variables to "" at the end of each loop where they are used.