Compare commits

..

No commits in common. 'main' and 'Doorlock' have entirely different histories.

@ -3,12 +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;
WiFiClient espClient; WiFiClient espClient;
PubSubClient client(espClient); PubSubClient client(espClient);
@ -46,7 +44,6 @@ void reconnect()
{ {
Serial.println("connected"); Serial.println("connected");
client.subscribe("door/lock"); client.subscribe("door/lock");
client.subscribe("alarm/state");
} }
else else
{ {
@ -60,7 +57,6 @@ void reconnect()
} }
#define RELAY_ACTIVATION_PIN D6 #define RELAY_ACTIVATION_PIN D6
#define REED_ACTIVATION_PIN D5
void callback(char* topic, byte* payload, unsigned int length) { void callback(char* topic, byte* payload, unsigned int length) {
StaticJsonDocument<256> doc; StaticJsonDocument<256> doc;
@ -69,40 +65,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"] == 2 && 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);
}
}
}
} }
} }
@ -111,7 +83,6 @@ void setup()
Serial.begin(115200); Serial.begin(115200);
pinMode(RELAY_ACTIVATION_PIN, OUTPUT); pinMode(RELAY_ACTIVATION_PIN, OUTPUT);
pinMode(REED_ACTIVATION_PIN, INPUT);
setup_wifi(); setup_wifi();
client.setServer(mqtt_server, 1883); client.setServer(mqtt_server, 1883);
@ -119,30 +90,7 @@ void setup()
reconnect(); reconnect();
} }
bool lastReed = 0;
void loop() void loop()
{ {
bool reed = digitalRead(REED_ACTIVATION_PIN);
if (reed != lastReed)
{
Serial.print("Reed has changed to: ");
Serial.println(reed);
DynamicJsonDocument doc(1024);
doc["deviceId"] = deviceId;
doc["open"] = reed;
char buffer[1024];
size_t n = serializeJson(doc, buffer);
client.publish("door/status", buffer, n);
lastReed = reed;
}
client.loop(); client.loop();
} }
Loading…
Cancel
Save