Temperature Sensor Application


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.

  1. (ThreadedConnectionHandler.class): Connection handler class creates an object of 3 classes as mentioned below:

    a. DateTimeService.class

    b. TemperatureService.class

    c. LED.class

  2. DateTimeService.class returns the date/time as a formatted string object.

  3. 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.

  4. 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.



The client class is executed on the terminal on the machine to connect to the server class to sends instructions and receive the data. The connection is made through the IP address of the Raspberry Pi as the port is already initialised. getstats() method is executed in the Client.class which sends the commands to the ThreadedServer.class and receives the data into string format for all the features.


Application.class is the main class which calls the client.class to send commands and receive data from the server (Raspberry Pi), function invoked is ‘getTemperatureFromServer’ which gets the data in string format in a comma separated format. Which is then split into different column and the data is formatted as shown below:


Application.class extends on JFrame and creates the objects on the Gaurav’s Application such as Scroll Pane, Graph Panel, Input Panel (JTextField) and Sampling Rate Button (JButton). Scrolling Pane helps in scrolling the page if the height and the width is below 810 X 680, Graph pane is where the graph is presented in the window. Bottom panel holds the Input field and button to set the sampling rate as shown below in the screenshot 1. Once the sampling rate is set and the graph is plotted on a real time from the server data, I have created a middle pane that holds the current reading information, date and last reading value, reading ID and temperature. Finally, I have created the main panel on which I have included all of the above panes based on the code below:


Application Window

Once the ‘ActionListener’ is implemented in Application.class it captures the action, which is when the user sets the value for sampling rate (i.e. 3 seconds), It then initialise the ‘actionPerformed’ method and parses the 3 to the variable ‘samplingIntervalInSeconds’, which is then initialised to the ‘timeInterval’. At this stage I have initiated the Client.class and created an empty array on which I am storing my string values for the last 20 data calls. The loop in initiated through ‘TimerTask’ method as shown in the code below:


In the final stage in Application.class I am invoking the Graph.class which takes the last 20 temperature values and put them into an empty array and plots the graph on it through Graph.class, Its a for loop which revalidates and repaints the graph everytime new value of the temperature reading is received from the server, I have created the 3 more lines on the plot in Graph.class which is Maximum, Minimum and Average reading based on the last 20 temperature reading from the array list as shown below in the code:


Under Graph.class I am executing the ‘paintComponent’ method in which initially I am drawing the x-axis and y-axis, secondly I am drawing the data point lines through a for loop, Maximum line, Minimum line and Average line based on the last 20 data points.

Code Run

Step 1: ​Connect the Raspberry Pi to the eclipse through ssh call on your Pi host and provide your connection name and execute ThreadedServer.class, you can also connect screen mouse and keyboards to your Pi and open a terminal and run the ThreadedServer.class as shown below in the screenshot 2.

Step 2: ​Open the terminal on the client machine and execute the Application.class and pass an IP address of the Raspberry Pi ( or play the Application.class from eclipse after including manual argument and set IP Address through run configurations in eclipse as shown below in the screenshot 4.

Step 3: ​Type an integer value in the input box and click on ‘Set Interval and Start’ as shown below in the screenshot 6.

Application Demo

Red: Maximum Line

Green: Average Line

Yellow: Minimum Line

Step 4: ​To stop the application click on the close button on the application window or stop the project from eclipse.


Java Files:









Binary Files: (In the above code I have reference the binary files in notes)










Click below to subscribe to my YouTube channel

Email: info.techfitlab@gmail.com

We use cookies to improve your experience and to help us understand how you use our site. Please refer to our cookie notice and privacy policy for more information regarding cookies and other third-party tracking that may be enabled.