Arduino uno sktech si blocca

#include <Ethernet.h>
#include <PubSubClient.h>
#include <SPI.h>
#define CLIENT_ID       "Giardino"
//#define TOPIC           "temp"
#define PUBLISH_DELAY    3000
#define subscribe_DELAY    50000
//#include <avr/dtostrf.h>
#include <avr/io.h>
#include <avr/wdt.h>

#define Reset_AVR() wdt_enable(WDTO_30MS); while(1) {}

#define relayPin1 6
#define relayPin2 4
#define relayPin3 5
#define relayPin4 7


String ip = "";
bool relaystate1 = LOW;
bool relaystate2 = LOW;
bool relaystate3 = LOW;
bool relaystate4 = LOW;
//int luminosita;  //Il valore letto dalla fotoresistenza
//int umidita;
//int sensorepioggia;
int x=0;
int a=0;
int b=0;
int c=0;
int CICLO=1;
unsigned long y=0;

EthernetClient Giardino;
PubSubClient mqttClient(Giardino);

unsigned long previousMillis;
unsigned long previousMillis2;
unsigned long lastMillis;

uint8_t mac[6] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x02};

void setup() {
   pinMode(relayPin1, OUTPUT);
  pinMode(relayPin2, OUTPUT);
  pinMode(relayPin3, OUTPUT);
  pinMode(relayPin4, OUTPUT);
  
   pinMode(A1, INPUT);
   pinMode(8, INPUT);
   pinMode(2,INPUT);
  // setup serial communication

  Serial.begin(9600);
  while (!Serial) {};
  Serial.println(F("MQTT Arduino Demo"));
  if (Ethernet.begin(mac) == 0) {
    //Serial.println(F("Unable to configure Ethernet using DHCP"));
    for (;;);
  }

  Serial.println(F("Ethernet configured via DHCP"));
  Serial.print(F("IP address: "));
  Serial.println(Ethernet.localIP());
  Serial.println();
  /*
    Serial.println(Ethernet.localIP()[0]);
    Serial.println(Ethernet.localIP()[1]);
    Serial.println(Ethernet.localIP()[2]);
    Serial.println(Ethernet.localIP()[3]);
  */
  if (Ethernet.linkStatus() == LinkOFF) {
    //Serial.println(F("Ethernet cable is not connected."));
  }

  ip = String (Ethernet.localIP()[0]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[1]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[2]);
  ip = ip + ".";
  ip = ip + String (Ethernet.localIP()[3]);
  //Serial.println(ip);

  // setup mqtt client
  mqttClient.setClient(Giardino);
  //mqttClient.setServer(mqttServer, 1883);
  // mqttClient.setServer("test.mosquitto.org", 1883);
  //  mqttClient.setServer( "raspberrypi.local",1883);
  //mqttClient.setServer("io.adafruit.com",1883);
  mqttClient.setServer( "192.168.1.100", 1883);
  //Serial.println(F("MQTT client configured"));
  mqttClient.setCallback(callback);
  
  previousMillis = millis();
 
 
    
    Serial.println();
}

  

  void loop() {
  
 
   char msgBuffer[20];
  if (millis() - previousMillis2 > subscribe_DELAY){
        //luminosita = !digitalRead(2);  //Lettura della luminosità
       //sensorepioggia=digitalRead(8);
       //umidita = analogRead(A1);
       previousMillis2 = millis();
      mqttClient.publish("casa/Giardino/umidità",dtostrf(analogRead(A1),0,2, msgBuffer));
      mqttClient.publish("casa/Giardino/luminosita",dtostrf( (!digitalRead(2)),1,0, msgBuffer));
      mqttClient.publish("casa/Giardino/sensorepioggia",dtostrf(digitalRead(8),0,1, msgBuffer));

}
  if (millis() - previousMillis > PUBLISH_DELAY) {
      
       
    sendData();
    previousMillis = millis();
    relaystate1 = digitalRead(relayPin1);// Reads the state of a relay
    relaystate2 = digitalRead(relayPin2);// Reads the state of a relay
    relaystate3 = digitalRead(relayPin3);// Reads the state of a relay
    relaystate4 = digitalRead(relayPin4);// Reads the state of a relay
  
  
  }

  mqttClient.loop();
  
   if (x == 1)
  {
    if (millis() - lastMillis > y && a == 1) 
    { //Serial.println(F("iniziato Ciclo If"));
      delay(10000);
      digitalWrite(relayPin2, LOW);
      b = 1;
      lastMillis = millis();
      digitalWrite(relayPin3, HIGH);
      a = 0;
      mqttClient.publish("casa/Giardino/conferma3", "1");
      mqttClient.publish("casa/Giardino/conferma2", "0");
    }
    if (b == 1)
    {
      if (millis() - lastMillis > y && b == 1)
      { //Serial.println(F("secondo step"));
        delay(10000);
        digitalWrite(relayPin3, LOW);
        b = 0;
        lastMillis = millis();
        c =1;
        digitalWrite(relayPin4, HIGH);
        mqttClient.publish("casa/Giardino/conferma4", "1");
      mqttClient.publish("casa/Giardino/conferma3", "0");
       
      }
    }
    if (c == 1)
    {
      if (millis() - lastMillis > y && c == 1)
      { Serial.println("terzo step");
        delay(10000);
        digitalWrite(relayPin4, LOW);
        c = 0;
        lastMillis = millis();
        
      mqttClient.publish("casa/Giardino/conferma4", "0");
          // Serial.println(F("fine ciclo irrigazione"));
      }
  }
  
  
}

 
if (!mqttClient.connected()) {
  
reconnect();
}
 
}


