md
SD Card Failure
March 31, 2018

There was a power outage at around 2 in the morning during the fourth snow storm in March. As often happens, the Raspberry Pi hosting my home automation system did not recover when the power came back on. When I did reboot the system, I would get Browser Cache refresh failed." in the Domoticz web interface. Trying to reboot the Raspberry Pi from the Domoticz web interface failed. Trying from an ssh session would result in a Segmentation error whenever sudo was invoked.

pi@domo:~ $ sudo reboot Segmentation fault

Looking up segmetation fault revealed that there was no way around the problem: the OS had to be reinstalled from scratch. The problem is probably because the power outage cause an improper shutdown. Also when power comes back on, there is often considerable noise on the power line which would correspond to multiple incomple power on and off cycles which cannot be a good thing.

The take away from this is

  1. See about mounting the file system on a USB stick instead of an SD card.
  2. Add a hardware watchdog to restart the Pi when Domoticz is off line.
  3. Look into a small UPS for the Raspberry Pi.

Table of Contents

  1. In the Beginning

  1. In the Beginning
  2. There were problems associated with the libssl1 and libssl-dev libraries when trying to install Domoticz with the latest version of Raspbian Stretch Lite. I could not surmount them. It does seem that this known to the developers of Domoticz, see SSL protocol issue #2062. So I had to use last version of Jessie Lite which is still available form the Raspberry Pi Foundation.

    As before, I used Etcher as per the instructions at raspberrypi.org to burn the last version Rasbpian Jessie Lite image.

    Before burning the image, you should uncheck the Auto-unmount on success option in the Etcher Settings. If this is not done, it will be necessary to remove and reinsert the SD card in the desktop SD card reader to perform the next step which consists of an empty file called ssh in the card's boot partition in order to configure the RPi without monitor and keyboard. It will be necessary to initially have a working Ethernet connection to the Raspberry Pi in order to configure it.

    michel@hp:~$ touch /media/michel/boot/ssh

    It may be necessary to mount the boot partition. An easy way of doing that in Ubuntu Linux is to look at the content of the partition in the usual file browser: Files. This is a good place to unmount and eject the SD card after creating the empty ssh file.

    Using zenmap to scan my local network, I was able to start an ssh session on the Raspberry Pi.

    michel@hp:~$ ssh pi@192.168.0.134 pi@192.168.0.134's password: raspberry not echoed to the screen Linux raspberrypi 4.9.59-v7+ #1047 SMP Sun Oct 29 12:19:23 GMT 2017 armv7l ... ... SSH is enabled and the default password for the 'pi' user has not been changed. This is a security risk - please login as the 'pi' user and type 'passwd' to set a new password.

    I used raspi-config to change the configuration to suit my situation.

    pi@raspberrypi:~ $ sudo raspi-config

    1. Change User Password mandatory
    2. Hostname - changed to domo
    3. Boot Options - did nothing
    4. Localisation Options
      I2: Change Timezone - set to to America/Moncton
    5. Interfacing Options
      P2: SSH - Enabled
      P5: I2C - Enabled
    6. Overclock - did nothing
    7. Advanced Options
      A1: Expland Filesystem
      A3: Memory Split - 16 MB for the GPU

    I rebooted as asked and then updated and upgraded the system.

    pi@domo:~ $ sudo apt update && sudo apt upgrade Get:1 http://mirrordirector.raspbian.org jessie InRelease [14.9 kB] ... 74 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 54.7 MB of archives. After this operation, 484 kB of additional disk space will be used. Do you want to continue? [Y/n] y

    While not mandatory and not even recommended, I decided to use static IP addresses for both the Ethernet and the WiFi interfaces. I just get tired of doing network scans to find headless systems whenever the router has to be restarted, which happens just a bit too often in this household. The following command confirmed that "classic" network interface manes are still in use.

    pi@domo:~ $ ls /sys/class/net eth0 lo wlan0

    Complete the wpa_supplicant configuration file

    entering the wireless network SSID and password. Also change the country code if appropriate

    michel@hp:~$ sudo nano /etc/wpa_supplicant/wpa_supplicant.conf
    country=US ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev update_config=1 network={ ssid="your_real_wifi_ssid" scan_ssid=1 psk="your_real_password" key_mgmt=WPA-PSK }

    Make a copy of the dhcpcd configuration file and then edit the original.

    michel@hp:~$ sudo cp /etc/dhcpcd.conf /etc/dhcpcd.conf.bak michel@hp:~$ sudo nano /etc/dhcpcd.conf
    ... # Example static IP configuration: #interface eth0 #static ip_address=192.168.0.10/24 #static ip6_address=fd51:42f8:caae:d92e::ff/64 #static routers=192.168.0.1 #static domain_name_servers=192.168.0.1 8.8.8.8 fd51:42f8:caae:d92e::1 interface eth0 static ip_address=192.168.1.34/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1 interface wlan0 static ip_address=192.168.1.35/24 static routers=192.168.1.1 static domain_name_servers=192.168.1.1 ...

    Again I rebooted and this time logged in via the wireless network and installed Domoticz.

    michel@hp:~$ ssh pi@192.168.1.35 domo@192.168.1.35's password: my_password (not echoed) pi@domo:~ $ sudo curl -L install.domoticz.com | bash ... ::: ::: Installation Complete! Configure your browser to use the Domoticz using: ::: 192.168.1.35:8080 ::: 192.168.1.35:443 pi@domo:~ $ sudo reboot

    Then log into the Domoticz Web interface at http://192.168.1.35:8080 or 192.168.1.35:443 with a web browser. Now that it works, I will install all the other components.

  3. Mochad
  4. pi@domo:~ $ sudo apt-get install libusb-1.0-0-dev ... Do you want to continue? [Y/n] y ... i@domo:~ $ mkdir mochad pi@domo:~ $ cd mochad pi@domo:~/mochad $ wget -O mochad.tgz https://sourceforge.net/projects/mochad/files/mochad-0.1.17.tar.gz/download ... pi@domo:~/mochad $ tar xf mochad.tgz pi@domo:~/mochad $ cd mochad-* pi@domo:~/mochad/mochad-0.1.17 $ ./configure pi@domo:~/mochad/mochad-0.1.17 $ make ... pi@domo:~/mochad/mochad-0.1.17 $ sudo make install

    Plug in interface, in my case CM19A and after a minute or so check the status of the service.

    pi@domo:~/mochad/mochad-0.1.17 $ sudo systemctl status mochad ● mochad.service - Start mochad service Loaded: loaded (/lib/systemd/system/mochad.service; disabled) Active: active (running) since Thu 2018-03-22 22:10:30 ADT; 1min 17s ago Process: 2019 ExecStart=/usr/local/bin/mochad (code=exited, status=0/SUCCESS) Main PID: 2022 (mochad) CGroup: /system.slice/mochad.service └─2022 /usr/local/bin/mochad Mar 22 22:10:30 domo mochad[2019]: starting Mar 22 22:10:30 domo systemd[1]: Started Start mochad service. Mar 22 22:10:30 domo mochad[2022]: Found CM19A Mar 22 22:10:30 domo mochad[2022]: In endpoint 0x83, Out endpoint 0x04 pi@domo:~/mochad/mochad-0.1.17 $
    pi@domo:~/mochad/mochad-0.1.17 $ echo "rf d1 on" | nc localhost 1099 03/22 22:13:18 Tx RF HouseUnit: D1 Func: On pi@domo:~/mochad/mochad-0.1.17 $ echo "rf d1 off" | nc localhost 1099 03/22 22:13:34 Tx RF HouseUnit: D1 Func: Off pi@domo:~/mochad/mochad-0.1.17 $ pi@domo:~/mochad/mochad-0.1.17 $ cd .. pi@domo:~/mochad/ $ cd rm -r mochad-0.1.17

  5. Real Time Clock
  6. src ha/rpi/rtc_en.html RTC
    pi@domo:~ $ sudo apt-get install -y python-smbus i2c-tools ... Setting up python-smbus (3.1.1+svn-2) ... pi@domo:~ $ lsmod | grep i2c_ i2c_bcm2835 6433 0 i2c_dev 6642 0 pi@domo:~ $ sudo i2cdetect -y 1 0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- 68 -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- -- pi@domo:~ $
    pi@domo:~ $ sudo nano /etc/modules
    # /etc/modules: kernel modules to load at boot time. # # This file contains the names of kernel modules that should be loaded # at boot time, one per line. Lines beginning with "#" are ignored. i2c-dev rtc-ds1307
    xx
    pi@domo:~ $ sudo nano /etc/rc.local
    ... echo ds1307 0x68 > /sys/class/i2c-adapter/i2c-1/new_device hwclock -s exit 0
    pi@domo:~ $ crontab -e ...
    # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command 45 * * * * sudo ntpd -gq; sudo hwclock -w

  7. Mosquitto mqtt broker
  8. pi@domo:~ $ sudo apt-cache policy mosquitto mosquitto: Installed: (none) Candidate: 1.3.4-2+deb8u1 Version table: 1.3.4-2+deb8u1 0 500 http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages pi@domo:~ $ wget http://repo.mosquitto.org/debian/mosquitto-repo.gpg.key ... 2018-03-22 22:36:25 (21.7 MB/s) - ‘mosquitto-repo.gpg.key’ saved [3120/3120] pi@domo:~ $ sudo apt-key add mosquitto-repo.gpg.key OK pi@domo:~ $ cd /etc/apt/sources.list.d/ pi@domo:/etc/apt/sources.list.d $ sudo wget http://repo.mosquitto.org/debian/mosquitto-jessie.list ... pi@domo:/etc/apt/sources.list.d $ cd $home pi@domo:~ $ sudo apt update .. pi@domo:~ $ sudo apt-cache policy mosquitto mosquitto: Installed: (none) Candidate: 1.4.14-0mosquitto1~jessie2 Version table: 1.4.14-0mosquitto1~jessie2 0 500 http://repo.mosquitto.org/debian/ jessie/main armhf Packages 1.3.4-2+deb8u1 0 500 http://mirrordirector.raspbian.org/raspbian/ jessie/main armhf Packages pi@domo:~ $ sudo apt-get install mosquitto ... After this operation, 433 kB of additional disk space will be used. Do you want to continue? [Y/n] y

  9. Temperature Sensor
  10. pi@domo:~ $ sudo apt-get install build-essential python-dev ... After this operation, 26.3 MB of additional disk space will be used. Do you want to continue? [Y/n] y ... pi@domo:~ $ git clone https://github.com/adafruit/Adafruit_Python_DHT.git ... done. pi@domo:~ $ cd Adafr* pi@domo:~/Adafruit_Python_DHT $ sudo python setup.py install ... Finished processing dependencies for Adafruit-DHT==1.3.2

  11. HTTP Server
  12. pi@domo:~ $ sudo apt-get install lighttpd ... After this operation, 943 kB of additional disk space will be used. Do you want to continue? [Y/n] y
    pi@domo:~ $ cd /var/www/html pi@domo:/var/www/html $ sudo mv index.lighttpd.html index.lighttpd.html.bak pi@domo:/var/www/html $ sudo nano index.html pi@domo:/var/www/html $ sudo cp index.html index.lighttpd.html pi@domo:/var/www/html $ sudo adduser pi www-data pi@domo:/var/www/html $ sudo chown -R pi:pi /var/www

  13. Log Server
  14. ha/rpi/syslog_en.html looks ok

  15. Avahi
  16. avahi mqtt https://dagrende.blogspot.ca/2017/02/find-mqtt-broker-without-hard-coded-ip.html
    pi@domo:~ $ sudo nan /etc/avahi/services/mosquitto.service
    <!DOCTYPE service-group SYSTEM "avahi-service.dtd"> <service-group> <name replace-wildcards="yes">Mosquitto MQTT server on %h</name> <service> <type>_mqtt._tcp</type> <port>1883</port> <txt-record>info=Publish and subscribe. mqtt.org</txt-record> </service> </service-group>
    While at it
    pi@domo:~ $ sudo nano /etc/avahi/hosts
    # avahi is distributed in the hope that it will be useful, but WITHOUT # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public # License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with avahi; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 # USA. # This file contains static ip address <-> host name mappings. These # can be useful to publish services on behalf of a non-avahi enabled # device. Please bear in mind that host names are expected to be # fully qualified domain names, i.e. ending in .local! # See avahi.hosts(5) for more information on this configuration file! 192.168.0.1 router.local
    # Edit this file to introduce tasks to be run by cron. # # Each task to run has to be defined through a single line # indicating with different fields when the task will be run # and what command to run for the task # # To define the time you can provide concrete values for # minute (m), hour (h), day of month (dom), month (mon), # and day of week (dow) or use '*' in these fields (for 'any').# # Notice that tasks will be started based on the cron's system # daemon's notion of time and timezones. # # Output of the crontab jobs (including errors) is sent through # email to the user the crontab file belongs to (unless redirected). # # For example, you can run a backup of all your user accounts # at 5 a.m every week with: # 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/ # # For more information see the manual pages of crontab(5) and cron(8) # # m h dom mon dow command # # Start the webcam on reboot @reboot /home/pi/.local/bin/webcam-streamer start && sleep 10 && /home/pi/.local/bin/webcam-streamer restart # # Once an hour on the 25th minute start the webcam which does nothing # if the webcam is already started but it does update Domoticz 25 * * * * /home/pi/.local/bin/webcam-streamer start # # Once an hour on the 45th minute updates the hardware clock from ntp time 45 * * * * sudo ntpd -gq; sudo hwclock -w # # Every five minutes read the temperature sensors and update Domoticz */5 * * * * /home/pi/pythons/temps.py

  17. USB camera