Arduino DUE with W5100 eth shield hangs/freez random time

I have a Relay controller an Arduino Due board with W5100 to turn on/off relays on MQTT commands

the board hangs at random time. even the serial output is frozen.

back to normal on manual rest.

tried to check if there is a memory leak with memoryfree.h, and the memory free isnt running out.

#include <MemoryFree.h>
#include <PubSubClient.h>
#include "RTClib.h"
#include <SPI.h>                  // For networking
#include <Ethernet.h>             // For networking
#include "Wire.h"                 // For MAC address ROM
#define ENABLE_OLED       true
#define PUBLISH_DELAY   3000
#define ENABLE_DHCP                 true   // true/false
#define ENABLE_MAC_ADDRESS_ROM      false   // true/false
#define MAC_I2C_ADDRESS             0x50   // Microchip 24AA125E48 I2C ROM address
//1void callback(char* topic, byte* payload, unsigned int length);
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEC };  // Set if no MAC ROM
IPAddress ip(192, 168, 31, 39);
char statusTopic []= "events";    // MQTT topic to publish status reports
char messageBuffer[100];
char topicBuffer[50];
char clientBuffer[50];
int Var_Sec = 0;
int pre_Var_Sec=0;
String topics = "/house/switch";
String Topics1 = " ";
String ipaddr= "";
RTC_Millis rtc;
IPAddress broker(192,168,31,219);
static int buttonArray[8] = {2, 3, 4, 5, 6, 7, 8, 9};
int laststate[8] = {0, 0, 0, 0, 0, 0, 0, 0};
static String swarray[8] = {"14", "15", "16", "17", "18", "19", "6", "7"};


EthernetClient ethclient;
PubSubClient client;

void setup() {
  pinMode(4, OUTPUT);
  digitalWrite(4, HIGH);
  Serial.begin(9600);
  delay(250);  
  rtc.begin(DateTime(F(__DATE__), F(__TIME__)));
  if ( ENABLE_OLED == true )
  {
    Serial.println(F("Getting MAC address: "));
    Serial.print  (F("  "));
  }

  //Wire.begin();        // Wake up I2C bus
    while (Ethernet.begin(mac) == 0) {
      Serial.println("Failed to configure Ethernet using DHCP");
      delay(1000);
    } 

  // Print IP address:
  Serial.print(F("My IP:"));
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    if ( thisByte < 3 )
    {
      Serial.print(".");
    }
  }
  Serial.println();
  Serial.println(Ethernet.localIP());
  Serial.println();
  Serial.println(F("My IP:"));
  Serial.print("  ");
  for (byte thisByte = 0; thisByte < 4; thisByte++) {
    // print the value of each byte of the IP address:
    Serial.print(Ethernet.localIP()[thisByte], DEC);
    if ( thisByte < 3 )
    {
      Serial.print(".");
    }
  }
  Serial.println();
  ipaddr = String (Ethernet.localIP()[0]);
  ipaddr = ipaddr + ".";
  ipaddr = ipaddr + String (Ethernet.localIP()[1]);
  ipaddr = ipaddr + ".";
  ipaddr = ipaddr + String (Ethernet.localIP()[2]);
  ipaddr = ipaddr + ".";
  ipaddr = ipaddr + String (Ethernet.localIP()[3]);
  //initialize the switch as an output and set to LOW (off)
  for (int i = 0; i < 8; i++) {
    pinMode(buttonArray[i], OUTPUT);
    digitalWrite(buttonArray[i], HIGH);
  }
  //start the serial line for debugging
  Serial.println("connecting...");
  client.setClient(ethclient);
  client.setServer(broker, 1883);
  client.setCallback(callback);
  String clientString = "Starting Arduino-" + ipaddr;
  client.publish(statusTopic, clientString.c_str());
  Serial.println(clientString);
  Serial.println("Ready.");
}


