Enclosure monitoring and automation using a Raspberry Pi

sghctoma

New Member
Joined
Mar 14, 2018
Messages
17
Location (City and/or State)
Hungary
I have had my three beautiful pancake tortoises for a little over a week now, and their enclosure is monitored and controlled with a Raspberry Pi Zero and a Python program I wrote.

terrapi-connected.jpg

In this post I'm going to describe my setup, so maybe I can help others creating their own enclosure automation. Please note that this is not a ready made solution, you have to get your hands dirty, and get your soldering iron ready! The code is also not production ready yet, you need to know your way around the Pi.

My setup currently includes two temperature and humidity sensors (warm side, cool side) that are read every 5 minutes and measurements are stored in a PostgreSQL database. The UVB bulb and daylight LEDs are also controlled by the program. Basking light is currently manually controlled (I want a separate relay box for 220V AC), and - although configured - the thermostat is not used, because temperatures are high enough without a CHE this time of year.

These are the components I've used:
And the equipment:
  • Soldering iron
  • Multimeter
  • Heat gun (or a lighter, but I didn't say that! :) ) to shrink the tubes
  • Wire cutter
  • Phillips-head screwdriver
  • Electric drill
Setting up the Pi

Currently I'm running Raspbian Lite, but any Raspberry compatible OS should suffice. The program I wrote is called TerraPi, and is open sourced under the MIT licence. I've almost named it TerraPin, but didn't want to make my pancakes jealous :) You can get the program from my GitHub page: https://github.com/sghctoma/terrapi. I will repeat here a warning from the README though:

Please note that this is by no means "production ready"! I am using it to control my enclosure, but it's still experimental. There is no proper error handling, no config validation, no installer, etc.

That said, it's been running for two weeks now without any problems. If you want something more thoroughly tested, and used for years though, you should give TerrariumPi a try! It also has much more functionality. This begs the question: why did I write my own? I wanted something simpler that I can easily maintain and extend, plus I could learn about interfacing the Pi with sensors, etc. this way.

The installation instructions are a bit vague, that is what I meant by writing "you need to know your way around the Pi" earlier. I am working on an installer, and a step-by-step guide, but it takes time. The README contains some useful information, and of course I am happy to answer any questions if you decide to give this a try.

ZooMed lamp modifications

I bought this specific lamp because it combines a T5 HO UVB bulb with some 6500K and red LEDs, and it seemed really easily serviceable from the user guide and videos I've seen. My plan was to de-solder the switches, and replace them with the relay board. As it turned out, I didn't even need to unsolder anything, since the switches use cable shoes, and the LED panels are connected with screw terminals.

zoomed-original_1.jpg

The lamp has two two-way, and one three-way switches. One of the two-ways control the UVB bulb, and the other is for the blue LEDs. I don't want to use the latter, so did nothing with that switch. The three-way switch controls the 6500K and red LED: it's either off, 6500K only, or both red and 6500K. I had to make two decisions here:
  • Do I want to use the red and white LEDs separately?
  • Do I want to be able to use the physical switches to turn on the light independently from the Pi?
I decided against separate use, since the plants in the enclosure will need those reds anyways. I also opted against being able to use the physical switches. This allowed me a very simple wiring by connecting the relays between screw terminals and the switches in series. This means I can cut the power using the switches, but won't be able to turn on the light without the Pi.

These are the steps I had to take:
  1. unscrew the cable going from the switch to the terminal,
  2. solder a banana socket on the unscrewed cable,
  3. screw a cable with a banana socket into the terminal.
Here is the modified wiring:

zoomed-bananaplugs_2.jpg

Please note that in this picture there is wiring for both the 6500K only, and the 6500K+red options. Since then I have removed the former, and there is only one red wire in the screw terminal (forgot to take a picture).

For the UVB light, I had to disconnect the cable shoes from the switch, and connect banana sockets to it. The picture was taken before applying heat shrink, so don't worry, I did not put those exposed wires back into the housing.

zoomed-uv-bananaplugs.jpg

I ditched the UVB switch, and used the hole to make the banana plugs accessible.

zoomed--bananaplugs-final.jpg

That's it, the lamp is ready to be Pi-controlled. One side note though: I am planning to add support for the Energenie programmable power strips to TerraPi. Since the LED and UVB parts are on separate mains connectors, it will be possible to control the lights without these modifications. This is cheaper though.

The relay box

Not much to say here, I simply used some servo cables to connect the board to the Pi, and screwed the cables with the male banana plugs into the terminals. Than I drilled holes in the plastic box for the screws and cables, and put the whole thing together with nylon spacers and screws:

relay-box.jpg

This is how it's connected to the Pi (GPIO pins 6, 13, 19 and 26 are used to control relay #1, #2, #3 and #4 respectively):

relay-board-connected.jpg

Sensors

Initially I tried to use the DHT22 sensor to measure temperature and humidity, bought two of them, put them on a breadboard, and started collecting data.

dht22-breadboard.jpg

Temperature values were good, but humidity was way off: one of the sensors showed 17%, the other 27%, where the real value should have been around 50%. The salt-water calibration resulted in readings around 40% with the first sensor instead of the expected 75%. I have found out about the BME280 sensor after this disappointment, bought two of them too. Both showed 75% during calibration, so I started using them.

The chip is able to talk on two buses: SPI or I2C. The SPI port on the Raspberry is needed for an MCP3008 analog-digital converter, therefore I did not have to make a decision there :) I2C is a two-wire bus, which means 4 wires per chip (2 for I2C, plus Ground and 3.3V). I bought a couple of 2 meters long 4-pin 3.5 Jack male-female cables to use for this purpose. I snipped them near the female end, and first soldered the chip to the longer cable with the plug:

