I've taken to playing Ping Pong recently, but I keep forgetting the score. :-) So I made this live streamer/scorekeeper for fun and to help me remember the score.
Parts List
- Raspberry Pi
- USB camera
- Monitor for Raspberry Pi
- Keyb0ard/mouse for Raspberry Pi
- Arduino UNO
- 2 pushbuttons
- 2 10KΩ resistors
Step 1: Software
Boot up your Raspberry Pi. Now install a package called motion with this command:
sudo apt-get install motion
Also (if it isn't already installed), install Python 3 and pip for Python 3 with these commands:
sudo apt-get install python3
sudo apt-get install python3-pip
Now edit the file at /etc/motion/motion.conf and change these values so that you have a high framerate, high quality stream and so that motion doesn't save pictures/videos:
- framerate should be 24
- stream_maxrate should be 30
- âstream_port should be 1234
- stream_quality should be 100
- stream_localhost should be off
- output_pictures should be off
- output_debug_pictures should be off
- ffmpeg_output_movies should be off
- ffmpeg_output_debug_movies should be off
- quality should be 100
Feel free to change more configuration settings. Maybe you can get a faster framerate! It's a little bit laggy for me.
Now, install 2 python modules: serial (to communicate with the Arduino) and Flask (to make the web server) using these commands:
sudo pip3 install Flask
sudo pip3 install pyserial
Next, create a file named pingpong.py in /home/pi with this code inside it:
from flask import Flask import threading, serial, os ip = "" while (not "." in ip): os.system("hostname -I > /tmp/ip.txt") with open("/tmp/ip.txt") as f: ip = f.read().replace('\n', '').replace(' ', '') print(ip) port = serial.Serial('', 115200) app = Flask(__name__) red = 0 blue = 0 def checkScore(): global red, blue while True: if (blue > 10 or red > 10): red = 0 blue = 0 line = port.readline() line = str(line, "UTF-8") if "red" in line: red += 1 elif "blue" in line: blue += 1 t = threading.Thread(target=checkScore) t.start() @app.route('/') def main(): return '
' @app.route('/score.html') def score(): return '
Red: '+str(red)+' Blue: '+str(blue)+'
' app.run(host="0.0.0.0", port="80", threaded=True)
Also, create a file called start in /home/pi with this inside it:
sudo motion &
sudo python3 /home/pi/pingpong.py &
You're done with software! Now, time for hardware!
Step 2: Hardware
Here's the circuit diagram:
Assemble that, plug the Arduino into the Raspberry Pi, and you're done!
Step 3: Use It!
âUse this command to run the streamer:
/home/pi/start
One side of the Ping Pong table is red, and the other is blue. Push the button for your side to record a point.
If you don't want to use an Arduino, you could just modify pingpong.py to use the Pi's GPIO pins and wire the buttons directly to the Pi.