ESP32 Alarm configuration issue

Hello folks,

I am building an home alarm, base on Blynk for control and Telegram Bot for any notification.

The system is pretty simple, from Blynk on my phone, I have the option to set the system in Home Mode or Away mode, by flipping a virtual switch.
When I do that, the red led on the ESP32 will go on for the Away mode and the Blue led will go on when at Home mode.

When in Away mode, if the Pir pin goes HIGH, the system will be triggered, sounding off the horn, sending me my Telegram notification, and Blink will also show me on my phone that someone have triggered the alarm, then I have the option to disarm the alarm with Blynk remotely, so far everything work as expected.

The only issue that I have is I am not able to arm that system locally, by the press of a switch.

I did include a code that will, when I press that arm button, show the alarm going into arm mode( both turning the Red LED on the board and showing me in Blink that it is arm) but when my Pir sensor get a movement, nothing is happening...

I believe that it have to do with the part of the sketch that stays in awayHomeMode == 0, but have no idea how to change that state from 0 to 1, if it's that would cause my system to not respond when arm locally...

Thank You

Serge Landry

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial

#define BLYNK_TEMPLATE_ID "*************"
#define BLYNK_TEMPLATE_NAME "ALARM TEST 1"
#define BLYNK_AUTH_TOKEN "F0jHNxOe1HJX1MZhl***********"

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiClientSecure.h>
#include <BlynkSimpleEsp32.h>
#include <UniversalTelegramBot.h>
#include <ArduinoJson.h>

  // Define WiFi credentials
  
  char ssid[] = "*********";
  char pass[] = "********";

  // Define the PIR sensor pin and related variables

  const int Pir = 4;
  int state = 0;
  int awayHomeMode = 0;
  int armpin = 5;
  int bluled = 18;
  int redled = 19;
  int dsmpin = 21;
  int horn= 22;
  int hornrst= 23;

  #define BOTtoken "6894941667:AAHN***************************"

  #define CHAT_ID "***********"

  WiFiClientSecure client;
  UniversalTelegramBot bot(BOTtoken, client);

  void sensor1()
{
  int Pir_data=digitalRead(Pir);
  Blynk.virtualWrite(V2, Pir_data);
}

  // Create Blynk Timer object

  BlynkTimer timer;

  void setup() 

{

  pinMode(Pir, INPUT);
  pinMode(armpin, INPUT);
  pinMode(dsmpin, INPUT);
  pinMode(redled, OUTPUT);
  pinMode(bluled, OUTPUT);
  pinMode(horn, OUTPUT);
  pinMode(hornrst, OUTPUT);
  digitalWrite(redled, HIGH);
  digitalWrite(bluled, LOW);
  digitalWrite(horn, HIGH);
  digitalWrite(hornrst, HIGH);
  
  Serial.begin(115200);  // Start serial communication at 115200 baud rate for debugging

  // Configure Blynk and connect to WiFi
  
  Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);

  client.setCACert(TELEGRAM_CERTIFICATE_ROOT);

  bot.sendMessage(CHAT_ID, "YOUR MAIN SYSTEM HAVE JUST REBOOTED", "");

  timer.setInterval(400L, myTimerEvent);  // Setup a function to be called every second
  timer.setInterval(500L, myTimerEvent2);
  timer.setInterval(600L, sensor1);

}


  // This function is called every time the device is connected to the Blynk.Cloud

  BLYNK_CONNECTED() 

{
  Blynk.syncVirtual(V0);
}

  // This function is called every time the Virtual Pin 0 state changes
  
  BLYNK_WRITE(V0) 
  
  {
    
  awayHomeMode = param.asInt();  // Set incoming value from pin V0 to a variable

  if (awayHomeMode == 1) 

{
    Serial.println("The switch on Blynk has been turned on.");
    Blynk.virtualWrite(V1, " AWAY MODE, ALARM IS ACTIVE");
    digitalWrite(redled, LOW);
    digitalWrite(bluled, HIGH);
} 
  else 
{
    Serial.println("The switch on Blynk has been turned off.");
    Blynk.virtualWrite(V1, "HOME MODE, ALARM DISABLE");
    digitalWrite(redled, HIGH);
    digitalWrite(bluled, LOW);
    digitalWrite(horn, HIGH);
    digitalWrite(hornrst, LOW);
    delay(500);
    digitalWrite(hornrst, HIGH);
}
}

    void myTimerEvent() 

{
  // Please don't send more that 10 values per second.
  
    sendData();  // Call function to send sensor data to Blynk app
}

    // Function to send sensor data to Blynk app
  
    void sendData() 
{
  if (awayHomeMode == 1) 
{
    state = digitalRead(Pir);  // Read the state of the PIR sensor

    Serial.print("state:");
    Serial.println(state);

    // If the sensor detects movement, send an alert to the Blynk app
    
  if (state == HIGH) 
{
    Serial.println("MAIN ALARM TRIGGERED");
    digitalWrite(horn, LOW);
    Blynk.virtualWrite(V1, "---MAIN ALARM TRIGGERED---");
    Blynk.logEvent("MAIN ALARM TRIGGERED");
    bot.sendMessage(CHAT_ID, "ALARM TRIGGERED IN MAIN LIVING ROOR AREA", "");
    delay(5000);
    digitalWrite(horn, HIGH);
  }
 }
}

  void myTimerEvent2()

{
  if (awayHomeMode == 0) 
{
    state = digitalRead(armpin);  // Read the state of the armpin

    Serial.print("state:");
    Serial.println(state);
    
  if (state == HIGH) 
{
    digitalWrite(awayHomeMode, 1);
    digitalWrite(redled, LOW);
    digitalWrite(bluled, HIGH);
    Blynk.virtualWrite(V1, "ALARM WAS ARMED FROM HOME");
    Blynk.virtualWrite(V0, 1);
    bot.sendMessage(CHAT_ID, "ALARM WAS ARMED FROM HOME", "");
    
  }
 }
}

  void loop() 

{
  Blynk.run();  // Run Blynk
  timer.run();  // Run BlynkTimer
}


//  if (digitalRead(armpin=HIGH));
//  {
//  digitalWrite(redled, LOW);
//  digitalWrite(bluled, HIGH);
//  Blynk.virtualWrite(V1, " AWAY MODE, ALARM ACTIVE");
//  Blynk.virtualWrite(V0, 1);
//  }
//  }

This line looks like a problem:

Hey Dave, Yes I did try all sort of thing, including that line....

But like stated previously in my message, I don"t know how to flip that awayHomeMode state from 0 to 1, witch I believe is the issue

Since awayHomeMode is 0 when it gets to this line:

Will set GPIO0 as HIGH.

This variable?

You could do:

  awayHomeMode = 1;

but this bit re-writes it every time Blynk syncs:

I think the Blynk.virtualWrite(V0, 1); in this updates it on Blynk so next Blynk.run(), the other bit will update awayHomeMode and overwrite V1.

    if (state == HIGH)
    {
      digitalWrite(awayHomeMode, 1);
      digitalWrite(redled, LOW);
      digitalWrite(bluled, HIGH);
      Blynk.virtualWrite(V1, "ALARM WAS ARMED FROM HOME");
      Blynk.virtualWrite(V0, 1);
      bot.sendMessage(CHAT_ID, "ALARM WAS ARMED FROM HOME", "");

    }

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.