Installing domoticz on raspberry pi (1/3)

By Atomstar on Sunday 2 December 2018 16:46 - Comments (0)
Categories: Linux, Smarthome, Views: 2.176

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:
  • Record (smart) meter data and graph it
  • Cheap/low-power solution (i.e. no Intel NUC)
  • Versatile wrt interfaces (e.g. bluetooth/wifi/GPIO)

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.
RPi Zero vs 3B+ pro/con
Pros:
  • + save 30e (cost + electricity);
  • + save power;
  • + can still play some emulator games after overclocking (All the Game Boys, GG, Master System, Genesis, NES, SNES and Turbografx at 60fps.)
  • + ...still faster than Pi1;
Cons:
  • - not sufficient to run desktop (source)
  • - No ethernet jack;
  • - no 3.5mm jack out
Rpi0
  • rpi zero W (11e)
  • 16 gb raspbian (10e)
  • usb charger 2A (9e)
  • housing (7e)
  • P1 kabel (20e)
RPi3B
  • Rip 3B+ (36e)
  • 64 GB (14e)
  • Housing (7e)
  • usb charger 2A (9e)
  • P1 kabel (20e)

Setup Raspbian

  1. Get Raspbian image -- 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, here or here.
  2. Write image to SD
    1. Enable Wifi: connect, login and enable
    2. Enable SSH from desktop: instructions
  3. Optional: customize hostname
    1. sudo hostname <newname>
    2. sudo vi /etc/hostname
    3. sudo vi /etc/hosts
  4. Secure Pi: instructions
  5. Optional: remove large packages
    1. Find large packages dpkg-query -Wf '${Installed-Size}\t${Package}\n' | sort -n
    2. Find reverse dependencies to check what you can safely remove: [mono]apt-cache rdepends jetty[/mon] or dry-run purging it: 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.
  6. 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: curl -L install.domoticz.com | sudo bash
  3. Chown files correctly: chown domoticz:domoticz * (see this pull request)
  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): sudo usermod -a -G dialout YOURUSERNAME
    2. Change PID location to /tmp and and update startup script: see this commit
  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)
    2. Alternatively, download obsolete SSL lib
  6. Add weather forecast, 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
    8. Use PiCameraApp 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 or these other consumer SSDs or Intel's server SSD). 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). This is reasonable, so these tricks might not be required, although it still helps to prevent your SD card filling up. Again, premature optimization is the root of all evil, 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)
    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 from letsencrypt (not tested)
    3. Add mainstream webserver (apache, nginx): guide here. 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: [mono]hcitool dev && hcitool -i hci0 scan[mono]
    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 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

Volgende: Smarthome data logging with RPi/domoticz/influxdb/grafana 02-12 Smarthome data logging with RPi/domoticz/influxdb/grafana

Comments

There are no comments for this post


Comment form
(required)
(required, but will not be displayed)
(optional)