Compare commits

..

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

@ -3,12 +3,10 @@
#include <PubSubClient.h>
#include <ArduinoJson.h>
const char *ssid = "PET Aflytningsvogn #42";
const char *password = "FuckGFwifi123";
const char *ssid = "PET Aflytningsvogn #43";
const char *password = "zwr33htm";
const char *mqtt_server = "192.168.137.86";
const int deviceId = 2;
const char *mqtt_server = "192.168.24.215";
WiFiClient espClient;
PubSubClient client(espClient);
@ -46,7 +44,6 @@ void reconnect()
{
Serial.println("connected");
client.subscribe("door/lock");
client.subscribe("alarm/state");
}
else
{
@ -60,7 +57,6 @@ void reconnect()
}
#define RELAY_ACTIVATION_PIN D6
#define REED_ACTIVATION_PIN D5
void callback(char* topic, byte* payload, unsigned int length) {
StaticJsonDocument<256> doc;
@ -69,40 +65,16 @@ void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (size_t i = 0; i < length; i++) {
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
if (strcmp(topic, "door/lock") == 0)
{
if (doc["deviceId"] == deviceId && doc["lock"] == true) {
digitalWrite(RELAY_ACTIVATION_PIN, HIGH);
} 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);
}
}
}
if (doc["deviceId"] == 2 && doc["lock"] == true) {
digitalWrite(RELAY_ACTIVATION_PIN, HIGH); // Turn the relay on
} else {
digitalWrite(RELAY_ACTIVATION_PIN, LOW); // Turn the relay off
}
}
@ -111,7 +83,6 @@ void setup()
Serial.begin(115200);
pinMode(RELAY_ACTIVATION_PIN, OUTPUT);
pinMode(REED_ACTIVATION_PIN, INPUT);
setup_wifi();
client.setServer(mqtt_server, 1883);
@ -119,30 +90,7 @@ void setup()
reconnect();
}
bool lastReed = 0;
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();
}
Loading…
Cancel
Save