Home automation - RaspberryPi, FHEM and Arch Linux

By Andreas Wilhelm Aug 17, 2014

Once upon a time, there was a computer science student blogging about all that stuff that comes to his mind … Yeah, it has been about a year since my last blog post and I apologize for that. However, I would like to share some experiences with you that I gathered during one of my projects, namely home automation using the Raspberry Pi.

In this post I will show you how you can setup the GPL’d perl server for house automation, fhem, on your Raspberry Pi or an other single board computer, like the Beaglebone (Black), Bananapi or Gooseberry, running the one and only Arch Linux distribution. Finally I will give an example of how you can use fhem to control HomeMatic or other home automation components. So lets get started with the Arch Linux setup.

Installing Arch Linux

Homematic-Adapter mit RaspberryPi

To get started, we download the latest release of Arch Linux for the Raspberry Pi, which is available on the ArchLinuxARM Project page. As the download has finished, extract the zip file to your hard drive, giving you the arch image. This image could then be written to an SD or micro SD card using the dd command:

dd bs=1M if=/path/to/archlinux-hf-2013-07-22.img of=/dev/sdX

Replace sdX in the command above with the location of the SD or micro SD card.

Please ensure that you just use /dev/sdX and not /dev/sdXi. So if your micro SD card has one partition /dev/sdb1, you should just enter /dev/sdb.

When the dd command has terminated, eject the card from your desktop PC, insert it into the Raspberry Pi and power it by plugging in the micro usb cable. You should now be able to ssh onto your Raspberry Pi using username ‘root’ with a password ‘root’.

The steps above are the only ones dedicated to the Raspberry Pi. The following steps can also be done on other boards. However, if you would like to use the Beaglebone or Beaglebone Black, you could use my Script to install Arch Linux. Just follow the Instractions in my post “Flashing Arch Linux to the Beaglebone, Beaglebone Black or Beaglebone Black eMMC”.

The first thing you should do is to set a new password for the root user:

passwd

Then you should create a new user for fhem

useradd -m -g users -G audio,lp,optical,storage,video,wheel,games, power, -s /bin/bash fhem

and set the password for this user:

passwd fhem

You could choose all usergroups you want, like audio, storage, etc., but you have to add the ‘wheel’ user group. Otherwise you will not be able to setup sudo for this user.

You should now be able to connect to your Raspberry Pi using the username ‘fhem and the password you entered for this user. However, this user is not able to install, delete or update anything on this system. That is why we are going to setup the sudo command:

pacman -S sudo

In order to enable sudo for the new user, you have to uncomment the following line in /etc/sudoers:

%wheel ALL=(ALL) ALL

The fhem user should now have root privileges. To test this you could check that sudo works for this regular user by logging back

su - fhem

and trying to run a system update:

sudo pacman -Syu

In some cases it can happen that pacman terminates with a SSL certificate error. This could be caused by a wrong date/time configuration. In this case you have to set the current timezone using:

timedatectl set-timezone Zone/SubZone 

The available timezones could be listed using:

timedatectl list-timezones

Then you should set the current time using

timedatectl set-time "2013-12-04 16:11:00"

because otherwise the Network Time Protocol Deamon (ntpd) would not be able to syncronize date and time with a server.

Finally you could manually syncronize your clock with the network to the correct time by running:

ntpd -qg

The date/time synchronization is also important, because fhem requires a synchronized clock to be able to execute commands at the right time.

Installing fhem

fhem is a perl server for house automation, thus you have to install some required perl packages:

sudo pacman -S perl-device-serialport perl-io-socket-ssl perl-libwww

You are now ready to install fhem. Therefore download the latest version of fhem from the project’s website to your desktop PC and copy the compressed tarball to the Raspberry Pi:

scp fhem-5.5.tar.gz fhem@192.168.1.77:~/fhem-5.5.tar.gz

Extract the file

tar xvfz fhem-5.5.tar.gz

and switch into the extracted directory. Now execute the Makefile provided with fhem to install fhem to /opt/fhem:

make install

That’s it! You successfully installed fhem.

Instead of installing fhem manually by downloading it and running make install, you could also install it from the AUR Repositories. Therefore you have to install the yaourt package manager and the binutils, which are available in the base development utils (base-devel), to install fhem directly from the repositories:

sudo pacman -S yaourt base-devel

However, there seems to be a problem with the fhem package provided in the AUR Repositories, so I installed it manually.

You should now be able to start the perl server by running the fhem.pl script:

perl /opt/fhem/fhem.pl /opt/fhem/fhem.cfg

It is not that smart to execute this command every time you reboot your Raspberry Pi. Thus, I created a service entry for systemd. Just save the following configuration file as /etc/systemd/system/fhem.service:

[Unit]
Description=FHEM Perl Server
After=syslog.target network.target

