Wednesday, January 18, 2017

Domoticz Installation



Instructions to install Domoticz and OpenZWave

This was a fresh install on a new RPi3.

Since I needed OpenZWave, I had to rebuild Domoticz



Installing from Source

Build times reported are when using a Raspberry Pi 3, using "make -j 3"
(Older revisions of the raspberry pi could take up to 10x longer to compile)
You also might need to install GCC 4.6 and compile with this version as 4.9 takes to much memory
Creating a large swap file does not solve the problem, and build with 'make -j 3'
If you are using an old version of the Raspberry Pi, remove the "-j 4" from the make commands!

sudo apt-get install cmake make gcc g++ libssl-dev git curl libcurl4-openssl-dev libusb-dev wiringpi

Now build and install the Boost library.
mkdir boost
cd boost
wget http://sourceforge.net/projects/boost/files/boost/1.60.0/boost_1_60_0.tar.gz/download
tar xvfz download
rm download
cd boost_1_60_0/
./bootstrap.sh
./b2 stage threading=multi link=static --with-thread --with-date_time --with-system --with-atomic --with-regex
sudo ./b2 install threading=multi link=static --with-thread --with-date_time --with-system --with-atomic --with-regex
cd ../../
rm -Rf boost/

Build Support for OpenZWave

If you need support for ZWave, you need to compile open-zwave before compiling domoticz Make sure to follow the below steps, you will end up with a folder layout like:
- open-zwave-read-only
- dev-domoticz
sudo apt-get install libudev-dev
For the first time, you need to clone the code (copy) from GitHub into your system:
git clone https://github.com/OpenZWave/open-zwave open-zwave-read-only
Refresh the source and Build OpenZWave(1m23.057s for initial build (rpi2 3 minutes))
cd open-zwave-read-only
git pull
make -j 3


go back to the top folder (your Home directory):
cd ..

Domoticz Source

