Introduction: Automated Chicken Coop
This will guide you through the basics of automating a chicken coop. I'm leaving out some of the specifics as far as door design and gearing, assuming that everyone's coop will be a little bit different. This will cover the sensors and Raspberry Pi front end. You'll need a few things:
- A Raspberry Pi. I used a B+, so the pinout is specific to it, but you can adapt
- A temperature sensor module: https://www.adafruit.com/products/1782
- Two infrared digital proximity sensors: https://www.adafruit.com/products/1927
- A motor board controller: https://www.adafruit.com/products/1687
- A breadboard
- Breadboard jumper cables, both male and female
- A suitable motor for your door: a low speed, geared, brushed motor is probably most appropriate
- A chicken coop
- Chickens (probably)
Step 1: Wire the Motor Controller Shield
Wire the motor controller as shown.
- Attach the power supply.
- Attach the motor to one of the outputs. Make a note of which GPIO pins correspond to that motor output, as you may have to modify the code to make it work.
- Mount the motor controller to the GPIO header.
Step 2: Piece Together the Circuits
Assemble the sensors and Pi board as illustrated. The important parts are:
- The output pin for each IR sensor goes to an input pin
- The SDA and SCL pins on the temperature sensor go to their respective inputs. These pins are connected to the I2C bus of the Pi. The board I used isn't identical to the one in the Fritzing illustration, but it serves.
Note that the motor controller board isn't included in this schematic. If you use the same one referenced in the intro, it should sit directly on top of the Pi and you can use the power and I2C pins as shown.
Step 3: Enable the I2C Bus
Install an OS (I used Raspbian) and either set up SSH access for your Pi or hook up a screen and keyboard. We won't cover that in depth here, but there are ample tutorials online. There are a couple small configurations we need to handle first.
- Enable I2C: With Raspian, this is as easy as turning it on with the raspi-config tool. Type 'sudo raspi-config' and then select Advanced > I2C. Confirm the prompts.
- Edit the modules file. Type 'sudo nano /etc/modules' and add the following, one each per line: i2c-bcm2708 i2c-dev
- Install some packages: 'sudo apt-get install -y python-smbus i2c-tools'
- Reboot your Pi.
- To make sure I2C is working, type 'sudo i2cdetect -y 1' - You should see a grid of available I2C bus addresses with an address enabled
Step 4: Install the Temperature Sensor Libraries
Next we'll grab the Adafruit libraries for our temperature sensor.
- Get all the dependencies: sudo apt-get install build-essential python-dev python-pip python-smbus git
- Clone the sensor library: git clone https://github.com/adafruit/Adafruit_Python_MCP98...
- Open the repository directory and install the package: sudo python setup.py install
Step 5: Installing the RPIO Module
The RPIO module extends RPi.GPIO with a few cool features. We utilize the GPIO interrupts to be quickly notified each time a proximity sensor is activated. One important thing to note is the support for threaded callback functions. This way, we can run callbacks concurrently (in case one sensor changes state while in the other’s callback function). This is distinguishable from the RPi.GPIO’s add_event_detect(...) because of support for multiple threads, rather than a single thread for all callbacks.
There is no inherent support for the model B+, but it’s easy to workaround. Simple install CorCornelisse’s modified version, and you’re ready to go! Just follow these steps:
$ git clone https://github.com/CorCornelisse/RPIO.git
$ cd RPIO
$ sudo python setup.py install
Otherwise, you can install the normal version with:
$ sudo apt-get install python-setuptools
$ sudo easy_install -U RPIO
Be sure to check out the documentation at: http://pythonhosted.org/RPIO/. Note that you can use everything from the RPi.GPIO module by replacing "GPIO" with "RPIO".
Step 6: Set Up the Database Backend
The database is the layer between the script that manages the sensors and the web interface.
First, you will need to make sure you have the proper sqlite dev libraries: 'sudo apt-get install sqlite3 libsqlite3-dev'. Then, install the pysqlite Python library. I find pip to be the easiest tool: 'sudo pip install pysqlite'
Create your database: 'sqlite3 coop.db'. Now you are in the sqlite command line interface. Run 'create table coop_data(id integer primary key, count tinyint(2), temp float(4,3), door tinyint(1));' to create your main data table. Run '.tables' to list tables and verify that it exists. '.exit' quits the sqlite CLI.
Step 7: Run Your Code
Install the Flask framework. If you have pip installed, 'sudo pip install flask.'
You can obtain a complete copy of my code at GitHub: https://github.com/llsmith05/chicken-coop. This code should work if you have wired things up the same way as I illustrated in step 1. If you used different GPIO pins, adjust the pin variables accordingly.
The web server and the sensor script run separately. There are several ways you can multitask in Linux, but I generally just use screen (sudo apt-get install screen - it wasn't on my base Raspbian install). Type 'screen' to switch to a new terminal screen, then 'python web.py' to start the web server. Hit ctrl+a to enter screen command mode, then hit d to detach from the screen. You're now back in your base terminal, but you can use 'screen -r' to return to the server screen any time. Now run 'sudo python coop.py' to start the main sensor script. The sudo is important here, since the script needs root access to read the i2c bus. This is definitely fixable, but I can't be bothered at the moment. You should see some debug text. The coop is running!
Step 8: Mount Your Setup on Your Coop
You have a lot of latitude here, depending on the final form of your hardware and the configuration of your coop. I haven't yet transitioned from breadboard to a permanent PCB, but I will then likely build a permanent enclosure for the Pi, PCB, temperature sensor and IR sensors. The most important part is that you attach one IR sensor inside and one outside the coop.
As a prototype, I have a simple motor mounted to a foamcore door to demonstrate the motor firing and opening/closing the door. A final version would likely be mounted to a vertical sliding door with a locking mechanism to support it when opened.