Flame Sensor

Hello friends.
Can anyone please help me with my project?
I have an issue with buzzer2 and the fire sensor.

The buzzer2 is not working at all, even when tested in the replacement of buzzer in the code, is not the module, i tested and it works fine.

The flame sensor its working properly, but the problem is with my code, i can make it light de led or turn on the buzzer or buzzer2.

Here’s my code.
Thanks.

//Definição dos templates gerados no site do Blynk
#define BLYNK_TEMPLATE_ID "TMPL2_9ioaApf"
#define BLYNK_TEMPLATE_NAME "ESP32 Home Automation"
#define BLYNK_AUTH_TOKEN "DiJTp_VJhdGhuger9iKs-V5HOg_ftifn"

//Inclusão das bibliotecas utilizadas
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <DHT.h>
#include <Servo.h>


//Impressão das informações na serial, pode ser retirado
#define BLYNK_PRINT Serial

//Definição da luminosidade do sensor LDR

#define LUMINOSIDADE  500
//Inicia o processo de autenticação do Blynk
char auth[] = BLYNK_AUTH_TOKEN;
BlynkTimer timer;


// Login e Senha da internet
char ssid[] = "Brigadeiro_2G";
char pass[] = "4890909090";

//Definindo os pinos conectados
#define LED_1       27
#define LED_2       26
#define LED_3       25
#define LED_4       33
#define SERVO_PIN   13
#define DHT_PIN     19
#define DHTTYPE     DHT11
#define LDR_SENSOR  35
#define LDR_LED     15
#define PIR_PIN     4
#define PIR_LED1    18
#define PIR_LED2    21
#define BUZZER      22
#define BUZZER2     2
#define FLAME       32


//Estado incial dos leds
int STATE_LED_1 = 0;
int STATE_LED_2 = 0;
int STATE_LED_3 = 0;
int STATE_LED_4 = 0;
//int PIR_LED1 = 0;
//int PIR_LED2 = 0;


//Pins definidos no site do Blynk
#define VPIN_BUTTON_1     V0
#define VPIN_BUTTON_2     V1
#define VPIN_BUTTON_3     V2 
#define VPIN_BUTTON_4     V3
#define VPIN_TEMPERATURE  V4
#define VPIN_HUMIDITY     V5
#define VPIN_BUTTON_5     V6
#define VPIN_BUTTON_6     V8
#define VPIN_BUTTON_7     V9


//Nomeia e inicia o sensor de temperatura
DHT dht(DHT_PIN, DHTTYPE);

//Nomeia e inicia o Servo
Servo servo;

bool botaopir = 0;
int botaochama = 0;
bool fireDetected = false;


//--------------------------------------------------------------------------
//Função de conexão do Blynk
BLYNK_CONNECTED() {
  Blynk.syncVirtual(VPIN_BUTTON_1);
  Blynk.syncVirtual(VPIN_BUTTON_2);
  Blynk.syncVirtual(VPIN_BUTTON_3);
  Blynk.syncVirtual(VPIN_BUTTON_4);
  Blynk.syncVirtual(VPIN_BUTTON_5);
  Blynk.syncVirtual(VPIN_BUTTON_6); 
  Blynk.syncVirtual(VPIN_BUTTON_7); 
}
//--------------------------------------------------------------------------

//Função que altera as saídas de acordo com os botões virtuais
BLYNK_WRITE(VPIN_BUTTON_1) {
  STATE_LED_1 = param.asInt();
  digitalWrite(LED_1, STATE_LED_1);
  Serial.print("Led1 State = "); Serial.println(STATE_LED_1);
}
BLYNK_WRITE(VPIN_BUTTON_2) {
  STATE_LED_2 = param.asInt();
  digitalWrite(LED_2, STATE_LED_2);
  Serial.print("Led2 State = "); Serial.println(STATE_LED_2);
}
BLYNK_WRITE(VPIN_BUTTON_3) {
  STATE_LED_3 = param.asInt();
  digitalWrite(LED_3, STATE_LED_3);
  Serial.print("Led3 State = "); Serial.println(STATE_LED_3);
}
BLYNK_WRITE(VPIN_BUTTON_4) {
  STATE_LED_4 = param.asInt();
  digitalWrite(LED_4, STATE_LED_4);
  Serial.print("Led4 State = "); Serial.println(STATE_LED_4);
}
BLYNK_WRITE(VPIN_BUTTON_5) {
  int pinValue = param.asInt();
  if (pinValue == 1) { 
    servo.write(0);
  }else
    servo.write(90); 
}
BLYNK_WRITE(VPIN_BUTTON_6) {
  botaopir = param.asInt();
 
}

BLYNK_WRITE(VPIN_BUTTON_7) {
  int botaochama = param.asInt();
}
//--------------------------------------------------------------------------


