24 catchen, raspberry p3 cat feeder


IoT, raspberry p3 cat feeder created due to the need that something has to feed my cats, because I live more than 2500 kms away. This is one of my most successful projects and still runs with almost no interruption.

An IoT cat feeder to replace me since I moved to Ireland

There wasn't an option to take my cats with me so I had to come with IoT solution. I had to feed my 3 cats in Bulgaria and while my parents are there, they are still 16 kms away from my house. I decided to not load them with the additional task to travel 32 kms every day to just to feed the cats. I had to come up with other solution.

Dimitar Rangelov, a smart guy with smart ideas came to help

So, there is one exceptional smart guy named Dimitar Rangelov. He won dozens of robotics competitions when he was just 17 years old and I have a great hope with him. Lucky me, we were neighbors and I am not sure who came up with the idea, but it was brilliant because I had 10 days before my flight to Ireland.

24 Cathen, IoT cat feeder was built from trash bin, pipes and smart hardware

Here is how car feeder version 1 was created:

24 catchen cat feeder iot raspberry pi

You can find also 2 mins video on my YouTube channel with brief explanation

We decided to build the body with available plastics

А trash bin was used for the body, 4 pipes to support the bin like legs, bottle from 10 liters placed inside the bin and turned bottom up and supported by styrofoam. Upside down because we could use it as cone where the food flows naturally down to a pipe with a compact disc to stop the flow when needed.

24 catchen, cat feeder iot body from trash bin

Hardware

We used one servo motor 9g was used with the CD to behave as gate for the food so we can control the flow from the pipe.

24 catchen, cat feeder servo motor 9g

One cheap web camera is attached to the bin pointing to ground so I can remotely see if the CD has opened and the food flows thought the pipes. One hc-sr04 ultrasonic sensor is positioned inside in the middle of the bottle to measure the level of the food. When the food goes bellow the middle of the bottle the sensor will measure longer distance from itself and this is how we can understand that the food is below the level so we can perform actions on time.

24 catchen, cat feeder hc sr04 ultrasonic sensor

All those are controlled by raspberry pi 3 running 24/7. The schema below shows how the pieces are connected to the raspberry pi and this all was done by Dimitar.

24 catchen, raspberrypi schema

Shown is how to connect the servo and the sensor. The servo requires longer cables. They have to be 40-45 cm long. The sensor cables are 20-30 cm long. The web camera is connected via the USB port.

Here are all IoT cat feeder parts and materials together

1 x Raspberry pi 3 kit, 1 x SD card 16 GB, 1 x servo motor 9g (3-4€),1 x Water bottle 10L (3-4€), 1 x hc-sr04 ultrasonic sensor (1-3€), 1 x normal USB camera (1-10€), 1 x trash bucket (1-5€), 2 x small pipes, 1 x pipe knee 45 degrees, 1 x CD, 2 x resistor 10Kohm, Cables, Soldering iron, Hot glue

Software that runs on the cat feeder

The raspberry pi is with installed Raspbian stretch with desktop OS. Here is the python code responsible for the rotation of the servo motor, the ultrasonic sensor and capturing picture before and after the servo let food to go through.

#catchen.py
import RPi.GPIO as GPIO
import time, logging, shutil
from datetime import datetime, timedelta

class Gate(object):
    def __init__(self):
        GPIO.setwarnings(False)
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(7, GPIO.OUT)
        self.p = GPIO.PWM(7,50)
        self.p.start(7.5)

    def open(self):
        # Gate open logic here
        self.p.ChangeDutyCycle(7.0)
        time.sleep(1)

    def close(self):
        # Gate close logic here
        self.p.ChangeDutyCycle(3.5)
        time.sleep(1)

    def proceed(self, times):
        count = 0
        while count < times:
            """open"""
            self.p.ChangeDutyCycle(7.0)
            time.sleep(1)
            """close"""
            self.p.ChangeDutyCycle(3.5)
            time.sleep(1)
            count += 1

