Raspberry Pi programming
The HawkEYE PLUS is a mains electricityfrequency monitor with 3 relays which control external devices when certain preset frequency thresholds are reached. It also contains a Raspberry Pi 3b which can communicate with the frequency monitor and the front mounted TFT display using Serial Peripheral Interface (SPI).
HawkEYE PLUS does not require the Pi to monitor and react to frequency thresholds. However the Pi can be used to query the current frequency and the status of the Relays from the HawkEYE PLUS. As the Raspberry Pi has local storage and also connectivity to wired and wireless LAN, it is possible to locally or remotely store readings or communicate with a central monitoring point.
The rest of this page contains Pi programming examples in Python to query the frequency, status of the Relays of the HawkEYE PLUS and display the information on the built in TFT.
Python and library installation on the Pi
The Raspberry Pi 3 supplied with the HawkEYE PLUS is installed with Raspbian and the following configuration steps have been applied. These instructions will help you if you decide to re-install the Pi.
- Install git
$ sudo apt-get install git
- Install Python and the required libraries
$ sudo apt-get install build-essential python-dev python-pip python-imaging python-numpy
$ sudo pip install RPi.GPIO
$ sudo pip install Adafruit_GPIO
- Clone the ST7735 library to control the TFT display
$ git clone https://github.com/cskau/Python_ST7735
- Add the ST7735 library to Python
$ cd Python_ST7735
$ sudo python setup.py install
More information about the libraries can be found from these links:
Preparing for the examples
This section helps you prepare the Raspberry Pi for the Python code examples. The files and folders mentioned in this section are supplied on Pi inside the HawkEYE PLUS. You can use the following for reference or to prepare a newly installed SD card.
The graphics library (Pillow) can work with various font formats including TrueType (TTF). A good source for fonts is Google. For the examples in this section I have downloaded the EXO 2 font package from Google and copied 2 of the TTF files. I recommend using an SCP client such as WinSCP for file handling on the HawkEYE PLUS Pi.
1. Create the directories ~/fonts and ~/mmix in the home folder of pi
2. Download the EXO 2 font from Google, extract the contents and copy Exo2-Regular.ttf and Ex02-SemiBold.ttf to the ~/fonts directory.
All code is expected be copied to and run from the ~/mmix folder.
I expect that you have a working knowledge of Python and do not require explanations for normal code associated with a Python script. Because of this you will find that there are gaps in the line numbers. The entire script is shown at the bottom of this page.
The code samples
The following code snippets will explain parts of the code necessary to be able to read from the frequency monitor and write to the TFT display.
Declare the Libraries
The Pillow libraries are used to create a virtual screen which is copied to the TFT when all changes are complete in memory. This simplifies and speeds the control of the pixels on the display.
During the startup of the Python an image is displayed on the screen. Sleep is used from the time library to create a pause so that the splash remains visible for a few seconds.