bme280.jpg

Than I soldered pin headers to the short cable with the socket. I2C devices need to be connected in parallel, if you want more of them on the same bus, so I have actually soldered two Jack sockets to the same pin headers. I have no picture of just this Y-cable, but you can see it in the first picture in this post.

I have not yet found a proper housing for these sensors, so right now they are attached to the side of the enclosure well out of tortoise reach. Of course this means that measured values are not the same as the ones the tortoises experience at ground level, but it is good enough for now. I'm planning to buy some plastic tubing, drill holes in it (so that humidity measurements can be accurate), and put the sensor inside.

Right now these are the only sensors I use, but I also have some UV sensors (GUVA-S12SD and ML8511) , and plan to get a weight sensor, so I don't have to write down every weight measurements. Not that it's too much work, I just like to build and try new stuff. I also have a camera under order for the Pi, so I can motion track my pancakes using OpenCV. (And have some other wild ideas, too. E.g. I have some stepper motors lying around here, so maybe there will be a Pi-controlled basking lamp height-adjustment someday. Which is totally unnecessary, but cool :) )

guva1918.jpg

Putting it all together

After every component was ready, I just had to connect the relay board and the I2C Y-cable to the GPIO pins, connect the lamp's banana plugs to the sockets on the relay board, add power to the Pi via a USB cable, and start TerraPi.

The Pi is attached to the relay box, and the relay box to the ZooMed lamp with Velcro:

pizero-box-velcro.jpg

I also have a LAN cable connected, so that I can access the Pi via SSH, and use the dashboard.py script included with TerraPi that draws graphs from collected measurements:

dashboard.png

I think that's all for now. This got rather lenghty, but I hope it was, or will someday be useful for somebody. Thanks for reading!
 

jakskillz

Member
Joined
Aug 8, 2017
Messages
72
Location (City and/or State)
Georgia
This is awesome! I ordered a RPi 3 this weekend and sensors to do this same thing but to control 6 units in a cabinet. My friend is helping me understand how to code and wire it up. Let me know if there are any tips to get this going. My friend is a great programmer but hasn’t done anything reptile related so if there’s anything we should plan for that’d be great :)

Also the reason why I chose this route is that the price for a decent multi thermostat is ridiculous and it’s usually some bulky device that wouldn’t look great on the cabinet I built.
 

RumWeatherReptiles

Active Member
5 Year Member
Joined
Apr 9, 2018
Messages
146
Location (City and/or State)
New Jersey
This is awesome! I ordered a RPi 3 this weekend and sensors to do this same thing but to control 6 units in a cabinet. My friend is helping me understand how to code and wire it up. Let me know if there are any tips to get this going. My friend is a great programmer but hasn’t done anything reptile related so if there’s anything we should plan for that’d be great :)

Also the reason why I chose this route is that the price for a decent multi thermostat is ridiculous and it’s usually some bulky device that wouldn’t look great on the cabinet I built.

Well done. Looks like instructions to build a battle bot.
 

Jay Bagley

Well-Known Member
Joined
Oct 11, 2017
Messages
1,481
Location (City and/or State)
Michigan
Wow!! I was excited when I started running my lights on digital timers from Walmart. Your setup is most impressive, nicely done.
 

Salspi

Active Member
Joined
Feb 1, 2017
Messages
339
Location (City and/or State)
Chicago
If you can figure this out and bring a quality product to market, there’s a ton of money to be made.... Look up Neptune controller as an example of how this technology is transforming marine aquarium keeping
 

sghctoma

New Member
Joined
Mar 14, 2018
Messages
17
Location (City and/or State)
Hungary
This is awesome! I ordered a RPi 3 this weekend and sensors to do this same thing but to control 6 units in a cabinet. My friend is helping me understand how to code and wire it up. Let me know if there are any tips to get this going. My friend is a great programmer but hasn’t done anything reptile related so if there’s anything we should plan for that’d be great :)

Also the reason why I chose this route is that the price for a decent multi thermostat is ridiculous and it’s usually some bulky device that wouldn’t look great on the cabinet I built.

Cool, good luck with your project! I don't really have any reptile-specific advice, building a thermostat with a Pi is pretty straightforward. The thermostat module in TerraPi can handle different temperature ranges for different time ranges (with 1-hour resolution), which I think is a handy feature. I have plans to add multi-sensor capability (e.g. heat turns on based on measurement from cool side, and turns off based on the warm side) - this may also be something worth thinking about while planning your setup.

What kind of sensors do you plan to use, and how do you plan to control the heat bulbs/CHEs? I would recommend using the Energenie power switches for control; there are USB and LAN controlled versions, and you don't have to mod your bulb fixtures. Also, what programming language will you implement this in? I like Python for prototyping, but I'm thinking about reimplementing this in Go (mainly because I want to learn Go, and this would be a nice starting point :) ).
 

sghctoma

New Member
Joined
Mar 14, 2018
Messages
17
Location (City and/or State)
Hungary
If you can figure this out and bring a quality product to market, there’s a ton of money to be made.... Look up Neptune controller as an example of how this technology is transforming marine aquarium keeping

Yeah, I know these ready made enclosure-automation systems can be quite pricey, but I don't want to make money of this. I also wouldn't have the time to do this beside my day job.
 
Top