Preparing the Raspberry Pi Zero W

Preparing the Raspberry Pi Zero W for ETHA Light Switch is not an easy task. That’s why I made a disk image with all preparations already in place. See here.

It all starts with a Raspberry Pi Zero W. Another Raspberry Pi model will do just fine, but the Zero W is cheap and small, and has onboard Wi-Fi. You can also use a Raspberry Pi you’re already using as a Kodi media player. ETHA Light Switch isn’t using many resources. But here I am describing a standalone Raspberry Pi Zero W.

You need some extras: an sd-card of at least 4 GB, a power supply (5 V, 1.2 A) and extension cables or adapters for temporarily connecting a monitor and a keyboard.

The first thing you’ll have to do is install an sd-card with the operating system. I choose to install Raspbian Stretch Lite. It has no desktop environment, but we’re not going to use that anyway.

Download and write the image to an sd-card. You can find a description of how to do that here. Don’t use the NOOBS method, just download and use Etcher to write the image to your sd-card. You need an sd-card of at least 4 GB. Once writing is completed, you can insert the sd-card into the Raspberry Pi and power on.

Now comes the tricky part: connecting to your network. There are multiple ways of doing that (you can, for instance, edit files on the sd-card in your desktop computer before booting up for the first time), but the easiest method is to temporarily attach a monitor and keyboard. You’ll need special cables, though. The Raspberry Pi Zero W is equipped with a mini-HDMI connector for the monitor and a micro-USB connector for the keyboard, so you’ll need extension cables or adapters.

At this point I’m presuming that this isn’t the first time you’re using Linux. You should have a basic understanding of networking concepts and how to use a terminal to interact with Linux.

Log in as user pi with password raspberry. Then change that password into something else: passwd. Following that we will activate the password for root. Everybody is recommending not to do anything as root, but hey, we’re grown-ups. Just don’t type rm / -Rf and you’re good to do everything as the root user. So the last command we’re issuing as the user pi is: sudo passwd. Then log out and log in as root.

Now continue with raspi-config. Important options here: 2 Network Options - N2 Wi-Fi, 4 Localization Options (all suboptions; make sure you choose your local timezone) and 5 Interfacing OptionsP2 SSH (enable SSH server).

I am using SSH with keys, but it can also be used with password authentication only. In both cases make sure that you have PermitRootLogin yes in /etc/ssh/sshd_config. (We’re using user root to log in, remember?) You may have to change PermitRootLogin without-password to PermitRootLogin yes. After that, restart the SSH server with systemctl restart ssh.
Your IP address can be found with hostname -I, and you should be able to log in with ssh.

Now is probably a good time to update your system:

apt-get update
apt-get dist-upgrade

If the kernel is updated then also reboot.

Now we’ve setup the network, we’re going to tear it down. The reason for that is that ETHA Light Switch needs NetworkManager. The state led can only detect network connection when NetworkManager is active. So:

apt-get install network-manager

Then stop the current network connection:

systemctl stop dhcpcd
systemctl disable dhcpcd
systemctl restart NetworkManager

Check if NetworkManager is running properly:

nmcli device wifi list

This should give you a list of wireless access points, including your own. Then create a connection to it:

nmcli device wifi connect "Your SSID" password "Your password"

Replace Your SSID and Your password with your SSID and your WPA pre-shared key or WEP key. Now you should have a working network connection. So you can remove the monitor and the keyboard, since you can now connect with ssh.

As a finishing touch we will need some extra packages:

apt-get install python3-rpi.gpio python3-pip python3-dateutil python3-ephem python3-ntplib python3-posix-ipc

pip3 install paho-mqtt python-networkmanager

Download the ETHA Light Switch disk image for the Raspberry Pi Zero W

Doing all the things mentioned above will take some time and testing. Especially the network stuff can give you a headache. So to relieve your suffering I’ve prepared an sd-card image that contains most of the above, and then some.

