[SOLVED] Arduino Mega unexpected restart

Hello
from several time my project restart when with one button , write a digital output pin where there is a relay.
When press the button after 5/6 times the program restart from setup!
Where i wrong?
Thank you

#define BLYNK_DEBUG // debugga
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d8985243b75b462b8d40fba31xxxxxc1";
// Your WiFi credentials.
 
char ssid[] = "TP-LINK_xxxx";
//char ssid[] = "Vodafone-xxxx4410";
//char ssid[] = "ubnt";
char pass[] = "wifidifabrizioexxxxxxecca";
//char ssid[] = "VodafoneMobileWiFi-599C69";
//char pass[] = "LE7Hxxxx44";
int enablezona1  ;
#define EspSerial Serial1
#define ESP8266_BAUD 9600
ESP8266 wifi(&EspSerial);
int  pinSettore1 = 41; //cancelllo
void setup()
{
  Serial.begin(9600);
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  
  EspSerial.begin(ESP8266_BAUD);
  delay(30);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode( pinSettore1, OUTPUT ); 
  digitalWrite( pinSettore1, HIGH );
   }
BLYNK_WRITE(V1)// input : accende zona 1
{ BLYNK_LOG("zona1 Got a value: % i", param.asInt());
  enablezona1 = param.asInt();
  if (enablezona1 == 1)
  { digitalWrite(pinSettore1, LOW); //accendo il relay
  } else
  { digitalWrite(pinSettore1, HIGH); //spengo il relay
  }
} 
 
void loop()
{ Blynk.run();
  } 

the result is this

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
[117] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on Arduino Mega