//Função Setup
void setup(){
  Serial.begin(115200);
  
  //Definindo os leds como pinos de saída
  pinMode(LED_1, OUTPUT);
  pinMode(LED_2, OUTPUT);
  pinMode(LED_3, OUTPUT);
  pinMode(LED_4, OUTPUT);
  pinMode(LDR_LED, OUTPUT);
  pinMode(PIR_LED1, OUTPUT);
  pinMode(PIR_LED2, OUTPUT);
  pinMode(BUZZER, OUTPUT);
  pinMode(BUZZER2, OUTPUT);
  
  pinMode(LDR_SENSOR, INPUT);
  pinMode(PIR_PIN, INPUT);
  pinMode(FLAME, INPUT);

  //Estado inicial dos leds
  digitalWrite(LED_1, HIGH);
  digitalWrite(LED_2, HIGH);
  digitalWrite(LED_3, HIGH);
  digitalWrite(LED_4, HIGH);
  
  //Inicia o sensor de temperatura
  dht.begin();
 
  //Inicia a autenticação do Blynk
  Blynk.begin(auth, ssid, pass);
    
  //Timer do sensor de temperatura
  timer.setInterval(500, Send_Sensor);

  timer.setInterval(500, presenca);


  //Define o servo no pino 13 e coloca na posição 0
  servo.attach(13); 
  servo.write(90);
}

//Função loop
void loop() {
  Blynk.run();
  timer.run(); 
  sensorFogo(); 

  int valorSensor = analogRead(LDR_SENSOR);
  Serial.println(valorSensor);
  
  delay(2000);
  if (valorSensor < LUMINOSIDADE)
  {
    digitalWrite(LDR_LED, HIGH);
  }else
  {
    digitalWrite(LDR_LED, LOW);
  }
}

/*  void sensorFogo() {
  bool chama = digitalRead(FLAME);
  Serial.println(FLAME);
  if (chama == 0) {
    digitalWrite(BUZZER2, LOW);
  } else if (chama == 1) {
    digitalWrite(BUZZER2, HIGH);
  }
}*/

void sensorFogo ()
{
  bool chama = digitalRead(FLAME);
  Serial.println(chama);

  if (botaochama  == 1)
  {
    if (chama == 0) 
    {
      digitalWrite(PIR_LED1, HIGH);  
      digitalWrite(BUZZER2, HIGH);
    } else if (chama == LOW)   
      {  
       digitalWrite(PIR_LED1, LOW);  
       digitalWrite(BUZZER2, LOW); 
      }
  } else if (botaochama  == 1)
  {
    
    digitalWrite(PIR_LED1, LOW);  
    digitalWrite(BUZZER2, LOW);
  }   
}


//Função do Sensor de Temperatura
void Send_Sensor()
{
  float h = dht.readHumidity();
  float t = dht.readTemperature();

  //Serial.print("Temperature - "); Serial.print(t); Serial.println("°C");
  //Serial.print("Humidity - "); Serial.print(h); Serial.println("%");
  //delay(5000);
}

void presenca ()
{
  bool pir = digitalRead(23);
  
  if (botaopir  == 1)
  {
    //Serial.println("Monitoramento de movimento ligado!!!");  
    if (pir == 1) 
    {
      digitalWrite(PIR_LED1, HIGH);  
      digitalWrite(PIR_LED2, HIGH);
      digitalWrite(BUZZER, HIGH);
    } else if (pir == 0)   
      {  
       digitalWrite(PIR_LED1, LOW);  
       digitalWrite(PIR_LED2, LOW);  
       digitalWrite(BUZZER, LOW); 
      }
  } else if (botaopir  == 0)
  {
    //Serial.println("Monitoramento de movimento desligado!!!");
    digitalWrite(PIR_LED1, LOW);  
    digitalWrite(PIR_LED2, LOW);  
    digitalWrite(BUZZER, LOW); 
  }   
}



//Função de Controle dos leds no Blynk
void ControlLed(int number, int led_pin, int &status, int virtual_pin){
    delay(200);
    status = !status;
    digitalWrite(led_pin, status);
    delay(50);
    Blynk.virtualWrite(virtual_pin, status); //update button state
    Serial.print("Led"+String(number)+" State = "); Serial.println(status);
}

You’re declaring botaochama as a global variable and assigning it a value of zero here…

Then re-declaring it as a local value here…

The local copy is only visible with BLYNK_WRITE(VPIN_BUTTON_7) so the value used here…

will always be zero, and your if statement will always evaluate as false

Removing the int before botaochama in BLYNK_WRITE(VPIN_BUTTON_7) will allow that function to change the value of the global variable rather than making a local copy, like this…

BLYNK_WRITE(VPIN_BUTTON_7) {
  botaochama = param.asInt();
}

However, you have other issues with your code. Your void loop is way too cluttered and you’re using delays. Blynk doesn’t like this.
You should read this…

and use your existing BlynkTimer to call the unnecessary code that is inside your void loop.

Pete.

Thank you Pete.
With your assistance the flame sensor is working properly now.
I read the article and create a void function for the LDR and keep only the necessary in the setup function.

1 Like