Monday 26 October 2020

PyBloom coding project part 3: Setting up the Raspberry Pi

In part 2, I set up the development environment. Now in part 3, I set up the production environment on my Raspberry Pi.

Production Environment: Raspberry Pi

Mine is a venerable RPi, a model B rev 2 from 2015. It was purposefully underpowered in order to be cheap, but nowadays it feels really slow. But my program isn’t very compute-intensive, so it’s a perfect match. It makes a great always-on machine that doesn’t drink a lot of power.

Installing the OS

I recommend starting every Pi project with a fresh install of the OS. It’s a chance to wipe away the acreted libraries that aren’t needed, and take advantage of the latest patches for everything. Simply download the laterst version, and put onto an SD card of at least 16GB (use something like Etcher), then boot up the Pi.


Once booted, remember to update the software, check the options to enable SSH and VNC on startup, and you’re done. Oh, make a note of its IP address; you’ll need it later.

Connecting the Mac to the RPi

I make use of three different ways of connecting the Mac to the RPi, each for different use cases:


  • SSH is built into both RPi OS and macOS and is the quickest way of communicating between the two. It’s the best way of installing and upgrading libraries, as described in the next section. The biggest limitation is that if you start a thread on the RPi, then close the Terminal window on your Mac, then the RPi thread shuts down as well.


  • VNC is built into the RPi OS, but needs an app downloaded to the Mac. It enables access to the GUI, which is the easiest way of navigating the Pi. It’s also the only way of starting processes in the RPi, which do not terminate if the VNC window is closed. 


  • AFP is supported by the Mac, and requires the netatalk library to be installed on the RPi. Obviously, you’ll need to install it by plugging in a keyboard, mouse and monitor otherwise you won’t be able to see what you’re doing! Once installed, the RPi can appear in the Finder app as just another server. This makes promoting code from dev to prod as simple as copying and pasting. 


Installing the packages

To recap, these are the packages to be installed on the RPi.


  • APScheduler

  • Flask

  • PyGal

  • PyOWM 

  • Qhue 

  • Rgbxy

  • Netatalk 


Virtual environments on the RPi

Let’s connect to the RPi using SSH. At the command line, enter the following:


sudo pip install virtualenvwrapper


As the Pi is a single purpose computer, there’s less of a need to create a virtual environment. If the worst comes to the worst on the RPi, I can just re-cut another SD card and start again. So why bother? Well, it’s mainly to ensure we use the right version of Python. The code will only work on v3.x but the Pi also has v2.7 installed as well. In fact, if we run the command Python PyBloom.py will try to run the code on v2.7 - and fail. So let’s go ahead and set up the virtual environment on the RPi.


export WORKON_HOME=$HOME/.virtualenvs

export PROJECT_HOME=$HOME/Projects

export VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3

source /usr/local/bin/virtualenvwrapper.sh


As with the Mac, these statements complete the configuration altering the Bash script. This needs to be done in two locations on the RPi: .bashrc and .profile files. The former is loaded when you load bash after logging on (as in, from a terminal from RPi GUI) and the latter when you load Bash with logon (as in via SSH). Note that the location of the Python file is different on the RPi than the Mac, so setting the VIRTUALENVWRAPPER_PYTHON global variable is different. (Note you can use source ~/.profile to reload the Bash profile without re-logging in.)


mkproject pybloom


As we saw before, this command sets up a project directory called pybloom in Projects, a virtual environment called pybloom in .virtualenvs, and links the two.



Having set up both environments, part 4 will look at the next part of preparation: organising the code. Also visit https://github.com/Schmoiger/pybloom for the full story.

No comments:

Post a Comment

It's always great to hear what you think. Please leave a comment, and start a conversation!