For the first time, you need to checkout the source from GitHub onto your system:
git clone https://github.com/domoticz/domoticz.git dev-domoticz
Build Domoticz (21m15.899s on a raspberry pi 3 , 35m0.646s on a raspberry pi 2 for initial build)
If you have updated dev-domoticz, the make step will only re-build those files that have been changed - although if a header-file common to a lot of source files has been changed, this will still take a long time
Note building on pi 3 with image september jessy with gcc 4.9, you need bigger swapspace 512 instead of 100 (see this  or else it will take ages...


cd /etc/dphys-swapfile

CONF_SWAPFILE=512

free -m

Change to the Domoticz directory, and start building
cd dev-domoticz
cmake -DCMAKE_BUILD_TYPE=Release CMakeLists.txt
make
The build failed with "make -j 3", so changed to "make" , and it worked with no errors
took about an hour on my pi3 to build

Make Domoticz startup at boot time (Also needed for Web Update)


cd /dev-domoticz
sudo cp domoticz.sh /etc/init.d
sudo chmod +x /etc/init.d/domoticz.sh
sudo update-rc.d domoticz.sh defaults
Edit the startup script and change the USERNAME, DAEMON and DAEMON_ARGS parameters to reflect your current settings
sudo nano /etc/init.d/domoticz.sh
Make sure the lines look like this:
USERNAME=USER
DAEMON=/home/$USERNAME/dev-domoticz/$NAME
DAEMON_ARGS="-daemon -www 8080"

if you are using the default user name, it should say:
USERNAME=pi
If you want to use another web interface port change:
DAEMON_ARGS="-daemon -www 8080"
Test Domoticz (Important to get the file ownerships right!)
cd ~/dev-domoticz
sudo ./domoticz
Start Domoticz
sudo service domoticz.sh start
Now connect with a browser to see if it is up and running.
You can now start/stop/restart domoticz with:
sudo /etc/init.d/domoticz.sh start to start Domoticz
sudo /etc/init.d/domoticz.sh stop to stop Domoticz
sudo /etc/init.d/domoticz.sh restart to restart Domoticz
sudo /etc/init.d/domoticz.sh status to check the status of Domoticz (running/not running)

Updating Domoticz

Binary install update

Use the Web Interface to update Domoticz (Setup->Check for Updates)
If this fails for any reason (e.g. partial download giving segmentation fault) then you should manually update by:
Stable:
sudo /etc/init.d/domoticz.sh stop
cd ~/dev-domoticz
./updaterelease
Beta:
./updaterelease
./updatebeta

Source Update

Login to your Raspberry Pi with the 'pi' user and issue:
cd domoticz
git pull
make -j 4


Install watchdog

sudo modprobe bcm2708_wdog
echo "bcm2708_wdog" | sudo tee -a /etc/modules

Install the software watchdog daemon

sudo apt-get install watchdog
sudo update-rc.d watchdog defaults

Configure the watchdog daemon

Open /etc/watchdog.conf with your favorite editor (mine is nano).
sudo nano /etc/watchdog.conf
Uncomment the line that starts with #watchdog-device by removing the hash (#) to enable the watchdog daemon to use the watchdog device. Uncomment the line that says #max-load-1 = 24 by removing the hash symbol to reboot the device if the load goes over 24 over 1 minute. A load of 25 of one minute means that you would have needed 25 Raspberry Pis to complete that task in 1 minute. You may tweak this value to your liking.
If you only want to setup the watchdog to restart in case your system hangs, continue to the section Start the watchdog daemon

Use watchdog to check Domoticz

This script relies on the fact that the Domoticz.log is written in /tmp/ newer installs have this option disabled by default. To make this script work you must enable the logfile.
sudo nano /etc/init.d/domoticz.sh
and change the line OPTIONS="-www 8080" to:
OPTIONS="-www 8080 -loglevel=1 -log /tmp/domoticz.log"
(* Alternative ) In my case, the option must be set up like this
DAEMON_ARGS="$DAEMON_ARGS -www 8080 -loglevel=1"
DAEMON_ARGS="$DAEMON_ARGS -sslwww 443"
DAEMON_ARGS="$DAEMON_ARGS -log /tmp/domoticz.log"

Setup mailer:
sudo apt-get install sendmail-bin
sudo apt-get install mutt
If mutt does not install, then try updating your pi first by running:
sudo apt-get update
Then continue to:
cd /home/pi
create the file .muttrc with the following content: (vi .muttrc or nano .muttrc)
# basic .muttrc for use with Gmail
# Change the following six lines to match your Gmail account details
set imap_user = "username@gmail.com"
set imap_pass = ""
set smtp_url = "smtp://username@smtp.gmail.com:587/"
set smtp_pass = ""
set from = "username@gmail.com"
set realname = "Firstname Lastname"
#
# # Change the following line to a different editor you prefer.
set editor = 'vim + -c "set textwidth=72" -c "set wrap"'
# Basic config
set folder = "imaps://imap.gmail.com:993"
set spoolfile = "+INBOX"
set imap_check_subscribed=yes
set hostname = gmail.com
set mail_check = 120
set timeout = 300
set imap_keepalive = 300
set postponed = "+[GMail]/Drafts"
set header_cache=~/.mutt/cache/headers
set message_cachedir=~/.mutt/cache/bodies
set certificate_file=~/.mutt/certificates
set move = no
set include
set sort = 'threads'
set sort_aux = 'reverse-last-date-received'
set auto_tag = yes
set pager_index_lines = 10
ignore "Authentication-Results:"
ignore "DomainKey-Signature:"
ignore "DKIM-Signature:"
hdr_order Date From To Cc
alternative_order text/plain text/html *
auto_view text/html
bind editor <Tab> complete-query
bind editor ^T complete
bind editor <space> noop
# # Gmail-style keyboard shortcuts
macro index,pager am "<enter-command>unset trash\n <delete-message>" "Gmail archive message" # different from Gmail, but wanted to keep "y" to show folders.
macro index,pager d "<enter-command>set trash=\"imaps://imap.googlemail.com/[GMail]/Bin\"\n <delete-message>" "Gmail delete message"
macro index,pager gi "<change-folder>=INBOX<enter>" "Go to inbox"
macro index,pager ga "<change-folder>=[Gmail]/All Mail<enter>" "Go to all mail"
macro index,pager gs "<change-folder>=[Gmail]/Starred<enter>" "Go to starred messages"
macro index,pager gd "<change-folder>=[Gmail]/Drafts<enter>" "Go to drafts"
macro index,pager gl "<change-folder>?" "Go to 'Label'" # will take you to a list of all your Labels (similar to viewing folders).
edit the first lines to setup your gmail account info
Create a mail script in /home/pi:
cd /home/pi/
sudo nano mailIP
Paste the following (repalce YOURMAIL with full email address of where you want the email sent i.e. fred@hotmail.com):
 #!/bin/sh
 mailreciever=YOURMAIL
 today=$(date)
 #my_ip=`ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{print $1}'`
 my_ip=`wget -q -O - checkip.dyndns.org|sed -e 's/.*Current IP Address: //' -e 's/<.*$//'`
 my_pi="Domoticz RaspberryPi has rebooted! "
 message="Your Pi has rebooted at $today. Current IP address = $my_ip"
 echo $message > message.txt
 mutt -s "${my_pi}" ${mailreciever} < message.txt
The commented line fetches the internal ip, example fetches external ip.
Make sure the mailIP can be executed:
sudo chmod +x mailIP
Test the script by running it:
./mailIP
Setup rc.local to send the mail at each reboot: Edit /etc/rc.local:
sudo nano /etc/rc.local
Add this above the exit 0 line:
sudo /home/pi/mailIP & 
Make sure the rc.local can be executed:
sudo chmod 0755 /etc/rc.local
sudo bash
cp /home/pi/.muttrc /root


sudo nano /etc/watchdog.conf
Uncomment the lines file and change and set them to:
file                    = /tmp/domoticz.log
change                  = 300
This setting will restart the pi when domoticz.log hasn't been updated in 5 minutes, the time is in seconds (tested).
Be sure to remember this when stopping the domoticz service by hand!
and for new system , that do nothing yet, you can prevent unwanted reboot by adding a script like this :
cd /home/pi/domoticz/scripts/lua
nano script_time_domotizAlive.lua
commandArray = {}
print('Domoticz alive !')
return commandArray
It will update the log every minute.

Start the watchdog daemon



sudo service watchdog start

















No comments:

Post a Comment