Domoticz on PI with relay

Domoticz part 1: Relays, magnetic sensors, Wake-on-LAN and Z-Wave – Domoticz with Imperi Home serving your house

Domoticz on PI with relay
Domoticz on PI with relay

This entry was initially about single and simple purpose: how to quickly develop a solution that would open gate or garadge door by simulating the remote’s operation mode – pressing a button – but using smartphone, tablet, web browser, app. Domoticz is such an overkill and also blessing for such project – that it evolved into a lot of small sub-projects – more or less andvanced.

Direct inspiration was another entry by colleague on  MalinowePi Facebook group – Roman Kubat, who showed ImperiHome. This application has great interface but does not support press-type (dorbell) buttons. Hence the whole idea!

What will you need?

Hadware:

  • Rapberry Pi (A, B, B+, 2 or even Pi Zero)
  • relay – single or better dual version
  • micorSD card – 4GB or better
  • jumper cables – for interconnection

Software:

Installing Domoticz on Rasbian

Domoticz ? This is out main software used to control the relay – very powerfull, but has nice basic function – allows control via web interface or application – and is not limited to relays – Z-Wave based devices, sensors etc. To properly install domoticz on our Raspbian system – I suggest the Jessie Light version – we need to update the system:

Last command reboots our device and the first will ask you to confirm the whole upgrade process. After the reboot – it’s time to upgrade the kernel – at the time of writting – we are at version 4.x:

Now – after the reboot, we should install additional packages needed for domoticz to work properly:

This will spawn more packages to install since they relay on others – no worry – just confirm wait until they are installed:

Now it’s time to build domoticz with these commands:

Which should give you this result:

Done? Great, now lets have domoticz started at boot:

Let’s complete the configuration by setting correct options for domoticz: uses user pi, port 8080 (we can use the classic port 80 fro something else):

Using edtior:

change the values to the following and press CTRL+x,confirm with y to save&exit:

Finally – we need to test if our domoticz starts with those values, please go to /home/pi/domoticz and run:

The expected output should be similar to this:

It works! Assuming that Pi’s IP address is 10.20.30.40- type in your browser: http://10.20.30.40:8080 and this is what we should get:

Czysty Domoticz
Clean, new Domoticz

For our last test – let’s ask domoticz to close by pressing CTRL+c:

At this point we should reboot out Pi to ckeck if domoticz starts up correctly.

WiringPi installation

WiringPi is an important additional software (library), that allows smooth control over our relay using the Pi’s GPIO ports.

Just as with domoticz – WiringPi needs to be built – meaning that we need to download current software using git command ( if you still don’t have git – install it by sudo apt-get install git-core):

This should give us:

Let’s make sure we have the latest version:

Build it:

The output should similar to this:

So – all installed – let’s test if we now have control over GPIO ports:

Relay installation

It’s high time to explain we do we need relay here. Well the idea behind it is simpe: simulate one second of button press on the remote. This is exactly how most of the radio controlled units work (but hey this is my experience). So we will use Pi with relay controlled GPIO to simulate short circuit for one second, then release it. Relay gives us galvanic separation and ability to have a long wire to the actual garage door motor/controller – extending the GPIO might cause some instability. As you’ve noticed – Pi has quite a lot of pins and we will use the one that is showing up all over the internet as GPIO4.
Let’s get back to domoticz and create virtual button – so virtual – doesn’t do anything – it’s not related to hardware that domoticz is supporting. Choose Setup and then Hardware – and add:

Domoticz - dodaj port dummy
Domoticz – dodaj port dummy

But why complicate it ? This is because we want the following control:

  • The button should have the same functionality whenever it is pressed to be ON or OFF. So works just like a dorbell but is not a dorbell button
  • If we press ON or OFF it should switch from open state to short stat for one second and go back to open state.

To get this we should create the button in Destkop menu (the star in Switches) and use LUA (language used by domoticz to do more under the hood). This script will do exactly what we said previoulsy – short for one second, but otherwise keep it open:
So, having the dummy switch “hardware” let’s choose Create Virtual Sensors and choose “Switch”:
add-dummy-switch
Go to Setup, then Devices – find our Dummy and use the green arrow to add it to Switches tab:

Adding switch
Adding switch

Now let’s name it “Garage” – notice it’s bit different on the screenshot:

Switch and new Icon
Switch and new Icon

The actual script that will emulate our button should be put in: ~/domoticz/scripts/lua, and the name has to be exactly with the Garage name we created the switch: script_device_Garage.lua
Here’s the script contents:

Now the hardware part – set-up the power for relay, connect the GPIO4 and … test!
You can use +5V from Pi, GND and GPIO4 – press the button on domoticz webpage and the relay should click for one second:

Pi B+ and relay - connected
Pi B+ and relay – connected

So all this just because we will later use ImperiHome that doesn’t do dorbells. So this can be done much easier, but it’ll get easier later.