Arduino due offline mentre lavora

//#define CAYENNE_DEBUG       // Uncomment to show debug messages
#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";

#define RELAY_DIGITAL_PIN 9
#define RELAY_DIGITAL_PIN2 4
#define RELAY_DIGITAL_PIN3 5
#define RELAY_DIGITAL_PIN4 7

#define PUBLISH_DELAY   5000

int luminosita;  //Il valore letto dalla fotoresistenza
int umidita;
int sensorepioggia;

int CICLO=1;
int x=0;
int a=0;
int b=0;
int c=0;

unsigned long lastMillis=0;
unsigned long previousMillis=0;


unsigned long y=0;

void setup() {
  Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
   pinMode(RELAY_DIGITAL_PIN, OUTPUT);
   pinMode(RELAY_DIGITAL_PIN2, OUTPUT); 
   pinMode(RELAY_DIGITAL_PIN3, OUTPUT);
   pinMode(RELAY_DIGITAL_PIN4, OUTPUT); 
   
   digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
  digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
  
  
  
  CAYENNE_LOG("Sync data");
  Serial.print("valore del ciclo =");
  Serial.println(CICLO);
  

        Cayenne.virtualWrite(4,0);
        Cayenne.virtualWrite(5,0);
        Cayenne.virtualWrite(6,0);
        Cayenne.virtualWrite(21,0);
        Cayenne.virtualWrite(22,0);
        Cayenne.virtualWrite(23,0);
   
   pinMode(A1, INPUT);
   pinMode(8, INPUT);
   pinMode(2,INPUT);
   }

void loop() {
  Cayenne.loop();
  if (millis() - previousMillis > PUBLISH_DELAY) {
    
    previousMillis = millis();
    
    luminosita = !digitalRead(2);  //Lettura della luminosità
 
    umidita = analogRead(A1);
  Serial.print("luminosita = ");
  Serial.println(luminosita);
  Serial.print("umidita = ");
  Serial.println(umidita);
  sensorepioggia=digitalRead(8);
   Serial.print("sensore pioggia =");
  Serial.println(sensorepioggia);
   

  }

  


  if (x == 1)
  {
    if (millis() - lastMillis > y && a == 1) 
    { delay(10000);
      digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
      b = 1;
      lastMillis = millis();
      digitalWrite(RELAY_DIGITAL_PIN3, LOW);
      a = 0;
      Serial.println("iniziato Ciclo If");
      Cayenne.virtualWrite(21,0);
       Cayenne.virtualWrite(22,1);
    }
    if (b == 1)
    {
      if (millis() - lastMillis > y && b == 1)
      { delay(10000);
        digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
        b = 0;
        lastMillis = millis();
        c =1;
        digitalWrite(RELAY_DIGITAL_PIN4, LOW);
        Serial.println("secondo step");
        Cayenne.virtualWrite(22,0);
        Cayenne.virtualWrite(23,1);
      }
    }
    if (c == 1)
    {
      if (millis() - lastMillis > y && c == 1)
      { delay(10000);
        digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
        c = 0;
        lastMillis = millis();
        
       
        Serial.println("terzo step");
     
        Cayenne.virtualWrite(23,0);
         Cayenne.virtualWrite(10,0);
          Cayenne.virtualWrite(11,0);
           Serial.println("fine ciclo irrigazione");
      }
  }
  
  
}

    
  
}

// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
  // Write data to Cayenne here. This example just sends the current uptime in milliseconds on virtual channel 0.
  Cayenne.virtualWrite(0, millis());
  
  //Cayenne.virtualWrite(1, luminosita);
  Cayenne.luxWrite(1, luminosita);
  Cayenne.virtualWrite(2, umidita,TYPE_RELATIVE_HUMIDITY,UNIT_PERCENT);
   Cayenne.virtualWrite(8, sensorepioggia);
   
   
    //Write data to Cayenne here. This example just sends the current uptime in milliseconds.
    
      Serial.println("Finito invio");
  //sensorepioggia=digitalRead(10);
  //Serial.println(sensorepioggia);
  //Cayenne.virtualWrite(10, sensorepioggia);
  
  //Cayenne.virtualWrite(3, 50, TYPE_PROXIMITY, UNIT_CENTIMETER);
}

// Default function for processing actuator commands from the Cayenne Dashboard.
// You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
 CAYENNE_IN(3)
 {
 CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN, LOW);
  }
  }
  

CAYENNE_IN(4)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN2, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN2,LOW);
  }
  
}
 CAYENNE_IN(5)
{
 CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN3, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN3, LOW);
  }
  }
  

CAYENNE_IN(6)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  // get value sent from dashboard
  int currentValue = getValue.asInt(); // 0 to 1

  // assuming you wire your relay as normally open
  if (currentValue == 0) {
    digitalWrite(RELAY_DIGITAL_PIN4, HIGH);
  } else {
    digitalWrite(RELAY_DIGITAL_PIN4, LOW);
  }
  
}
CAYENNE_IN(10)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
   x = getValue.asInt(); // 0 to 1
  lastMillis = millis();
  digitalWrite(RELAY_DIGITAL_PIN2 , LOW);
 
  a = 1;
  Serial.println(" inizio ciclo da  =");
  Serial.println(y);
  Cayenne.virtualWrite(11,1);
  Cayenne.virtualWrite(21,1);
  }
  
