Hey guys, sorry for the slow response… This is my arduino scrip below that I have loaded on a Wemos D1 Mini which has the relay shield that they also sell.
This uses the “Homie” firmware which you can find by searching these forums. It takes care of the wifi and MQTT stuff nicely.
My gate has 2 terminals for a push button switch to be used, I’ve connected these up to my relay. I have also mounted a magnetic reed switch to the gate and loaded it up as a contact so that I can tell if the gate is open or closed.
I needed to use the debouncer because the magnetic reed switch flicks between open/close a few times in the split second that it registers as opened or closed.
#include <Homie.h>
const int PIN_RELAY = D1;
const int PIN_DOOR = D2;
Bounce debouncer = Bounce(); // Bounce is built into Homie, so you can use it without including it first
int lastDoorValue = -1;
HomieNode GateSensorNode("gatesensor", "sensor");
HomieNode GateRelayNode("gaterelay", "switch");
bool doorOnHandler(String value) {
if (value == "true") {
digitalWrite(PIN_RELAY, HIGH);
Homie.setNodeProperty(GateRelayNode, "on", "true"); // Update the state of the relay
Serial.println("Gate relay is on");
delay(100); // waits for half a second and then switches relay off again, my gate just needs to the circuit to be closed for a short time
digitalWrite(PIN_RELAY, LOW);
Homie.setNodeProperty(GateRelayNode, "on", "false");
Serial.println("Gate relay set back to off");
} else if (value == "false") {
digitalWrite(PIN_RELAY, LOW);
Homie.setNodeProperty(GateRelayNode, "on", "false");
Serial.println("Gate relay is off");
} else {
return false;
}
return true;
}
void loopHandler() {
int doorValue = debouncer.read();
if (doorValue != lastDoorValue) {
Serial.print("Door is now: ");
Serial.println(doorValue ? "open" : "close");
if (Homie.setNodeProperty(GateSensorNode, "open", doorValue ? "true" : "false", true)) {
lastDoorValue = doorValue;
} else {
Serial.println("Sending failed");
}
}
}
void setup() {
pinMode(PIN_DOOR, INPUT_PULLUP);
pinMode(PIN_RELAY, OUTPUT);
digitalWrite(PIN_DOOR, HIGH);
digitalWrite(PIN_RELAY, LOW); // turn off relay with voltage LOW
debouncer.attach(PIN_DOOR);
debouncer.interval(500);
Homie.setFirmware("homie-gatesensor", "1.0.1");
GateRelayNode.subscribe("on", doorOnHandler);
Homie.registerNode(GateSensorNode);
Homie.registerNode(GateRelayNode);
Homie.setLoopFunction(loopHandler);
Homie.setup();
}
void loop() {
Homie.loop();
debouncer.update();
}
This is what my items in Openhab 1.8 look like.
Contact Gate01Contact "Front Gate" <slidinggate> (GroupOutdoors) {mqtt="<[mosquitto:homie/0fc787e0/gatesensor/open:command:OPEN:true], <[mosquitto:homie/0fc787e0/gatesensor/open:command:CLOSED:false]"}
Switch Gate01Button {mqtt="<[mosquitto:homie/0fc787e0/gaterelay/on:state:ON:true],<[mosquitto:homie/0fc787e0/gaterelay/on:state:OFF:false],>[mosquitto:homie/0fc787e0/gaterelay/on/set:command:ON:true],>[mosquitto:homie/0fc787e0/gaterelay/on/set:command:OFF:false]"}
And as you can see from the up time below, it’s pretty reliable for a $15 setup.