What I did was the following:

  • I downloaded the official Raspbian Stretch Lite sd-card image (the version of November 2018 (2018-11-13) with kernel version 4.14) on 3 March 2019.
  • I then updated all packages with apt-get update and apt-get dist-upgrade.
  • Then I installed and configured all of the beforementioned software (including NetworkManager).
  • I removed the user pi.
  • I preconfigured SSH for user root (the password is etha-light-switch).
  • I installed Mosquitto (see here) and configured it to use user etha-light-switch (with password etha-light-switch) for the ETHA Light Switch Python program, and user alice (with password etha-light-switch) for the Android app.
  • I also installed and configured the ETHA Light Switch Python program (see here).
  • I reactivated all initial actions (like expanding the rootfs partition and file system).
  • I removed all log files and other files that don’t have to be present to boot.

You can download that sd-card image here: 2019-03-03-etha-light-switch.zip (you can check the download with sha256sum or md5sum). Run Etcher to write the disk image to sd-card (you don’t have to unpack the ZIP file because Etcher will do that for you).

There’s still one thing to do that I can’t do for you: you’ll have to manually change the credentials of your Wi-Fi access point. To do that, edit the file NetworkManager/wlan0 on the boot partition (the FAT32 partition). You’ll have to modify at least ssid and psk to accomodate your own wireless access point. If you are going to use Mosquitto on this device you may also consider using a fixed IP address (see here). Also read the comments in the file.

This file (/boot/NetworkManager/wlan0) will be copied to the directory /etc/NetworkManager/system-connections/ every time the Raspberry Pi Zero W boots and every time NetworkManager is started, thereby overwriting the file that is already there. If you don’t want that anymore (and your network connection is properly setup), just delete /boot/NetworkManager/wlan0.

So now you can insert the sd-card into the Raspberry Pi Zero W and boot up. You don’t need a monitor and a keyboard because when the Raspberry Pi Zero W is finished booting, you can connect to it (if your network setup is in order) with your SSH client (user root and password etha-light-switch). If you don’t have a fixed IP address you’ll have to search for it. This can be done by nmap -p 22 192.168.1.0/24 if you’re using Linux (substitute your network). It should be the one where manufacturer is Raspberry Pi Foundation and where 22/tcp open ssh is shown. If you have multiple Raspberry Pi’s, you can run Nmap before and after booting your Raspberry Pi Zero W and spot the difference.

Windows users can also use Nmap. You can download it here. Download and install the latest stable release. Enter your network (for example 192.168.1.0/24 in the Target field and nmap -p 22 in the Command field.

Another way of finding the dynamic IP address of your Raspberry Pi Zero W is to keep an eye on the logs of your DHCP server or look it up in your wireless access point (router). If your DHCP server dynamically updates your DNS records, you can find your Raspberry Pi Zero W with its hostname as etha-light-switch.

Once you know the IP address of the Raspberry Pi Zero W, you can use your SSH client to connect. First thing to do there is, of cource, change the password of the root user (passwd). If you are going to use Mosquitto on this ETHA Light Switch you should change the Mosquitto passwords as well (and maybe add your own users and delete user alice (unless your name happens to be Alice)).

The ETHA Light Switch Python program and Mosquitto are both present and defined, but not started at boot time. You can start them with:

systemctl start mosquitto
systemctl start etha-light-switch

When your setup is ready, you can start them automatically at boot time:

systemctl enable mosquitto
systemctl enable etha-light-switch

After starting etha-light-switch, you can show its log with:

journalctl -b -e -u etha-light-switch

You can continuously follow the log with the -f option:

journalctl -b -e -f -u etha-light-switch

What to do next? Well, you’d better configure your Raspberry Pi Zero W so it fits your situation. You can use raspi-config to do that. It’s very important to change the time zone to your local one (under 4 Localisation Options - I2 Change Timezone). Don’t use the option 2 Network Options - N2 Wi-Fi to change your Wi-Fi configuration! For that, edit the file /boot/NetworkManager/wlan0 and reboot.

But you’d better start with reading the rest of this website to become knowledgeable of the project.

A special note to Windows users

When you’re writing the disk image to your sd-card or editing the NetworkManager/wlan0 file, the first partition (FAT32) is mounted automatically. The second partition, however, is an ext4 partition. That file system is widely used in the Linux world but unknown to Windows. That’s why Windows will ask you (several times) to format it. Make sure you don’t!

There are several SSH clients available for Windows. Often PuTTY is used. It could also be helpful to install an SFTP (SSH File Transfer Protocol) client like WinSCP. That’s an easy way to edit files directly on your Raspberry Pi Zero W.

Leave a Reply

Your email address will not be published.