ให้อาหารแมวอัตโนมัติโดยใช้ Raspberry pi

Automatically feed your cat using Raspberry pi

, by Sukanya Eiampinit, 4 min reading time

Automatic cat feeder using Raspberry pi board and Raspberry pi Camera

We often go on weekends, but we also have small furry pets to take care of at home, so an automatic feeder is the answer!

We usually feed it around 7am every morning and when we get home from work at 7pm every evening.

But this feeder has some limitations. Basically, there is only 1 timer. You can only set the interval between feeds. So to feed every 12 hours, we need to set the timer to 12 hours at 7:00 am.

I want to be able to feed whenever I choose.

My plan is to use the GPIO (General Purpose Input Output) port to send a signal to the feeder, which will then 'press a button' and take control of the timing from the feeder and let me 'program' the timing, this is where the Raspberry Pi comes in.

I can also take a photo using PiCamera when the bowl opens and email it to myself. This gives me some peace of mind :)

Gather a coding plan

Mostly we want 4 things to happen.

  • Send a signal to the GPIO pin to turn on the feeder.
  • Take one or more photos.
  • Send these pictures to someone.
  • Schedule these events to occur at certain times of the day.

First we need to set up an email client. My suggestion is to create a new Gmail account. You can name it something related to your raspberry Pi, as you can use this for multiple projects if you want (but it’s up to you).


import RPi.GPIO as GPIO
import time
from picamera import PiCamera
import datetime
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
from email import encoders

These libraries are required for GPIO ports, camera, SMTP client (email) and text/image encoding to add images, as well as datetime and time so that we can add date and time to emails.

Next we will start setting up email (use your own email/password here).

SMTP_SERVER = 'smtp.gmail.com'
SMTP_PORT = 587
GMAIL_USERNAME = 'YOUREMAIL@gmail.com'
GMAIL_PASSWORD = 'YOURPASSWORD'

Then we fire up the camera and take our 'first' photo.

camera = PiCamera() camera.start_preview()
 camera.capture('/home/pi/Desktop/FeedCapPre.jpg')
 camera.stop_preview()

Initialize the GPIO port we want to use.

GPIO.setmode(GPIO.BOARD)
GPIO.setup(11,GPIO.OUT)

GPIO.BOARD' just tells the program that we are using the 'board' number and the 'GPIO.OUT' command sets the pin as an output.

Next we wait 1 second (to save the image) and set the pin to 'true', wait half a second and then test. Then we clear the GPIO port since we are done using it.

time.sleep(1)
GPIO.output(11,True)
time.sleep(0.5)
GPIO.output(11,False)
GPIO.cleanup()

Now we wait 5 seconds to give the cat time to walk up to the feeder and start eating before we take the 'after' photo. We also turn the camera off since we're done using it.

time.sleep(5)
camera.start_preview()
camera.capture('/home/pi/Desktop/FeedCapPost.jpg')
camera.stop_preview()
camera.close()

 class Emailer:
 def sendmail(self, recipient, subject, content, image1, image2):

 #Create Headers
 emailData = MIMEMultipart()
 emailData['Subject'] = subject
 emailData['To'] = recipient
 emailData['From'] = GMAIL_USERNAME

 #Attach our text data
 emailData.attach(MIMEText(content))

 #Create our Image Data from the defined image
 imageData = MIMEImage(open(image1, 'rb').read(), 'jpg')  
imageData.add_header('Content-Disposition', 'attachment; filename="image.jpg"')
 emailData.attach(imageData)#Create our Image Data from the defined image
 imageData2 = MIMEImage(open(image2, 'rb').read(), 'jpg')
 imageData2.add_header('Content-Disposition', 'attachment; filename="image.jpg"')
 emailData.attach(imageData2)

 #Connect to Gmail Server
 session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
 session.ehlo()
 session.starttls()
 session.ehlo()

 #Login to Gmail
 session.login(GMAIL_USERNAME, GMAIL_PASSWORD)

 #Send Email & Exit
 session.sendmail(GMAIL_USERNAME, recipient, emailData.as_string())
 session.quit

 sender = Emailer() 

sendTo = 'SENDTOEMAIL@gmail.com'
emailSubject = "Olly has been fed! - " + time.ctime()
emailContent = "Olly fed at: " + time.ctime()
image1 = '/home/pi/Desktop/FeedCapPre.jpg'
image2 = '/home/pi/Desktop/FeedCapPost.jpg'
sender.sendmail(sendTo, emailSubject, emailContent, image1,image2)

sudo crontab -e

30 19 * * * python /home/pi/FeedOlly.py &

To save and exit, press Ctrl+X (to exit) and Y to save changes.

_____

Full video creation here:
Part1 : https://youtu.be/JkKmJ7eeQjQ
Part2: https://youtu.be/tp6coaSNY9I

_____

Thanks for the information from

Automated cat feeder using a Raspberry Pi and Pi Camera

https://medium.com/@ian.k9.burton/automated-cat-feeder-using-a-raspberry-pi-and-pi-camera-31743f7f05da


Blog posts

© 2025 บริษัท โกลบอลโทรนิค อินเตอร์เทรด จํากัด, Powered by Shopify

  • PayPal

Login

Forgot your password?

Don't have an account yet?
Create account