Making the Puzzle Pieces Fit

Home Automation

Advertisements

In an effort to simplify my Home Assistant install in the future. Here is the list of steps that I go through to install it.

Prerequisites:

Install Raspbian on a USB Flash Drive—

I have had troubles with using a micro SD card to run Home Assistant on. After about 3 weeks it would just look up and I couldn’t even SSH into the pi to see what happened. I tried rebooting and nothing seemed to fix it unless I re-formatted the SD card and started all over. and then a few weeks later I would have the same issue again. I have read that this could be an issue with the all the writing to the SD card and so I am going to try running HA on a Flash Drive instead.

To do that I follow this blog post by LazyManJoeto set up my Flash Drive and SD card. Here is the TLDR of that post:

  1. Format the Flash Drive with a FAT32 file system.
  2. Using Etcher, write the Raspbian .img to the Flash Drive.
  3. Open up the flash drive’s root directory and add a text file that is named “ssh” (this is required to enable ssh by default.
  4. Format the SD Card and name the partition “boot”.
  5. Copy all of the contents from the Flash Drvie’s “boot” partition to the SD card.
  6. Edit the “cmdline.txt” file on the SD card by deleting the text that reads root=/dev/mmcblk0p2 and replace it with root=/dev/sda2.
  7. Save the file. This tells the Pi to boot from the USB drive instead of the SD card.
  8. Put both the SD card and the Flash Drive into the pi and apply power.
  9. To expand the partition of the Flash Drive, preform the following steps:
    • SSH into the pi with user “pi” and the default password “raspberry”
    • sudo fdisk /dev/sda
    • press p to see the partitions.
    • note the start position for the sda2 partion. it will be something like 137216.
    • press d then 2 then ENTER (this will delete the 2nd partition).
    • press n then p then 2 then ENTER (this will start creating the new partition).
    • provide the start position that as noted before.
    • leave the end position blank to use the entire drive.
    • press w to commit the changes.
    • you may get an error message about the partition table is busy, but that is ok.
    • sudo reboot
    • once rebooted, sudo resize2fs /dev/sda2 (*This didn’t fork bot me, but df -h showed  it was ok).
    • sudo reboot
    • confirm the changes by checking the allocated space by df -h

Install Home Assistant —

To install home assistant I like to follow along with Ben’s BRUH Automation post. On that post he walks through setting put the pi, installing HA, Samba, Duck DNS and Let’s Encrypt. You can follow along his post for more detail. I just like the using his post for setting up the pi, installing HA and Samba. I currently don’t have my pi’s port forwarded to the internet and I just use my HA only when I am on my home wifi network.

  1. passwd to change the pasword for the user pi. You will need to enter the current password “raspberry” and then your new password twice.
  2. sudo apt-get update
  3. sudo apt-get upgrade (press Y when prompted)
  4. sudo raspi-config (no need to expand the file system, so just change the time zone.)
  5. sudo reboot
  6. I use the pi 3’s wifi for an access point for my devices and have it connected to my home network via an Ethernet cable. So I skip the section where Ben describes setting up the wlan0.
  7. Now its time to install HA. Copy the All-In-One (AIO) command found on HA webpage.
  8. Run the AIO command as described on the webpage. *
    • If it fails within the firest few minuets, read a few lines above where it fails and it may tell you a command that needs to run to fix something to allow the install to complete. after running that command, re-run the AIO command.
  9. Enter the password for the use pi when prompted. (about 15- 20 minuets into the install). The install will take a few hours.
  10. sudo reboot

Install SAMBA —

SAMBA is how I edit my HA configuration.yaml file from my Windows PC over the network.

  1. sudo apt-get install SAMBA
  2. sudo nano /etc/samba/smb.conf
  3. press and hold control-k to clear initial config, paste the following configuration
  4. [global]
    netbios name = raspberrypi
    server string = The Pi File Center
    workgroup = WORKGROUP
    hosts allow =
    remote announce =
    remote browse sync =
    
    [HOME ASSISTANT]
    path = /home/homeassistant/.homeassistant
    comment = No comment
    browsable = yes
    read only = no
    valid users =
    writable = yes
    guest ok = yes
    public = yes
    create mask = 0777
    directory mask = 0777
    force user = root
    force create mode = 0777
    force directory mode = 0777
    hosts allow =
  5. sudo smbpasswd -a pi
  6. type a new password went prompted
  7. sudo service smbd restart

Install HostAPD —

HostAPD is the software that sets up an access point using the pi 3’s on-board wifi adapter. I use this to setup a new wireless network for all my home automation sensors to connect to. I like to keeps these separate from my usual home wifi network to keep down on the number of ip addresses and network traffic on my primary wifi.

To setup HostAPD, I like to follow along with a post on the lean.adafruit.com website. I have tried to consolidate these steps here.

  1. sudo apt-get install hostapd isc-dhcp-server
  2. sudo apt-get install iptables-persistent
  3. On the two config screens, select yes to both of them.
  4. sudo nano /etc/dhcp/dhcpd.conf
  5. Find the two lines that say
    • option domain-name “example.org”;
      option domain-name-servers ns1.example.org, ns2.example.org;

  6. Comment them both out by adding a # in front of them.
    • #option domain-name “example.org”;
      #option domain-name-servers ns1.example.org, ns2.example.org;

  7. Now find the lines that read
    • # If this DHCP server is the official DHCP server for the local
      # network, the authoritative directive should be uncommented.
      #authoritative;

  8. And un-comment the last line by deleting the #, so it reads
      • # If this DHCP server is the official DHCP server for the local
      • # network, the authoritative directive should be uncommented.
      • authoritative;
  9. Then scroll down to he bottom and add the following lines.
    • subnet 192.168.42.0 netmask 255.255.255.0 {
      range 192.168.42.10 192.168.42.50;
      option broadcast-address 192.168.42.255;
      option routers 192.168.42.1;
      default-lease-time 600;
      max-lease-time 7200;
      option domain-name “local”;
      option domain-name-servers 8.8.8.8, 8.8.4.4;
      }

  10. Save the file by pressing Control-x then y then Enter
  11. sudo nano /etc/default/isc-dhcp-server
  12. Find the test that reads, INTERFACES=”” and change it to INTERFACES=”wlan0″
  13. Save the file by pressing Control-x then y then Enter
  14. sudo ifdown wlan0
  15. sudo nano /etc/network/interfaces
  16. Keep the line that says allow-hotplug wlan0, but comment out any other lines dealing with the wlan0 (we don’t want the lines that start with iface or have the wpa-supplicant.conf file)
  17. Add the lines
    • iface wlan0 inet static
      address 192.168.42.1
      netmask 255.255.255.0

  18. Save the file by pressing Control-x then y then Enter
  19. sudo ifconfig wlan0 192.168.42.1
  20. sudo nano /etc/hostapd/hostapd.conf
  21. Add the following lines. Be sure to change the ssid= and wpa_passphrase= lines to correspond with what you want you network to have.
    • interface=wlan0
      #driver=rtl871xdrv   #driver=nl80211
      ssid=Pi_AP
      country_code=US
      hw_mode=g
      channel=6
      macaddr_acl=0
      auth_algs=1
      ignore_broadcast_ssid=0
      wpa=2
      wpa_passphrase=Raspberry
      wpa_key_mgmt=WPA-PSK
      wpa_pairwise=CCMP
      wpa_group_rekey=86400
      ieee80211n=1
      wme_enabled=1

    • The code above is for using the Raspberry pi 3’s on-board wifi. If using a USB wifi adapter then try using one of the drivers that is listed in the second line by un-commenting the line.
  22. Save the file by pressing Control-x then y then Enter
  23. sudo nano /etc/default/hostapd
  24. Find the line #DAEMON_CONF=”” and change it to DAEMON_CONF=”/etc/hostapd/hostapd.conf”, (don’t forget to remove the # in front)
  25. Save the file by pressing Control-x then y then Enter
  26. sudo nano /etc/init.d/hostapd
  27. Find the line DAEMON_CONF= and change it to DAEMON_CONF=/etc/hostapd/hostapd.conf
  28. Save the file by pressing Control-x then y then Enter
  29. sudo nano /etc/sysctl.conf
  30. Scroll to the bottom and add net.ipv4.ip_forward=1 on a new line.
  31. Save the file by pressing Control-x then y then Enter
  32. sudo sh -c “echo 1 > /proc/sys/net/ipv4/ip_forward
  33. sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
  34. sudo iptables -A FORWARD -i eth0 -o wlan0 -m state –state RELATED,ESTABLISHED -j ACCEPT
  35. sudo iptables -A FORWARD -i wlan0 -o eth0 -j ACCEPT
  36. sudo sh -c “iptables-save > /etc/iptables/rules.v4”
  37. Time to test it….
  38. sudo /usr/sbin/hostapd /etc/hostapd/hostapd.conf to start it manually
  39. If you see any errors or warnings, please check the Adafruit tutorial for help.
  40. Try to connect to the newly created wifi network using the wireless device of your choice.
  41. Once everything is working, run sudo service hostapd start
  42. sudo service isc-dhcp-server start
  43. sudo update-rc.d hostapd enable
  44. sudo update-rc.d isc-dhcp-server enable

Setup Mosquitto MQTT —

When the All-In-One command is used to install Home Assistant, it also installs Mosquitto to be used as a MQTT broker. It has a base configuration that is good, but you need to setup a username and password for your IoT devices to use to connect. To do this I like to follow along with Ben’s BRUH Automation video in which he describes the process of setting it up.

  1. sudo nano /etc/mosquitto/mosquitto.conf
  2. Make sure these lines can be found somewhere in the mosquitto.conf file and that they are not commented out.
    •  allow_anonymous false
      password_file /etc/mosquitto/pwfile
      listener 1883

  3. Save the file by pressing Control-x then y then Enter
  4. sudo mosquitto_passwd -c /etc/mosquitto/pwfile <the user name you want> (i.e. sudo mosquitto_passwd -c /etc/mosquitto/pwfile username)
  5. Enter in a new password for that user when prompted.
  6. sudo reboot

Creating a backup —

Now that you have spent an entire day getting this set up and it seems to be working great. you should create a new image of the Flash Drive so if anything happens you can just re-write this new image to the Flash Drive and ahead of all these step. To do this I use Win32 Disk Imager.

  1. sudo shutdown -h now
  2. Remove the Flash Drive and insert it into your computer.
  3. Open Win32 Disk Imager and select the flash drive.
  4. Browse to a location on your hard drive and enter in a new file name to save the image as with a .img file extension.
  5. Click the read button and wait for it to complete.

Now that you have a backup .img, if anything happens to your system, you can recreate it in a matter of minuets instead of hours.

Advertisements

Advertisements