This is a project that I did back in good old 2013, just found it in some archives… enjoy!
Folks got some fish for the house. Within a week, people started forgetting to feed the fish.
Lightbulb!
Today, I will share how I made my Raspberry Pi be able to feed the fish.
Let’s get started.
Difficulty Level: 3.5 / 5
Things that you will need:
- Raspberry Pi (duh)
- Ribbon cable + cobbler (optional)
- Transistor (one that can handle 3v easily)
- External power (I chose 3v, 2AA batteries)
- A DC motor (I used a 3v motor, you can choose to do higher, but make sure you have the appropriate power for it)
- A button
- Wires
- A breadboard
- Metal, axles, wheels, gears, shaft collars, screws, bolts, other misc building items (I recommend buying an electronics kit)
- A keyboard
- A mouse
- A sd card of 4gb or more
- Internet supply
- Some wood
- A drill or a wood screw
- Paper Towel tube
Step 1: The Basics
Assuming that you already know how to set up a Raspberry Pi, we are using Raspbian. If you do not, a post will be up soon about it.
Step 2: The Mechanics
So we actually need to build device, or the thing that our motor runs. Here are some pictures of my device:
Sorry for the bad quality on the last ones. But, as you can see, I just have a simple small gear to 2 big gears that move in opposite directions and a piece of wood hot glued to a paper towel tube which puts the food on the main wheel. I have a small wheel there as well, so that it “spits” out the food. NOTE: The motor I had was EXTREMELY low-torque, so I couldn’t actually use shaft collars to fasten the shafts, because it was “too much strain for the motor”, so I had to improvise and use other gears instead.
Step 3: The Wiring
Here is a little mock up of what I did…
I tried to make it as obvious as I could (red is positive, black is negative)
NOTE: You don’t need the buttons, I just used them to test, they are not in my final code
Step 4: Programming
First thing that we want to make is motor spin.
This is the code that I used:
#motor.py
import RPi.GPIO as GPIO
import timeGPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(11, GPIO.OUT)
print (“Fish Fed”)
GPIO.output(11, True)
time.sleep(0.05)GPIO.output(11, False)
time.sleep(0.5)GPIO.cleanup()
Alright, lots of things going on. In the first part, I just import some modules to help us out. I then set the GPIO.setwarnings to False, because it just got plain annoying. I set the GPIO.setmode to GPIO.BOARD because I like the way that the setup of the pins are, as well as the fact that it remains constant between both models of the RPi.
I then setup the 11 GPIO pin to GPIO.OUT, because it is an output pin, and I don’t want get input, just want to run the motor. I put in a little print(“Fish Fed”) just so I can tell if it is working or not. I turn on the current for the 11 pin for 0.05 seconds (the time in the time.sleep() is actually seconds), then shut it off for 0.5 seconds. NOTE: If you don’t shut a motor off, it will keep on running even when you quit it. Then I use the GPIO.cleanup() function to reset the GPIO pins.
It works…but how do we make it automatic?
First follow gigafide’s tutorial on how to make your RPi into a webserver then we will start making things work.
This is my index.html:
<html>
<head>
<title>Fish Feeder</title>
</head>
<body>
<div id=”timer”>
<span id=”timerUpFront”></span>
<br>
<span id=”feed”>Until the fish are fed</span>
<script type=”text/javascript”>function timer()
{
var currentTime = new Date()
var hours = currentTime.getHours()
var minutes = currentTime.getMinutes()
var seconds = currentTime.getSeconds()if (minutes < 10)
minutes = “0” + minutesif (hours < 6)
{
var hoursLeft = 5 – hours;
var minsLeft = 60 – minutes;
if(minsLeft==60)
{
minsLeft=0;
hoursLeft++;
}
var secsLeft = 60 – seconds;
if(secsLeft==60)
{
secsLeft=0;
minsLeft++;
}
}else if (hours < 18)
{
var hoursLeft = 17 – hours;
var minsLeft = 60 – minutes;
if(minsLeft==60)
{
minsLeft=0;
hoursLeft++;
}
var secsLeft = 60 – seconds;
if(secsLeft==60)
{
secsLeft=0;
minsLeft++;
}
}
else if (hours < 24)
{
var hoursLeft = 29 – hours;
var minsLeft = 60 – minutes;
if(minsLeft==60)
{
minsLeft=0;
hoursLeft++;
}
var secsLeft = 60 – seconds;
if(secsLeft==60)
{
secsLeft=0;
minsLeft++;
}
}
else if (hours == 6)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(“autoFeed.php”, true);
xmlhttp.send();
}
else if (hours == 18)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.open(“autoFeed.php”, true);
xmlhttp.send();
}
else
{
document.write(“Error, please contact admin”);
}document.getElementById(‘timerUpFront’).innerHTML= hoursLeft + ” hours ” + minsLeft + ” minutes ” + secsLeft + ” seconds”;
}var countdownTimer = setInterval(‘timer()’, 1000);
</script>
</div>
<br>
<div id=”button”>
<center><a href=”feed.php”>Feed the Fish</a></center>
</div>
<div id=”css”>
<style type=”text/css”>
#timer
{
font-family: “Comic Sans MS”, cursive, sans-serif;
margin-top: 20px;
font-size:36px;
text-align:center;
}#button
{
background-color:#008CFF;
font-family:Segoe, “Segoe UI”, “DejaVu Sans”, “Trebuchet MS”, Verdana, sans-serif;
font-size:26px;
width:160px;
padding: 10px;
margin: 2em auto;
margin-top: 10px;
}
#timerUpFront
{
color:#009DE3;
}
a
{
text-decoration:none;
color:white;
}
#feed
{
font-size:18px;
margin-top:170px;
color:#E90003;
}
</style>
</div>
</body>
</html>
Nothing to get worked over, just a basic JavaScript timer counting down to the nearest 6:00, and a manual feed the fish button. This is my code for feed.php:
<html>
<head>
<title>Fish Fed! | Gupta Technologies</title>
</head>
<body>
<h3 id=”title”>Fish Fed!</h3>
<br>
<center><a href=”index.html” id=”home”>Back to Home</a></center>
<div id=”php”>
<?php
system(‘sudo python /home/pi/Desktop/pythonPrograms/fishFeeder/motorAutoFeed.py’);
?>
</div>
<style type=”text/css”>
#title
{
color: blue;
text-align: center;
font-size: 23px;
font-family: “Comic Sans MS”, cursive, sans-serif;
}
#home
{
background-color: green;
font-size: 16px;
padding: 10px;
margin-top: 10px;
font-family: Segoe, “Segoe UI”, “DejaVu Sans”, “Trebuchet MS”, Verdana, sans-serif;
}
a
{
text-decoration: none;
color: white;
}
#php
{
color: white;
}
</style>
</body>
</html>
Also nothing too bad, just runs a system command to feed the fish using the motorAutoFeed.py (same thing as motor.py, just a bit OCD about naming :D), and just has a link back to the index.html
And here is my code for autoFeed.php:
<?php
system(‘sudo python /home/pi/Desktop/pythonPrograms/fishFeeder/motorAutoFeed.py’, $return);
?>
That thing probably took me the longest to write….jk. Really, it was a simple 3-liner, running the same code as the feed.php.
Conclusion
Hopefully this helped you, as it helped me and my folks’ not dead fish.
Here is a quick video of showing it work:
Any questions, comments, concerns…just stick them down at the bottom here, and hopefully I, or someone else, can help you out.