As the New Year approaches, the fish I raised will soon meet their demise after I return home. I thought of using the zero gifted by a friend to create a small device for remote fish feeding, which shouldn’t be too difficult.
Concept: Use a dual-channel relay to control the light and water pump, utilize mjpg-streamer to obtain the camera’s video stream, and automatically open and close the relay at specific times.
Network environment: A home network with a public IP, using DDNS from the router or a peanut shell, where the Raspberry Pi acts as a TCP server to provide external access. However, this condition is becoming increasingly difficult to meet, as most networks are large intranets. In this case, the Raspberry Pi can act as a TCP client to actively request instructions from the server. This article introduces the first scenario.
The fish food tank is temporarily unfinished; I plan to use two larger bottle caps, glue them together with hot melt glue, place fish food in the middle, and make two holes on the edges, ultimately fixing it to a stepper motor. One rotation will complete the fish feeding action.
The installation and configuration of the Raspberry Pi will not be elaborated on here. This article is divided into “Hardware Part”, “Software Part”, and “Autostart Configuration” to explain the entire project.
Hardware Part
The hardware used in this project: the indispensable brain:
1. Dual-channel relay
Use the gpio readall command to obtain all interface information on the Raspberry Pi. Here, the BCM method is used to control the GPIO interface, selecting BCM pin numbers 18 and 27, which are GPIO1 and GPIO2, as the signal control for the two-channel relay. The relay’s VCC and GND are connected to the Raspberry Pi’s 5V and 0V interfaces, respectively. Let’s borrow a diagram to make it clearer.
2. Stepper motor and ULN2003 control module The stepper motor uses 4-step or 8-step pulse signals to drive the motor. Here, dual 4-steps (ab bc cd da) are used to control the motor, which can achieve relatively strong torque and better precision than single 4-steps. This ULN2003 control module has a drawback: the control interval cannot be less than 3ms; otherwise, the motor will only vibrate and not rotate. The connection is also straightforward; the positive and negative poles are connected to the zero, and the control pins use BCM pin numbers 23, 24, 25, and 12. The BCM numbers can be seen in the first image.
3. Compatible USB camera Just plug it into the USB hub, and use lsusb on the Raspberry Pi to check if it is recognized. If not, it is likely due to incompatibility.
4. Compatible USB wireless network card for Raspberry Pi
5. USB hub
Software Part
The software is mainly divided into three major parts: 1. Relay control, timing control, stepper motor control (code file saved to /home/pi/scripts/MyTcpControl.py) 2. Real-time video stream deployment of the camera (script to start the video stream service saved to /home/pi/scripts/startCamera.sh) 3. Android remote control APP>1. Dual-channel relay control, automatic timing control, stepper motor control This module is written in Python.
1. Establish a TCP server with a communication port of 7654
2. High and low level control. Since the relay used connects the circuit with a low signal, the code uses GPIO.LOW to connect the relay circuit and GPIO.HIGH to disconnect the relay circuit.
3. Motor stepping sequence control. The stepper motor uses dual 4-steps to control the GPIO level signals; the source code can be viewed by clicking on the original article at the end.
2. Real-time video stream deployment of the camera I tried the motion component but found it very laggy, so I switched to using mjpg-streamer, which is very smooth and recommended! (1) Install dependency libraries
sudo apt-get install libjpeg62-dev
sudo apt-get install libjpeg8-dev
(2) Access https://github.com/jacksonliam/mjpg-streamer from the Raspberry Pi browser to download the source code, which defaults to the /home/pi/Downloads directory. After completion, unzip it. Since most cameras on the market output in YUYV format, the code file of the mjpg-streamer project needs to be modified to support this format by default. Use the nano command or TextEditor to open the file mjpg-streamer-experimental/plugins/input_uvc/input_uvc.c, find the input_init function, and change “format = V4L2_PIX_FMT_MJPEG” to “format = V4L2_PIX_FMT_YUYV”.
(3) Compile and deploy the mjpg-streamer project
sudo apt-get install cmake
cd /home/pi/Downloads/mjpg-streamer-master/mjpg-streamer-experimental
sudo make clean all
After compilation, copy the relevant files to the designated directory
sudo cp mjpg_streamer /usr/local/bin
sudo cp output_http.so input_uvc.so /usr/local/lib/
sudo cp -R www /usr/local/www
Finally, use the command to start the video component
LD_LIBRARY_PATH=/usr/local/lib mjpg_streamer -i "input_uvc.so -r 320x240 -f 12" -o "output_http.so -p 12001 -w /usr/local/www"
In Google Chrome, you can view the video with the preview address http://RaspberryPiIP:12001/?action=stream
3. Android remote control APP Use Android Studio as the IDE and utilize the webview control for human-computer interaction, which is simple and quick. (1) Place the fish.html file in the assets directory; the source code can be viewed by clicking on the original article at the end. (2) The Activity contains only one WebView component; the backend code of the main form is in MainActivity.java; the source code can be viewed by clicking on the original article at the end. (3) TcpClient.java; the source code can be viewed by clicking on the original article at the end. (4) AndroidManifest.xml permission configuration; the content can be viewed by clicking on the original article at the end.
Autostart Configuration
First, change the default Python running version of the system:
sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python
Enter the /home/pi/.config directory, create an autostart folder, enter this folder, and create two files with the suffix “.desktop”. The content of the camera.desktop file is:
[Desktop Entry]
Type=Application
Exec=/home/pi/scripts/startCamera.sh
The content of the tcpserver.desktop file is:
[Desktop Entry]
Type=Application
Exec=python /home/pi/scripts/MyTcpControl.py
After completion, restart the Raspberry Pi, and all configurations will be complete.
Final completion status: The box is huge and ugly, but it has enough space!
Android APP, the icon chosen by my baby, loves it 🙂
Author: wszhoho Project homepage: http://maker.quwj.com/project/86
Links in the text can be clicked to view the original article at the end
More exciting content
Drawing robot made with Raspberry Pi
Making a “pocket TV” with Raspberry Pi
Building a decent NAS with Raspberry Pi
DIY WI-FI remote control boat with ESP32 development board
Raspberry Pi (Raspberry Pi) 3rd generation A+ version release
Petoi Nybble: A boon for Raspberry Pi cat slaves
Raspberry Pi UPS: Uninterruptible power supply modified from a power bank