class DistanceSensor(object):
    def __init__(self):
        self.trig = 38  # sends the signal
        self.echo = 40  # listens for the signal
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(self.echo, GPIO.IN)
        GPIO.setup(self.trig, GPIO.OUT)


    def measure_distance(self):
        """ Measure the distance per ultrasonic.  """

        GPIO.output(self.trig, True)
        time.sleep(0.00001)
        GPIO.output(self.trig, False)

        while GPIO.input(self.echo) == 0: pass

        start = time.time()  # reached when echo starts listening

        while GPIO.input(self.echo) == 1:  pass

        end = time.time() # reached when the signal arrived

        distance = ((end - start) * 34300) / 2
        # GPIO.cleanup() should i add this here

        return distance

class File:
    def copy(self, name):
        try:
            shutil.copy('/home/pi/Desktop/flask-video-streaming-master/image.jpg', '/home/pi/Desktop/catchen_pics/{0}_{1}.jpg'.format(datetime.utcnow(), name))
        except Exception as ex:
            print('Could not copy image. Error: {0}'.format(ex))


# Basic logging added to track the actions.
logging.basicConfig(filename='/home/pi/log.txt',level=logging.INFO, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')


logging.info('Starting 24 catchen job.')

try:
    gate = Gate()
    sensor = DistanceSensor()
    file = File()
    file.copy('before')
    gate.proceed(2)
    #gate.close()
    time.sleep(3)
    file.copy('after')
    logging.info('Success. Sensor: {0}, Times: {1}.'.format(sensor.measure_distance(), 3))
except Exception as ex:
    logging.error('Error: {0}.'.format(ex))
    

Then I created python flask app to make the web camera capture image every 1 second and display it via web. This will give me the ability to see almost in real-time the feeder. I store only one image where the previous is overwritten every second to save space on the SD card. No need of streaming programs, capturing new image over period works just great for me. Note the flask app has not security at all, so it is up to you to secure it. My plan is to add some security soon, but we were limited by time back then.

24 catchen source code, including 2 cron jobs and flask app

Two cron jobs are set to feed the cats two times a day

Here are the corn jobs running on the raspberry. Two for feeding the cats in the morning and afternoon and one additional for the web camera to start capturing picture every 2 seconds when the raspberry is on.


pi@raspberrypi:~ $ crontab -l
30 6 * * * /usr/bin/python /home/pi/Desktop/catchen.py
00 16 * * * /usr/bin/python /home/pi/Desktop/catchen.py
@reboot /usr/bin/python /home/pi/Desktop/flask-video-streaming-master/job.py
# @reboot /usr/bin/python /home/pi/Desktop/flask-video-streaming-master/app.py

Version 1.1, METAL disk instead of CD, the cats broke the CD

The cats broke the disk very quickly after I went to Ireland and that was issue. Seems the CD plastics are not a good fit. My dad together with Dimitar replaced the CD with METAL disk with the same shape. That did the work for some time.

Version 2, NET around the cat feeder because the cats cracked it again

So, after a few months the cats learned to push the disk with his strong heads. They could rotate the disk to let the food flow down. My parents added extra net around the legs of the feeder and you can see the is lifted on an extra aerated concrete block. Any net can do the work as long as the cats cannot directly access the 45 degrees and the METAL disk. You can also see from the picture that the aerated concrete block the feeder is on has slight modification to let the food flow in the right direction.

24 catchen secured by net

Cat feeder version 3 plan is power bank, sent SMS after successful feeding and better web site

There is another infrastructure issue for the village itself. The electricity is not always on so to prevent the outages I would have to upgrade it with proper power bank. For your information not all power banks can work as UPS. I also plan update on the site being moved from basic flask site to Django site with support to send SMSs on successful feed.

Conclusion

The "24 CATchen" was created 7th of January 2017 for a few hours of work on a cost close to one raspberry pi 3. It still keeps 3 cats happy. The cats are smart enough to crack your IoT feeder so be prepared to upgrade it after they break through.

24 catchen, happy cats
Posted on

Tags: IoT, raspberry p3, python, cat feeder

Comments