DEV Community

Gökhan ERGEN
Gökhan ERGEN

Posted on

Raspberry PI Apache Web Server, Pigpio and Ultrasonic Sensor With Java By Using PI4J Library

What is Raspberry PI

Raspberry PI was developed for low cost and to teach students about computers. But today, it can also be used for personal and daily use. You can create circuits and projects on it. It is an embedded Linux system that becomes more powerful with new versions.

You can build your own circuits on it, create servers and work on image processing. Raspberry takes its name from the name of the fruit and PI comes from Python, which belongs to RPI and also refers to the number PI.

Image description

As seen in Figure 1, Raspberry PI model 4b has 4 USB ports, 1 Gigabit internet port and 2 HDMI converter ports.

Raspberry PI as an Embedded System

Embedded systems can be defined as a combination of hardware and software such as memory, IO and CPU. Raspberry PI is a mini computer that can fit in our hands and we can think of it as an embedded system. A system can be obtained by combining hardware, and software is required for this system to perform certain functions. The software contains the commands for the hardware to perform its function, and the commands are converted into the language that the machine understands, that is, 0s and 1s. Instructions are executed on the processor. This logic also works in embedded systems, and we can write software containing commands and manage hardware on Raspberry PI.

Installing Apache Server on Raspberry PI

Image description

The application was made on a 4GB version of the Raspberry PI 4B model. There is an SD card slot on it. A 32GB SD card was used as storage.

Image description

In order to make any development, a Linux system must be installed. First of all, we make the SD card bootable. Operating system is a system software. Thanks to this software, we can manage the hardware. In addition, the operating system is a software and we can produce and run software through this software.

Linux-based Raspberry PI OS was installed on this computer. This operating system offers us GUI and you can also use the terminal so you can benefit from the services of the operating system.

Image description

After downloading the software here, it can be used with a laptop or another computer with SD card reader feature. With this program, you can easily install the operating system by choosing an operating system on your SD card. To get information about installation and more, you can visit their website [9].

You can use Raspberry PI on a display that supports HDMI. For this, you can connect your HDMI converter cable to the micro HDMI port to the monitor.

Image description

In the picture in Figure 5, 2 USBs were used for the keyboard and mouse and were powered by a power adapter. Raspberry PI is ready for use with a bootable SD card.

Figure 6 Raspberry PI OS

Figure 6 Raspberry PI OS

In Figure 6, you can go to the configuration settings and make keyboard settings and activate the SSH option. You can also access your computer’s command line remotely via SSH.

Image description

Figure 7 Raspberry PI OS Configuration Settings

Image description

Figure 8 Terminal Screen

In Figure 8, we opened the terminal software and will operate on the command line. First of all, we can get the current version of our packages and new versions with update, and we can move the packages we have to the latest version with upgrade. If yes or no questions come up during the process and you want the default to be yes, you can add the -y argument.

Image description

Figure 9 Apache Installation

You can easily install the Apache web server by entering the sudo apt-get install apache2 command.

Image description

Figure 10 Learning Apache Server Status

You can find out whether the web server is active or not with sudo systemctl status apache2 command.

Image description

Figure 11 Accessing the Address on the Local Network

You can access your 32-bit address assigned to you on the local network with the hostname -I command. The web page was served by Apache via this address.

Image description

Figure 12 Apache Default Page Location

Apache http server is served at /var/www/html by default. For detailed information, you can visit [18][19]. It will contain an index.html file.

Image description

Figure 13 /var/www/html Folder Content

Image description

Figure 14 Changing Html File Content

You can use the file content using nano or vim, whichever you prefer.

Image description

Figure 15 Accessing the Web Page

You can access the web page by typing the address you obtained with the hostname -I command into the URL section.

Raspberry PI Pigpio Usage and Java PI4J Distance Sensor Example

We can say that Pigpio is a tool that allows us to operate the pins on the Raspbery PI.

There is also a library that you can use with Java language. PI4J is an I/O library that allows us to develop with Raspberry PI. You can do various projects with this library.

Java is a powerful language that has a lot of support and you will not have any difficulty in searching for resources, and if you already have experience in this language, you will not have any difficulty. First of all, let’s give information about writing and reading values on some small pins with pigpio.

Image description

Figure 16 Pigpio Installation

If the package is not installed, you can quickly install it with sudo apt-get pigpio command.

