Raspberry Pi IoT Foosball Table

Introduction: Raspberry Pi IoT Foosball Table

About: I am a NMCT student

Welcome to my version of a hacked foosball table, as part of a student project for New Media and Communication technology. I basicly grew up around foosball and pool tables so i thought it'd be great to try to connect one of these devices to the internet.

The main setup is using an existing foosball table connected to a Raspberry Pi which can do all of the following tasks:

  • Uses GPIO pins to drive the IR LED/Receivers and the 8x8 dot matrix modules
  • Runs a Flask website using nginx
  • Runs a MySQL/MariaDB database for data storage

To be able to recreate this project u will need the following skills:

Backend:

  • Understanding HTML/CSS/Javascript for the client of the website
  • Understanding Python with Flask/Flask-SocketIO/Jinja2 for the server side of the website
  • Basic knowledge of MySQL
  • Know how to run a webserver

Prototype

  • Basic knowledge on how to wire an electrical circuit
  • Soldering
  • Basic understanding on how to write a script in Python
  • Working with Linux (Raspbian)
  • Having a lot of patience because there will be a lot of debugging involved

Step 1: The Materials

Here is the list with all necessary parts for the table:

  • Raspberry Pi Model 3 (with casing)
  • T-Cobbler for connecting the Pi to the breadboard
  • Foosball table (I used a very old table similar to this one. Must be willing to drill holes in it)
  • MAX7219 Arduino Dot Matrix Module (2)
  • IR LED Emitters (2+ because they break, similar to this one)
  • IR Receivers (2+ because they also break, similar to this one)
  • Basic solderless breadboard
  • Lots and lots of wire (depends on the size of the foosball table)
  • male connectors (30+)
  • female connectors (10+)
  • 100-220 Ohm Resistors (4+)
  • soldering materials
  • wire straps
  • LED holders
  • some wood/screws in case u want to relay the ball in the gutter

The total cost of this project depends entirely on the cost of your foosball table (expensive). Besides the table the materials will be maximum 150 euros.

Step 2: The Electrical Circuit

Before you attempt to solder, assemble all the components I highly recommend testing it on a breadboard first. It is easier to replace faulty components before you've spent hours soldering them.

At first i tried to implement the 8x8 LED Matrix with a 74HC595 shift register (first image) and transistor array but due the many wires and very low output I switched to the MAX7219 Dot Matrix Module because it takes only 5 wires and is directly driven by an SPI bus.

The circuit I eventually used is drawn with Fritzing. Please note that the IR LEDS and Receivers can be connected to any of your free GPIO Pins.

The IR Receivers and LED should be directly across each other and the top of the LED should be pointed towards the receiver. Because we want to simulate a direct beam which can be broken by the ball movement in which case there will be a state change of the DATA line of the receiver from 0 to 1.

Step 3: Coding the Sensors

I have coded most of this project using Pycharm because it allows easy SSH deployment to your Raspberry Pi using a remote interpreter. I won't go in detail as how this program works but a lot of information can be found on the pycharm website.

I am currently still working on the project but once everything is done the whole project will be available on my github profile

The code for the sensors consists of 3 classes which are run in a background thread on my Flask server (which will be explained later):

  1. The Goal Class (link) -This file initiaties all the separate components, which can be called upon by inserting the correct SPI device/bus and pin number
  2. The Matrix Class (link) -This is the main class to power up the MAX7219 module
  3. The LED and Receiver Class (link) - This is the main class to power up the infrared break-beam using separate Threads to reduce CPU load of the RPi

The LED and receiver both work on a 38kHz frequency and the receivers always expects a 50% up and a 50% down pulse to work properly.



Step 4: Preparing and Placing the Sensors

Now we are going to prepare the IR LED and receiver. In the picture of the table u can find the locations for where the RPi and sensors should be placed.

But first we need to prepare the wiring:

  1. Make sure u measure the amount of wire needed from the RPi/breadboard location to the sensor location
  2. Solder the IR receiver pins to one end of the wire (COM/GND/V+)
  3. Solder the male connector pieces on the other end of the wire

Now we are going to prepare the table:

  1. Make a basic drawing (based on the picture) on where to drill. It is very important that the 2 holes are aligned across each other because this will be the beam location.
  2. Drill the holes
  3. If you have some LED holders (link) u can put these inside the hole to make it more sturdy
  4. Insert + tape the LED + receiver on both sides
  5. Strap the wires + tape them on the wood so they dont cross too much
  6. Insert the male pins on the breadboard according the previously provided circuit

Step 5: Preparing and Placing the Matrix Module

  1. Next up we are going to connect the 2 LED matrix modules.

Note:

Because I used an old foosball table there were already holes going up towards the upper part because of cigarette holders. If u don't have these u will need to create them.

To prepare the wires:

  1. Measure the wire from the breadboard towards the top part of the table
  2. Solder some female connectors to the first end of the wire
  3. Solder some male connectors to the other end of the wire