void sendData() {

  for (int i = 0; i < 8; i++) {
    Topics1 = (topics + "Confirm" + buttonArray[i]);
    if (laststate[i] == 0) {
      client.publish(Topics1.c_str(),"0");
    }
    if (laststate[i] == 1) {
      client.publish(Topics1.c_str(),"1");
    }
  }
}
void callback(char* topic, byte* payload, unsigned int length) {
  //convert topic to string to make it easier to work with
  String topicStr = topic;
  //EJ: Note:  the "topic" value gets overwritten everytime it receives confirmation (callback) message from MQTT
  Serial.println("Callback update.");
  Serial.print("Topic: ");
  Serial.println(topicStr);
  int j;
  char message_buff[10];
  for (j = 0; j<length; j++) {
  message_buff[j] = payload[j];
  }
  message_buff[j] = '\0';
  const char *p_payload = message_buff;
  Serial.println(p_payload);
  for (int i = 0; i < 8; i++) {
    Topics1 = (topics + "Confirm" + buttonArray[i]);
    strcpy(statusTopic,Topics1.c_str());
    if (topicStr == topics + buttonArray[i] ) {
      //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
      if (payload[0] == '1') {
        digitalWrite(buttonArray[i],LOW);
        laststate[i] = 1;
        client.publish(statusTopic, "1");
      }
      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
      else if (payload[0] == '0') {
        digitalWrite(buttonArray[i],HIGH);
        laststate[i] = 0;
        client.publish(statusTopic, "0");
      }
    }
    else if (topicStr == "buttons") {
      String button= "20-" + String(swarray[i]);
      if (button == p_payload)
      {
        if (laststate[i] == 0) {
          digitalWrite(buttonArray[i], LOW);
          laststate[i] = 1;
          client.publish(statusTopic,"1");
        }
        else {
          digitalWrite(buttonArray[i], HIGH);
          laststate[i] = 0;
          client.publish(statusTopic,"0");
        }
      }
    }
  }
}

void reconnect() {
  while (Ethernet.maintain() % 2 == 1) {
      Serial.print("waiting for dhcp reconnect...");
  }
  Serial.print("inside reconnect MQTT connection...");
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    String clientBuffer = "Reconnecting Arduino-" + ipaddr;
    if (client.connect("client1", "xxxxxxxx", "xxxxx")) {
      Serial.println("connected");
      client.publish(statusTopic, clientBuffer.c_str());
      for (int i = 0; i < 8; i++) {
        Topics1 = topics + buttonArray[i];
        client.subscribe(Topics1.c_str());
      }
      client.subscribe("buttons");
      sendData();
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(3000);
    }
  }
}
void loop() {

  //reconnect if connection is lost
  if (!client.connected()) {
    Serial.println("disconncted need to reconnect");
    reconnect();
  }
  DateTime now = rtc.now();
  Var_Sec=now.second();
  if (pre_Var_Sec != Var_Sec){
    if ((Var_Sec % 30) == 0) {
      sendData();
      Ethernet.maintain();
      pre_Var_Sec=Var_Sec;
     }
  }
  client.loop();
}

any help plz

Which version of Ethernet are you using?

its on rev 3

No, the version of the Ethernet library, not the hardware.

In Arduino, click Sketch > Include Library > Manage Libraries. Then choose "Installed" from the "Type" drop-down list in the upper left order, and in the upper right, search for "Ethernet". Then look over the list to find the Ethernet library and check which version you have installed.

sorry its on 1.1.2

Does updating to 2.0.0 help?

just tried to upgrade to 2.0.0
waiting for it to fail

it failed again

started at 9:05 pm worked for 12:10+ hours failed at 9:00 am next day
a manual reset at 9:30 am worked till 2:45 pm

Looking at the code, if I wanted to try running this, seems like I need at least to set up a server/broker at 192.168.31.219. I'd also need to know which libs were used for MemoryFree.h, PubSubClient.h and RTClib.h. Any other details I'd need to know?

Thank you very much for helping me out

here are the requested details

  1. memoryfree.h is just for verifying memory doesn't have functional value was just added recently during tshoot.
  2. pubsubclient 2.6.0 by nick O'leary
  3. RTClib v 1.2.0 by Adafruit

I have an Rasberrypi acting as mqtt broker

mosquitto version 1.4.8 MQTT v3.1 broker

have tried with a poe switch and a normal switch in both cases the issue persists.

for testing reliability I have a python script tying to test the circuit posts mqtt messages to toggel outputs every 10 s
can share that if you need.

thank you