Chickens roam around the yard during the day, they instinctively go to bed in the chicken coop at night. After sunrise they come back out again. Rinse & repeat. The coop door needs to be closed every night to avoid possums from eating the chickens’ food and the foxes from eating the chickens. Some nights I forget to close the door, this is really bad. Automating the opening/closing of the chicken coop door will solve the problem.
- Device mounted inside chicken coop (Arduino and RasPi) will connect to the internet and know what time to open and close the door.
- Mechanical arm (servo) on the device (Arduino + RasPi) opens and closes the door at the right times.
- This device (RasPi) talks to a web server (CentOS Linux on local network), giving it status information and waiting for commands.
- Home owner connects to the web server to monitor status of the door (and when it is going to open/close), and can also send a command to control the door.
This is the third part of the 3 part Coopener (Chicken Coop Door Opener) series.
Click here for part 1
Click here for part 2
TL;DR download the source code from here
Standalone Python Script
Don’t let the alpha fool you, Coopener v1.1.1alpha is a fully tested and functional standalone script. This script will grab the required times from the internet or, failing that, read them from a file. Then it will open/close the door accordingly and get new times for each new day. The script runs continuously on one thread and there is no communication with SmartHome.
If this kind of simple operation is all you need, then v1.1.1alpha of both Python and Arduino code is for you.
The script is designed to be executed every 5 minutes by Cron. If the script sees it is already running it will quit. This way the script will auto-run if a failure occurs, but avoid running multiple instances of itself. The Cron configuration is below (accessed using crontab -e):
*/5 * * * * exec /usr/bin/python3 /home/pi/bin/coopener.py
The exec at the start of the crontab command avoids cron from launching the script from /bin/sh which uses two processes (PID) to run the script. Using exec makes the script run as a single process, this is important because Coopener will kill itself if it sees two processes.
Once installed check the log file (coopener.log in the same directory as the script by default) to check runtime logs. By default, there is no output direct to screen upon execution.
If all you need is the standalone script, then your journey ends here. Continue below if you plan to remotely interact with Coopener.
Complex Python Script
In addition to the functionality of the standalone script, the complex script allows you to:
- See the status of Coopener on a webpage (door state, open/close times)
- This webpage is running on a CentOS Linux server named SmartHome.
- Allow you to flip the door (open it if it’s closed, or close it if it’s open)
- Initiate a reconnection of Coopener to SmartHome.
The interface is not pretty but it works. Provided you set up the required networking around it then you should be able to access the interface from anywhere in the world and let your chickens out!
Warning: The web server is not secure and if you’re planning on accessing it outside your internal network you should do so through a VPN tunnel only.
Programming the logic for Coopener and the network communication protocol/scripts turned out to he tougher than I imagined, and as a result I had to revise the original programming plans seen in Part 2.
The new data flow looks like this. The below image represents the Python script running on the RasPi.
SmartHome is a Linux CentOS 7 server running HTTPD (Apache) in a virtual environment on my local server at home (which itself is a CentOS server running KVM hypervisor). Both Coopener and SmartHome are on the same local network. As mentioned above, I would strongly discourage allowing these two devices to talk across a public network.
You will need to install HTTPD, Python3, pip, Flask, WSGI. Setting these up is beyond the scope of this tutorial. There are other tutorials online on how to set up Flask with WSGI for this purpose. Don’t forget to open the TCP port used for incoming connections on both Coopener and SmartHome!
Once all is set up, download the SmartHome scripts from my GitHub account. They are fairly simple scripts and only build a very basic interface for Coopener. I’m not much of a front-end developer, so if you wish to improve on this code please do so.
Obligatory video showing it working
November 3, 2016 at 3:55 am
This is a great idea! I used to help our on a farm that had chickens, and they were always forgetting to close the chicken coup door at night. A couple of years ago they lost all their chickens, I don’t think it was to a fox, but it was definitely to another type of predator. I will definitely have to pass this information on to them.
November 3, 2016 at 10:31 am
Thanks! Glad to see the project helped you out. Let me now if you need any extra info.
November 5, 2016 at 4:32 pm
I love your projects! It’s simple as that… is it something you do every day? than automate it! Home automation is such an interesting topic, because everyone has other needs… As always, great article! Everything is explained well and clear. You should include videos more often 😉
November 5, 2016 at 8:51 pm
Thanks very much Sirio.
I have another (BIG) project coming up soon for the Smart Home. It’s actually quite an awesome one which I had some friends tell me not to post it up and just monetise it 😉 We’ll see…
July 30, 2018 at 1:54 pm
Thanks for this post, I just finished installing mine. With the extra GPIO on the newer RaspberryPi I was able to skip the Arduino though am still working on the web-server portion. Though I would also like to be able to add additional functionality in the future, I would think though that I could run the web-server right off the Pi? A bit of re-coding, but sounds like a great challenge. For now I just have it running in stand-alone mode… I did however add in an HMI and have run CAT5 out to the chickens for time updates (now they can also binge watch Netflix with the rest of us). I added an led indicator for both open and closed (future ability to see which state it is in from a web-cam) and a physical button to flip the state of the door, a button to stop the door from continuing to move, and a switch to turn off the door so the program keeps running but just skips the door changing state (future functionality to not open the door if it is below a certain temperature.)
Again thank you so much for putting this out there to share with others.
July 31, 2018 at 8:31 pm
Thank for the feedback John. It seems you have improved considerably on the initial design. Good idea about putting an override switch in there too.
April 11, 2020 at 6:45 am
This is exactly the functionalities I would need for my coop. I’m not that technical but willing to try a build.
I would like to use only a raspberry PI and skip the Arduino. I just need a Pin set to high to open, and a pin to trigger a close. I’m not really a programmer so question if you could help me with modifying your code to quit the arduino and get a Pin high when closing or opening needs to start.
I already have some hardware build (Motor that switches from direction (up/down) when I change polarity via 2 relais and 2 end-loop switches to cut motor power.) So the program should just trigger a GPIO pin to start the process off opening/closing the door.
Could you help me with that modification?
April 21, 2020 at 4:53 pm
Hi Johan. Using a GPIO pin on a RasPi to set hi/lo is one of the first projects newbs will do. Have a look around on google for example code. I am sure you will be able to figure out how to make it happen using some example code. Good luck