Tim's blah blah blah

Installing domoticz on raspberry pi (1/3)

(Updated: )

Although there are many guides on this topic available, I’m recording my personal setup here for my reference and with the hope that some issues I ran into will help others.

Goal

As goal for my Rpi/Domoticz project I had the following:

Hardware

Initially I ran on a RPi0, which is fine for domoticz but lacked the power to run grafana and influxdb which I added later, for which I upgraded to an RPi3B plus. Remember: premature optimisation is the root of all evil (wikiquote.org).

RPi Zero vs 3B+ pro/con

Pros:

Rpi0

RPi3B

Setup Raspbian

  1. Get Raspbian image (raspberrypi.org) -- use light image where possible, only use full image if you need desktop to debug etc.
    1. Optionally: enable SSH and wifi to run Rpi headless out of the box. See here (stackexchange.com), here (raspberrypi.org) or here (styxit.com).
  2. Write image (raspberrypi.org) to SD
    1. Enable Wifi: connect, login and enable
    2. Enable SSH from desktop: instructions (raspberrypi.org)
  3. Optional: customize hostname
    1. sudo hostname <newname>
    2. sudo vi /etc/hostname
    3. sudo vi /etc/hosts
  4. Secure Pi: instructions (raspberrypi.org)
  5. Optional: disable cron logging (serverfault.com) and e-mails (stackexchange.com)
  6. Optional: remove large packages
    1. Find large packages (stackexchange.com) dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n
    2. Find reverse dependencies (askubuntu.com) to check what you can safely remove: apt-cache rdepends jetty or dry-run purging it (stackexchange.com): aptitude -s purge libplrpc-perl.
    3. Remove graphical user interface: sudo aptitude remove x11-common. N.B. aptitude will try to solutions that satisfy all dependencies, the first solution for me was to keep the package, the second solution (enter . or n to see) proposed to delete many/all packages that depend on x11-common and was the one I wanted.
  7. Set timezone to Europe/Amsterdam: sudo timedatectl set-timezone Europe/Amsterdam

Install domoticz

  1. Optional: create dedicated, non-root domoticz user: useradd domoticz
  2. Install domoticz (domoticz.com): curl -L install.domoticz.com | sudo bash
  3. Chown files correctly: chown domoticz:domoticz * (see this pull request (github.com))
  4. Optional: enable non-root user to run domoticz:
    1. Allow USB access as normal user (required to read out P1 smart meters with USB cable) (domoticz.com): sudo usermod -a -G dialout YOURUSERNAME
    2. Change PID location (superuser.com) to /tmp and and update startup script: see this commit (github.com)
  5. Fix libssl error for https access (./domoticz: error while loading shared libraries: libssl.so.1.0.0: cannot open shared object file: No such file or directory)
    1. Switch to beta: cd domoticz; ./updatebeta (source (domoticz.com))
    2. Alternatively, download obsolete SSL lib (diyprojects.io)
  6. Add weather forecast (domoticz.com), using station PWS:IUTRECHT432. N.B. IDEBILT9 (has data gaps, temperature was wrong, 16 vs 10 deg).
  7. Enable camera (for machine vision reading meter data later on)
    1. Use pre-applied adhesive tape to glue camera to PCB
    2. Shutdown Pi
    3. Use flat cable supplied with RPi (Zero) to connect to RPi
    4. Boot Pi
    5. Enable camera in raspi-config
    6. Reboot Pi
    7. Enable X11 forwarding (iu.edu)
    8. Use PiCameraApp (github.com) to view live camera: sudo apt install python python-tk python-pil.imagetk (N.B. this never worked for me)
  8. Reduce filesystem usage to prevent SD card wear
    1. SD cards have a limited lifetime. While data on SD cards is hard to find, SSDs with much more robust memory can support 200-6000x write cycles for consumer laptops (e.g. Samsung SSDs (samsung.com) or these other consumer SSDs (techreport.com) or Intel’s server SSD (intel.com)). Likely, an SD card has worse performance than an SSD, such that 200x write cycles is probably optimistic. This means for a 64GB card which you want to use 30 years, you can write «1 GB per day (200*64/365/30). Even if an SD card is 100x worse than an SSD, you still have 10MB per day, which is reasonable, so these tricks might (stackexchange.com) not (stackexchange.com) be required (stackexchange.com), although it still helps to prevent your SD card filling up. Again, premature optimization is the root of all evil (wikipedia.org), so be careful
    2. Disable domoticz automatic backup of database (66 MB/day = 24.000MB/yr)
    3. Disable swap (load unknown): sudo apt-get remove dphys-swapfile or sudo systemctl disable dphys-swapfile (source (github.com))
    4. Use noatime (default in raspbian)
    5. Reduce rsyslog syncing needs (default in raspbian)
  9. Improve security
    1. Domoticz is a monolithic program that works great if you run it as root. It has a built-in custom webserver, which is very convenient, but security-wise risky. I recommend against opening domoticz to the outside world
    2. Add real SSL certs (domoticz.com) from letsencrypt (eff.org) (not tested)
    3. Add mainstream webserver (apache, nginx): guide here (pestmeester.nl). This did not work: reverse proxy has extremely slow performance (on RPi0). Since domoticz is a lot of json requests, this puts a (too) heavy load on the reverse proxy.
  10. Use external bluetooth adapter (e.g. for improved range)
    1. Check new device is available and works: hcitool dev && hcitool -i hci0 scan
    2. Disable built-in adapter so SBFspot uses external one: rfkill list | grep DE:AD:BE:EF:AA:AA; sudo rfkill block X
  11. Disable WiFi (stackexchange.com) to prefer ethernet connection: Add dtoverlay=pi3-disable-wifi to /boot/config.txt

Manual fixes in database

In case some measurement is wrong, here is a (rather laborious) method to fix that:

  1. Determine meter & correct readings (from domoticz)
  2. Stop domoticz: sudo service domoticz stop
  3. Backup domoticz.db: cp domoticz.db domoticz.db.backup
  4. Get SQLite editor (e.g. DB Browser for SQLite)
  5. Copy domoticz.db to machine with SQLite editor: scp pi@raspberrypi.home:domoticz/domoticz.db .
  6. Update meter
    1. Fix DeviceStatus table - updates sValue
    2. Fix Meter table - update / delete wrong data
    3. Fix Meter_Calender - update / delete wrong data
    4. Alternatively, data is in “MultiMeter” or other tables
  7. Copy domoticz.db back to domoticz machine and restart domoticz: scp domoticz.db pi@raspberrypi.home:domoticz/ and sudo service domoticz start

#Linux #Smarthome