void sendData() {
  
  char msgBuffer[20];
   
  if (mqttClient.connect(CLIENT_ID)) {
    
      
    mqttClient.subscribe("casa/Giardino/sb1");
    mqttClient.subscribe("casa/Giardino/sb2");
    mqttClient.subscribe("casa/Giardino/sb3");
    mqttClient.subscribe("casa/Giardino/sb4");
    mqttClient.subscribe("casa/Giardino/ciclo");
    mqttClient.subscribe("casa/Giardino/iniziociclo");
    mqttClient.subscribe("casa/Giardino/presenza");
   
  }
}

void callback(char* topic, byte* payload, unsigned int length) {
  char msgBuffer[20];
  
  // I am only using one ascii character as command, so do not need to take an entire word as payload
  // However, if you want to send full word commands, uncomment the next line and use for string comparison
   //payload[length] = '\0';            // terminate string with '0'
  //String strPayload = String((char*)payload);  // convert to string
  // Serial.println(strPayload); //can use this if using longer southbound topics
  //Serial.print(F("Message arrived ["));
  //Serial.print(topic);
  String topicStr = topic;
  String messageTemp;
 /* Serial.print("] ");//MQTT_BROKER
  Serial.println();
  Serial.print(F("Topic: "));
  Serial.println(topicStr);
  */
  for (int i = 0; i < length; i++) {
    //Serial.print((char)payload[i]);
    
    messageTemp += (char)payload[i];
  }
  //Serial.println();
 // Serial.println(payload[0]);
  
  
  
   if (topicStr == "casa/Giardino/sb1") 
    {


  if (messageTemp == "1")             // Message "1" in ASCII (turn output ON)
  {
        //
    digitalWrite(relayPin1, HIGH);
    mqttClient.publish("casa/Giardino/conferma1", "1");
  } else if (messageTemp == "0")      // Message "0" in ASCII (turn output OFF)
  {
    digitalWrite(relayPin1, LOW);     //
    mqttClient.publish("casa/Giardino/conferma1", "0");
    ;
  }

    
   }
   
   if (topicStr == "casa/Giardino/sb2") 
    {


  if (messageTemp == "1")             // Message "1" in ASCII (turn output ON)
  {
        //
     mqttClient.publish("casa/Giardino/conferma2", "1");
    digitalWrite(relayPin2, HIGH);
  } else if (messageTemp == "0")      // Message "0" in ASCII (turn output OFF)
  {
    digitalWrite(relayPin2, LOW);     //
     mqttClient.publish("casa/Giardino/conferma2", "0");
    
  }

    
   }
    if (topicStr == "casa/Giardino/sb3") 
    {


  if (messageTemp == "1")             // Message "1" in ASCII (turn output ON)
  {
        //
     mqttClient.publish("casa/Giardino/conferma3", "1");
    digitalWrite(relayPin3, HIGH);
  } else if (messageTemp == "0")      // Message "0" in ASCII (turn output OFF)
  {
    digitalWrite(relayPin3, LOW);     //
     mqttClient.publish("casa/Giardino/conferma3", "0");
    
  }

    
   }
    if (topicStr == "casa/Giardino/sb4") 
    {


  if (messageTemp == "1")             // Message "1" in ASCII (turn output ON)
  {
        //
     mqttClient.publish("casa/Giardino/conferma4", "1");
    digitalWrite(relayPin4, HIGH);
  } else if (messageTemp == "0")      // Message "0" in ASCII (turn output OFF)
  {
    digitalWrite(relayPin4, LOW);     //
     mqttClient.publish("casa/Giardino/conferma4", "0");
    
  }

    
   }

    if (topicStr == "casa/Giardino/ciclo") 
    {
CICLO =  messageTemp.toInt();


  mqttClient.publish("casa/Giardino/confermareciclo", messageTemp.c_str()); 
   y = CICLO * 50000;
    
   }
 
   if (topicStr == "casa/Giardino/iniziociclo") 
    { 
       if (messageTemp == "1")             // Message "1" in ASCII (turn output ON)
  {
        //
      x=1;
      a=1;
      digitalWrite(relayPin2, HIGH);
      mqttClient.publish("casa/Giardino/conferma2", "1");
      mqttClient.publish("casa/Giardino//conferma/iniziociclo", "1");
  } else if (messageTemp == "0")      // Message "0" in ASCII (turn output OFF)
  {
      x=0;
      a=0;
      
       mqttClient.publish("casa/Giardino//conferma/iniziociclo", "0");
  digitalWrite(relayPin2, LOW);
  digitalWrite(relayPin3, LOW);
  digitalWrite(relayPin4, LOW);
  mqttClient.publish("casa/Giardino/conferma2", "0");
  mqttClient.publish("casa/Giardino/conferma3", "0");
  mqttClient.publish("casa/Giardino/conferma4", "0");
  
  
  }

 
    
   }
   
  if (topicStr == "casa/Giardino/presenza") 
    {
    if (messageTemp == "ip") {
      mqttClient.publish("casa/Giardino/ip", ip.c_str());
      mqttClient.publish("casa/Giardino/nb/relay1", (relaystate1 == LOW) ? "OPEN" : "CLOSED");
      mqttClient.publish("casa/Giardino/nb/relay2", (relaystate2 == LOW) ? "OPEN" : "CLOSED");
      mqttClient.publish("casa/Giardino/nb/relay3", (relaystate1 == LOW) ? "OPEN" : "CLOSED");
      mqttClient.publish("casa/Giardino/nb/relay4", (relaystate2 == LOW) ? "OPEN" : "CLOSED");
   
      
      
    }
  
  
    
   }  
    
   }
  
    
  

   void reconnect()
   {
// Loop until we're reconnected
while (!mqttClient.connected()) {

// Attempt to connect
if (mqttClient.connect("Giardino")) {
Serial.println("connected");
// Once connected, publish an announcement...

} else {

Serial.print(mqttClient.state());

// Wait 5 seconds before retrying
delay(5000);
Reset_AVR();

 
}
}
}

Aiuto. Questo sketch gira,funziona ma ogni tanto si blocca. Memoria libera ce.ne(40%) e occupazione circa 70%.
Secondo voi?

i did not understand what you mean.

Perche lo sketch si blocca?!

you not using the cayenne library and pinpoint what is causing the code to freeze is difficult.

vabbe io provo a chiedere una mano, ho provato il codice su arduino due e arduino uno, su arduino uno resisiste 3/4 ore, su arduino due 1/2 giorni

provo a riproporre la domanda. qualcuno potrebbe aiutarmi?

this,