[740] Connecting to TP-LINK_Outdoor_871430
[3888] AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32
[11164] +CIFSR:STAIP,"192.168xxxx"
+CIFSR:STAMAC,"60:01:94:0fxxxx"
[11172] Connected to WiFi
[11852] Ready (ping: 108ms).
[19944] zona1 Got a value:  1
[21011] zona1 Got a value:  0
[22090] zona1 Got a value:  1
[22808] zona1 Got a value:  0
[23766] zona1 Got a value:  1
[24608] zona1 Got a value:  0
[25331] zona1 Got a value:  1
[26295] zona1 Got a value:  0
[27279] zona1 Got a value:  1
[285<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
[117] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on Arduino Mega

[740] Connecting to TP-LINK_Outdoor_871430
[3889] AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32
[4940] Failed to enable MUX

i add:
1- the power are all separated , there is a power supply for the Esp8266 stabilizated
2 - the power supply for the Arduino is 5A stabilizated
3 - on the digital pin there are 16 relay - the relay has hits power supply 12v
4 - on arduino board are connected one RTC on SDA and SLC and on A1there is a Hygrometer sensor
5 - i tested without Blynk , with a RTC timer that manage the command LOW and HIGH and work fine
i can post the sketch

@Faben I notice from the timestamps that the button presses are quite close together. To be honest you would think the mega would handle it but you might want to slow it down a bit.

You could also try commenting out the first line of your sketch.

Non [quote=“Costas, post:3, topic:15603, full:true”]
@Faben I notice from the timestamps that the button presses are quite close together. To be honest you would think the mega would handle it but you might want to slow it down a bit.

You could also try commenting out the first line of your sketch.
[/quote]

Thank you
The first liine off debug was commented when i Idid this test
I’m testing this issue from several weeks.

At begin i belived this crash was for the power supplìy of Esp but now i can say that this issuie is on the Mega
Because the power supply now is double.
The crash born even when turn on / off the relay by RTC at specific time
Now i isolated the problem with one button
Next a will give you a test every 1 minute
Thanks

Since you are using one of the Mega’s hardware UARTs you could probably set your BAUD to 19200

ok thank you
now i changed by AT commad from 9600 to 19200 baund
after i used this


//#define BLYNK_DEBUG // debug
#define BLYNK_PRINT Serial    // Comment this out to disable prints and save space
#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "d8985243b75b462b8xxxxx31ec5c3c1";
// Your WiFi credentials.
 
char ssid[] = "TP-LINK_Outdoor_871430";
//char ssid[] = "Vodafone-33734410";
//char ssid[] = "ubnt";
char pass[] = "wifidifaxxxxxterrebecca";
//char ssid[] = "VodafoneMobileWiFi-599C69";
//char pass[] = "LE7HL07244";
int enablezona1  ;
#define EspSerial Serial1
 
#define ESP8266_BAUD 19200
ESP8266 wifi(&EspSerial);
int  pinSettore1 = 41; //cancelllo
void setup()
{
  
  Serial.begin(19200);
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  
  EspSerial.begin(ESP8266_BAUD);
  delay(30);
  Blynk.begin(auth, wifi, ssid, pass);
  pinMode( pinSettore1, OUTPUT ); 
  digitalWrite( pinSettore1, HIGH );
   }


BLYNK_WRITE(V1)// input : accende zona 1
{ BLYNK_LOG("zona1 Got a value: % i", param.asInt());
  enablezona1 = param.asInt();
  if (enablezona1 == 1)
  { digitalWrite(pinSettore1, LOW); //accendo il relay
  } else
  { digitalWrite(pinSettore1, HIGH); //spengo il relay
  }
} 
 
 
void loop()
{ Blynk.run();
  } 

the result is Always one CRASH!

[7766] zona1 Got a value:  1
[11698] ⸮<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
[73] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on Arduino Mega

[619] Connecting to TP-LINK_Outdoor_871430
[3703] AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32
[4734] Failed to enable MUX
[9848] +CIFSR:STAIP,"192.168.1.13"
+CIFSR:STAMAC,"60:01:94:0f:8f:e6"
[9852] Connected to WiFi
[10219] Ready (ping: 59ms).
[22861] zona1 Got a value:  1
[30632] <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
[73] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.4.7 on Arduino Mega

[619] Connecting to TP-LINK_Outdoor_871430
[3703] AT version:0.22.0.0(Mar 20 2015 10:04:26)
SDK version:1.0.0
compile time:Mar 20 2015 11:00:32
[4734] Failed to enable MUX
[9849] +CIFSR:STAIP,"192.168.1.13"
+CIFSR:STAMAC,"60:01:94:0f:8f:e6"
[9853] Connected to WiFi
[10211] Ready (ping: 60ms).
  • i tested every 30 seconds but crash every time
    an others thing : i tested also without BLYNK , i belived that this no crashed but… after 3 hours has crashed again!
    this is the sketch without Blynk , managed by RTC:

  // versione senza Blynk 
#include <Wire.h>
#include <RTClib.h>
  
int mattina=0;
int sera=1;
int pranzo=1;
int zona;
 
 
 

 
//-----rtc
// 43 rele 1 siepe
// 41 cancello
//39 rele 3 nord sinistra
//37 rele 4 fronte
//35 patio 5
//33 nord destro rel 7
// 31 nord centro rel 6
//29 piscina piccola rel8
//27 tubo piscina rel 9
//25 rele vuoto
//23 vuoto

short pinSettore1 = 41; //cancelllo
short pinSettore2 = 27; //tubo piscina
short pinSettore3 = 43  ;//siepe
short pinSettore4 = 29; //piscina piccola
short pinSettore5 = 37 ; //fronte
short pinSettore6 = 35 ; //patio
short pinSettore7 = 31; //nord centro
short pinSettore8 = 39;//nord sin
short pinSettore9 = 33;//nord dest
short pinSettore10 = 25; // servizi tubo 25
short pinSettore11 = 23; //libero
short pinSettore12 = 23; //libero
short pinSettore13 = 23; //libero
short pinSettore14 = 23; //libero
short pinSettore15 = 23; //libero
short pinSettore16 = 23; //libero
 

RTC_DS1307 RTC;

 
void setup()
{
  // Set console baud rate
  Serial.begin(9600);
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
  
  //----rtc
  Wire.begin();
  RTC.begin();
  //dichiarazione pin che danno 0/1 ai relay devono essere output perchè se no non accendo i rele
  pinMode( pinSettore1, OUTPUT ); //pin2
  pinMode( pinSettore2, OUTPUT );
  pinMode( pinSettore3, OUTPUT );
  pinMode( pinSettore4, OUTPUT );
  pinMode( pinSettore5, OUTPUT );
  pinMode( pinSettore6, OUTPUT );
  pinMode( pinSettore7, OUTPUT );
  pinMode( pinSettore8, OUTPUT );
  pinMode( pinSettore9, OUTPUT );
  pinMode( pinSettore10, OUTPUT );
  pinMode( pinSettore11, OUTPUT );
  pinMode( pinSettore12, OUTPUT );
  pinMode( pinSettore13, OUTPUT );
  pinMode( pinSettore14, OUTPUT );
  pinMode( pinSettore15, OUTPUT );
  pinMode( pinSettore16, OUTPUT );
   



  all_off();
  

  //----- da commentare
  //RTC.sqw(1);       //0 Led off - 1 Freq 1Hz - 2 Freq 4096kHz - 3 Freq 8192kHz - 4 Freq 32768kHz
  if (! RTC.isrunning()) {
    Serial.println("RTC is NOT running!");
    //RTC.adjust(DateTime(F(__DATE__), F(__TIME__)));
    // RTC.adjust(DateTime(__DATE__, __TIME__));
    // This line sets the RTC with an explicit date & time, for example to set
    // January 21, 2014 at 3am you would call:
    //RTC.adjust(DateTime(2017, 6, 7, 12, 19, 0));
    //-- da commentare al secondo avvio
  }
 // RTC.adjust(DateTime(__DATE__, __TIME__));
  RTC.adjust(DateTime(2017, 6, 7, 20, 59, 53));
  adesso();
  delay(3000);
  //----rtc



}//fine setup


void all_off () {
  //HIGH mette tutti OFF è spento
  digitalWrite( pinSettore1, HIGH );
  digitalWrite( pinSettore2, HIGH );
  digitalWrite( pinSettore3, HIGH );
  digitalWrite( pinSettore4, HIGH );
  digitalWrite( pinSettore5, HIGH );
  digitalWrite( pinSettore6, HIGH );
  digitalWrite( pinSettore7, HIGH );
  digitalWrite( pinSettore8, HIGH );
  digitalWrite( pinSettore9, HIGH );
  digitalWrite( pinSettore10, HIGH );
  digitalWrite( pinSettore11, HIGH );
  digitalWrite( pinSettore12, HIGH );
  digitalWrite( pinSettore13, HIGH );
  digitalWrite( pinSettore14, HIGH );
  digitalWrite( pinSettore15, HIGH );
  digitalWrite( pinSettore16, HIGH );
}
 


 
void loop()
{  delay(1000);
  adesso();
     
      DateTime now = RTC.now();
      
      int _hour   = now.hour();
      int _minute = now.minute();
      int _secondi = now.second();

       if (mattina == 1) 
        {
        if (_hour == 06 &&  _minute == 00 && _secondi ==00) {accendi(pinSettore1); Serial.println("Start irrigazione");}
        if (_hour == 06 &&  _minute == 10 && _secondi ==00) spegni(pinSettore1);
        if (_hour == 06 &&  _minute == 10 && _secondi ==00) accendi(pinSettore2);
        if (_hour == 06 &&  _minute == 20 && _secondi ==00) spegni(pinSettore2);
        if (_hour == 06 &&  _minute == 20 && _secondi ==00) accendi(pinSettore4);
        if (_hour == 06 &&  _minute == 30 && _secondi ==00) spegni(pinSettore4);
        if (_hour == 06 &&  _minute == 30 && _secondi ==00) accendi(pinSettore5);
        if (_hour == 06 &&  _minute == 40 && _secondi ==00) spegni(pinSettore5);
        if (_hour == 06 &&  _minute == 40 && _secondi ==00) accendi(pinSettore6);
        if (_hour == 06 &&  _minute == 50 && _secondi ==00) spegni(pinSettore6);
        if (_hour == 06 &&  _minute == 50 && _secondi ==00) accendi(pinSettore7);
        if (_hour == 07 &&  _minute == 00 && _secondi ==00) spegni(pinSettore7);
        if (_hour == 07 &&  _minute == 00 && _secondi ==00) accendi(pinSettore8);
        if (_hour == 07 &&  _minute == 10 && _secondi ==00) spegni(pinSettore8);
        if (_hour == 07 &&  _minute == 10 && _secondi ==00) accendi(pinSettore9);
        if (_hour == 07 &&  _minute == 20 && _secondi ==00) spegni(pinSettore9);
        if (_hour == 07 &&  _minute == 20 && _secondi ==00) accendi(pinSettore10);
        if (_hour == 07 &&  _minute == 30 && _secondi ==00) {spegni(pinSettore10) ; Serial.println("Stop irrigazione");}

      }
      if (pranzo == 1) 
      {
        if (_hour == 13 &&  _minute == 00 && _secondi ==00) {accendi(pinSettore1); Serial.println("Start irrigazione");}
        if (_hour == 13 &&  _minute == 10 && _secondi ==00) spegni(pinSettore1);
        if (_hour == 13 &&  _minute == 10 && _secondi ==00) accendi(pinSettore2);
        if (_hour == 13 &&  _minute == 20 && _secondi ==00) spegni(pinSettore2);
        if (_hour == 13 &&  _minute == 20 && _secondi ==00) accendi(pinSettore4);
        if (_hour == 13 &&  _minute == 30 && _secondi ==00) spegni(pinSettore4);
        if (_hour == 13 &&  _minute == 30 && _secondi ==00) accendi(pinSettore5);
        if (_hour == 13 &&  _minute == 40 && _secondi ==00) spegni(pinSettore5);
        if (_hour == 13 &&  _minute == 40 && _secondi ==00) accendi(pinSettore6);
        if (_hour == 13 &&  _minute == 50 && _secondi ==00) spegni(pinSettore6);
        if (_hour == 13 &&  _minute == 50 && _secondi ==00) accendi(pinSettore7);
        if (_hour == 14 &&  _minute == 00 && _secondi ==00) spegni(pinSettore7);
        if (_hour == 14 &&  _minute == 00 && _secondi ==00) accendi(pinSettore8);
        if (_hour == 14 &&  _minute == 10 && _secondi ==00) spegni(pinSettore8);
        if (_hour == 14 &&  _minute == 10 && _secondi ==00) accendi(pinSettore9);
        if (_hour == 14 &&  _minute == 20 && _secondi ==00) spegni(pinSettore9);
        if (_hour == 14 &&  _minute == 20 && _secondi ==00) accendi(pinSettore10);
        if (_hour == 14 &&  _minute == 30 && _secondi ==00) {spegni(pinSettore10);   Serial.println("Stop irrigazione");}
      }
     if (sera == 1) 
      {
        if (_hour == 20 &&  _minute == 00 && _secondi ==00) {accendi(pinSettore1); Serial.println("Start irrigazione");}
        if (_hour == 20 &&  _minute == 30 && _secondi ==00) spegni (pinSettore1);
        if (_hour == 20 &&  _minute == 30 && _secondi ==00) {accendi(pinSettore2); Serial.println("Start settore 2");}
        if (_hour == 21 &&  _minute == 00 && _secondi ==00) spegni (pinSettore2);
        if (_hour == 21 &&  _minute == 00 && _secondi ==00) {accendi(pinSettore4); Serial.println("Start settore 4");}
        if (_hour == 21 &&  _minute == 30 && _secondi ==00) spegni (pinSettore4);
        if (_hour == 21 &&  _minute == 30 && _secondi ==00) {accendi(pinSettore5); Serial.println("Start settore 5");}
        if (_hour == 22 &&  _minute == 00 && _secondi ==00) spegni (pinSettore5);
        if (_hour == 22 &&  _minute == 00 && _secondi ==00) {accendi(pinSettore6); Serial.println("Start settore 6");}
        if (_hour == 22 &&  _minute == 30 && _secondi ==00) spegni (pinSettore6);
        if (_hour == 22 &&  _minute == 30 && _secondi ==00) {accendi(pinSettore7); Serial.println("Start settore 7");}
        if (_hour == 23 &&  _minute == 00 && _secondi ==00) spegni (pinSettore7);
        if (_hour == 23 &&  _minute == 00 && _secondi ==00) {accendi(pinSettore8); Serial.println("Start settore 8");}
        if (_hour == 23 &&  _minute == 30 && _secondi ==00) spegni (pinSettore8);
        if (_hour == 23 &&  _minute == 30 && _secondi ==00) {accendi(pinSettore9); Serial.println("Start settore 9");}
        if (_hour == 00 &&  _minute == 00 && _secondi ==00) spegni (pinSettore9);
        if (_hour == 00 &&  _minute == 00 && _secondi ==00) {accendi(pinSettore10); Serial.println("Start settore 10");}
        if (_hour == 00 &&  _minute == 30 && _secondi ==00) {spegni (pinSettore10); Serial.println("Stop irrigazione");}

        if (_hour == 20 &&  _minute == 00 && _secondi ==00) accendi(pinSettore3); // siepeee insieme a cancello e piscina tubo
        if (_hour == 21 &&  _minute == 00 && _secondi ==00) spegni (pinSettore3);
      }
     


}// fine loop

void accendi(int zona)
{ digitalWrite( zona, LOW );
}
void spegni(int zona)
{ digitalWrite( zona, HIGH );
}
void adesso()
{ DateTime now = RTC.now();
  int _hour   = now.hour();
  int _minute = now.minute();
  Serial.print("ora attuale ");
  Serial.print(now.day(), DEC);
  Serial.print('/');
  Serial.print(now.month(), DEC);
  Serial.print('/');
  Serial.print(now.year(), DEC);
  Serial.print(' ');
  Serial.print(now.hour(), DEC);
  Serial.print(':');
  Serial.print(now.minute(), DEC);
  Serial.print(':');
  Serial.print(now.second(), DEC);
  Serial.println();
}//adesso
  

This work 1/2 hours after restart to begin…
i don’t understand where is the problem…
An other thing : during thousand tests i tried to set INPUT the pin mode instead OUTPUT and seems work till the end !!
But … this does not turn on the relays… Only OUTPUT turn on the relay. thank you to who help me…

What board have you selected in the app?

Edit, OK I see Arduino Mega in your Serial Monitor.

Arduino Mega
connection type= wifi

Now i tested without nothing (are connect the pin A1 , SDA , SLC)

void setup()
{
  // Set console baud rate
  Serial.begin(9600);
  Serial.println("<<<<<ON OFF VERY 60 sec<<<<<<<<<<<<<<<<<inizio setup");
 Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
 Serial.println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup");
 
//dichiarazione pin che danno 0/1 ai relay
pinMode( 41,OUTPUT ); 
digitalWrite( 41,HIGH);
 }
 
void loop()

{
  Serial.println("ON....");
  digitalWrite(41,0 );
  delay(60000);
   
 Serial.println("spengo....");
  digitalWrite(41,1 );
  delay( 60000);
  
}

the crash is after 1 cycle :

<<<<<ON OFF VERY 60 sec<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
ON....
spe⸮<<<<<ON OFF VERY 60 sec<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
ON....
spengo....
ON....
spengo....
ON....
spengo....
ON....
spe⸮<<<<<ON OFF VERY 60 sec<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<inizio setup
ON....

could be a broken board?
Could the RTC give this problem?

Unlikely but not impossible.

With Blynk physical RTC’s are pretty redundant, remove and test. Upgrade your library to 0.4.8 and if you need the time use Blynk’s RTC.

Perhaps you are running into power issues. Are you running the Mega via USB power alone or also external power? When using the ESP, does it have it’s own external power? And how about the relay, what is it’s power source?

All should have independent power sources, but must all share a Ground connection.

Hello i resoved this issue: there was a electrical valvole that had a big magnetic field . This caused the restart of board . thank you

1 Like