|
|
|
@ -3,10 +3,10 @@
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <PubSubClient.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
#include <ArduinoJson.h>
|
|
|
|
|
|
|
|
|
|
|
|
const char *ssid = "PET Aflytningsvogn #42";
|
|
|
|
const char *ssid = "PET Aflytningsvogn #43";
|
|
|
|
const char *password = "FuckGFwifi123";
|
|
|
|
const char *password = "zwr33htm";
|
|
|
|
|
|
|
|
|
|
|
|
const char *mqtt_server = "192.168.137.86";
|
|
|
|
const char *mqtt_server = "192.168.24.215";
|
|
|
|
|
|
|
|
|
|
|
|
const int deviceId = 2;
|
|
|
|
const int deviceId = 2;
|
|
|
|
|
|
|
|
|
|
|
|
@ -46,7 +46,6 @@ void reconnect()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
Serial.println("connected");
|
|
|
|
Serial.println("connected");
|
|
|
|
client.subscribe("door/lock");
|
|
|
|
client.subscribe("door/lock");
|
|
|
|
client.subscribe("alarm/state");
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
else
|
|
|
|
{
|
|
|
|
{
|
|
|
|
@ -69,40 +68,16 @@ void callback(char* topic, byte* payload, unsigned int length) {
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
Serial.print("Message arrived [");
|
|
|
|
Serial.print(topic);
|
|
|
|
Serial.print(topic);
|
|
|
|
Serial.print("] ");
|
|
|
|
Serial.print("] ");
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
Serial.print((char)payload[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Serial.println();
|
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
|
|
|
|
if (strcmp(topic, "door/lock") == 0)
|
|
|
|
if (doc["deviceId"] == deviceId && doc["lock"] == true) {
|
|
|
|
{
|
|
|
|
digitalWrite(RELAY_ACTIVATION_PIN, HIGH); // Turn the relay on
|
|
|
|
if (doc["deviceId"] == deviceId && doc["lock"] == true) {
|
|
|
|
} else {
|
|
|
|
digitalWrite(RELAY_ACTIVATION_PIN, HIGH);
|
|
|
|
digitalWrite(RELAY_ACTIVATION_PIN, LOW); // Turn the relay off
|
|
|
|
} else {
|
|
|
|
|
|
|
|
digitalWrite(RELAY_ACTIVATION_PIN, LOW);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
else if (strcmp(topic, "alarm/state") == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (doc["changed"] == true) {
|
|
|
|
|
|
|
|
if (strcmp(doc["status"], "Armed") == 0)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
if (bool reed = digitalRead(REED_ACTIVATION_PIN) == 1)
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
DynamicJsonDocument doc(1024);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
doc["deviceId"] = deviceId;
|
|
|
|
|
|
|
|
doc["open"] = reed;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
char buffer[1024];
|
|
|
|
|
|
|
|
size_t n = serializeJson(doc, buffer);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
client.publish("door/status", buffer, n);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@ -123,7 +98,7 @@ bool lastReed = 0;
|
|
|
|
|
|
|
|
|
|
|
|
void loop()
|
|
|
|
void loop()
|
|
|
|
{
|
|
|
|
{
|
|
|
|
bool reed = digitalRead(REED_ACTIVATION_PIN);
|
|
|
|
bool reed = !digitalRead(REED_ACTIVATION_PIN);
|
|
|
|
|
|
|
|
|
|
|
|
if (reed != lastReed)
|
|
|
|
if (reed != lastReed)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|