[Service]
User=fhem
Type=forking
WorkingDirectory=/opt/fhem
ExecStart=/opt/fhem/fhem.pl /opt/fhem/fhem.cfg
Restart=always

[Install]
WantedBy=multi-user.target

The fhem service could now be enabled and started:

sudo systemctl enable fhem
sudo systemctl start fhem

This will also ensure that the service automatically restarts if the Raspberry Pi is rebooted.

Finally you have to change the owner of the fhem installation folder, because otherwise our fhem user will not be able to change the fhem configuration file:

sudo chown -R fhem /opt/fhem

Basic Setup of fhem

Homematic Components

Now that you have installed fhem, you could customize it by changing some configurations within the fhem.cfg configuration file. You could change the file directly from the fhem web interface. Therefore go to [:8083](192.168.1.1:8083 "The local Fhem server") and choose *Edit files* -> *fhem.cfg* from the menu at the left side. Here you could update your configuration file and save your changes by clicking *Save fhem.cfg*.

At first you should set a password for the fhem web and mobile interface, so nobody could access the system and change something without granted permission. Therefore switch to your desktop PC and generate a password using the following command:

echo -n <username>:
<password> | base64

Replace the username before the colon (:) and the password after with your own. This will give you a base64 string like this one:

dXNlcm5hbWU6cGFzc3dvcmQ=

This string could now be used to set the password for the fhem interfaces. Just add a basicAuth attribute for the interface that should be protected by your username and password combination. The attribute definition should look like this:

attr <interface> basicAuth dXNlcm5hbWU6cGFzc3dvcmQ=

The interface part of your fhem.cfg file should now look like this:

define WEB FHEMWEB 8083 global
attr WEB basicAuth dXNlcm5hbWU6cGFzc3dvcmQ=

define WEBphone FHEMWEB 8084 global
attr WEBphone stylesheetPrefix smallscreen
attr WEBphone basicAuth dXNlcm5hbWU6cGFzc3dvcmQ=

define WEBtablet FHEMWEB 8085 global
attr WEBtablet stylesheetPrefix touchpad
attr WEBtablet basicAuth dXNlcm5hbWU6cGFzc3dvcmQ=

The changes to the configuration file take effect as soon as you restart fhem. Therefore click in the input prompt at the top of the fhem web interface and enter:

shutdown restart

After a few seconds the server is reachable again and will ask for a username and password.

Connecting the HMLan adapter

HMLan adapter setup

The HomeMatic LAN adapter handels the communication between fhem and the various HomeMatic components. To be able to use it with fhem, you have to disable the AES encryption of the LAN communication, since fhem does not yet support encrypted communication. Then you can register the adapter with fhem by defining it within the fhem configuration file.

In order to disable the AES encryption of the LAN adapter, you have to download the Configuration LAN adapter Usersoftware from the eq-3 Homepage. The provided .zip file contains two Windows executables (.exe files). Just install the one named Setup_HMCFG.exe.

Even if you disable the AES encryption between the HomeMatic LAN adapter and your Raspberry Pi, the communication between the LAN adapter and the different HomeMatic components is still encrypted.

If you are one of the clever guys running a Linux desktop, no problem. The HomeMatic software tools work great with wine.

When the installation has finished open the Homematic LAN-Interface Configurator, select your LAN interface and click Change IP Settings. At the end of the dialog you will find an entry AES Encrypt LAN Communication, which you have to uncheck to be able to use this adapter with fhem.

If your LAN adapter is not displayed in the list, it might be that the Homematic LAN-Interface Configurator has a problem with other open ethernet ports. To tackle this issue, close all your ethernet connections, except the one that connects you to the network of your LAN adapter.

Establishing a connection between the adapter and the Raspberry Pi that runs the fhem server is now quite easy. All you have to do is to define a name for the adapter along with its IP address within the fhem configuration file, as well as a unique ID for the LAN adapter:

define HMLAN1 HMLAN 192.168.1.77:1000
attr HMLAN1 hmId 123ABC

HMLAN1 is in this case the name of the LAN adapter, 192.168.1.77 the IP of the adapter, 1000 the port and 123ABC the HomeMatic ID. The here used HomeMatic ID has to be a unique six digit hexadecimal code, which you could choose by your own.

Some people recommend to use the HomeMatic ID provided by the HomeMatic tools. My experience is that this is not needed. However, if you would like to use this hmid, you have to start the Configure HomeMatic Components tool, that ships with the .zip file you already downloaded. It will ask you for the product code and the AES key, which you find at the back side of your LAN adapter. Once the entry has been confirmed, you will see a message in the lower left corner of the window, telling you that you are connected to *JEQ07…. When the connection is established, you will find a new hidden file named ids in *C:\Program Files (x86)\BidCoS Service* that contains an entry *BidCOS-Adress = * followed by your HomeMatic ID. In order to ensure that this approach works, you have to turn on AES encryption again, because otherwise the adapter will refuse the connection.