CAYENNE_IN(12)
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
 
  CICLO = getValue.asInt(); // 0 to 1
  y = CICLO * 50000;
  Serial.print(" Valore variabile ciclo =");
  Serial.println(y);
}
CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("Channel %u, value %s", request.channel, getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

7:41:18.197 -> [205390] Publish: topic 1, channel 8, value 0, subkey , key
17:41:18.272 -> Finito invio
17:41:18.306 -> luminosita = 0
17:41:18.306 -> umidita = 1023
17:41:18.340 -> sensore pioggia =0
17:41:21.291 -> [208613] Message received: topic 2, channel 3
17:41:21.358 -> [208613] In: value 0, channel 3
17:41:21.393 -> [208613] CAYENNE_IN_DEFAULT(3) - qLLH7wwHozzGqIr, 0
17:41:21.426 -> [208619] publishState: topic 1 channel 3
17:41:21.497 -> [208662] Publish: topic 1, channel 3, value 0, subkey , key
17:41:21.532 -> [208727] Send response: qLLH7wwHozzGqIr
17:41:23.229 -> [210530] Disconnected
17:41:23.229 -> [210530] Connecting to mqtt.mydevices.com:1883
17:41:25.030 -> [212352] Network connect failed
17:41:27.878 -> [215174] Network connect failed
17:41:30.690 -> [217996] Network connect failed

come si evince dal codice e da cio che succede con arduino ide, come si va dare il comando del rele (3) si disconnette sempre, ho optato per la modalità debug cosi si vede precisamente a quale listato succede.
sapete aiutarmi?

can you try changing the relay pins.

lo fa su tutti e 4 i pin rele

can you disconnect all the relay and use this code to just give it a try if it is something to do with arduino pins:

/*
Cayenne Relay Switch Example
This sketch sample file shows how to set up a Relay Switch with Cayenne.
The CayenneMQTT Library is required to run this sketch. If you have not already done so you can install it from the Arduino IDE Library Manager.
Steps:
1. In the Cayenne Dashboard add a new Relay Switch widget.
2. Select a virtual channel number for the widget.
3. Set the VIRTUAL_CHANNEL value below to the virtual channel you selected.
4. Connect your relay switch to a digital pin.
5. Set the ACTUATOR_PIN value below to the digital pin number you selected.
6. Set the Cayenne authentication info to match the authentication info from the Dashboard.
7. Compile and upload this sketch.
8. Once the Arduino connects to the Dashboard you can use the widget button to turn the relay switch on and off.
*/

#define CAYENNE_PRINT Serial  // Comment this out to disable prints and save space
#include <CayenneMQTTEthernet.h>

// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

#define VIRTUAL_CHANNEL 1
#define ACTUATOR_PIN 4 // Do not use digital pins 0 or 1 since those conflict with the use of Serial.

void setup()
{
  Serial.begin(9600);
  pinMode(ACTUATOR_PIN, OUTPUT);
  Cayenne.begin(username, password, clientID);
}

void loop()
{
  Cayenne.loop();
}

// This function is called when data is sent from Cayenne.
CAYENNE_IN(VIRTUAL_CHANNEL)
{
	// Write value to turn the relay switch on or off. This code assumes you wire your relay as normally open.
	if (getValue.asInt() == 0) {
		digitalWrite(ACTUATOR_PIN, HIGH);
	}
	else {
		digitalWrite(ACTUATOR_PIN, LOW);
	}
}

credo sia un problema hardware. lo fa solo con il rele dove ci sono collegati delle lampadine, mentre a quelli con le elettrovalvole no.
forse qualche ritorno di tensione o qualche oscillazione che fa scollegare l arduino.
l’arduino è collegato in ethernet insieme ad uno switch a cui è collegato il pc.
l’arduino è alimentato dal pc .
non so cosa fare

are you powering the relay from arduino? https://forum.arduino.cc/index.php?topic=425880.0

si, come sempre fatto, ma perche i primi 3 si e l’altro no? io penso sia un problema di tensioni di ritorno

check the arduino forum link.

mi parla di alimentazioni da 12v a 5v. nel mio caso le alimentazioni e i rele funzionano

i rele funzionano perfettamente, forse alimentati a 3.3v da arduino 2 potrebbero non chiudere bene. non far disconette la scheda, forse si tratta di potenzioni trasferiti

niente, anche lasciandolo tutta la notte senza toccare niente al mattino tutto disconnesso

i am having difficulty understanding what you want

Potrebbe essere un problema di rete?

why it will be an internet problem. Without relay connected it works fine.

Se cambio il pin rele e ne metto uno senza hardware. Tutto funziona bene. Sto facendo varie prove

that what i mentioned without relay there is no issue. You need to use proper hardware to power and isolate the relay.

i rele sono isolati perche lavorano a 24v AC e comandano dei contattori, l unico dubbio è l’hardware di arduino e la switch a cui è sotteso tutto. devo provare a sostituirli,
ho provato anche ha cambiare programmazione e usare la libreria pubsub con sistema mqtt e anche quella si blocca in ricezione/invio.

regarding the hardware issue you can find more on Arduino forum https://forum.arduino.cc/index.php?topic=365156.0