Hey guys Matt here from MKSmartHouse.com and in this video I am going to show you how to
setup the software for the door sensor using a MAC.
[Intro]
So in the last video we left off with the door sensor being fully built so all it needs
is firmware and to be connected to the home automation server.
So let's start off with the firmware.
I recommend having my website open up so that way you have all the steps and commands ready,
and so you do not have to type in everything you can just copy and paste.
I know this is the software video but, there are some hardware things that we need besides
a computer.
We are going to need an arduino of some kind preferably a UNO or a MEGA equivalent with
its usb cable of course and male to male dupont jumper cables.
Links to these items will be in the description or on the website.
The first thing we are going to do is grab the arduino and put a jumper cable from RES
to GND.
Then grab the door sensor and connect all the pins to their corresponding spots so,
TX to TX, RX to RX, GND to GND, and 5V to 5V.
Before we continue, check to make sure the 2 pin jumper is above PGM.
Next we are going to go to the computer and go to the Arduino website https://www.arduino.cc/en/Main/Software
scroll down to download the Arduino IDE, and click Mac OSX, then press download.
Next, go to finder and your downloads folder.
Drag the Arduino Application to your applications folder.
Then click on launchpad and then on the arduino application.
When it asks if you want to open click open.
Great we installed the arduino IDE, so now plug in your arduino.
If you are using a chinese uno like me there is one more thing we have to do which is install
the CH430 drivers.
So go to http://blog.sengotta.net/wp-content/uploads/2015/11/CH341SER_MAC-1.4.zip and it should automatically download the driver.
Then go to finder and your downloads folder.
Open the folder and double click on the CH43x_Install package.
Go through the setup by clicking Continue, then install and type in your admin password
and press install software as well as clicking continue installation.
Once it is done click restart and if it asks you can say move to trash.
When your computer is back up, open up the arduino IDE again.
To confirm your arduino is connected in the top bar go to Tools and if you hover over
Port: you should see /dev/cu.wchusbserialXXXXX if you can then select it.
Now we are going to add the ESP8266 boards so go to Arduino, then preferences and in
Additional Boards Manager URLs: type in http://arduino.esp8266.com/stable/package_esp8266com_index.json then press OK.
Next go to tools.
Then hover over board and to the right click Boards Manager.
In the search box type in esp8266.
It should be the first one and it should say esp8266 by esp8266 Community.
Click on it and press install.
Once it is installed press the close button.
Now, go back to Tools, hover over board and go to the right and scroll down to and click
Generic ESP8266 Module.
Next we are going to add the MQTT Library, so go to sketch and in the drop down hover
over Include Library and in the right, click on Manage Libraries.
In the search type in MQTT.
Scroll through the list to find the one that Says MQTT by Joel Gaehwiler, click on it and
press install.
Once installed click close.
Great!
All the prep work is complete.
Close out of arduino and now all we have to do is grab the code so we are going to head
over to my site, the link is in the description to the exact page and press the download MK-DoorSensor
Firmware.
On the new page press download.Then go to your finder and downloads folder and double
click on MK-DoorSensor.ino.
A pop up will come up asking if you want to put it in a folder, click OK.
It should bring up the code for the door sensor, and there are only a few things we have to
change.
The first thing is the wifi settings which are the ssid and password so change those
according to your network.
Please keep in mind that the esp8266 only works on 2.4ghz so type in your 2.4ghz wifi
ssid and password not your 5ghz.
Also when adding the information only change what is inside the quotation marks.
Next set of parameters are the Web Updater settings.
The devices I designed are great because I implemented a web user interface for each
individual device so that way if you ever have to flash new firmware you just go to
its web address.
The web address information is found at that top of the code in the giant comment block.
The first parameter is the hostname of the device, usually I only change the last digit
but since this is the first door sensor I will keep it as it is.
Next is the update path and personally I don’t change that.
After that is the web user interface username and password, these are the credentials you
use to access the webpage because each device is protected.
The next set of parameters are for mqtt.
The first one is the outTopic and this is the topic for which the device sends commands
or messages to on the server.
The next one is the MQTT Server Ip address and this is simply the IP address of your
home automation or openhab server.
The last one is the Unique device ID and this simple differentiates each device on the MQTT
side, I usually just change the last digit for every single device.
That is it the code is ready to be flashed.
So go to tools and make sure the Board: is Generic ESP8266 Module and the port is /dev/cu.wchusbserialXXXX.
Once those are good press the upload button, it is the one with an arrow pointing to the
right.
When it is uploading you should see dots moving at the bottom and some percents.
After it is done uploading you should see it say 100% and Done uploading.
To confirm that it flashed correctly and is working you can fire up MQTT.fx, connect to
the server and in the subscribe section type in # and press subscribe.
Now if you separate and bring together the two contacts of the door sensor you should
see it either say OPEN or CLOSED depending on its current status.
If you do not have MQTT.fx then check out my Home Automation Server Setup Guide.
Now that we know it works let's close up the device and get it ready for use with the Automation
System.
First unplug the dupont wires in between the arduino and the device.
Then take the 2 pin jumper and move it over so it is above RUN.
After that take the lid to the device and put it on.
Finally plug the device into the wall.
Now the device is complete and just needs to be added to OpenHAB.
So ssh into your pi or whatever your server may be.
The first thing we are going to do is create the door sensor item.
So type in sudo nano /etc/openhab2/items/home.items and press enter, you may need to type in admin
password.
Then type in the comment //Security items then underneath that we are going to create
a software switch to control the security system.
Type in Switch SecuritySystem "Security System" and press enter.
Now we are going to create the actual item type in Contact MKDoorSensor1 "Door Status
[%s]" { mqtt="<[broker:MK-SmartHouse/security/MK-DoorSensor1:state:default]" } and press enter.
Now let me go over the two items we created.
The first one is a dummy switch and we will implement it later more in the rules but what
it will do is turn the openHab security system on and off.
The second item is the door sensor.
This is the line you would replicate if you had more door sensors.
Anyway the first part is Contact and since this device is a door sensor we use contact
type.
The next part is the item name and I just used its hostname without the dash.
After that is the label text and it is what shows up in the interface and how it is formatted
with the name and value.
Next to that is the icon name which is what picture shows up in the interface.
Then lastly, we have the mqtt path to the device in there is the outTopic that we coded
into the device.
Now press control x then y and enter.
Next up is the sitemap file so we can see the device status and our switch.
Type in sudo nano /etc/openhab2/sitemaps/home.sitemap and press enter.
It will bring up the sitemap.
If you are following along with my series then we have many different frames in our
sitemap.
I am going to replace what is in the security frame with our new items.
So delete the demo switch and type in Switch item=SecuritySystem and press enter and line
up your cursor underneath the S in switch and type in Text item=MKDoorSensor1 what we
did is import the items into the sitemap.
Now press control x then y and enter.
Before we go any furthur lets confirm that everything works so go to your web user interface
and then Basic UI.
You should see the Security system switch which right now does not do do anything but
you should also see the the door sensor or in my case the side door and if you open and
close the contacts you should see the status change.
Great it all works except the switch so let's fix that and make the security system do something.
In terminal type in sudo nano /etc/openhab2/rules/home.rules and press enter.
In the file type in //Side Door Sensor Is Open
rule "Side Door Sensor" when
Item MKDoorSensor1 received update OPEN then
if(SecuritySystem.state == ON) {
sendBroadcastNotification("SECURITY SYSTEM: SIDE DOOR OPEN")
} End
Let me explain what this does, it checks for whenever the door sensor sends open and if
it does, then it checks for if the security system is on and if it is then it sends every
user you setup in myopenhab.org a notification.
Now press control x then y and enter.
Go back to the basic UI and make sure the Security System switch is on.
Now make the door sensor closed then open.
If everything works right and you have a mobile device setup with openhab then you should
get a notification.
Also if you turn of the switch then you will not get notifications.
That is it!
The software is complete, now all we have to do is install the device in its final place
which will be completed in the next final installation video.
Alright thank you for watching and If you have any questions leave them in the comments
section below or head over to mksmarthouse.com/forum where you have a better chance of it getting
answered.
Good Bye!