Hello,
I'm working on this home made external siren for my alarm. Practically it exchanges 2 variables between the home alarm and the external siren. Alarm on and PanicBuzzer on.
Both devices are bridged with 2 wemos and connected with a dedicated AP.
I've written my code, but unfortunately the alarm main function is not working (only the first condition)
I have an earlier version where all the if conditions are broken up- nd everything is working.
i'm trying to rewrite the code for easier understanding. So I've put all the conditions together.
Would you please give me a hint on my logical fallacies?
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <SimpleTimer.h>
#include <SPI.h>
#include <OneWire.h>
#include <SimpleTimer.h>
// Server declaration for variable exchange
IPAddress server(10, 0, 0, 201); // fix IP of the server - GT HOUSE ALARM 2200
const int API_TIMEOUT = 1500; //Connection timeout
WiFiClient client;
String answer; //Variable exchange
//Debug mode
bool debug = 0;
//Variables ON OFF
bool ALM_ON; //The alarm is active.
bool PANIC_ON; //The alarm is in panic mode! Burglar inside!
bool Keys_ON; //0= Keys in off position 1= Keys in on position
SimpleTimer timer; //Timer function declaration
//For state change detection
int lastButtonState = 0;
int buttonState = 0;
//Monitor reed for anti tamper function
int state; // 0 close - 1 open wwitch
//Monitor Key function
int sensorVal; // 0 open alarm - 1 alarm working
//Hardware GPIOS
const int sensor = D2; //reed
const int button = D5; //Key
const int RDH1 = D4; //Big led
const int LWH2 = D6; //Small led
const int RELE = D1; //Relay
const int resetPin = D7; //Reset enable
//Network configuration
const char * ssid = "ESPsoftAP_Gt2200"; //your wifi ssid
const char * password = ""; //your wifi password
IPAddress ip(192, 168, 4, 1); //your arduino IP
IPAddress gateway(192, 168, 4, 1); //your network gateway
IPAddress subnet(255, 255, 255, 0); //your network subnet mask
void setup() {
Serial.begin(74880);
// WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet);
//Configurazione di default Gpio
delay(500);
pinMode(button, INPUT_PULLUP);
pinMode (LWH2, OUTPUT);
pinMode (RDH1, OUTPUT);
pinMode (resetPin, OUTPUT);
pinMode (RELE, OUTPUT);
pinMode (sensor, INPUT_PULLUP);
digitalWrite(LWH2, LOW);
digitalWrite(resetPin, HIGH);
digitalWrite(RDH1, LOW);
digitalWrite(RELE, HIGH);
//Scantime simpletimer functions
timer.setInterval(50L, disable);
timer.setInterval(100L, alarm);
}
void loop()
{
while ( client.connect(server, 80))
{ // connects to the server
Serial.println(" Connected to Gt2200 ");
client.println("Send data!\r"); // trigger message to the server, its value is scrapped
answer = client.readStringUntil('\r'); // received the server's answer
client.flush();
ALM_ON = answer.substring(0, answer.indexOf(',')).toInt();
PANIC_ON = answer.substring(answer.indexOf(',') + 1).toInt();
Serial.print("ALM_ON: ");
Serial.println(ALM_ON);
Serial.print("PANIC_ON: ");
Serial.println(PANIC_ON);
timer.run(); //Simpletimer functions call
delay (600);
} yield();
{
Serial.println("not connected"); //Flash small led while connecting to the internet
digitalWrite(LWH2, HIGH);
delay(500);
digitalWrite(LWH2, LOW);
}
}
void disable() //Keys function - check if they are turned off (0) or turned on (1)
{
sensorVal = digitalRead(button);
Serial.print("SCAN: for key pressed");
//Serial.print(sensorVal);
Serial.print(" ");
delay(50);
if (sensorVal == LOW)
{
Serial.print("KEY ON\n");
Keys_ON = 1;
delay(50);
}
else
{
Serial.print("KEY OFF\n");
Keys_ON = 0;
delay(50);
}
}
void alarm()
{
state = digitalRead(sensor);
Serial.print("Reed state 0= Closed 1=Opened: ");
Serial.print(state);
Serial.print("\n");
if ((ALM_ON == 1) && (Keys_ON == 1)) //if the keys are turned and the alarm is on, keep flashing big LED
{
Serial.print("Lights on");
digitalWrite(RDH1, HIGH);
delay(1000);
digitalWrite(RDH1, LOW);
delay(2000);
}
else if ((ALM_ON == 1) && (Keys_ON == 1) && (PANIC_ON == 1)) //Main function: Sound the external siren when Gt2200 is in PANIC mode.
{
Serial.print("WORKING: Alarm and Buzzer ");
digitalWrite(RELE, LOW);
delay(22000); //Time for buzzer
digitalWrite(resetPin, LOW); //After the alarm has gone on, reset arduino, to exit cycle
}
else if (((Keys_ON == true) && (state == HIGH)) || ((Keys_ON == true) && (state == HIGH) && (ALM_ON == true))) //Tamper function: sound the alarm in case somebody is opening the box without permission
{
Serial.print(" working: Alarm - TAMPERED");
digitalWrite(RELE, LOW);
delay(22000); //Time for buzzer
digitalWrite(resetPin, LOW); //After the alarm has gone on, reset arduino, to exit cycle
}
else if ((Keys_ON == 0) && (PANIC_ON == 1)) //Signal for malfunction small led on in case the alarm goes off while the keys are off - No buzzer
Serial.print("Malfunction");
digitalWrite(LWH2, HIGH);
}