Cheap garagedoor sensor using limitswitches on NodeMCU ESP8266 and HomeyDuino

Hi all,

As I wanted to be notified when our garagedoor opened I figured out a way to install two cheap limitswitches in the overhead sled track and connect these to a NodeMCU ESP8266. Running HomeyDuino you can set notifications on the triggering of one or two of these limitswitches.

Thanks to @robertklep and @Stekkies12!

A shortlist of things you need:
5V USB power adapter
NodeMCU ESP8266
two resistors 10K
two Limitswitches DL mini Panasonic
couple of feet of wire 2core electric wire 0,5mm stranded

The code is verified but please use this code at your own risk.
Please setup your hardware parts on breadbord first to test and note that I take no responsibility or liability.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <Homey.h>

/* Garagedoor Sensor with NodeMCU ESP8266 V1 and HomeyDuino

The NodeMCU is powered with a 5V 700ma microusb power adapter
The Garagedoor sensors are 2 Panasonic Limit switches are one side connected to Pin D1/D2, other side to 10K resistor to GND.
Once this code is correctly uploaded to the ESP8266 NODEMCU using the Arduino IDE, goto the Homey app and install the HomeyDuino app, then in the devices menu add a device, select     homeyduino and wait for the discovery to show your 'Limitswitch' project. 
In homey goto flows and create a new flow. Create a card Trigger [Boolean] with Tags Value is _Ls1state in the When section.
In homey goto flows and create another new flow. Create a card Trigger [Boolean]  with Tags Value is _Ls2state in the When section.
Create a notification Gargagedoor Alarm Opening! with 0 seconds delay in the in the Then section. Don’t forget to save the flow as Garagedoor Alarm.
Create a notification Gargagedoor Status fully opened with 0 seconds delay in the in the Then section. Don’t forget to save the flow as Garagedoor Status.

*/

unsigned long previousMillis = 0;
const unsigned long interval = 2000; //Interval in milliseconds for reading setpoints from Homey

// variables will change:
int LS1state = 5; //connect one side of the limit switch to D1, the other side through a 10K resistor to GND
int LS2state = 4; //connect one side of the second limit switch to D2, the other side through a 10K resistor to GND
int LS1 = 0;
int LS2 = 0;
int stepsHomey1;
int stepsHomey2;
float PositionFloatLS1 = 0; //Setpoint from Homey for limitswitch1
float PositionFloatLS2 = 0; //Setpoint from Homey for limitswitch1

void wifi() { //This loop is for connecting to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print(“Connecting with WiFi “);
WiFi.begin(“YOUR_SSID”, “YOUR_WIFI_KEY”);
uint8_t timeout = 30;
while (WiFi.status() != WL_CONNECTED) {
delay(500); Serial.print(”.”);
if (timeout<1) break;
}
if (WiFi.status() == WL_CONNECTED) {
Serial.println(" “);
Serial.println(“Connected to WiFi!”);
Serial.print(WiFi.localIP());
Serial.println(”)");
}
}
}

void setup() {
Serial.begin(9600);
Homey.begin(“Limitswitch”); //The name of your project, every project needs another name!
Homey.setClass(“Wall Plug”); //Put here the device class, in this case Blinds
Homey.addCapability(“onoff_set”, ValHomey); //Put here the capability
Homey.setCapabilityValue(“onoff_set”, ValHomey);
pinMode (LS1state, INPUT_PULLUP);
pinMode (LS2state, INPUT_PULLUP);
}

void loop() {
// put your main code here, to run repeatedly:
Homey.loop();
wifi();
limitswitches();
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
ValHomey ();
}
}

void limitswitches() {
stepsHomey1 = (PositionFloatLS1 * 2); //[2] Is the total ammount of positions which can be taken
stepsHomey2 = (PositionFloatLS2 * 2); //[2] Is the total amount of positions which can be taken
LS1 = digitalRead(LS1state);
LS2 = digitalRead(LS2state);

if (stepsHomey1 != LS1){ //To compare the homey value to the limitswitch value
LS1 = 1; //Limitswitch 1 activated
}

if (stepsHomey1 == LS1){ //To compare the homey value to the limitswitch value
LS1 = 0; //Limitswitch 1 not activated
}

if (stepsHomey2 != LS2){ //To compare the homey value to the limitswitch value
LS2 = 1; //Limitswitch 2 activated
}

if (stepsHomey2 == LS2){ //To compare the homey value to the limitswitch value
LS2 = 0; //Limitswitch 2 not activated
}
}

void ValHomey (){
PositionFloatLS1 = Homey.value.toFloat(); //number 0.00 - 1.00
PositionFloatLS2 = Homey.value.toFloat(); //number 0.00 - 1.00
Serial.print ("LS1 state from the Homey: "); Serial.println(stepsHomey1);
Serial.print ("LS2 state from the Homey: "); Serial.println(stepsHomey2);
Serial.print ("Actual position of LS1: "); Serial.println(LS1);
Serial.print ("Actual position of LS2: "); Serial.println(LS2);

1 Like

Wow, super to hear you could use my code and really good to see that you also share the code! I hope kant Roger Homey / Homyduino can use our input! :smile:

Thanks for sharing this great example and code.
Can you also share some pictures of the installation?