This summer I created a pool temperature monitoring and pump control system using a Pi Zero W. This article gives an overview of the system and how I put it together. It allows the temperature of the air and water to be displayed on a web page while automatically turning the pump on and off according to a preset schedule.

The Pool

We originally had an 8ft Bestway “fast set” pool with an inflatable ring around the top. This proved to be slightly incompatible with the 3 cats we share the garden with. So this year we changed it for a 10ft metal framed “Summer Escapes” pool. It holds 4100 litres of water and I had no idea what temperature the water ever reached or how this related to the air temperature. I also had no way of easily controlling the pump without fiddling with the settings on a mains timer.

The Requirements

I decided my Pool Temperature Monitoring system should be able to :

Measure the air and water temperature

Log temperatures to the internet

Display temperatures on a web page accessible from a mobile phone

Allow the pump to be turned on, off or placed in an automatic mode that would follow a schedule

The Water Pump

As with the previous pool it came with a mains powered pump containing a filter. In previous years I controlled this with a traditional mains timer but there were times I wanted to manually turn it on to take advantage of the free electricity available from my solar panels. If the pool had been used in the day I also wanted an easier way to give it an extra few hours without messing with a timer unit.

The Weatherproof Box

To power the pump I bought a weather proof box that had room for a 4-way extension block. A 10m mains cable runs into the house. There was plenty of room to put in a 5V power supply and a Pi Zero. The sensor cables come in via the rubberised slot.

These boxes seem expensive but when mains is involved you really want something designed to keep out water rather than trying to use an old ice cream tub.

The Parts

My finished system includes the following :

Pool [Amazon]

Pump

Weatherproof box [Amazon]

10m mains extension lead

Raspberry Pi Zero W [Amazon]

5V power supply [Amazon]

4GB microSD card

2x waterproof DS18B20 temperature sensors with 3m cables [Amazon] [eBay]

4.7Kohm resistor for DS18B20s

Energenie Socket [Amazon] [eBay]

Energenie Pi-mote add-on [Amazon] [eBay]

Energenie Sockets & Pi-mote

Energenie produce remote control sockets. They operate like most of the remote control sockets out there but with one crucial difference. You can buy a “Pi-mote” add-on for the Pi that lets you control sockets using Python.

It’s really easy to setup and was the perfect combination of hardware to allow me to control the pump. The Pi-mote simply plugs onto the GPIO header and allows sockets to be controlled using single lines of Python.

Temperature Sensors

I opted for two DS18B20 “1-wire” interface temperature sensors as I had used them before. Multiple sensors can be connected to the same GPIO pins. The DS18B20 can be purchased as a waterproof version with all the cabling attached. They are slightly more expensive than the standard sensors but all I had to do was solder the three wires to the appropriate GPIO pins on the back of the PiMote and they were ready to go.

One was strategically placed in a hedge to measure air temperature and the other dropped into the pool to measure the water temperature. My sensors had 3m of cable.

The Software

All the software is available on my Pool Monitoring BitBucket Repository. There are two main scripts involved in this system and both are written in Python. They are launched at boot time using cron via the launcher.sh script.

The first (poolmain.py) runs in a continuous loop and checks the mode. If in “auto” it keeps an eye on the time and decides when to turn the pump on and off. It also sends regular temperature readings to the “Thingspeak” cloud-based IoT platform. You can see my public channel with recent air and water temperature readings here : https://thingspeak.com/channels/293211

The second (poolweb.py) uses the Flask framework to create a basic set of webpages. This includes a dashboard, a schedule and a login page. The dashboard shows the air and water temperatures and also allows the pump mode to be changed. There are three modes. On, Off and Auto. When in auto the pump is automatically turned on and off depending on the Schedule defined on the schedule page. The login page ensures only authorised people can mess with the pump!

The Web Interface

The system presents a web-page either over the local network or the internet (if you’ve configured your router appropriately). It looks something like this :

The schedule can be modified on the schedule screen. Ticks are placed against the hours the pump should be active when in “auto” mode :

The default username and password is “admin” and “splishsplosh”. The pages can be accessed via 192.168.1.42:5000 where “192.168.1.42” should be replaced with the IP address of your Pi on your network. 5000 is the default port used by Flask.

Pushover and Thingspeak

When the system boots it sends a notification using Pushover. This can then be read on a mobile phone using their app. It’s a great service and you can use it to manage notifications from other systems.

The notification message contains the internet IP address of your network and if you have set up port forwarding to the Pi you can access the dashboard when away from home. It assumes you are forwarding port 50000 to port 5000 on the Pi.

Temperatures are sent to Thingspeak which allows you to see your data plotted as a graph.

Both these services require you to create an account and obtain some API keys. They can be added to the config.py file.

Hardware Setup

The hardware setup is fairly straightforward.

The pump is connected to the power strip using an Energenie remote socket.

The Pi Zero is powered with a 5v phone charger.

The PiMote is attached to the Pi’s GPIO header.

The temp sensor wires were soldered onto the back of the PiMote. “Red” to Pin 1 (3.3V), Black to Pin 9 (Gnd) and Yellow to Pin 7 (GPIO4). A 4.7Kohm resistor is put between Pin 1 and Pin 7 as per the 1-wire interface requirements.

