Chicken Coop Automation

I started out with a linear actuator to open/close the coop door. It is wired to a couple relays that are controlled by an Arduino microcontroller with a toggle switch to send it the signal to open or close the door. After getting that to work, I added a couple temperature sensors to measure the temperature inside and outside the coop. I then added a 2 line by 16 character liquid crystal display to display the temperatures. I then added another relay wired to an outlet which I have a heating lamp plugged in to. I programmed the Arduino to turn on the power to the outlet when the outside temperature gets below 25 degrees F to turn on the heat lamp and keep the chickens warm.

Side Note: (Arduino’s are amazing little devices. They give the common nerd an easy introduction to programming and designing with microcontrollers. They have built a great platform around it and there are thousands of libraries you can install with preprogramming to make it extremely easy to add devices to it. Like the temperature sensors and the display, add the libraries, wire them up and with a few lines of C+ programming code you are off to the races. Where as microcontrollers like the popular Microchip PIC series are feature rich but can be quite intimidating to get working. Each feature has control registers that you have to setup plus a bunch of configuration bits that you have to set. I spent many an hour trying to just get an output to toggle an LED. To Microchips credit though, they have updated their software (MPLAB X-IDE) and have a built in Code Configurator that lets you select features and it configures the bits for you. The Arduino IDE does all this in the background and you never have any exposure to the actual internal workings of the controllers.)

After I got all that working, I did a lot of research on the web on home automation with Arduinos and decided to add a Raspberry PI (RPI) single board computer to the mix. The RPI has an ethernet connection, blue tooth and multiple USB ports. It has a built in web server as well. I thought I’d build a webpage on it so I could open and close the chicken coop from that instead of going out to the coop to do it.

Venturing into the RPI world, I had to learn a new operating system (Linux). There is tons on info on the web. I ran into lots of problems along the way, but any problem I had someone else also had it and posted the problem where someone else was kind enough to help out. Bottom line is with a well worded search you can find an answer on the web to just about any problem you run into. I also had to learn a bit about HTML to make a webpage and the language Python which works well on the RPI. Also had to load the Apache web server software on the RPI and setup. Again, how to do all this can be found on many different places on the world wide web. I connected the RPI to my router through its wifi connection. Went into the router setup and reserved that IP address for the RPI so that the address will always be the same. After a power outage or router reset, the router assigns addresses to each device as they try to connect to it so they can be different each time.

I added a 915 mhz transmitter to the chicken coop Arduino and bought a Moteino (from LowPowerLabs) which has a built in 915 mhz transmitter. I bought a FTDI adapter (serial to USB) which is plugged into the Moteino. I have a USB cable connected to the FTDI board and into the RPI. I wish I had found the Moteinos to start out with because it was rather difficult to solder and wire up the little transmitter to the Arduino. LowPowerLab.com has a ton of great info on working with the transmitters. They have built an easy to use library with lots of example code to get you started. The owner Felix will even reply to emails on his products.

Once I got the two controllers talking and passing info, I had to figure out how to get that into the webpage. There are lots of gateway software packages you can buy, but I wanted to figure it out on my own and keep it as simple as I could. I experimented with writing some code in C+ but had problems with it connecting to the USB port consistently. Ended up with some Python code running in the background on the RPI that opens the USB port and continually monitors data coming in. Once that was working I had to figure out how to store the data that was received by the coop controller.

Next step was loading a database system on the RPI. I went with MariaDB which is a free version of MySQL. This resulted in having to learn SQL programming. Luckily I had some experience with SQL in a previous job so it wasn’t to hard get it working. I built a couple tables in the database to store the data received. I then had to setup a connection to the database in the Python program so it could push the received data into the database.

Now to get the data from the database to the webpage. This led to learning another language PHP. PHP is an open source powerful server-side scripting language which is used for creating dynamic webpages. Adding some PHP scripting to my HTML webpage lets you setup a connection to the database and query out the data you want then display it on the webpage.

With that all done, I could open the webpage from my computer by just opening a new tab in my browser and typing in the ip address assigned to the RPI (198.162.44.88). I could now see the info I programmed into the the webpage sent over from the coop Arduino. Now I wanted to be able to see it from any where I could access the internet. So I got bought a domain name from NameCheap.com. Costs about $4 a year to have your own domain name. Once you have that all setup, you have to tell them where your web server is located which is finding the IP address assigned to your router from your ISP (internet service provider). With that all complete, I could now see my webpage from anywhere with internet access.

The next problem that arose was when the router reset after a power outage, the IP address would change. You can purchase a set IP from you ISP but it was way to unreasonable for me. After searching the web once again, I found a script you can run on the RPI to retrieve your routers IP address (wget -q -O – checkip.dyndns.org|sed -e ‘s/.Current IP Address: //’ -e ‘s/<.$//’). And another script that would push that address to your Domain name server. I put this code into another Python program running in the background and have it executing every hour. Probably easier and better ways to do this but being a novice at all this, this is what works for me.

Being your typical lazy American, the next idea I had was to add more code so that I could open and close the chicken coop door from my webpage (www.beservices.us). That required another table added to the database to hold any requests from the webpage. Added more PHP scripting to have buttons on my webpage to open/close the coop door. If one of the buttons is clicked, it then writes data to the “Request” table in the database. (I actually added another webpage that is password protected so random people are messing with the chickens). Code was added to the Python script monitoring the USB port to also monitor that table. If a request is seen in the table, it sends that data out the serial port to the Moteino. Added code to the Moteino to send any data it receives on the serial port to the coop Arduino. Added code to the coop Arduino to open or close the door depending on the data received from the Moteino.

With that all working, I thought why not just have the RPI send a door open signal each morning and a close door signal each evening since the RPI keeps the actual time and date updated from the web. I added a couple data entry boxes on the coop webpage to enter in the time in the morning and evening to open and close the door which is also stored to the database. Updated the Python code to check the time and if it was equal to the open or close time, then send the signal to the coop controller to getter done. I also added an Enable/Disable button so if we don’t want it to automatically send the signals we could disable it from the webpage.

I learned a lot doing this project and it has been running flawlessly for more than 2 years. Only glitch I’ve had was a brief brownout that locked up the RPI. I have a small battery backup UPS (sold by LowpowerLab.com) but it didn’t catch that quick brownout. Works great on actual power outages though.

If anyone wants more info on our Chicken Coop Automation project, drop us an email and we’ll post some schematics and code snippets.

Leave a Reply

Your email address will not be published. Required fields are marked *