Arduino and ESP8266-01

I am using an interrupt to turn on and off a led with a sound sensor attached to the pin 2 of my arduino mega. I have another functions sending and reciving data to an app trough the ESP8266-01 module connected to the communications pins 18 and 19 . Everithing works fine until i call the interrupt, the led goes on and then the communication with ESP stopps and nothing work anymore.

Why? How can i fix this?

denispupaza:
I am using an interrupt to turn on and off a led with a sound sensor attached to the pin 2 of my arduino mega. I have another functions sending and reciving data to an app trough the ESP8266-01 module connected to the communications pins 18 and 19 . Everithing works fine until i call the interrupt, the led goes on and then the communication with ESP stopps and nothing work anymore.

Why? How can i fix this?

Can you please post your code in code tags.

denispupaza:
I am using an interrupt to turn on and off a led with a sound sensor attached to the pin 2 of my arduino mega. I have another functions sending and reciving data to an app trough the ESP8266-01 module connected to the communications pins 18 and 19 . Everithing works fine until i call the interrupt, the led goes on and then the communication with ESP stopps and nothing work anymore.

Why? How can i fix this?

#define BLYNK_PRINT Serial

#include <ESP8266_Lib.h>
#include <BlynkSimpleShieldEsp8266.h>
#include<Servo.h>
#include <dht.h>
#include <CheapStepper.h>

Servo usca;  //servomotor camera
Servo usba;  //servomotor baie
Servo usbuc;  //servomotor bucatarie
Servo usfa;  //servomotor fata
Servo geam;// servomotor geam;

unsigned long alt_baie = 0;
int led_baie = 41;
int  pir_senz = 42;

int incuie;

CheapStepper stepper (37, 38, 39, 40);
boolean stare_poarta = false;

int sez_lum = 34; //senzor de lumina
int ali_lum = 35; //lumina de pe alee
int buton = 36;  //buton poarta

dht DHT ; //senzor de temperatura
int heat = 32;
int w_temp;
int stare_sistem;
#define dht_pin A0

int buzzer = 33;
int gaz_pin = A1;

int ir1 = 22;        // senzor camera
int ir2 = 23;        // senzor baie
int ir3 = 24;        // senzor bucatarie
int ir4 = 25;      // senzor fata

unsigned long alta1 = 0;  //Memoreaza timpul
unsigned long alta2 = 0;  //Memoreaza timpul
unsigned long alta3 = 0;  //Memoreaza timpul
unsigned long alta4 = 0;  //Memoreaza timpul

const int interval = 5000;

const int led_cam = 4;
int led_suf = 27;
int led_buc = 28;
const int soundSensor_cam = 2;
int soundSensor_suf = 30;
int soundSensor_buc = 31;

volatile boolean LEDStatus_cam = false;
volatile boolean LEDStatus_suf = false;
volatile boolean LEDStatus_buc = false;

char auth = “811ebef18fe74d0fb17e5d007cbae0cc”;

char ssid = “Den’s Network”;
char pass = “Porcusor01”;

// Hardware Serial on Mega, Leonardo, Micro…
#define EspSerial Serial1

// Your ESP8266 baud rate:
#define ESP8266_BAUD 9600

ESP8266 wifi(&EspSerial);