Image description

Figure 17 Pigpiod Status

You can see the status of the package with sudo systemctl status pigpiod command.

Image description

Figure 18 Basic Pigpio Commands

pigs modes ‘pin code’ ‘mode (w)write or (r)read’

pigs modes 17 w

pigs modes 17 r

Pin 17 is set to read or write mode.

pigs w 17 1 with this command, high, i.e. 3.3v, was transmitted to pin 17.

At this stage, if an LED circuit is installed on the relevant pin, the LED will turn on. In this case, pigs w 17 0 will switch to the off state, that is, low. pigs r 17 checks whether there is voltage on pin 17 or not. If it exists, it returns 1, otherwise it returns 0.

Distance Sensor with Java PI4J Library

Let’s look at an example distance sensor application with the Java pi4j library. Pi4j library uses the pigpio package and you can check out the sources for detailed information [20].

Image description

Figure 19 Distance Sensor Circuit

Image description

Figure 20 https://www.researchgate.net/figure/Raspberry-Pi-4-pin-details_fig4_352212777

In the application, 1 HC-SR04 distance sensor, 1 LED and 2 resistors were used. The red LED turns on when the distance between the sensor and the object is less than 10cm. GPIO 2 was connected to echo on the sensor and GPIO 3 was connected to trigg. 3.3v was used as voltage. GPIO 17 pin was used for the LED.

Image description

Figure 21 Distance Sensor Application Command Line

Image description

Figure 22 Example Distance Less than 10cm

Image description

Figure 23 Distance More Than 10cm

Github: https://github.com/gokhanergen-tech/pi4j-java-distance-sensor-and-vcore-raspberry-pi

As a result, Raspberry PI is a popular educational computer today. You can install a web server such as Apache on it or use it in your daily work. You can design circuits and make projects using sensors. You may not limit yourself to the programming language and you can choose other programming languages as needed. Within the scope of this article, examples of a library developed in Java language were shown.

Sources

1-https://www.researchgate.net/figure/The-structure-of-the-Raspberry-Pi-4B_fig1_370357718

2-https://www.deviceplus.com/raspberry-pi/the-history-of-raspberry-pi/

3-https://electronicguidebook.com/is-raspberry-pi-an-embedded-system%EF%BF%BC/

4-https://www.raspberrypi.com/software/

5-https://mediatrend.mediamarkt.com.tr/windows-10-bootable-usb-nasil-yapilir/

6-https://edu.gcfglobal.org/en/computerbasics/understanding-operating-systems/1/

7-https://www.freecodecamp.org/news/command-line-for-beginners/

8-https://www.prepbytes.com/blog/operating-system/operating-system-services

9- Raspberry Pi Documentation — Getting started

10-https://pimylifeup.com/raspberry-pi-apache/

11-https://linuxize.com/post/how-to-create-groups-in-linux/

12-https://www.freecodecamp.org/news/file-permissions-in-linux-chmod-command-explained/

13-https://devconnected.com/how-to-list-users-and-groups-on-linux/

14-https://www.geeksforgeeks.org/permissions-in-linux/

15-https://stackoverflow.com/questions/29475138/what-is-the-difference-between-update-and-upgrade

16-https://www.ihs.com.tr/blog/apache-nedir/

17-https://ubuntu.com/tutorials/install-and-configure-apache#3-creating-your-own-website

18-https://ubuntu.com/server/docs/how-to-configure-apache2-settings#:~:text=The%20default%20value%20is%20%2Fvar,create%20that%20directory%20if%20necessary!

19-https://operavps.com/blog/vim-vs-nano/#:~:text=Nano%20can%20be%20suitable%20for,capabilities%20to%20improve%20your%20productivity.

20- https://pi4j.com/about/

21-https://www.researchgate.net/figure/Raspberry-Pi-4-pin-details_fig4_352212777

22-https://kalitut.com/control-gpio-pigpiod-commands/

23-https://raspberrypi.stackexchange.com/questions/17121/ultrasonic-sensor-get-distance-with-java

24-https://github.com/gokhanergen-tech/pi4j-java-distance-sensor-and-vcore-raspberry-pi
^

Top comments (1)

Collapse
 
nigel447 profile image
nigel447

great article, only issue for me is there are many alternatives I think better than java to use however for may java will be a good way into embedded