Problem:

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.

Solution:

  1. Device mounted inside chicken coop (Arduino and RasPi) will connect to the internet and know what time to open and close the door.
  2. Mechanical arm (servo) on the device (Arduino + RasPi) opens and closes the door at the right times.
  3. This device (RasPi) talks to a web server (CentOS Linux on local network), giving it status information and waiting for commands.
  4. 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.

flow

Circles are separate Python threads. Green arrows are lines of communication between threads, and blue arrows are network communication.


SmartHome

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.

coopener

User interface on mobile


Obligatory video showing it working