Member-only story
How to create Temperature Sensor Application on Raspberry Pi 4 – Java Project

In this application I have used Raspberry Pi 4 to build and temperature sensor client/server application, where the embedded SBC reads the CPU core temperature from a file system entry and sends the data to a desktop computer client, which displays the data in a graphical form.

Raspberry Pi
The server class executes on the Raspberry Pi ThreadedServer.class
which starts listening for connection at port 5050 its an infinite loop for each client’s request. Which creates an object of connection handler class and starts the initiation of that class through start() method.
ThreadedConnectionHandler.class
: Connection handler class creates an object of 3 classes as mentioned below:
a. DateTimeService.class
b. TemperatureService.class
c. LED.class
DateTimeService.class
returns the date/time as a formatted string object.
TemperatureService.class
returns the CPU temperature of the server which is in this instance is Raspberry Pi from the absolute path of Raspberry pie as mentioned below: Location: ‘/sys/class/thermal/thermal_zone0/temp.
LED.class
triggers the change in the value of the variable from ‘0’ to ‘1’ on the below path to turn on/off the LED on Raspberry Pi.
Location: ‘/sys/class/leds/led0’.
ThreadedConnectionHandler.class
then receives and processes the incoming commands from the client and checks if it’s a valid string object which in this case is ‘GetStats’. It uses the custom DateTimeService.class
to extract the current date and time and extracts the current CPU temperature from the path and turns the LED on before making this call through led.flash(“On”) as shown in the below code snippet.