The plan is to make two series of posts showing how I use the home automation software Domoticz on the very popular Raspberry Pi single board computer. The first two posts will be common for both series. Then one series of posts will be about using X10 power line hardware with Domoticz on an older Raspberry Pi. The second series of posts will be about using ESP8266 based hardware, mostly reprogrammed Sonoff WiFi switches from ITEAD, with Domoticz on the newest Raspberry Pi.
Two reasons explain why I am doing this. Most of my X10 power line devices will soon have a new home. Lamp modules, appliance modules, socket receptacles, a couple of old Radio Shack plug'n power remote controller, a CM11A computer power line interface and an older Raspberry Pi hosting a Domoticz home automation server will be going to my sister's house in a few weeks.
At the same time, I trashed the SD card on the Raspberry Pi 3 running my current version of Domoticz. This is a lesson in the importance of backing it up regularly. This is also a great opportunity to start over and hopefully improve things the second time around. At the same time I am getting rid of some of the clutter associated with using what I now consider legacy hardware. However, until I build controllers to replace the wireless controllers that I presently use, support for X10 wireless hardware will be installed on the newer Pi.
Table of Contents
- Hardware
- Install Raspbian Jessie Lite
- Boot and Log In with Keyboard and Monitor
- Boot and Log In with SSH
- Udpate Raspbian
- Static IP Address and Wireless Connection
- Routing Around in the Router
- Raspberry Pi Configuration
- SSH Session
- Journalling Tweak
- Install Domoticz
- Basic Domoticz Settings
Hardwarel
My sister's home automation server will be a Raspberry Pi model B (Rev 2.0 with time stamp of 2011.12). This is an early model, with two USB 2.0 ports, one 10/100Mb Ethernet port and 512 MB of memory. The CPU is a single-core, 32 bit, ARM1176JZF-S which runs at a nominal 700Mhz but which is slightly overclocked to run at 800Mhz.
My own home automation system will be Domoticz on a Raspberry Pi 3 model B. It has four USB 2.0 ports, one 10/100Mb Ethernet port, built in WiFin and 1 GB of memory. The CPU is a quad-core, 64 bit, ARM Cortex-A53 which runs at 1.2GHz. As of the 21st of May 2017, this is the latest Raspberry Pi model, aside from the Model Zero.
There are very few differences between the two systems when it comes to installing the operating system and the home automation software. Hence this post applies to both.
Install Raspbian Jessie Lite
There's a new way to burn an image on an SD card: ETCHER. Its available for Linux, Windows and OS X, so flashing the SD card with the OS should be, for the most part, the same in all cases.
- Download the latest version of Raspian Jessie Lite. I obtained version 2017-04-10, Kernel 4.4, but the foundation will update this file from time to time so expect small changes to the configuration procedure. There is no need to extract the image file.
- Download the correct version of ETCHER.
- Extract the application. In Linux 64 bit, it is called
Etcher-1.0.0-linux-x64.AppImage. - Insert the SD card into a card reader.
- Double-click on
Etcher-1.0.0-linux-x64.AppImage. When asked if the desktop file should be installed, I answered No, but if you think you will reuse this application often you can answer Yes. - Wait until Etcher launches.
- Click on Select image and select the downloaded Raspian Jessie archive.
- Click on Change if the target SD card
is not correct. Since one SD card connected was connected to the desktop
computer,
ETCHERfound it without problem. - Click on Flash!.
- Wait until
Etcherhas flashed the card and checked its content and then close it.
Simple, straightforward, ETCHER (or is it Etcher?) is very good. Thank you to the developers.
The easiest way to start using the Raspberry Pi is to connect a keyboard and monitor to it and to power it up. This is described in the next section. If a free keyboard and monitor or not available, go to the section named Boot and Log In with SSH.
Boot and Log In with Keyboard and Monitor
Insert the SD memory card into the Raspberry Pi, connect a USB keyboard (there is no need or use for a mouse) and a monitor, and finally connect the power supply. If all goes well, Raspbian will boot up. It may resize the file system, in which case it will reboot very quickly. Log in after the boot sequence:
Later on, I will show how to change the password. That should always be done. Remember, numerous hackers get into systems because well-known default passwords are not changed. For now, skip the following section and move on to Update Raspbian.
Boot and Log In with SSH
If a keyboard and monitor are not available then it may be possible to
boot the Raspberry Pi and log in using an SSH session from a desktop
computer. By default, the Raspberry Pi SSH server is not enabled. However
adding a file named ssh in the
Once ETCHER has finished copying the OS image on the SD card, the latter
will be mounted on a Linux system. In Ubuntu, the two partitions created
on the card it shows up in the launcher bar. The drives are actually
mounted in a directory called /media/user where of course
user is your user name. The following shows how to create an empty
file with the touch utility in Linux. First open a terminal
by pressing simultaneously on the three keys
Alt Ctrl T.
DHCP server on the network
which in all probability is the router supplied by the Internet service provider
(ISP). So you must have a way of finding out that address. ....
Alternatively, a static IP address can be assigned to the
Raspberry Pi. This can be done by editing configuration files in
ext partition created on the SD card. This is done with relative
ease from the desktop computer if it is a Linux system (and perhaps with a Mac)
but it probably requires installing some additional drivers in Windows to
access ext partitions.
Since I have a Ubuntu desktop, I went ahead and edited the configuration
files. The mounting point of the partition was the GIUD type name shown in the
/media/user directory. It is rather tedious to type in
such a long name, so I first made it the working directory with the
cd command. By editing the etc/dhcpcd.conf
file, a static address was assigned to both the ethernet link
(eth0) and the WiFi link (wlan0).
interface eth0 static ip_address=192.168.0.21 static routers=192.168.0.1 static domain_name_servers=192.168.0.1 interface wlan0 static ip_address=192.168.0.22 static routers=192.168.0.1 static domain_name_servers=192.168.0.1
Save this configuration by press on the keys Ctrl O (letter O not the number 0) simultaneously and then press Enter to save the file with its current file name. Exit nano by pressing span class="key">Ctrl X.
To complete the configuration of the WiFi interface, the
SSID and password of the WiFi network must be specified in a file named
wpa_supplicant.conf in the directory named
etc/wpa_supplicant/ in the ext partition.
network={ ssid="Name_of_your_wifi_network" psk="your_wifi_password" }
Insert the SD memory card into the Raspberry Pi and connect it to the local area network (with an Ethernet cable if not using WiFi) and finally connect the Raspberry Pi to its power supply. If all goes well, Raspbian will boot up and eventually the SSH server will start. Read the section SSH Session to find out how to log in from the desktop computer with an SSH session. Then go back to the previous sections to Update Raspbian and Raspberry Pi Configuration. Finally continue with 10 Journalling Tweak.
Udpate Raspbian
I updated and upgraded Raspbian immediately because it was possible to connect the Raspberry Pi to the local area network with an Ethernet cable.
On the first generation Raspberry Pi, this last step will take a long, long time. Treat yourself to a coffee. You may have time to finish reading Tolstoy's War and Peace. On the Raspberry Pi 3, you will have time to savour a chocolate bar along with your coffee.
If you can only manage a WiFi connection, go to the next step immediately and upgrade latter.
Static IP Address and Wireless Connection
The home automation program Domoticz contains
a Web server which is accessed with a Web browser such as Firefox, Edge,
Safari etc. This means that the Web server must be at a fixed, static
IP address. However, by default the Raspberry Pi uses the DHCP
protocol to obtain a variable, dynamic IP address from a DHCP
server on the local area network. That server will in all likelihood will be
the router provided by the Internet service provider.
That means that some changes have to be made to the network configuration. It also means that a decision must be made as to which interface will be used. The Raspberry Pi 3 contains two network interfaces: a wired Ethernet link (Cat5 or Cat6 cable with RJ45 connectors) and a wireless WiFi link. I use the wireless interface because it allows me to place the Raspberry Pi out of the view under a small bookcase. A USB-WiFi dongle provides wireless connection to the LAN on the older Raspberry Pi. I do not yet know which one of the wired or wireless interface will be used. Hopefully, it will be possible to set the small computer close to the router and to use an wired connection because there are only two USB connectors on the older Raspberry Pi and I would like to avoid using a USB hub.
Here is how to set the static IP address for the wired and wireless
interfaces. Open the dhcpcd configuration file in the
nano editor and add the following lines, albeit with appropriate
changes!
interface eth0 static ip_address=192.168.0.21 static routers=192.168.0.1 static domain_name_servers=192.168.0.1 interface wlan0 static ip_address=192.168.0.22 static routers=192.168.0.1 static domain_name_servers=192.168.0.1
By "appropriate changes" I meant that you will have to choose the correct
local area network (LAN) addresses for the interfaces, router and domain name
server for your LAN. Remember, interface eth0 refers to a wired
link to a switch or router on the local area network (LAN) over an Ethernet
cable while interface wlan0 refers to a wireless link to a WiFi
router on the LAN. Of course, in some cases it would be perfectly acceptable
to have only one static address. However if you do have two, then their IP
addresses must be different. And of course, the assigned IP addresses must
not be used by other devices connected to the LAN.
Since I am preparing two servers and will be running both of them on the same network, at least for a few weeks, each got a different set of static addresses. For argument's sake, I'll suppose from now on that the static IP address used by the Raspberry Pi is 192.168.0.22.
Save the configuration file. In the nano editor, do that
by pressing Ctrl O
(letter O not the number 0) simultaneously and then press
Enter to save the file with its current file name.
Exit nano by pressing
Ctrl X.
There is an extra step required to enable the wireless connection if used.
The following instructions comes from the Raspberry Foundations's documentation. Open the
wpa-supplicant configuration file with the nano editor
and add the following at the bottom of the file.
network={ ssid="Name_of_your_wifi_network" psk="your_wifi_password" }
nano by pressing
Ctrl X.
Time to test. First cycle the Raspberry Pi off and then back on, or better yet reboot the Raspberry Pi and then log on.
Secondly, check the network interface configuration.
If you don't see something like inet addr:192.168.0.21
as shown above then it may be that only version 6 IP addresses are assigned to
interfaces. I have had that problem with Raspian Jessie. After
some searching, I found the
following solution. Create the file
/etc/sysctl.d/local.conf containing a single line disabling IPv6.
net.ipv6.conf.all.disable_ipv6=1
Save this configuration file in the usual way. Press
Ctrl O
(letter O not the number 0) simultaneously and then press
Enter to save the file with its current file name.
Exit nano by pressing
Ctrl X.
Even if the IP version 4 static addresses are assigned, you may want to disable IP version 6 on the Raspberry Pi as shown above. As far as I can tell, there is no real need for the newer version of IP on a local area network and by not running that stack you may be giving the Raspberry Pi a little bit more breathing space. Having said that, my ISP provided router supports IPv6 so who knows what is coming up?
Reboot and check with ifconfig. Everything should now work.
If it does not seem to work look at the
next section.
If the contents of dhcpcd.conf looks like gibberish, take
heart, finding needed IP addresses needs to be done only once and there is a
lot of information on the Web if what you find in this box is not sufficient.
The first step is to find out the IP address of your router. Have a look on the back or bottom of the device itself. If you are out of luck, and find nothing try running nmap on a Linux desktop machine (it is installed by default in Ubuntu), Angry IP Scanner on a Windows computer (also runs on Linux and MAC). If you have a wireless router, you can try NetX on an Android tablet or phone. I am sure there is something equivalent on IOS. Of course that machine must be connected to your LAN.
Here is the nmap output on my Ubuntu desktop. Note that
this is done from a terminal (i.e. command line for Windows folks). The
easiest way to start a terminal in Ubuntu is with the "three finger salute"
Alt Ctrl T.
Fortunately, the first entry also contains a host name and, as luck would have it, my router is a Hitron. Presto! 192.168.0.1 is the correct router IP address. In the great majority of cases, it is also the address of the domain name server (DNS). (Ok,if you want to be pedantic, it's the route towards a couple of DNS servers run by my internet service provider.)
You can check that this is the correct address. Open a Web browser on
your desktop or other computer connected to the LAN and put the IP address
in the address bar. You should get a log on screen that looks something like
mine, but remember every router model is different.
Where did the magical number 192.168.0.0/24 used in the nmap
come from? Typical "subnet" addresses in consumer routers are 192.168.0.0/24,
192.168.1.0/24 and perhaps 192.168.10.0/24. You could try those. To be more
scientific about it, run ifconfig (ipconfig in
Windows) in a terminal on a computer connected to the LAN:
Look for inet adr:aaa.bbb.ccc.ddd on one of the interfaces.
My desktop has only a wireless connection to the LAN with IP address
192.168.0.152. The subnet mask is 255.255.255.0. That last
0 in the mask means that the "subnet prefix" is made up of first 3 bytes
(hence 24 bits which explains the /24) while devices on the network, including
the router, are identified by the last byte of the address with decimal values
from 0 to 254. So we keep the first three bytes and stick a 0 for the last
byte to get 192.168.0.0.
How to make sure that you have chosen unused static IP addresses? Look
at the output of nmap again. It should show all currently used
IP addresses. So just pick one that is not used. I my experience, the
router will twig on to this self-assigned IP address and not assign it to
other devices using the DHCP protocol to obtain a dynamic
address.
Routing Around in the Router
Since you had to find the router's IP address, you may want to change some of its settings. This is optional. Remember that not only must you know the IP address of the router but you will also need the the user name and password. A good search engine may be your best friend to find out how to log onto the router's web server and how to change settings.
First, I suggest that you change the default password. Routers are small computers and they can be hacked in some cases.
My second suggestion is to is to limit the range of IP addresses that can be assigned by the DHCP server. In that case, you should always choose a free static address outside of that range. That will avoid all possible conflicts (except those you create yourself by assigning the same static address to another device... it happens, believe me).
If you could not get a static IP address to stick by modifying the
Raspberry Pi configuration, then you can probably do it with the router.
This is done by assigning an IP address to a device based on the universally
unique media access control address (MAC address) that all network interfaces
have. Again, setting parameters on each router is different, but with my
router, I need to click on the DHCP Reservation
button on the Basic Settings page and then scroll to the
Manually Add Client area.
Then I specify an Client Name ("Domo" for example) the desired static IP
address ("192.168.0.21") and the interface MAC address ("b0:c7:eb:ff:56:fd")
which is called HWaddr in the output of the ifconfig
utility.
If everything failed and you could not get a static IP address, I
suggest that you increase the DHCP lease time as much as
possible. Basically that "reserves" an IP address for a device for the
specified time even if the device goes offline. On my router I can specify
a "forever" lease time. That would almost amount to a static address I
assume. But it does not have to be "forever", just choose the longest lease
available.
While I managed to define a static address in the Raspberry Pi, I still found it useful to have a longish lease time. It helps with IoT type devices (such as Sonoff switches) so that their IP address does not change on every reboot.
As you can see, except for the password change, all my suggestions can
be done in a single Basic Settings page on my router:
But remember, things will be done differently on other models.
Raspberry Pi Configuration
It is far easier to run the Raspberry Pi as a "headless server", which
means running it without a keyboard and without a monitor. Remember, I hide
my home automation system under a bookcase and I adjust the Raspberry Pi from
my desktop computer wired to the same local area network
using an SSH session. But before that can be done, the Raspberry Pi SSH server
must be enabled. Other parameters should be set at the same time. All this
can be done with the raspi-config utility:
The following text mode window will appear. Use the up and down cursor keys
to move the selection bar in the menu, and the tab key or the right and left
cursor keys to move from the menu to the action buttons at the bottom.
It is a "very good thing" to change the password, which is the first configuration
option. Click on the Tab key to move to the
<Select> action and press on the Enter key or
the space bar to continue with setting a new password.
Returning to the main menu, the host name can be changed.
I changed the host name from raspberrypi to
domopole on the older Raspberry Pi and to domo on the
newer. This is optional, but useful.
The Boot Options do not need to be changed. But the
Localisation Options should be set.
At a minimum the Timezone should be set by selecting the
nearest city picked from a list.
Activate <Select> to get to the list.
First select the continent (Geographic area):
Then select the city.
They are almost sorted alphabetically. Typing a letter moves the selection to
the first city starting with the corresponding letter.
It is now time to enable the SSH server. First select the Interfacing
Options in the main menu:
Then select SSH in the following window:
And answer Yes when asked to enable the SSH server:
From this point on, this can be done from another computer on the local
area network over an SSH connection, as explained in the next section.
Back in the main menu, select Advanced Options:
If this was not done on the first boot, select the
Expand Filesystem option:
and click on Ok when the root partition has been resized:
I think, there will be no harm done if you expand an already expanded file system.
When back in the Advanced Options screen, select the Memory Split
option:
and set the GPU memory to the minimum value, 16 MB:
Again, this will be a headless server that will not be offering a graphical
user interface. There is no need to allocate memory to the graphic processor.
To optionally speed up the system, select the Overclock option
in the main configuration menu. Below I show how I increased the speed of the
older Rapsberry Pi. I could not overclock the Rapsberry Pi 3 using this utility,
but if you insist there is some information on the Web.
Read the warning:
And then leave the CPU frequency and voltage unchanged or increase them
at your own risk
If you do overclock the system, you should consider dissipating the extra
heat with heatsinks and a fan.
There should be no need to Update since that was done with the system wide
update. The final step is to select Finish in the main menu:
If asked, reboot:
If not asked, but you changed the hostname, it is better to reboot. Be brave,
throw caution to the wind and reboot anyway, just to be safe:
There are numerous ways to check the size of the file system, if you are wondering if it has been expanded to fill the SD card. Here is one of them:
SSH Session
From now on, I will control the Raspberry Pi from my desktop, called
hp (guess why?). For that, I open an SSH session. Since
this is the first time, a security key will be added.
Loging in for an SSH session will be simpler after:
Since everything seems to be working, I have disconnected the keyboard and monitor from the Raspberry Pi.
It is not necessary to use a desktop, it is just simpler for me. Everything shown below can be done using the connected keyboard and monitor.
If you could not log in and got a long message such as
Journalling Tweak
By default, logs or journals are files saved on the SD card by
systemd. This may not be the best thing, since there is a limit on
the number of reads and writes that can be performed on an SD card. It is
best to write the log to memory.
Edit the journald configuration file, changing three lines
as shown next.
# This file is part of systemd. # [Journal] Storage=volatile Compress=yes RuntimeMaxUse=48M
The source for this tweak is the Domoticz wiki. It may be best to hold off until all is stable before doing this so that logs will be persistent.
Install Domoticz
As the download page at Domoticz says, a one-line script will install the program.
There's a bit more to it of course. A few questions must be answered, but the default answers are acceptable:
inqr The first screen shown by the installer is a reminder that a static
IP address is required, and a suggestion to donate to the project:
Next is a screen asking if HTTP and HTTPS (secure HTTP) access to the
server is to be allowed.
As can be seen both are enabled by default. So I accepted that but HTTPS
access is problematic without security certificates.
The next two screen ask for the HTTP and HTTPS port numbers
I accepted the default port 8080 for HTTP requests
and the default port 443 for HTTPS requests.
Then the installation folder must be specified:
Again I accepted the default value. The final screen gives the addresses of
the HTTP and HTTPS access pages and gives the addresses of the
The installation then concludes:
Start a web browser on your desktop and connect to the
Domoticz built-in web server on the Raspberry Pi at
http://192.168.0.22:8080. You will not see much,
because nothing is installed, but at least you are now assured that
everything is working.
If you want to change the HTTP and HTTPS ports used by
Domoticz, edit the -www and -sslwww arguments
in the domoticz.sh script file in the /etc/init.d
directory. You have to be root. Here is an example.
... DAEMON_ARGS="$DAEMON_ARGS -www 8888" DAEMON_ARGS="$DAEMON_ARGS -sslwww 444" ...
~/domoticz/domoticz.sh and it contains the default values
8080 and 443. I have renamed the template file domoticz.sh.org
to make sure that I don't confuse it with the actual start script.
Basic Domoticz Settings
Its time to define some basic system settings. Click on the Setup button at the top right and then select Settings. By default the System tab is opened.
At a minimum, the Location (longitude and latitude) should be defined. Presumably, that information is used to calculate sunset and sunrise. I used my car's GPS to find the exact values. Perhaps a GPS equipped smart phone or tablet could be used. If none of these possibilities are available, Domoticz can provide reasonably accurate coordinates given a street address. Click on the Here link underneath the Longitude box.
It is a good idea to password protect the website. Provide a username and a password in the Website Protection section. It does become cumbersome to log in every time. Thankfully, it is possible to specify "login free" local networks. Using a browser on a machine located on any one of those specified networks will log in automatically, bypassing the login requirement. You will notice that I did not include the suggested local host network 127.0.0.* because I run a Tor service on the server instead of "port forwarding" outside requests through the router. If 127.0.0.* were included as a local network, the password would not be required when connecting with the Tor browser from outside the local area network.
Do not forget to click on the Apply Settings button, otherwise the changes made will not take effect.
Consult the Domoticz wiki page Application Settings for more information about other settings.
Consult the page dedicated to installation on the Raspberry Pi. In particular you should look at the section entitled Raspberry Pi additional software installations. You will notice that I shamelessly copied the "Setting up a RAM drive on Raspberry Pi" from that page.
