Measure temperature using Raspberry Pi and Spring Boot

This tutorial will help you to measure temperature using Raspberry Pi and Enviro Phat. Enviro Phat is an add-on board and can not only measure temperature but also pressure and motion.

This tutorial is using technology such as Java, Spring Boot and Pi4J to measure the temperature from a sensor called

What you will need

If you have not setup your Raspberry Pi with an operating system this tutorial will show you how that is done.


The main advantage of using Raspberry Pi for your home project is the big community. The community provides add-on boards which are cheap to buy and easy to use. One of the boards is the Enviro Phat which I decided to use for a home project. The idea was to measure temperature and if it is too hot automatically start a fan.


The board comes in pieces, just like an IKEA package, and we will have to solder them together. As you can see in the image I am not pro at this, but as long as it works I am happy 🙂


The Enviro Phat board includes the sensor BMP280 which we will be using to tell what the temperature is. The way the sensor is communicating with the sensor is through a communitcation called I2C and it is quite common in the world of add on boards to Raspberry Pi.

To be able to communicate with the sensor you must first enable I2C for the Raspberry Pi. You do this by typing the following on a terminal for a Raspberry Pi. (Check out this tutorial if you do not know how to setup the Raspberry Pi with SSH)

# sudo raspi-config

With the help of your keyboard enter the page “Interfacing Options” and enable the option “I2C”

In order to make the changes you made to work you have to reboot the Raspberry Pi.

# sudo reboot

To check that I2C works and Raspberry Pi can detect the board you can use something called I2C-tools. It should look something like the image below, something is answering on address hex 77.

# sudo apt-get install I2C-tools

# sudo i2cdetect -y 1


The idea of the application is to use Java and Spring boot to create a “Component” which can be used in any other Spring boot application I build. The application does not do anything special at the moment, it is just printing the current temperature to the console.

You can find the code on github here.

“EnviroPhatDevice” class is initiating the I2C communication with the board as well being able to retrieve the temperature.

The EnviroPhatTempCollector is just reading the temperarature every other second and printing the temperature to the console.


The application uses a library called Pi4J and is a library which makes it easy to use Raspberry Pi GPIO and other utilities from a Java application.

Pi4J has a dependency towards another library called Wiring Pi and you have to install Wiring Pi before you application works.

# sudo apt-get install wiringpi

I used Spring boot with Java 8 because the dependency Pi4J does not seems to support Java 11 yet.

If you have not installed java yet, this tutorial will show you how.

Not sure about how to use Spring Boot on Raspberry Pi, follow this guide.

To run the application just run the command Java -jar {appName}”


This tutorial has shown you how to measure temperature using Raspberry Pi and it is not that complicated. The I2C communcation is a valuable thing to know in the future because it is quite often used for different applications.

What’s next?

As you probably noticed the temperature is quite a bit higher than it, this is due it is close to the Raspberry Pi. I am thinking of creating an algorithm to predict a temperature which is closer to the room temperature. It would include an offset which depends on the cpu temperature.

Next step for me is to find a fan which can be controlled from my Raspberry Pi and be turned on if it is too hot in the room.