|
|
|
|
@ -20,146 +20,145 @@ int lcdRows = 2;
|
|
|
|
|
|
|
|
|
|
LiquidCrystal_I2C lcd(0x3f, lcdColumns, lcdRows);
|
|
|
|
|
|
|
|
|
|
float readADC_Avg(int pin)
|
|
|
|
|
{
|
|
|
|
|
float avg = analogRead(pin);
|
|
|
|
|
for (int i = 0; i < 10; i++)
|
|
|
|
|
{
|
|
|
|
|
avg = (avg + analogRead(pin)) / 2;
|
|
|
|
|
delay(20);
|
|
|
|
|
}
|
|
|
|
|
return avg;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setup_wifi()
|
|
|
|
|
{
|
|
|
|
|
delay(10);
|
|
|
|
|
// We start by connecting to a WiFi network
|
|
|
|
|
Serial.println();
|
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
|
Serial.println(ssid);
|
|
|
|
|
|
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
|
|
|
{
|
|
|
|
|
delay(500);
|
|
|
|
|
Serial.print(".");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
|
Serial.println("WiFi connected");
|
|
|
|
|
Serial.println("IP address: ");
|
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
delay(10);
|
|
|
|
|
// We start by connecting to a WiFi network
|
|
|
|
|
Serial.println();
|
|
|
|
|
Serial.print("Connecting to ");
|
|
|
|
|
Serial.println(ssid);
|
|
|
|
|
|
|
|
|
|
WiFi.begin(ssid, password);
|
|
|
|
|
|
|
|
|
|
while (WiFi.status() != WL_CONNECTED)
|
|
|
|
|
{
|
|
|
|
|
delay(500);
|
|
|
|
|
Serial.print(".");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Serial.println("");
|
|
|
|
|
Serial.println("WiFi connected");
|
|
|
|
|
Serial.println("IP address: ");
|
|
|
|
|
Serial.println(WiFi.localIP());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void reconnect()
|
|
|
|
|
{
|
|
|
|
|
// Loop until we're reconnected
|
|
|
|
|
while (!client.connected())
|
|
|
|
|
{
|
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
|
// Attempt to connect
|
|
|
|
|
if (client.connect("ESP8266Client"))
|
|
|
|
|
// Loop until we're reconnected
|
|
|
|
|
while (!client.connected())
|
|
|
|
|
{
|
|
|
|
|
Serial.println("connected");
|
|
|
|
|
Serial.print("Attempting MQTT connection...");
|
|
|
|
|
// Attempt to connect
|
|
|
|
|
if (client.connect("ESP8266Client5"))
|
|
|
|
|
{
|
|
|
|
|
Serial.println("connected");
|
|
|
|
|
|
|
|
|
|
client.subscribe("alarm/state");
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Serial.print("failed, rc=");
|
|
|
|
|
Serial.print(client.state());
|
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
|
// Wait 5 seconds before retrying
|
|
|
|
|
delay(5000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
Serial.print("failed, rc=");
|
|
|
|
|
Serial.print(client.state());
|
|
|
|
|
Serial.println(" try again in 5 seconds");
|
|
|
|
|
// Wait 5 seconds before retrying
|
|
|
|
|
delay(5000);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#define RELAY_ACTIVATION_PIN D6
|
|
|
|
|
#define REED_ACTIVATION_PIN D5
|
|
|
|
|
|
|
|
|
|
void callback(char *topic, byte *payload, unsigned int length)
|
|
|
|
|
void isArming()
|
|
|
|
|
{
|
|
|
|
|
StaticJsonDocument<256> doc;
|
|
|
|
|
deserializeJson(doc, payload, length);
|
|
|
|
|
|
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
|
Serial.print(topic);
|
|
|
|
|
Serial.print("] ");
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
|
}
|
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
|
|
if (doc["deviceId"] == deviceId && doc["lock"] == true)
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(RELAY_ACTIVATION_PIN, HIGH); // Turn the relay on
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
digitalWrite(RELAY_ACTIVATION_PIN, LOW); // Turn the relay off
|
|
|
|
|
}
|
|
|
|
|
lcd.clear();
|
|
|
|
|
lcd.setCursor(0, 0);
|
|
|
|
|
lcd.print("Arming");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void setup()
|
|
|
|
|
void isArmed()
|
|
|
|
|
{
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
lcd.clear();
|
|
|
|
|
lcd.setCursor(0, 0);
|
|
|
|
|
lcd.print("Armed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pinMode(RELAY_ACTIVATION_PIN, OUTPUT);
|
|
|
|
|
pinMode(REED_ACTIVATION_PIN, INPUT);
|
|
|
|
|
void isDisarmed()
|
|
|
|
|
{
|
|
|
|
|
lcd.clear();
|
|
|
|
|
lcd.setCursor(0, 0);
|
|
|
|
|
lcd.print("Disarmed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lcd.init();
|
|
|
|
|
lcd.backlight();
|
|
|
|
|
pinMode(buzzerPin, OUTPUT);
|
|
|
|
|
void isTriggered()
|
|
|
|
|
{
|
|
|
|
|
lcd.clear();
|
|
|
|
|
lcd.setCursor(0, 0);
|
|
|
|
|
lcd.print("Triggered");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setup_wifi();
|
|
|
|
|
reconnect();
|
|
|
|
|
client.setServer(mqtt_server, 1883);
|
|
|
|
|
void isDisarming()
|
|
|
|
|
{
|
|
|
|
|
lcd.clear();
|
|
|
|
|
lcd.setCursor(0, 0);
|
|
|
|
|
lcd.print("Disarmed");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void isArmed()
|
|
|
|
|
void callback(char *topic, byte *payload, unsigned int length)
|
|
|
|
|
{
|
|
|
|
|
DynamicJsonDocument doc(1024);
|
|
|
|
|
|
|
|
|
|
doc["deviceId"] = deviceId;
|
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
|
Serial.print(topic);
|
|
|
|
|
Serial.print("] ");
|
|
|
|
|
for (int i = 0; i < length; i++)
|
|
|
|
|
{
|
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
|
}
|
|
|
|
|
Serial.println();
|
|
|
|
|
StaticJsonDocument<512> doc;
|
|
|
|
|
deserializeJson(doc, payload, length);
|
|
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
size_t n = serializeJson(doc, buffer);
|
|
|
|
|
client.publish("display/disarm", buffer, n);
|
|
|
|
|
lcd.print("Armed");
|
|
|
|
|
delay(5000);
|
|
|
|
|
lcd.clear();
|
|
|
|
|
if (strcmp(doc["status"], "Arming") == 0)
|
|
|
|
|
{
|
|
|
|
|
isArming();
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(doc["status"], "Armed") == 0)
|
|
|
|
|
{
|
|
|
|
|
isArmed();
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(doc["status"], "Disarmed") == 0)
|
|
|
|
|
{
|
|
|
|
|
isDisarmed();
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(doc["status"], "Disarming") == 0){
|
|
|
|
|
isDisarming();
|
|
|
|
|
}
|
|
|
|
|
else if (strcmp(doc["status"], "Triggered") == 0)
|
|
|
|
|
{
|
|
|
|
|
isTriggered();
|
|
|
|
|
tone(buzzerPin, 1000);
|
|
|
|
|
delay(1000);
|
|
|
|
|
noTone(buzzerPin);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool lastReed = 0;
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
|
void setup()
|
|
|
|
|
{
|
|
|
|
|
if (!client.connected())
|
|
|
|
|
{
|
|
|
|
|
reconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool reed = !digitalRead(REED_ACTIVATION_PIN);
|
|
|
|
|
|
|
|
|
|
if (reed != lastReed)
|
|
|
|
|
{
|
|
|
|
|
Serial.print("Reed has changed to: ");
|
|
|
|
|
Serial.println(reed);
|
|
|
|
|
|
|
|
|
|
Serial.begin(115200);
|
|
|
|
|
|
|
|
|
|
DynamicJsonDocument doc(1024);
|
|
|
|
|
lcd.init();
|
|
|
|
|
lcd.backlight();
|
|
|
|
|
pinMode(buzzerPin, OUTPUT);
|
|
|
|
|
|
|
|
|
|
doc["deviceId"] = deviceId;
|
|
|
|
|
doc["open"] = reed;
|
|
|
|
|
setup_wifi();
|
|
|
|
|
client.setCallback(callback);
|
|
|
|
|
client.setServer(mqtt_server, 1883);
|
|
|
|
|
reconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
size_t n = serializeJson(doc, buffer);
|
|
|
|
|
|
|
|
|
|
client.publish("door/status", buffer, n);
|
|
|
|
|
lastReed = reed;
|
|
|
|
|
void loop()
|
|
|
|
|
{
|
|
|
|
|
if (!client.connected())
|
|
|
|
|
{
|
|
|
|
|
reconnect();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
client.loop();
|
|
|
|
|
client.loop();
|
|
|
|
|
}
|