void setup()
{

attachInterrupt(digitalPinToInterrupt(soundSensor_cam), lumina_camera, RISING);

pinMode(led_baie, OUTPUT);      //lumina baie
  pinMode(pir_senz, INPUT);// senzor pir baie
  pinMode(buton, INPUT_PULLUP);
  stepper.setRpm(15); //turatia motorului pas cu pas
  pinMode(ali_lum, OUTPUT);
  pinMode(sez_lum, INPUT);

pinMode(buzzer, OUTPUT);
  pinMode(gaz_pin, INPUT);
  digitalWrite(buzzer, HIGH);

pinMode(heat, OUTPUT);
  digitalWrite(heat, HIGH);

pinMode(soundSensor_cam, INPUT);
  pinMode(soundSensor_buc, INPUT);
  pinMode(soundSensor_suf, INPUT);

pinMode(led_cam, OUTPUT);
  pinMode(led_suf, OUTPUT);
  pinMode(led_buc, OUTPUT);

usca.attach(12);
  usba.attach(11);
  usbuc.attach(10);
  usfa.attach(9);
  geam.attach(8);
  usca.write(0);
  usba.write(13);
  usbuc.write(170);
  usfa.write(180);
  geam.write(143);
  pinMode(ir1, INPUT);
  pinMode(ir2, INPUT);
  pinMode(ir3, INPUT);
  pinMode(ir4, INPUT);

// Debug console
  Serial.begin(9600);

// Set ESP8266 baud rate
  EspSerial.begin(ESP8266_BAUD);
  delay(10);

Blynk.begin(auth, wifi, ssid, pass);
}

/////////Deschidere geam

BLYNK_WRITE(V8)
{
  if (param.asInt() == HIGH)
  {
    geam.write(40);
  }
  else
    geam.write(143);
}

//////Lumina baie
void lumina_baie(unsigned long timp)
{

if (digitalRead(pir_senz) == HIGH)
  {
    digitalWrite(led_baie, HIGH);

}

else if (timp - alt_baie >= 30000 && digitalRead(pir_senz) == 0)
  {

digitalWrite(led_baie, LOW);
    alt_baie = timp;
  }
}

////Poarta de la curte
void poarta_curte()
{

if (digitalRead(buton) == 0 && stare_poarta == false)
  {
    if (digitalRead(sez_lum) == 1)
    {
      digitalWrite(ali_lum, HIGH);
    }
    Blynk.virtualWrite(V7, HIGH);
    stare_poarta = true;
    stepper.moveDegreesCW (520);

}

else if (digitalRead(buton) == 0 && stare_poarta == true)
  {
    digitalWrite(ali_lum, LOW);

Blynk.virtualWrite(V7, LOW);
    stare_poarta = false;
    stepper.moveDegreesCCW (520);
  }
}
////////////////////////

BLYNK_WRITE(V7)
{
  if (param.asInt() == HIGH)
  {
    stare_poarta = true;
    if (digitalRead(sez_lum) == 1)
    {
      digitalWrite(ali_lum, HIGH);

}
    stepper.moveDegreesCW (520);

}
  else
  {
    digitalWrite(ali_lum, LOW);
    stare_poarta = false;
    stepper.moveDegreesCCW (520);

}

}
////////////////////////

/////// Alarma gaz

void alarma_gaz()
{
  if (analogRead(gaz_pin) > 300)
  {

digitalWrite(buzzer, LOW);
  }
  else
  {
    digitalWrite(buzzer, HIGH);
  }

}

//Sistemul de incalzire si temperatura
BLYNK_WRITE(V3)
{
  w_temp = param.asInt();
}
BLYNK_WRITE(V5)
{
  stare_sistem = param.asInt();
}

void temperatura()
{
  DHT.read11(dht_pin);
  Blynk.virtualWrite(V4, DHT.temperature);
  if (stare_sistem == HIGH)
  {
    if (w_temp > DHT.temperature) //read dht11 sensor)
    {
      digitalWrite(heat, LOW);
    }
    else if (w_temp <= DHT.temperature )
    {
      digitalWrite(heat, HIGH);
    }
  }
  else
    digitalWrite(heat, HIGH);
}
///Usa Camera
void usa_camera(unsigned long timp)
{

if (digitalRead(ir1) == 0)
  {
    usca.write(90);
  }

else if (digitalRead(ir1) == 1)
  { if (timp - alta1 >= interval)
    {
      alta1 = timp;
      usca.write(0);
    }
  }
}

//////////Usa Baie
void usa_baie( unsigned long timp)
{
  if (digitalRead(ir2) == 0)
  {
    usba.write(120);
  }
  else if (digitalRead(ir2) == 1)
  {

if (timp - alta2 >= interval)
    {
      usba.write(13);
      alta2 = timp;
    }

}
}

