This Java project enables the user to control relays via a graphical user intreface and have the state of the relays displayed. It is meant to be used on the Raspberry Pi 4, which is an SBC (Single Board Computer) made by the Raspberry Pi Foundation, based in England, Cambridge.
- Raspberry Pi 4
- Micro SD card, minimally 16Gb
- Another computer to write the system image on the Micro SD card.
- Power supply (5V, 3A - for Pi 4)
- Monitor, keyboard, mouse
- Waveshare 8 Channel Relay Expansion Board
You can download the image yourself from the web and use any popular software (such as Win32DiskImager, Rufus, Balena Etcher, etc) to write it to the SD card. Or, you can use the official Pi Imager and you also have the possibility of setting up thigs such as WiFi, SSH, main user account right before starting. This is useful in case you need a fast deployment of your Pi and have no monitor at a moment's notice.
Source: https://www.raspberrypi.com/software/
In this project's case, make sure you are not running a headless version (that means not having any desktop environment installed).
The most recent build of the RPi OS should already come wtih a Java Run-Time Environment preinstalled, make sure you have it installed by chekcing the version:
java -version
sudo apt update
sudo apt full-upgrade
The main library we will need in order for this project to work is pi4j which is a "friendly object-oriented I/O API and implementation libraries for Java Programmers to access the full I/O capabilities of the Raspberry Pi platform".
Source: https://pi4j.com/
We have one dependecny to take care of which is WiringPi (it is a PIN based GPIO access library written in C). It should be preinstalled with the RPi OS, but if for whatever reason it is not, we use the following command to install it:
sudo apt-get install wiringpi
Installing pi4j:
curl -sSL https://pi4j.com/install | sudo bash
sudo apt-get install pi4j
sudo pi4j --update
In case you want to connect remotely to the Pi and have some useful info on a physical display then the PiOled is a great addition. It requires Python, the pip manager and a package made by Adafruit.
Source: https://www.adafruit.com/product/3527
- Install the OLED Package
sudo apt-get install python3-pip
sudo pip3 install adafruit-circuitpython-ssd1306
- Enable I2C bus from the Configuration tab
- Reboot
- Check for the OLED on the I2C bus
sudo i2cdetect -y 1
- To make the script from the package to run on startup:
sudo nano /etc/rc.local
- Then add the line
sudo python3 /{folder_where_you_saved_the_script}/stats.py &
-
After setting up the Pi insert it into the Waveshare Relay Board and secure it with the included screws
-
Connect the power pins of the relay board to a regulated 5V 3A PSU (or the official USB C 5V 3A Pi power brick)
-
If you have only a 12V power supply at hand make sure to use a step down converter such as the LM2596 (supports 3A) to bring it down to 5V.
-
Open the terminal
-
Compile the source in the direcory where you downloaded the file. This will create
relay_GUI.class
javac relay_GUI.java
- Run the app with:
java relay_GUI