Intro: Web-Based Pool Controller W/ Raspberry Pi, Arduino, Xbee and Digital Thermometer
This simple system allows web based remote control of a swimming pool pump and heater, or any device that be switched with simple relays. It uses a Raspberry Pi as the main controller and web server. It also uses a remote thermometer run by an Arduino at the pool, powered by a battery and a solar panel. Two small XBee radio units supply the data link between the pool thermometer and the main controller.
I hacked into the physical pool controller timer unit to tap into the relay driver circuits, low voltage/low power Darlington transistor arrays which control the high power main pump relays. I control the heater (a Laars Lite 2, 400,000 BTU unit) via the "Fireman's Switch" control line that allows for a remote kill switch in series with the other safety fuses. Then I set the thermostat to maximum. That way, as long as the pump is on, I can control whether or not the heater is on as well. If you don't know what you're doing, don't even think about doing a project like this with a pool pump circuit and heater. You'll void any warranty or worse...get a professional to install a proper remote system. Think of this as an outline for you to do something else that is simple and safe. That said, this is a wonderful way to control things in your house by your cell phone or laptop when you're away.
Step 1: Parts Required
- Raspberry Pi, version 2 running Raspbian OS version "Jessie" or newer
- Arduino (Uno, Nano or "barebones" --make your own low power with ATMega328 chip)
- LT1529 3.3 low-power voltage regulator if you want to make a low power Arduino
- FTDI cable for programming the barebones Arduino chip, or just get a regular Arduino unit that has its own FTDI programming circuit onboard...but it will run your battery down faster.
- Solar panel, 6 volt unit. Not a 5V unit.
- Lipo battery, 3.7 volt, 4000mAH (you may get by with a smaller capacity battery)
- Lipo charger board (Adafruit)
- 5 volt buck/boost unit (Adafruit)
- Two Xbee radio units, Series 2 (Digikey)
- Breakout socket board for one Xbee, and another XBee socket with a USB type A (rectangular) connector
- Two 5 Volt relays of the type used with Arduino or Raspberry Pi projects
- Wifi USB dongle (not necessary if using Raspberry Pi 3 -- it's onboard)
- Power supply, 5V (at least 2 amps) to supply the Raspberry Pi, Wifi dongle, relays and Xbee radio
- Waterproof thermometer, one-wire type, must be a Dallas DSB-20 or equivalent
- Tupperware containers to keep the rain out
- You can find most of these things on eBay, Amazon, Adafruit, Sparkfun or any number of other sites.
Step 2: Raspberry Pi Main Controller
I use a Raspberry Pi 2, running Raspbian version "Jessie." It runs an Apache2 web server. By using a Wifi dongle plugged into one of the USB ports you can connect it to your local network and control the pool pump and heater with a simple html interface. By configuring port forwarding on your router you can access the controller remotely through your cell phone. Use a port that your ISP doesn't block, and forward all html requests to port 80 on the Raspberry Pi on your internal network.
The Raspberry Pi drives two 5 volt relays directly from two pins on the GPIO header. These little relays have opto-isolators on the input side, so they work directly with the low power pins on a Raspberry Pi. The temperature data (4-digit numeric strings with a decimal point) are received through the XBee which is mounted on a USB adapter.
Step 3: Remote Thermometer Using Arduino and XBee
The thermometer is a standard one-wire Dallas DSB-20 unit. While it's easy to connect this directly to the Raspberry Pi (there's a one-wire library that comes with the Raspberry Pi) it may not work if the data line runs more than 30 feet, and besides, I don't want anything in the pool that might short to the power line. You could use an opto-isolator to separate the lines, but a short might still occur. I prefer the safety of a battery operated thermometer in the pool. I use two little XBee radio units to send the data to the Raspberry Pi. A low-power Arduino takes the data from the DSB-20 thermometer and feeds it to the XBee transmitter. There is an Arduino library for the one-wire DSB-20.
Waterproof versions of the Dallas thermometer are easy to find as are all of the parts in this project. It's important to note that this is a digital device that sends a data stream down a single wire. In reality it uses three wires--one for data, one for power (3 to 5.5V) and one for ground. The data line needs a 4.7k ohm pull-up resistor connected across to the positive power line.
Step 4: Configuring Raspberry Pi
The Raspberry Pi can be connected to a monitor with the HDMI connector and the mouse and keyboard through the USB connectors. Before you set up the Wifi you can use an Ethernet connection to your router. This is important for setting up the Raspberry Pi and getting updates and any software that may not come with the initial operating system on your micro-SD card. (Once it's configured the unit becomes "headless"--without a monitor.) At this point I'm assuming you've booted your Raspberry Pi with a downloaded operating system (Raspbian for a Raspberry Pi 2 in my case). Now connect the Raspberry Pi to a USB Wifi dongle if you have a Raspberry PI 2. (The Raspberry Pi 3 has built-in Wifi.) Make sure you're using the later "Jessie" release of the operating system for a Raspberry Pi 2. It's much better at maintaining a stable Wifi connection than the earlier "Wheezy" version. Also, you'll probably want to give it a static IP address that won't be changed by your router from week to week. A good tutorial on this is found here.
Once you've got Wifi running you can access the Pi remotely using SSH and a command-line interface. A good tutorial is here.
Now you need to have a webserver running on your Raspberry Pi. This involves a set of four packages called LAMP (Linux, Apache, MySQL, PHP) and that's easy to do. Here is one tutorial.
I use some very basic html pages to connect with simple Python and shell scripts that check on the temperature and determine whether the pool heater and/or pool pump should be on.
Step 5: Thermometer--Arduino--Solar Panel and Battery Supply
The pool thermometer uses a "barebones" Arduino made from a ATmega328P chip. Make sure you buy a chip with the bootloader already burnt on it--they go for about $2 on eBay. You can then upload the Arduino program by putting the chip in a regular Arduino Uno board and then removing it carefully, but if you just get an FTDI cable (Adafruit or Sparkfun) on your breadboard you can avoid bending the chip pins. A barebones Arduino gives a lower power drain than a regular Arduino because it skips the onboard voltage regulator. I used my own low-loss voltage regulator chip, LT1529 3.3 which won't drain the battery as quickly. A tutorial is here.
The battery is a standard 3.7V LiPo unit with a solar panel. You can't simply charge these batteries by connecting the solar panel--they require a dynamic 4-step charging protocol. I use a 6 volt panel (5 volts will not work) with a charging unitfrom Adafruit that has a nice large electrolytic buffer capacitor on the output side.
The output is on the order of 3.7-4V, so to ensure the LT1529 3.3V regulator always has a higher input voltage I use a simple buck/boost unit to jump it to 5 volts.
Step 6: Screenshot
Here is a screenshot. It's plain and simple, but it works.
Step 7: Notes
A few notes:
--when running the Apache2 webserver, commands are executed from /usr/lib/cgi-bin. Your Apache service may point to this folder by default when the website address calls www.yourserver/cgi-bin/command.py but if it doesn't, you have to include in your Apache config files a line such as ScriptAlias "/cgi-bin/" "/usr/lib/cgi-bin/" Also, make sure the Apache webserver has the permission to execute commands from this folder with the +ExecCGI option: Options +ExecCGI ...details can be found here.
--The Raspberry Pi uses a built-in serial interface to communicate with the keyboard by default. In order to use this serial interface for the XBee you have to tell it to stop communicating with the keyboard. There are a number of sometimes contradictory instructions on how to do this floating on the web, and they differ based on which version of Raspberry Pi you have. On my Raspberry Pi 2 running "Jessie" release of Raspbian operating system, there are two steps: (Do this ONLY after you have a reliable SSH connection to your Raspberry Pi: )
1. I place these lines in /etc/rc.local (which is the script always run at bootup after everything else):
systemctl disable ModemManager.service
systemctl stop serial-getty@AMA0.service
systemctl disable serial-getty@AMA0.service
systemctl mask serial-getty@AMA0.service
(insert the above lines before the last line of the rc.local file, "exit 0")
2. edit /boot/cmdline.txt (make sure you copy the original to /boot/cmdline.txt_backup ) and remove all elements referring to the serial connection to the keyboard-- ttyAMA0 on Raspberry Pi 2 with Jessie, or ttyS0 on Raspberry Pi 3 ) thus
"dwc_otg.lpm_enable=0 console=ttyAMA0,115200 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline rootwait" becomes
"dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait" in RPi 2 Jessie.
--Raspberry Pi wall-wart power supplies are often underpowered and noisy. (In the old Raspberry Pi 1 the unit would reboot if you simply plugged in a USB Wifi dongle.) Here I use a larger 6 volt power supply connected to a 117V AC waterproof outlet at the pool pump and an adjustable voltage controller set to 5 V with a large 1000 uF capacitor on the output to the Raspberry Pi and relays. I haven't had any stability problems with this setup, even with a 3/4 HP pool motor nearby.
--I use XBee radio units, series 2. These are completely different from series 1.The unit at the Raspberry Pi is set as a Coordinator, and the unit at the pool thermometer is set as a Router. The router acts as a simple conduit passing the digital stream (ASCII number strings of temperature, e.g. 78.36) on to the receiver Xbee at the Raspberry Pi without any other data (no frames). That keeps it simple, and it works. A very good set of Xbee-Arduino tutorials can be found here.
--The Raspberry Pi has two Python Daemons running, one to regularly check the pool temperature and another to regularly decide which relays should be on or off based on the thermostat setting and the current pool temperature. Other scripts are called based on web commands to report the pool status, change the thermostat setting, or enable the heater on or off. Other script are run regularly as cronjobs to restart the daemons if they somehow terminate.
--I also have two of the Raspberry Pi GPIO pins set aside for a pushbutton to allow a controlled software shutdown of the Pi. Remember, if you simply pull the plug when it's running you can corrupt important files. The script for checking whether the button is pushed, is started in /etc/rc.local, and an example can be found here.
Step 8: Files
Here are some software files. They're scrounged and hacked from sources you may have seen elsewhere from Arduino and Raspberry Pi sites. They're not clean and pretty, but I'm not a professional coder, and if you're like me you have other jobs that are more pressing. Nevertheless these may help if you're not a professional. Poke around with it. It's simple, but it works for me.
Arduino sketch to get the temperature from the DSB20: TempRemoteSend-Sleep2.ino
POOLREPORT.py, heatON.py, heatOFF.py and tempInText.py belong in cgi-bin. (Raspberry Pi)
tempInText.html, heatON.html and heatOFF.html belong in /var/www/html or whatever your website html directory may happen to be.
The shell scripts (.sh) belong in cron.
Please strip the .txt suffixes!