Dienstag, 19. November 2013

Installing OpenHAB on a Beaglebone Black

Just a few notes on how i installed the OpenHAB home automation software onto the beaglebone black single board computer.

For beaglebone black development, i found the homepage of Derek Molloy (http://derekmolloy.ie/) to be extremely helpful, most of the below setup-steps are found there. 

1. Installing the Java JRE

The Oracle JRE is reported to perform much better then the OpenJVM implementation,
so i chose to use that as java runtime. It can be downloaded here:

http://www.oracle.com/technetwork/java/embedded/downloads/javase/java-embedded-java-se-download-359230.html?ssSourceSiteId=ocomen

As i am using the original Ångström distribution, i took ejre-7u45-fcs-b15-linux-arm-vfp-sflt-client_headless-26_sep_2013.tgz

I copied this to /usr/java and extracted the file:
# mkdir /usr/java
# cd /usr/java
# tar xfvz ejre-7u45-fcs-b15-linux-arm-vfp-sflt-client_headless-26_sep_2013.tgz

The bin-folder can be added to the search-path by adding it to the PATH environment variable and i also exported the JAVA_PATH variable:
# export PATH=$PATH:/usr/java/jdk1.7.0_45/bin/
# export JAVA_HOME=/usr/java/jdk1.7.0_45/
These two lines can be added to ~/.profile (the users shell profile file) to have it set automatically upon next login.

2. Installing OpenHAB

The next step was to install OpenHAB. I copied the OpenHAB folder into /home/root/OpenHAB. Currently the OpenHAB process also runs as user root - i will change this in future, but as the system is not exposed to the public internet, it don't think its too critical.

I adapted the OpenHAB start-skript as follows:
#!/bin/sh
# turn off that annoying blue flashing LEDs on the beaglebone black
echo "taking over LEDs"
echo gpio > /sys/class/leds/beaglebone\:green\:usr0/trigger
echo gpio > /sys/class/leds/beaglebone\:green\:usr1/trigger
echo gpio > /sys/class/leds/beaglebone\:green\:usr2/trigger
echo gpio > /sys/class/leds/beaglebone\:green\:usr3/trigger
cd `dirname $0`
# set path to eclipse folder. If local folder, use '.'; otherwise, use /path/to/eclipse/
eclipsehome="server";
# set ports for HTTP(S) server
HTTP_PORT=8080
HTTPS_PORT=8443
# get path to equinox jar inside $eclipsehome folder
cp=$(find $eclipsehome -name "org.eclipse.equinox.launcher_*.jar" | sort | tail -1);
echo Launching the openHAB runtime...
 /usr/java/jdk1.7.0_45/bin/java -Dosgi.clean=true -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=$HTTP_PORT -Djetty.port.ssl=$HTTPS_PORT -Djetty.home=.
 -Dlogback.configurationFile=configurations/logback.xml -Dfelix.fileinstall.dir=addons -Djava.library.path=lib -Djava.security.auth.login.config=./etc/login.conf -Do
rg.quartz.properties=./etc/quartz.properties -Dequinox.ds.block_timeout=240000 -Dequinox.scr.waitTimeOnBlock=60000 -Djava.awt.headless=true -jar $cp $* -console 5555
The first 4 echo commands are used for taking over control of the four blue User-LEDs on the beaglebone. By default these LEDs are controlled by the kernel and signal current CPU utilization and some other info. The flashing LEDs started to annoy me, so i turned them off.

OpenHAB will now run (when configured properly) when the start.sh skript is called. As i wanted the system to start automatically in a proper way after a reboot, some additional work had to be done:

3. NTP Support

Next thing was installing the NTP support for the beaglebone. As the systems RTC is not battery buffered,  the date and time information will be lost upon each power cycle. I worked around this by syncing the time via NTP upon each startup:

First i installed the NTP package:
# opkg update
# opkg install ntp
Then i edited the ntp config file, basically to use the local (germany based) NTP servers from the ntp.org timeserver pool. I also added a security rule that limits access to my local network.

/etc/ntp.conf:
# This is the most basic ntp configuration file
# The driftfile must remain in a place specific to this
# machine - it records the machine specific clock error
driftfile /etc/ntp.drift

server 0.de.pool.ntp.org
server 1.de.pool.ntp.org
server 2.de.pool.ntp.org
server 3.de.pool.ntp.org

# Defining a default security setting
restrict 192.178.2.0 mask 255.255.255.0 nomodify notrap
After that, the local timezone needed to be adapted by letting the symlink /etc/localtime point to the right zone-file:
# rm /etc/localtime
# ln -s /usr/share/zoneinfo/Europe/Paris /etc/localtime
After this, it is necessary to change the config file of the ntpdate service as follows:

/lib/systemd/system/ntpdate.service:
[Unit]
Description=Network Time Service (one-shot ntpdate mode)
Before=ntpd.service
[Service]
Type=oneshot
ExecStart=/usr/bin/ntpd -q -g -x
ExecStart=/sbin/hwclock --systohc
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
The setting ExecStart needs to be changed as shown above. The first ExecStart line is changed, the second is newly added. The second one syncs the hardware clock to the system time after the NTP sync was performed.

Next is to enable the NTP daemon, so that it is run upon system start:
# systemctl enable ntpdate.service 
# systemctl enable ntpd.service
Upon next reboot, the date and time should be correct.

4. Autostart OpenHAB

Next thing i installed was a configuration file for the systemctl service, so that OpenHAB gets started upon every reboot.


/lib/systemd/system/openhab.service:
[Unit]
Description=OpenHAB
ConditionPathExists=|/usr/bin
After=network.target

[Service]
ExecStart=/home/root/OpenHAB/start.sh
Restart=always
RestartSec=10
StartLimitInterval=0

[Install]
WantedBy=multi-user.target
Then this service is activated by
# systemctl enable openhab.service
and started by
# systemctl start openhab.service
The current state can be queried this way:
# systemctl status openhab.service
That should give an output like:
openhab.service - OpenHAB
  Loaded: loaded (/lib/systemd/system/openhab.service; enabled)
  Active: active (running) since Sat 2000-01-01 01:00:02 CET; 13 years 10 months ago
Main PID: 127 (start.sh)
  CGroup: name=systemd:/system/openhab.service
  |-127 /bin/sh /home/root/OpenHAB/start.sh
  `-247 /usr/java/jdk1.7.0_45/bin/java -Dosgi.clean=true -Declipse.ignoreApp=true -Dosgi.noShutdown=true -Djetty.port=8080 -Djetty.port.ssl=8443 -...
5. Setting up a Samba share for easy configuration

Install the SMB server via opkg:

         # opkg install samba
         # smbpasswd

Edit the Samba config file:

        # vi /etc/samba/smb.conf

I stepped through the config and configured samba to the local needs, most defaults already fitted. I installed a new share:

[openhab]
   comment = OpenHAB
   path = /home/root/OpenHAB
   public = yes
   writable = yes
   printable = no

   write list = root

Samba can be started using systemctl:


    # systemctl restart openhab.service


6. Setting up a static (non DHCP) IP configuration

I prefer to have static IPs for servers in my network. Here i found a description on how to set up a static config:

http://derekmolloy.ie/set-ip-address-to-be-static-on-the-beaglebone-black/