This article is part of a serie about how to spy your pet with Raspberry Pi, don’t miss the first steps !
On the previous articles, we learned how to build a Camera server and access it from anywhere. But having a camera is quite basic stuff nowadays right ? How cool would it be to trigger a food dispenser for your companion at home !
Let’s sum up the required materials that you’ll have to buy.
About motors : There are many kind of motors. The simple spinning ones are called DC motors. Then, steppers motors which can stop at certain positions. Finally, servo motors where position can generally be accurately set between 0 and 180°. (However, 360° servo motors exists aswell). For more details, check out this great summary.
Because I wanted to have the possibility to trigger different food doses for my bunny, I decided to go for a servo motor. I took the Adafruit Continuous Rotation Servo since it was perfectly suited for Raspberry Pi GPIO voltage.
Good ! We are all geared up to plug-in everything and start coding our first python script to activate the servo motor. Let’s first plug everything correctly by following this scheme.
Pretty straight forward right ? Let’s sum up
How to recognize the pins names ? Go and bookmark this page
In the next chapter, we are going to code scripts and launch many commands. Knowing that it can be tiring to switch from our computer to our Pi every time. If you would like to configure your computer so you can develop and launch commands remotely on your Pi using your favorite code editor, I strongly recommend following this great tutorial : Getting started on raspberry PI in less than 10min
Now let’s get back to our servo ! We need to build the script that is going to trigger it. While the RaspberryPi (& Raspian) run Python out-of-the-box, you need to install pip for easy installation/removal of packages. On your raspberry Pi, type :
# Install python pip package manager
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Thanks to pip, we are now able to download and use a super usefull library to manipulate by code the raspberry PI electronic pins.
pip install RPi.GPIO
Now lets create our servo script !
# Create the directory
# Create the file with execution rights
chmod +x feed.py
Everything is ready, we can start coding. What we want to do is control the servo by sending electricity pulses into the control wire of the servo. That’s the way is works. Referring to Adafruit documentation we have this :
Position "90" (1.5ms pulse) is stop, "180" (2ms pulse) is full speed forward, "0" (1ms pulse) is full speed backwards
This means that, as long as the control wire is receiving electricity pulses (with a duration of 2ms each), the servo will continuously go forward at full speed.
But.. the Pi is only capable of delivering a constant current of 3V through the GPIOs, how am I going to make pulses ?
The library RPi.GPIO that we installed earlier has everything we need ! What we want to use is called “Pulse wave modulation”. You can import it in your Python script like this.
import RPi.GPIO as GPIO
Now, let’s configure the library with our plugged servo. First, set the mode to BCM in order to have your pins matching the GPIO names and NOT the physical locations.
Next, let’s setup which pin is going under our control. If you took the BCM18 like in the picture above, then :
FEED_SERVO_CONTROL_PIN = 18
Finally, let’s initiate the pulse wave modulations to activate the servo forward movement. To do this, you’ll have to play with two variables : frequency and duty cycle in order to reach the required pulses. (1.5ms pulse is stop, 2ms pulse is forward, 1ms pulse is backwards)
Frequency is the amount of pulses there are going to be per second. It is always expressed in Hertz. Example :
Duty cycle is the fraction of one period in which a signal is active. In simple terms, the percentage of active signal during a pulse. Example :
Wait… isn’t it the exact pulses we’re looking for ? Yes !! we have our variables all set !
# Import time to manage servo activation duration
# Let's set the frequency and dutycycle
PWM_FREQUENCY = 100
FULL_SPEED_FORWARD_DC = 20
FULL_SPEED_BACKWARD_DC = 10
# Activate the servo !
pwm = GPIO.PWM(FEED_SERVO_CONTROL_PIN, PWM_FREQUENCY)
# Let it run for 3 seconds
# Let's go backward now !
# Stop and Clean everything
GPIO.cleanup() # Very important !
Ready to try out your script ? Save your code and launch it using :
# and the magic appears
Let’s move further and launch our script though a browser instead of typing command lines in the terminal. First of all, we need to have a local server on our Pi, capable of running python scripts.
# Install apache
sudo apt-get install apache2
# Make it Python friendly
sudo a2enmod cgid
By default, Apache will create a test file under /var/www/html/index.html. Copy paste your directory next to that file.
mv ~/pet-feeder /var/www/html
Finally, make Apache able to read our script inside this particular folder.
# open the file as root
sudo vim /etc/apache2/sites-enabled/000-default
Add the python cgi-script handler to it.
Finally restart apache and test everything in your computer browser directly !
# Restart Apache
sudo service apache2 reload
# Find out your Pi IP address
ip addr show wlan0 | grep inet
# Go to your computer browser
If you didn’t follow the previous tutorial, I strongly recommend you to check it out. You’ll have to do exactly the same thing ! However this time, we will change the redirection port. Previously, we were handling request from port 8081 because it is the camera stream server. Now, we also want to handle requests from port 80, simply because it is apache default port.
To do that, we simply add another Port Forwarding rule to our router. Please find all the explanations on the “Configure the port forwarding to your PI” section. At the end, that’s how it should look like.
We are actually telling our router: Please redirect all outside world request coming in with port 80 to the raspberry Pi on port 80.
Reboot your router to take account of the new port forwarding rule. and let’s find out if it works !
Just like on the previous tutorial, try this with a different connection than your home. For example, on your smartphone 4G. When being inside your local network, trying to use the DDNS may not work. This is a limitation with routers.
What about making a daily task that will give a treat to our pet every couple hours ? Does that sounds cool 🐇? Here you go !
Crontasks are instructions that you can configure to execute desired tasks (in the background) at designated times.
# Open the crontab panel
# Add your feeding task
# FEED PET EVERY DAY AT 9AM AND 6PM
0 9,18 * * * python /var/www/html/pet-feeder/feed.py >/dev/null 2>&1
The five time-and-date fields are as follows: minute (0–59), hour (0–23, 0 = midnight), day (1–31), month (1–12), weekday (0–6, 0 = Sunday).
OMG ! At last ! We finally have a way to both see and feed our little companion through a connected cage !
Why not going one step further ? In the next tutorial we will learn how to incorporate even more electronics by adding buttons on the cage. Check it out 🙌 !