Introduction: Pet Feeder Machine With RasPi and Telegram Bot
First of all I need to clarify that this is not an original Idea Mine, just update and adapt the programming scripts to work with telegram, I found it in a previous Instructable so the credits really are the author of it.
You could see the spanish version of this in my personal Blog:
You need to build a small circuit that activates a high torque motor,
and since my knowledge of electronics is very, very scarce, I had to spend a couple of days learning the basics by watching videos on YouTube.
The original automation script is written in Python and uses a GMail connection to consult the commands, I had never used this programming language but the truth is not so different from the others, I have modified it a bit so that it adapts better to the new ones Python libraries and the automation process together with the operating system configurations allow you to parameterize the following:
- Interaction through commands sent to Chat controlled through ChatBots.
- It allows to track the status of the food dispensed.
- Controls how much food is going to be dispensed.
- It has buttons that allow you to feed manually.
- It does not allow overfeeding by disabling the feeding at 8-hour intervals.
- It has a status LCD that shows the data as the date and time of the next time for the next power supply, the SSID to which it is connected to the WiFi and the IP address of the device.
- Optional: Shows some Chuck Norris Random Jokes and / or Trivia of Numbers using a pair of public APIS (Ingenious by the original author).
- The System is immune to restarts due to power cuts as it saves a status file.
- The System Detects when there is a connection loss and tries to re-connect until it is successful.
Step 1: Used Materials
- 1 Raperry Pi, preferably the version 3 that is already integrated with the Wireless card, you can also use the Pi 3 Zero version, it takes a little more work because you have to solder the pins, but I found out very late that it could be use one of these that are more economical.
- 1 high torque electric motor, 37mm, 3.5rpm and 12V.
- 1 Braket for 37mm motor.
- 1 character LCD screen of 20x4 lines with HD44780 controlle.
- 1 Cereal Dispenser Zevro Model WM1001 shaft-D of 5 inches long, 0.63cm in diameter (D cut is necessary to attach it to the dispenser)
- 1 Shaft coupler from 1/4 "to 6mm to join the to the motor.
Allthe previous materials except for the last two I could not get them in my country (or at least I did not know where to get them), however the rod and the coupler are probably in a welding workshop but being the first time I do something like that, I had no idea how it should be to fit the engine so I asked the page of the links described above; Below the materials that I could buy in local stores:
- 1 wooden box, I use a 20.3 cm wide × 26.7 cm high x 13 cm deep. the box has a door that opens to the right with a hole of 10cm x 4cm to place the LCD screen (local joinery)
- 3 Push buttons
- 1 Small Protoboard
- 1 LED of 3.3 Volts (It does not matter the color but for the voltage they are usually Red)
- 1 NPN PN2222 transistor
- 1 Resistance of 270 Ω
- 1 Resistance of 10 KΩ
- 1 Potentiometer of 10 KΩ
- 1 IN4003 diode (IN4001 or IN4004 can operate)
- 1 12V 3A adapter
- 1 5V 2A adapter
- 4 screws with wing nut from 1.5 inches to 2 inches (depends on the thickness of the wood used, they are to fix the motor bracket to the wooden box)
- Cables Jumper strands of various colors
- 1 folding tube or a set of PVC of 4 Inches Diameter, this depends on the height at which the wooden box will be placed.
- 1 PVC elbow that attaches to the previous cast.
- 3 clamps for car pipes (The World)
- 4 Feet of UTP Cable (we need the twisted pairs to make the connections)
- 1 USB WebCam, it is not necessary to be high resolution.
Some miscellaneous materials that can be obtained in hardware stores or possibly we already have:
- Tin welder
- Drill 5/16
- S8 screws with expanders
Step 2: Placing the Structure
The main idea is to replace the dispensing handle that comes in the ZEVRO by the rod D which will then be connected to the Motor by means of the Coupler. The dispenser will be attached to the wooden box and the wooden box to the wall. As I do not know much electronics I did not use any Bakelite plate to mount the circuit so I used the breadboard to put all the components so at the bottom of the box is fastened with screws the Rapberry Pi and the breadboard which already had a adhesive on the back so I just went to paste it. The box must have three buttons which will have the function of Restart the timer, activate the feeder and the last one will be a direct step to activate the motor without going through the circuit. The motor will be held inside the box by means of the bracket, so only the rod D that connects to the dispenser will protrude from the box, in the bottom of the box fix and adjust the webcam so that in each mail consultation and confirmation I sent a photo of how is the plate, this to not over feed in case they have not finished the last round of food.
From the bottom of the dispenser, the tube that goes down to the plate was placed and the PVC elbow was placed on the base, I placed a little Industrial Tape in the outlet to minimize the speed of the food flow and made a dispensing base for prevent food from spraying everywhere. To fix the pipe to the wall, use metal brackets that you fix to the wall with the S8 stud screws.
On the door of the box hold the plate of the LCD screen and use the twisted pairs of the UTP cable to take them directly to the Raspberry, at the other end of the cables splice the female tips of the jumpers to make easier the connection in the GPIO ports of the Raspberry. This would be the circuit diagram. I will try to explain it with what little I can get from Electronics.
The motor is connected directly to the positive pole of the 12 volt transformer but for the current to flow it must pass through the circuit in the neutral pole of the motor, for this transistor N2222 is used. The transistors normally have 3 legs which correspond to a collector, a base and an emitter, depending on the model of the transistor the location of these legs may vary; This transistor makes the switch function. This is where we connect the neutral pole of the motor to the collector of the transistor, pin # 19 of the rapberry is connected to the base by means of a resistance of 270Ω and the emitter is connected to the neutral terminal of the transistor. 12V transformer along with one of the earth poles of the raspberry; the transistor will let the current flow between collector and emitter as long as the base is stimulated with sufficient voltage; later we will program the rapberry so that according to certain orders port 19 will emit 3.3 volts, enough for the circuit to continue and activate the motor.
For the LCD screen, another part of the protobard is used in a different circuit where you connect the 10KΩ potentiometer which rejects the contrast of the text that appears on the LCD, so if nothing appears on the screen it is probably because the potentiometer is totally closed; in my case I leave it completely open so that the text is better visualized. In the end the connections in the breadboard would be as follows.
Step 3: Raspberry Pi Configuration
First of all it is important to create the Telegram Bot that the system will be managed, You'll need a key to modify the attached python script. Please read the steps on the telegram Documentation:
To configure the rapberry, use the Lite version of the Rapbian (we do not need the graphic interface) and apply the basic configuration that shows the raspbian once it is installed or using the raspi-config command: expand the space to 100% and enable the SSH but especially the most important thing is to change the password and username to the user pi that comes by default (Otherwise, as happened to me, they can suffer an attack by port 22 if they have a public IP addressed to the raspberry) . Apart from that the configuration to connect automatically to my WiFi network (Assuming my network is called "BlogSoriano" and my password is "$ecure123!") We generate and save the key in the Wireless connections file with the following command:
sudo wpa_passphrase "BlogSoriano" "$ecure123!" | sudo tee -a /etc/wpa_supplicant/wpa_supplicant.conf > /dev/null
the next thing is to update and install the python installation system "pip" so we will log in as root, update and install the necessary packages:
sudo -i apt-get update apt-get install build-essential python-dev python-smbus python-pip
With this we already have access to the python class library, for the script that we are going to use, we need to install the following:
pip install RPi.GPIO Adafruit-CharLCD httplib2 html2text netifaces wireless telepot cv2
Once the installation of dependencies is finished, we will use the script attached to this step, I save it in the path /opt/petfeeder.py and the file can be created using the command nano /opt/petfeeder.py and inside this file we paste the code or, if you have more Linux experience, you can download directly with wget. In the script it is important to modify the variables BOTKEY (in line 36) and SYSPASSWORD (in line 23).
I clarify that this code was not written by me since I had never programmed in Python, I only modified it to work with the new libraries and with the 20x4 LCD screen and to use telegram Bot.
Once we have the Script, we must change the values of the variables with the key for the bot; for
this it is necessary to create a new Telegram Bot, with that it would only be save the file and exit (with Ctrl + or save and ctrl + x leaves the editor nano), we just need to be proof of restarts; the script itself saves the last time the power system is activated, so we only have to make the script run every time the operating system starts, for this I found a very particular solution with a program called supervisor, which is installed from the Debian repositories:
apt-get install supervisor
and once installed, we only need to create a configuration file in /etc/supervisor/conf.d/petfeeder.conf, as in the previous case we can create and save it with nano, this file will have the following:
[program:petfeederd]<br>directory=/opt command=python petfeeder.py autostart=true autorestart=true
Once the file is saved we can use the command supervisorctl [start | stop | restart] petfeederd, as in this case the service has not been started, we execute it with start:
supervisorctl start petfeederd
Step 4: Ready to Test
And with this the system should work, the LCD screen should show information about when it should be the next feed or if it is ready to feed should I say so. You must also show the name of the wireless network to which you are connected and the IP address that is being used in case we need to connect through SSH, I share a small video that I made, I apologize for the quality of it, I am not very good is this subject of the vlogs.
You should be able to search your bot on Telegram and Send one of the next command after you /authorize your chat:
/feed: Starts the feeding process as long as the parameterized time period has elapsed.
/when: it returns information about when was the last feeding and a picture of the food plate.
/photo: Return a picture of the food plate.
/restart: Restart the system, set the variable of last feed to 0 to be able to feed immediately.
/status It informs about the Internet connection status: SSID of the connected Wireless and the IP address that the system has within the network.