SD card in the Pi’s SD card slot (obviously!)

SD Card Setup

To get the software working you can follow this process. Start by creating a fresh SD card using the latest version of Raspbian. I used the “Lite” version as I didn’t need the desktop environment.

Enable SSH using your preferred method. I used my Windows PC and simply created a blank file named “ssh” on the boot partition of the card before inserting it into the Pi.

I then manually setup the WiFi using a wpa_supplicant.conf file. This should be done before you boot the fresh image for the first time.

Please change the default Pi password to something sensible using this guide. This is particularly important if you are allowing your Pi to be accessed over the internet.

Updates and Package Installation

Run the following commands to update the image :

sudo apt-get update sudo apt-get -y upgrade

Then install the following packages :

sudo apt-get -y install git sudo apt - get install python3 - gpiozero sudo apt-get -y install python3-pip sudo pip3 install flask sudo pip3 install requests

These packages support the features used in the Pool Monitoring Python scripts.

Pool Monitoring File Downloads

The next step is to clone the software from my BitBucket repository :

git clone https://MattHawkinsUK@bitbucket.org/MattHawkinsUK/rpispy-pool-monitor.git

Now rename the directory to something a bit easier to type :

mv rpispy-pool-monitor pool

and navigate into it :

cd pool

Make the launcher.sh file executable using :

chmod +x launcher.sh

Energenie Socket and Pi-Mote Pairing

The first time the Pi-Mote is used it must be paired with the socket.The process is described in the official user manual. The socket must be put into “learning mode” and this can be done by:

If socket is on press the green button to turn it off

Hold the green button for 5 seconds or more and then release it when the lamp starts to flash at 1 second intervals.

Then run the pairing script in the utils directory :

cd /home/pi/pool/utils python3 energenie_pair.py

Pressing Enter when prompted will pair the Pi-mote to the socket and the socket will have an ID of 1. This ID is used in the Python scripts to turn this specific socket on and off.

DS18B20 Sensor Setup

In order to configure the DS18B20 sensors you need to make a small change to the config.txt file using :

sudo nano /boot/config.txt

add the following line to the bottom :

dtoverlay=w1-gpio,gpiopin=4

There is more detail on the DS18B20 on the DS18B20 1- Wire Digital Thermometer Sensor page.

Web Interface Password

The default username and password is “admin” and “splishsplosh”. The password is stored as a hash so to change it you must use the hashgenerator.py script to convert your new password into a new hash value.

cd /home/pi/pool/utils nano hashgenerator.py

Then change the default password “splishsplosh” to your password. Save using CTRL-X, Y and Enter. Run the script to create the hash value :

python3 hashgenerator.py

The new hash can be inserted into the config.py file.

Config File

Edit config.py using :

cd /home/pi/pool nano config.py

Paste in the new hash.

The “FLASHSECRET” can be changed to anything you like. Stick in some random characters to personalise yours.

To send a boot notification to Pushover and temp data to Thingspeak you will need to register with those services and obtain API keys. These are personal to you and should be carefully inserted into the config.py file.

Save and exit the nano editor using CTRL-X, Y, ENTER.

Cron Setup

To get the scripts running when the Pi boots we need to create a cron entry. Do this using :

sudo crontab -e

If prompted select a default text editor. I usually choose nano which is option “2”.

Then add this line at the bottom :

@reboot sh /home/pi/pool/launcher.sh > /home/pi/pool/logs/cronlog 2>&1

Make sure there is a blank line after this line.

This will run the launcher script at boot time and will in turn run the two main Python scripts.

Timezone Setup

One final step is to set the correct timezone for your location. I has to do this to ensure my system knew the correct time and wasn’t out by an hour. You can do this by :

Running “sudo raspi-config”

Selecting “Localisation Options”

Selecting “Change Timezone”

Select your region

Select your nearest city/region

Save and exit by selecting “Finish”

Typing the command :

timedatectl

should report the correct “Local time”.

Ready to go!

Assuming the sensors are connected and you’ve configured everything correctly it should all work when the Pi is rebooted.

Troubleshooting

As with most projects that involve a mixture of hardware and software things might not work straightaway. Here are some tips :

Check the contents of the logs in /home/pi/pool/logs

Ensure the temp sensors are wired correctly to 3.3V, GPIO4 and Ground

Ensure the temp sensors are wired correctly and that there are two “28-00” directories in /sys/bus/w1/devices

Check all the files are located in /home/pi/pool/

Check “crontab -e” contains the @reboot line

Check the launcher.sh script is executable. Use the “ls” command and it should show up in green.

References

The following links provide additional technical information on the technologies I used in this Pool Monitoring and Pump control project :

Energenie Pi-Mote Manual

https://energenie4u.co.uk/res/pdfs/ENER314%20UM.pdf

Energenie Support in gpiozero Library

http://gpiozero.readthedocs.io/en/v1.2.0/api_boards.html#energenie

Flask Documenation

http://flask.pocoo.org/docs/