Story of automated fish feeder with RaspberryPi
Tags: python, beginners, iot, codenewbie
Published: 2022-02-01 10:01:25

All started 2 years ago, when a colleague got a Beta fish and brought it to the office. By that day all of us started to learn how to care about aquarium. But our office building is closed every Christmas for almost 2 weeks. That might be too much for the fish without food.

---

> 🇨🇿 V češtině si lze článek přečíst na [kutac.cz](https://www.kutac.cz/co-na-srdci-to-na-blogu/automaticke-krmitko-do-akvaria-s-raspberrypi) 

Before Christmas 2020 we used our smaller tank and moved the fish to a colleague's flat. But during 2021 that tank became an [Aquarium PC](https://www.profiq.com/linux-aquarium-pc/). So there was no spare tank to move the fish again. Automated fish feeder is must have. But the existing ones are either expensive or not compatible with our aquarium.

> For more about side projects in our company, visit [profiq.com/blog](https://www.profiq.com/blog/)

## 3D printed solution

On [ThingiVerse.com](https://www.thingiverse.com/thing:2089866) we have found ideal solution. First, we had to do a small modification to properly attach stepper motor. The first prototype actually worked! But in the tank, we have also snail, not just fish. And the snail’s food was bigger than the bowl in the feeder. So we needed to do the next iteration and print the inner part again with bigger bowls. 

![Detail of feeder with bowls and stepper motor](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r1w3a2l6szlv3o0ko98f.jpg)

## Android Things vs RaspberryPi

I got an Android Things kit during Google Developer Days back in 2017 in Krakow, Poland. And I thought I could use it for feeder. However, Google stopped this project meanwhile. I thought it means, that only new features are not coming. But they removed also all images required to make the kit alive. I spent the whole weekend trying to find another solution to make it work, but unsuccessfully. So with colleagues, we decided to use the RaspberryPi which was unused in our office.

After we prepared RaspberryPi and we were able to control LED, we realized what will be the next challenge. The stepper motor driver needs input voltage in the range 8.2 - 45V and a logic voltage between 2.5V and 5.25V. But RaspberryPi needs 5V and has 3.3V GPIO pins. And we had an old laptop power adapter that produce 19.5V. The decision was made. We will use only 1 power supply and the feeder will contain also DC/DC step-down module to power Raspberry.

![Schema of electrical circuit](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5lryjw2fad8j373i2ry6.png)

## CRON with Python script and monitoring

RaspberryOS comes with some nice integration into Python, so controlling GPIO (General-purpose Inputs Outputs) is super easy. In some situations, I could call myself a control freak. I wanted to be sure, it will be working flawlessly. So besides some basic logging into the local file, I set up also monitoring with service Healthchecks.io. I was [writing about it earlier](https://dev.to/arxeiss/cron-monitoring-for-better-sleep-3o4g).And for the worst case, we assigned a static IP address and set up port-forwarding to have access with SSH into the Raspberry.

The python script itself was executed regularly every hour by Linux Cron. And the last feeding time was controlled programmatically. So even during power strike, fish and snail would be fed after that. You can find the full script in our [public Gitlab repository](https://gitlab.com/profiq/public/fish-feeder).


![Descriptions of parts on the board](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3a11uijn3muvaqwrz1e0.jpg)

## Visual feedback with non-stop stream to YouTube

Access with SSH, logging, and monitoring might check the software behavior. But as the feeder is a piece of hardware, it is much harder to monitor. So we decided to do a stream with a web camera on YouTube. We have used ffmpeg tool and the prepared script can be found again in our [public Gitlab repository](https://gitlab.com/profiq/public/fish-feeder).

Everyones know, the developers are lazy. It is not a big surprise we kept everything to the last days. So we weren’t able to optimize streaming enough. The stream was in resolution 640x480px and 15 FPS and it still uses almost 95% of CPU time. So for safety reasons, we used another RapsberryPi just for streaming. If it would not survive, it wouldn’t affect our feeder.

## Oh yes, we succeeded. But what about next year's improvements?

This article would be most likely private if the fish or the snail would die. And because you are reading it, they survived. Yaay! And we also have some baby snails. Yaay again! But there is still space for some improvements for Christmas 2022.

Food for the snail is a little bit bigger and sometimes it is stuck with fish food. So nothing or just a small portion fell into the water. It would be helpful to also control the direction of moving and do some shaking when food should fall down. And for sure we need to find a better streaming setting that would allow us to watch the stream in better quality.