////// //Usa Bucatarie
void usa_bucatarie(unsigned long timp)
{

if (digitalRead(ir3) == 1)
  { if (timp - alta3 >= interval)
    {
      alta3 = timp;
      usbuc.write(170);
    }

}

else if (digitalRead(ir3) == 0)
  {
    usbuc.write(70);
  }
}

/////////Usa Fata
void usa_fata(unsigned long timp)
{

if (digitalRead(ir4) == 0 && incuie == 0)
  {
    usfa.write(70);
    if (stare_poarta == true)
    {
      digitalWrite(ali_lum, LOW);
      Blynk.virtualWrite(V7, LOW);
      stepper.moveDegreesCCW (520);
      stare_poarta = false;

}

}
  else if (digitalRead(ir4) == 1)
  {
    if (timp - alta4 >= interval)
    {
      alta4 = timp;
      usfa.write(178);
    }
  }
}

/////////////
BLYNK_WRITE(V6)
{
  incuie = param.asInt();
}

////////

///Aprindere bec camera
void lumina_camera()
{

if (LEDStatus_cam == false)
    {
      digitalWrite(led_cam, HIGH);
      Blynk.virtualWrite(V0, HIGH);
      LEDStatus_cam = true;
    }
    else
    {
      digitalWrite(led_cam, LOW);
      Blynk.virtualWrite(V0, LOW);
      LEDStatus_cam = false;

}
 
}

/////////////////////////////////
BLYNK_WRITE(V0)
{
  if (param.asInt() == HIGH)
  {
    digitalWrite(led_cam, HIGH);
  }
  else
  {
    digitalWrite(led_cam, LOW);
  }
}
///////////////////////////////////

///Aprindere bec sufragerie
void lumina_sufragerie()
{
  if (digitalRead(soundSensor_suf) == 1)
  {

if (LEDStatus_suf == false)
    {
      LEDStatus_suf = true;
      digitalWrite(led_suf, HIGH);
      Blynk.virtualWrite(V1, HIGH);
delay(250);

}
    else if (LEDStatus_suf == true)
    {
      LEDStatus_suf = false;
      digitalWrite(led_suf, LOW);
      Blynk.virtualWrite(V1, LOW);
delay(250);
    }

}
}

/////////////////////////////////
BLYNK_WRITE(V1)
{
  if (param.asInt() == HIGH)
  {
    digitalWrite(led_suf, HIGH);
  }
  else
  {
    digitalWrite(led_suf, LOW);
  }
}
///////////////////////////////////

///Aprindere bec bucatarie//////////
void lumina_bucatarie()
{
  if (digitalRead(soundSensor_buc) == 1)
  {

if (LEDStatus_buc == false)
    {
      LEDStatus_buc = true;
      digitalWrite(led_buc, HIGH);
      Blynk.virtualWrite(V3, HIGH);
delay(250);

}
    else
    {
      LEDStatus_buc = false;
      digitalWrite(led_buc, LOW);
      Blynk.virtualWrite(V3, LOW);
delay(250);
    }

}
}
/////////////////////////////////
BLYNK_WRITE(V2)
{
  if (param.asInt() == HIGH)
  {
    digitalWrite(led_buc, HIGH);
  }
  else
  {
    digitalWrite(led_buc, LOW);
  }
}
///////////////////////////////////

void loop()
{
// Blynk.run();
  unsigned long timp = millis();

// lumina_camera();
  lumina_bucatarie();
  lumina_sufragerie();
  usa_camera(timp);
  usa_baie(timp);
  usa_bucatarie(timp);
  usa_fata(timp);
  temperatura();
  alarma_gaz();
  poarta_curte();
  lumina_baie(timp);
}

Does the problem still occur if you comment out the calls to Blynk.virtualWrite() in lumina_camera()?

You have a 5V GPIO Mega connected to a 3.3V GPIO using level shifters?

Whiles some will indicate that the ESP is 5 volt tolerant, Espressif removed that mention from its documentation on the ESP8266.