Creating a garage door contact switch for detecting if the door is open.

I had my garage door added to my house alarm. In doing so, it was added to a button on the remote we use for arming and disarming. Well after one morning where my son accidentally opened the garage door (which we can’t see from the house) I looked at adding a reed switch to the door to detect it’s open state.

I hadn’t used a Wemos D1 Mini before but I liked the size of them, and there seemed to be a fair bit of code around to base off.

After buying 3, and finally taking delivery of some DHT shields (DHT12 for temperature), some reed switches and a few other bits and pieces for future projects I started playing in the Arduino IDE.

What most blogs will give you is a schematic breadboard with the Wemos pins, but then it tends to get a little fuzzy in showing how the various components are wired up.

eg:

So in actual case with my 2 wire reed switch, I wired 1 wire to the ground header of the wemos, the other wire to the D2 header.

Excuse the soldering, it’s been a long time. Also I’ve moved these connections to the rear, as I intend to mount in an enclosure and want the DHT Shield on top exposed the the air (not that the temperature on this is important).

But what you should see is the D2 and GND pins being used. The DHT Shield communicates on D4.

Next the code.

I’ve decided on a framework called Homie. This is an MQTT enabled framework that allows for over the air (OTA) firmware updates and a consistent messaging protocol for sensors.

There are a number of blog posts aroudn getting a Wemos D1 to work in the Arduino IDE, so I wont cover this.

Here is all your specific code for your sensors. So in this case I’m setting the reed property type (eg on off), and defining the pin that is read. For the DHT I’m setting the unit type for the temperature and humidity.

The state of the reed switch is sent regardless on the same interval, if not already set. Probably not needed as homie defaults each message to retained on the MQTT broker.

Lastly the function is looped each second.

Once this code is uploaded to the D1 via the Arduino IDE. Open the Serial Monitor to see messages with the connected D1 Mini. This should show the device attempting to connect to a WIFI network – of which it doesn’t know any. So you need to post it a config file which tells the device how to connect. http://marvinroger.github.io/homie-esp8266/2.0.0-beta.1/configuration/json-configuration-file/. Once the device has figured it there is no known network, it will actually create an AP. Connect to this and from here navigate to http://setup.homie-esp8266.marvinroger.fr which should take you to the Wemos D1 mini running homie. Here there are pages to configure the device name (this is the MQTT topic it will use), it’s nice name, the WIFI network and password, the location of your MQTT broker and base topic. Enter all these and save.

I use a base of sensors/d1mini for my MQTT topic. And for a device name garage-sensor, I should expect to see mqtt messages appearing in the topic sensors/d1mini/garage-sensor. I use this naming scheme for other devices as well, such as a raspberry pi with a DHT sensor is on sensors/dht/<device>.

At this point the device should reboot. I found mine do not, so I have to hard reboot (pull power). Again keep it connected via serial monitor and you should see it come up and connect to your WIFI network.

If you seen the connect to the network, then you can now go to your mqtt broker and check for messages. Given mine is on a linux, I’ll be using the linux mosquitto commands. But any MQTT client will work.

To list all entries for this example device I’d enter (assuming that I was running the command on the same server as the mqtt broker.:

So if I see this then I know the device is a, working, two, reading my reed switch and DT shield. It may take a little while for the DHT values to be populated. The $online tag is really useful for know the device in online and running.

Next we’ll get the values into OpenHAB – but the concept should be similar for Home Assistant. Here is an items file I have for my D1Mini sensors. Once thin I like with the new version of OpenHAB2 is that you can (if you wish) segregate items types by technology.

The important bits here is that it’s using the MQTT binding, with a defined mqtt server (done as part of the OpenHAB config, and looking at sensors/d1mini/garage-sensor/reed/state for it’s state information. If we look back at the code, when the reed switch is open (too far from each other) the word “open” will be posted to this topic. When closed, ”closed” is posted. As that wasn’t what OpenHAB was actually expecting, it expected OPEN and CLOSED in capitals, the MAP(onoff.map) is a translation table which translates open to OPEN.

davidcole's profile

Been thinking it would be nice to have a blog but not sure if I have enough to say.

I'm an I.T worker from Wellington New Zealand.

I like my toys so this will probably have posts about my dealings with those.

My Cellphone is an iPhone 5s

I run a NextPVR based PVR at home to replace my video recorder, DVD player and to host all my music. I'm also really big on Plex, for centralising all my music, videos and I've written a plugin or two for it for accessing live TV and for storing recordings with metadata.

This blog is hosted by Geekzone. You can have a Geekzone Blog, free for non-commercial use, when you participate!
Report this post. Contents are property and copyright of the author, or licensed. Geekzone®