Now your LAN adapter is setup for communication with fhem. To approve your setup, just click at Everything in the left menu of fhem and search for the entry HMLAN1. If the state of the adapter is opened, everything is fine. If the state is disconnected, ensure that AES encryption is disabled and no other software is connected to the adapter.

Pairing devices

Now your LAN adapter is setup for communication with fhem and you could start pairing devices with fhem. I started with setting up a remote control switch, a three state position sensor and a thermostat. In order to pair a device with fhem, you just enter

set HMLAN1 hmPairForSec 60

in the fhem terminal at the top of the fhem page. The HomeMatic LAN adapter is now in ready-to-receive state for 60 seconds. You now have to press the hardware pairing button of the device you would like to connect to fhem.

Homematic Devices

Homematic Devices After a few seconds the device should show up in a new device list called CUL_HM that should be reachable from the navigation on the left site. This list should hold two entries, one for the device itself and one for the log file of this device. So for example as I connected the remote control switch, I got an entry CUL_HM_HM_LC_SW1_PL2_20747A for the device and an entry FileLog_CUL_HM_HM_LC_SW1_PL2_20747A for the corresponding log file. If you now open your fhem configuration file fhem.cfg you should also see two new definitions like the follwoing:

# Define the first switch ...
define CUL_HM_HM_LC_SW1_PL2_20747A CUL_HM 20747A
attr CUL_HM_HM_LC_SW1_PL2_20747A .devInfo 010100
attr CUL_HM_HM_LC_SW1_PL2_20747A .stc 10
attr CUL_HM_HM_LC_SW1_PL2_20747A expert 2_full
attr CUL_HM_HM_LC_SW1_PL2_20747A firmware 1.9
attr CUL_HM_HM_LC_SW1_PL2_20747A model HM-LC-SW1-PL2
attr CUL_HM_HM_LC_SW1_PL2_20747A peerIDs
attr CUL_HM_HM_LC_SW1_PL2_20747A room CUL_HM
attr CUL_HM_HM_LC_SW1_PL2_20747A serialNr KEQ0036609
attr CUL_HM_HM_LC_SW1_PL2_20747A subType switch
attr CUL_HM_HM_LC_SW1_PL2_20747A webCmd toggle:on:off:statusRequest

# ... and the switch log file.
define FileLog_CUL_HM_HM_LC_SW1_PL2_20747A FileLog ./log/Lamp1-%Y.log Lamp1
attr FileLog_CUL_HM_HM_LC_SW1_PL2_20747A logtype text
attr FileLog_CUL_HM_HM_LC_SW1_PL2_20747A room CUL_HM

There are now two things you might want to change - the name of the device and the room name (currently CUL_HM). To change the name of the device you just have to enter the following command in the fhem console:

rename <old-name> <new-name>

So for example as I renamed my remote control switch, I had to enter the following:

rename CUL_HM_HM_LC_SW1_PL2_20747A Lamp1

To change the name of the room open your fhem.cfg and search for the room definition for your device

attr CUL_HM_HM_LC_SW1_PL2_20747A room CUL_HM

and change the name to your prefered one.

You might wonder that the entry for the log file of your device disappeared after you defined the room for your device, but it is still there. You just cannot see it, because the entry remains in the old room CUL_HM. The reason for this is that you could specify different rooms for the logs and the devices. To change this, you must also change the name of the log files room.

attr FileLog_CUL_HM_HM_LC_SW1_PL2_20747A room <new-room-name>

You should also take a look at some other attributes of your devices that you paired with fhem.

The webCmd attribute that you find in the extract from my fhem.cfg above, controls the different buttons that are displayed in the web frontend. In the case of my remote control switch there are four buttons on, off, toggle and statusRequest. To remove one of this buttons just remove the corresponding entry. To add a button that for example turns on the switch for 5 seconds, you could add an entry on-for-timer 5:

attr Lamp1 webCmd toggle:on:off:statusRequest:on-for-timer 5

You could also rename this buttons using the attribute eventMap:

attr Lamp1 eventMap on:An off:Aus

If you use the eventMap command to rename actions in the web interface, you do not need to declare their usage using the webCmd command. If you do so this can even create conflicts and the renaming is ignored.

The next step

You now have setup a Raspberry Pi (or another single board computer) with Arch Linux, installed the fhem home automation server and paired a first HomeMatic component. So you have made the first step towards automating your home. But this is just the beginning. You can do so much using fhem:

  • Use other home automation systems
  • Control actors like a radiator thermostat on sensor events
  • Monitor the power consumption
  • Plot sensor events
  • Send mails on sensor events
  • Execute bash scripts on sensor events

You see, there is still much to discover. I hope you enjoyed this post and until next time, happy tinkering.


is a Computer Science MSc. interested in hardware hacking, embedded Linux, compilers, etc.