Introduction: Home Environmental Sensor Array -- Phase 1
My basement has flooded several times. Every incident had several things in common:
- No one was home;
- The water softener was running, dumping gallons of water into the crock;
- The sump pump failed.
I want a solution that will prevent this from happening or at least warn me if water is flooding my basement. This instructable is about the device I built to fill this need.
This is phase one of my home environmental sensor array (HESA) project. The device I built in this phase will only detect water. Future phases will add the capability to detect more things.
Most of the effort to make the HESA took place at the Milwaukee Makerspace. I would like to thank all the members of the 'space who helped me in so many different ways. There is no way I could have built this without them. If you live near Milwaukee, WI, stop in and visit or consider becoming a member. You won't regret it.
At the time I made this device, I was just starting my Maker journey. For me, this was a great first project. I learned or practiced many Maker skills like soldering, basic electronics, and CNC routing. I made my own PCB (that I did not end up using). I did some basic metal work with a jig saw. I learned how to use several software tools for CAD and design. I learned to program in Python. And I had a lot of fun doing it.
Finally, I tried to journal the progress of making this device on my Maker blog. You can read my notes here. In some cases, the notes have more details than this instructable. I recommend checking them out to get the complete picture.
As I mentioned above, the HESA will expand in the future. The phase 1 solution has the following features:
- Auto-startup on power up
- All components mounted in an enclosure that is mounted on the wall and connected to 120v power
- Check for water in the basement. If found:
- Shut off power to the water softener
- Send email to mail addresses
The pi will also serve as a development environment for my web site.
Phase 2 Description (not in scope for this Instructable)
Make the device more user-friendly and interactive. Replace the Pi with a Beaglebone Black to make it able to connect to more things. It should record daily data and statuses in a web-based database. It should check the internet connection regularly. If there are any non-standard conditions, alert the humans with email (if possible), lights, and sound until the problem conditions are gone.
Phase 3 Description (not in scope for this Instructable)
Add a battery backup feature so the system can send alerts and shutdown gracefully in case of a catastrophe. Detect other environmental problems, record regular readings and send alerts.
Step 1: Procure a Raspberry Pi
I bought my raspberry pi from Newark. There are other vendors that sell raspberry pis but I went with Newark.
This pi is installed in a project box. So, a keyboard, mouse, and video are not needed. There are some parts that you must have for this project. Other parts were needed to setup the pi, test the design, connect to a tv etc. You could use other microcontrollers like an Arduino or Beaglebone to do the same thing but I wanted a project that I could do with a raspberry pi.
Components Used in Final Project
* Rasperry Pi Model B. Newark PN: 43W5302
* 4gb SD card
* RASPBERRY PI BOX ENCLOSURE, ACRYLIC, TRANSPARENT. Newark PN: 44W3465
* USB power adapter and USB power cable (I used one from a cell phone)
Other Components Used During Project
* PI Cobbler breakout kit. Newark PN: 44W3497
* Solderless breadboard. Newark PN: 56T0250
* Jumper Wire Kit - 140PCS. Newark PN: 54T9598
* HDMI video cable. Newark PN: 74M6165
* USB keyboard
* USB mouse
Next: Format the SD Card
Step 2: Format the SD Card
This is an optional step. It may be easier to buy an SD card that already has the latest version of the Raspbian distribution on it. At the time of this writing, you could get an 8gb card with Raspbian on it for $5 plus shipping. If you can wait for the SD card, this might be the way to go.
Another option is to download Raspberry Pi's "New Out of Box Software" (NOOBS). NOOBS has several different OS versions that can be installed on the SD card. You still have to go through the steps I describe below but it looks like NOOBS has some handy startup options.
For this project, I used an SD card I already had sitting around and installed a version of Raspbian on it. There are basically three steps to do this.
1) Download and unzip the latest raw image of Raspian from the raspberry pi downloads page.
2) Download and install the Win32DiskImager utility on a Windows computer.
3) Use the Win32DiskImager utility to install the Raspian image file onto the SD card.
Note: You can do these same steps on a Linux machine using a utility called dd. However, I could not get that to work. I had to use a Windows computer.
More detailed instructions for getting the SD card setup are here.
Next: First time setup of the Raspberry Pi.
Step 3: First Time Setup of Raspberry Pi
Configuring the Raspberry Pi
When you first boot the raspberry pi, you will see the Raspi-config window. This is where you tell the pi what time zone you are in, etc. Here is a link to a great tutorial about doing the config. I recommend you review the following entries:
* ssh (so you can connect to the pi from your Linux laptop)
After you complete the config, the Raspi-config window does not appear again on bootup. If you need to run the configuration again, type sudo raspi-config at a terminal prompt.
Here are some notes about the Linux OS on the pi.
1) I had problems with my keyboard not entering some of the symbols on the keys correctly. I had to change the keyboard configuration from UK to US to fix that.
2) Periodically run these commands to keep the OS up-to-date:
sudo apt-get upgrade
sudo apt-get update
Install required software
The only software required for this project is Python. The Raspian distribution comes with Python and the GPIO module for the Pi already installed. Basically, it is ready to rock-and-roll.
Install optional software
One optional application that I would highly recommend is Avahi server. Avahi lets you connect to the Pi using a name instead of an IP address. For example, I can type ssh hesa.local to connect to the HESA.
As the sensing component of the Pi is not a very taxing solution for the processor, I wanted my Raspberry Pi to do other things. I installed the software tools to host my website so I can use the Pi as a development environment. This includes:
Here is a site that explains how to install the L.A.M.P. applications.
Another bonus to installing Apache is that you can use it to host a web page showing the status of the HESA. The python code will create a web page and update it if Apach is installed.
Backing Up the SD Card
Once you get everything setup and working on the SD card, make a backup image. Basically, you use the same Win32DiskImager software that you used to install the Raspberry Pi distribution on the SD card. Lifehacker has some good instructions for backing up the SD card. I recommend making a new backup image anytime you make major changes to the setup.
Next: PowerSwitch Tail
Step 4: Procure and Assemble the PowerSwitch Tail
One of the key components of the water sensor is a 120 volt switch that is controlled by the Raspberry Pi. The water softener in my basement is plugged in to the switch. When the water sensor detects water in the basement, it is probably because the sump pump is not working but the water softener is running and dumping water into the basement. So, the Pi shuts off the switch, cutting off power to the water softener.
I bought the PowerSwitch Tail 120 Kit (part number PSTK-120) from powerswitchtail.com to control the power. It was cheap (< $20), easy to assemble, and very effective.
Full instructions for assembling the PSTK are here. It was pretty simple but I still managed to make some mistakes. This is a great mini-project to learn basic soldering.
One thing that stumped me for awhile is that the power goes in to the right-hand side of the board and out the left. I had that reversed at first and could not figure out why it did not work. (Thanks, Steve from PowerSwitch Tail for your patience helping me troubleshoot this.)
Next: Write the Python Code
Step 5: Write Python Code
I wrote a small program using python to control the features of the HESA. The main program is called hesa.py. I also made a module to send email called rynokEmail.py. The email module can be used in other programs. The links will take you to the source code.
One important thing to mention is that the input pin on the Raspberry Pi is setup as a pulldown like this:
Here is the pseudo-code for the program that shows the basic steps:
1. Load the libraries used by the program and define global variables
2. Initialize the GPIO on the Raspberry Pi
3. Power on the relay
4. Create a new status web page
5. Begin looking for water by sending signals out one electrode and looking for the current on the other electrode.
5a. Update the status page every 10 minutes
5b. If current is found on the input electrode
5b1. Shut off power to the relay and reset the GPIO on the PI
5b2. Send an email
5b3. Update the status page
As an aside, there are a couple of things I like to do when coding in any language. First, I make a debug global variable. Then, I put debug statements in all of my modules so I can troubleshoot the program if it is not working. Ideally, the program will report when it enters a module, the status of key variables in the module, when a key event happens, and when the module is exited. All of the messages can be turned off simply by changing the value of the debug variable in one place.
Second, I try to comment everything thoroughly. This helps when I come back a year later and try to figure out how some snippet of code works. I also make the code as simple as possible. This may mean breaking one complex line into several simpler lines of code. I also try to explicitly detail all If/Then conditions and not use ELSE unless I am sure that all IF conditions will not be met.
Third, I make reusable modules whenever possible and put them in a common folder that all programs can see. This way, I don't have to reinvent the wheel for each program I write.
Next: Make the enclosure
Step 6: Make the Enclosure
An enclosure is needed to hold everything and attach it to the wall in the basement. This step will vary depending on the project box you use. For this project, I found an old electrical box on the hackrack at the Milwaukee Makerspace and modified it to work with my project. The box I used is 37mm long x 33mm high x 13mm deep. That seems like a good size overall.
Whatever enclosure is used, it needs to accommodate the following:
* hole for the power input plug
* hole for a power switch
* hole for the duplex outlet
* hole for the ethernet jacks
* holes for the sensors
* space for lights and an LCD display in the future
Get the External Parts
Find the parts to connect the parts inside the enclosure with the outside. I took the power input plug and power switch from an old PC power supply. Any standard duplex power outlet and cover can be used for the external power. Also, any ethernet keystone jacks with cover can be used for the network outlet. My enclosure already had an LED. I will use that to show that the Raspberry Pi is working.
Acrylic for Opening
My enclosure did not have a piece on the front. It may have originally had a door on hinges but that was gone when I found it. Cut a piece of acrylic to fit the front opening with a table saw. Simply measure the opening, and cut the acrylic to cover the entire opening. My enclosure already had holes on the front that could be used to attach the acrylic. Holes were drilled in the acrylic and small screws were used to attach the acrylic to the enclosure.
The acrylic is a nice feature because it allows people to see inside and because it is easy to cut holes in it. In phase 2 of this project, an LCD display will be installed in the acrylic. Wood or metal could also be used for the front piece.
The enclosure needs a piece of wood on the back to attach the components to. Use a table saw to cut the wood for the back of the enclosure. The wood I used was 1/4" thick particle board. Any type of wood would work but plywood is easy to shape. The wood should cover the entire back of the enclosure but it does not need to be an exact fit. Once the wood fits in the enclosure, mark the outline of the mounting holes on the wood. Use a drill or jigsaw to cut away the wood that currently covers the mounting holes. The hole can be square to expose the mounting holes or can be an exact match to the shape of the mounting hole. Mount the wood to the enclosure with screws or bolts and nuts. Use existing holes on the enclosure, if they exist. Drill holes if, needed. The bolts should be inserted from the back to the inside so the enclosure will fit snug to the wall. The bolts should be slightly longer than the thickness of the enclosure plus the wood.
Cut the Holes for the External Parts
Cut holes in the sides of the enclosure for the power input, power switch, duplex power outlet, and the network outlet. The easiest way to do this is by cutting rectangular holes using a jigsaw. For the duplex power outlet and the ethernet outlet, measure the dimensions needed to fit the entire duplex outlet or ethernet jacks in the enclosure using a caliper or ruler. Mark the outside dimensions on the enclosure. The holes need to be slightly larger then the devices themselves. The holes will be covered with cover plates.
Start by using a drill to make pilot holes for the jigsaw blade. Use the smallest drill bit to create a pilot hole. Then, use a bigger bit to make the actual hole. Use a jigsaw with a metal cutting blade to cut the hole for the components. Smooth out any rough or sharp bits with a hand file.
The holes for the power input and the switch need to be pretty exact. The power input has screw holes right next to it and the switch has tabs on the sides to attach it to the enclosure. Again, make pilot holes with a drill and then use the jigsaw for the final cuts. Use a hand file to finish shaping the holes.
Next: Assemble everything and install
Step 7: Assemble Everything and Install
Now that all the pieces are ready, the final step is to assemble all the parts and install the HESA.
The schematic diagram for the water detection circuit and all the pieces is here.
Power input plug
As mentioned in the previous step, the power input plug was taken from an older PC power supply unit (PSU). The plug itself is attached to the enclosure with two screws and nuts that were originally in the PSU. The plug has three power connections -- positive, neutral, and ground. Cut a red, brown, or black wire for the positive power connection long enough to reach from the plug to the power switch. Cut three white wires each about six inches long. Cut a green wire long enough to go from the plug to the ground screw in the enclosure.
Solder flat blade connectors to each wire that will connect to the plug. This makes it easy to remove the wires and also protects the connections if the connectors have a plastic coating.
The other end of the positive wire will connect to the power switch. The other end of the white wire will be connected to the other two negative wires with a wire connector. Solder an eyelet connector to the other end of the ground wire and attach it to the grounding bolt on the enclosure.
As mentioned in the previous step, the power switch was taken from an older PC power supply unit (PSU). It had tabs on each side to attach it to the enclosure with the pressure of the tabs. Simply push the switch into the hole that was cut for the switch.
Attach a flat blade connector to the positive power wire from the power plug. Plug the wire into one of the connectors on the switch. Optionally, the wire could be soldered directly to the connector but then it can't be easily removed.
Cut three six inch long black wires. Solder a flat blade connector on one of the wires and attach it to the other connector on the switch. Connect the other end of that wire along with the other two wires together with a wire connector.
Duplex power outlet
Take an unused power cable and cut both ends off. Then, cut a ten inch length off of the cable. Strip about an inch of the casing off of each end. Strip 1/4" of the casing off of the wires on one end and 1/8" of the casing off of the wires on the other end. Connect the 1/4" long copper side to the duplex power outlet.
Next, attach the duplex power outlet to the enclosure with screws and nuts.
Ethernet outlet and keystone jacks
Take an unused CAT5 or better ethernet patch cable and cut off one of the male plugs. Attach the cut end to a female keystone jack. Attach the ethernet faceplate to the enclosure with screws and nuts. Insert the keystone jack into the faceplate.
Remove the cover on the Powerswitch Tail (PST). Attach one of the black wires from the wire connector and a white wire from the wire connector to the line side of the PST.
Cut a green wire long enough to go from the line connector on the PST to the ground bolt on the enclosure. Solder an eyelet connector on one end of the ground wire and connect it to the ground bolt. Strip off 1/8" of the wire casing from the other end of the wire and insert the bare copper wire into the line connector on the PST.
Take the bare wires on the cable from the duplex power outlet and connect them to the load connector on the PST.
Reattach the cover to the PST. Take two small wood screws and attach the Powerswitch Tail to the wood at the back of the enclosure.
Power for Raspberry Pi
The Raspberry Pi needs 5 volts and 1 amp of power to run. The simplest way to get power to the pi is to use a 120 volt to 5 volt USB power converter. Solder flat blade connectors to one of the unused black and one of the unused white wires in the wire connectors. Connect the flat blade connectors to the plugs on the USB power converter. It should not matter which wire connects to which plug.
Cut a six inch length of one electric wire and solder eyelet connectors on each end of the wire. Use wood screws to attach the wire to the wood with enough slack so the USB power converter will barely fit under the wire. This will keep the USB power converter from moving in the enclosure.
Water sensor connectors
Connect wires from GPIO pins 11 and 22 on the Pi to the external connectors that will be used to detect water. It does not matter which wire goes to which pin.
I found some screw on connectors to put in the external holes. Solder eyelets onto the wires coming from the GPIO pins on the Pi and attach them to the connectors using the eyelets. That way, they are easy to remove when things need to change.
Attach the raspberry pi to the enclosure with two wood screws that will fit in the mounting holes in the pi. Then, connect all the other parts to the pi. (I was going to have a circuit board to go between the raspberry pi and the other parts. However, this phase has the parts wired directly to the pi.)
Take a USB cable and plug the USB power convertor to the RPI's power input.
Plug the ethernet cable into the RPi's ethernet port.
Take a female to male wire and connect pin 7 on the RPi to the power in on the PowerSwitch tail. Take another F-M wire and connect a ground pin on the RPi to the power out connector on the PST.
Take a F-M wire and connect pint 11 on the RPi to one of the connectors for the water sensor. Take another F-M wire and connect pin 22 on the RPi to the other connector of the water sensor.
Connect a wire from pin 2 on the RPI to the positive connector on the LED. Connect another wire from a ground pin on the RPi to the a 100k resistor that is connected to the other connector on the LED.
Mount the enclosure on the wall
Use a hammer drill to drill two holes in the wall for the screws. Ideally, the screws will have heads that fit through the larger hole on the enclosure but will not fit through the smaller hole at the top of the opening. Insert the screws into the wall most of the length of the screw. Hang the enclosure on the screws. Tighten the screws so the enclosure does not move.
Attach the acrylic to the front of the enclosure.
Connect external connections
Once the enclosure is on the wall, plug a power cord from a 120v outlet to the power input plug. Plug another power cord from the duplex outlet to the water softener. Plug a nightlight into the other outlet on the duplex outlet. Plug an ethernet connection from the local area network into the network jack. Finally, plug the leads from the water detection cord to the circuit connectors. Run the other end of the cord to the crock in the floor.
Power Up and Test
That's pretty much it. Flip the power on and give the Pi a few minutes to boot up. The nightlight should turn on when the Pi runs the HESA program.
If you installed Avahi and got the Apache web server running, you can bring up the web page that the HESA program creates when it runs.
Next: Phase II
Participated in the
Spring's Coming Contest