Placing the matrix:

  1. Bring out the matrix through the hole on the top
  2. Strap + tape the wires inside on the wood to avoid crossing
  3. Insert the male pins on the breadboard according the previously provided circui

At some point I will add a small DIY step to add a casing for the matrix module, but for now they are naked.

Step 6: Making It IoT

If you just want to register and display the scores, you can finish off the project by writing a small running python script that loops until one of the scores reaches 9 and then resets.

However if you want to connect your table to the internet the next few steps should right up your alley.

In the next few steps we will address the following:

  • Configuring the Raspberry Pi
  • Making a database for storage
  • Creating the website
  • Putting it online

At this point, if you are familiar with git, I recommend u make a repository on GitHub/GitLab to keep track of your files. If you are not you can create a folder with the same structure as in the picture.

The full project will be available soon on GitHub. However a temporary rar file with all the necessary files is available.

Step 7: Connecting the Raspberry Pi

Next we are going to setup the raspberry pi environment, to do this you need to execute the following steps:

  • Connect over SSH to your Rasberry Pi (u can use PuTTY)
  • Create a folder (example mkdir project) and move to this folder using the cd command
  • Create a virtual Python environment in this folder using the python3 -m venv --system-site-packages env command
  • Activate the virtual interpreter with the source /env/bin/activate command
  • Install the packages from the requirements.txt with python -m pip installpackage-name command
  • Transfer the files from the previously provided project_example.rar file over SSH in your project folder

Now you should be able to run the full project on your Raspberry Pi. I advise you to use a Python IDE like PyCharm which allows u to use you to debug from your remote interpreter over SSH and directly upload changes if necessary.

Step 8: Setting Up the Database

Now you need to set up a very basic database, based on this model.

The easiest way to go with this is to create your database in the MySQL workbench where u can also do some testinng.

Once done you can export a dump of your database and upload it on your RPi and then execute it with sudo mariadb < pathtofile/file.sql



Step 9: Creating the Website

Next up u can analyse (and use) the code provided in the project_example.rar file.

The main file is the Flask.py which is the bread and butter of this project:

  • Runs a Flask-SocketIO app that handles the backend of the website
  • Creates a connection between the database and Flask
  • Provides login validation and user registration
  • Provides the necessary code on how to play a game uses socketio to update the website realtime during the game
  • Puts the game results into the database

In the static and templates folders u can find the HTML/CSS/JS which provide the frontend part of the website. Feel free to modify these according to your own likings.

Step 10: Connecting to the World Wide Web

To connect our website to the web we will use nginx and uwsgi. In the project example u can find the necessary files in the conf folder.

First of all u need to update the following in these files:

  • In the uwsgi-flask.ini u need to change the path of the virtualenv parameter to your interpreter
  • In the project1-flask.service u need to update the [Service] part of the file with your credentials and paths to the associated files
  • In the nginx file u need to update the server and location / path to your associated socket

Next u need to replace the default nginx webserver file with the location of your nginx config file, below is an example of linux commands to do this

  • me@my-rpi:~/project1 $sudo cp conf/nginx /etc/nginx/sites-available/project1
  • me@my-rpi:~/project1 $ sudo rm /etc/nginx/sites-enabled/default
  • me@my-rpi:~/project1 $ sudo ln -s /etc/nginx/sites-available/project1 /etc/nginx/sites-enabled/project1
  • me@my-rpi:~/project1 $ sudo systemctl restart nginx.service

Finally u need to add the custom services to your systemd folder, this is an example on how to do it:

  • me@my-rpi:~/project1 $ sudo cp conf/project1-*.service /etc/systemd/system/
  • me@my-rpi:~/project1 $ sudo systemctl daemon-reload
  • me@my-rpi:~/project1 $ sudo systemctl start project1-*
  • me@my-rpi:~/project1 $ sudo systemctl status project1-*

If u want the webserver to start on boot of your raspberry pi u need to use the sudo systemctl enable project1-*.service command.

If done correctly, after a system reboot your website should be running on your IP address. Should you want to edit one of these config files u always need to stop the service, reupload the files and use the daemon-reload command followed by a start, otherwise the changes will not be effective.

Step 11: Finishing Off

While typing the final part of this instructable, this little school project is still a work in progress.

I've spent countless hours into getting this done in 2.5 weeks. Even though everything was rushed a bit I'm still proud of what I have achieved. During the assembly phase I've encountered countless bugs/errors/faulty sensors so don't be too discouraged if everything isn't working on the first try.

The best thing u can do is ask or look for help on the internet, there are a lot of people with far better knowledge who are very eager to help u.

Last but not least i want to thank my teachers from the New Media and Communication Technology for giving me a lot of advice and helping me to finish this project.

Share

    Recommendations

    • Metalworking Contest

      Metalworking Contest
    • Fix It! Contest

      Fix It! Contest
    • Water Contest

      Water Contest

    Discussions