Design of Remote Real-Time Measuring System of Temperature and Humidity based on Raspberry Pi and Java Language

: In order to improve the temperature and humidity monitoring level in substations, a remote real-time measuring system of temperature and humidity based on Raspberry Pi and Java language is developed. This system consists of three layers of hardware architecture, including client, server and temperature and humidity measurement module. The client and server are connected to the Internet or LAN, using Java language programming, and there are four modes of communication between the client and the server, while the client can display the visual control interface and measurement results. The system in operation shows high efficiency and fast speed, and it can greatly improve the temperature and humidity monitoring level, with great flexibility and a strong scalability.


Introduction
Temperature and humidity are the main indicators of the environment.In the traditional power system substation, temperature and humidity need to be monitored on many occasions (such as secondary screen cabinet, outdoor terminal box).At the present stage, the main means of monitoring is on-site inspections, depending on the human observation, feeling or artificial use of temperature-humidity measurements, which is not accurate enough, large workload, low efficiency and unable to know historical changes, and cannot view the data of temperature and humidity remotely either.
At present, the research on remote monitoring of the temperature and humidity in the transformer substation is few.Literature [1] developed a temperature and humidity real-time control system in substation.The system realizes the remote viewing of temperature and humidity data and the monitoring of historical data.However, since the field of hardware is implemented based on MCU (MicroControllerUnit), where the database is unable to establish, the monitoring client must constantly be connected with each measurement device to update the database, and the measuring data will be lost in the time period of disconnection.When the number of field measurement devices is large, the workload of connection and database operation for monitoring client machine will be very hard, which will make the measuring system slow and even wrong.What's more, the client of this system must be acted by computers with external conversion equipment.There are quite a lot of complex equipments in the whole system and it's difficult to implement the system.With the development of science and technology, temperature and humidity monitoring systems are researched and developed in other places such as greenhouse of vegetables, warehouses, piggeries in recent years [2][3][4][5].Controllers of measurement of these methods are mostly also MCUs, unable to achieve the site data storage.In order to handle the storage of the field data and access from the clients, some others set a server in addition, but that greatly increased the cost and complexity of the system.
A remote real-time measuring system of temperature and humidity based on Raspberry Pi and Java language is developed.In this system, there is one Raspberry Pi set in each measuring point, as a measurement controller and server in the same time.Clients are acted by the ordinary computers, and both server and client are programmed using Java language, which is excellent in cross platform features.There are a variety of communication modes between clients and Raspberry Pi servers, and in each kind of communication mode and even when the client exit, the Raspberry Pi server can still store measuring results, without data loss.And the connection efficiency is greatly improved by flexible switching of various communication modes.The clients realize the visual control interface and the data vivid display with Java program, making the operation simple and the result clear at a glance.
The Raspberry Pi, whose processor is ARM-controller and operating system bases on Linux, is an open type of embedded system developed by a British small charities "The Raspberry Pi Foundation".The size of one Raspberry Pi is merely equal to credit card, cheap, with small size and wide community.However it has all the functions of the host computer, provides a variety of interface, and can achieve powerful functions.

Overall Structure
The temperature and humidity remote real-time measurement system has a threelayer hardware architecture including client, server, and temperature and humidity measurement module, as shown in Figure 1.The client can be acted by the computer, and interact with the human through the custom GUI interface, obtaining the command and sending it to the server through the network.The server is acted by the embedded system Raspberry Pi.It accesses network, receives instructions from the client and completes the corresponding operation, controls the temperature and humidity measurement module, and sends the data to the client.The temperature and humidity measurement module contracts with Raspberry Pi through a general IO port, and makes the measurement in accordance with the requirements of communication.

Client
Client uses java graphical user interface (GUI) technology to generate visual control interface.The interface can be input the server IP and port number to be connected, and provides a plurality of keys, each of which achieve a specific function.The client realizes the connection and communication with the server by network programming technology based on TCP/IP protocol.It also achieves the data operation and real-time data and historical data display using database technology and JfreeChart module.

Server
The server uses the network multi-thread programming technology to enables the server to be able to realize the real-time monitoring to the network port when executing the command, which is called multi-tasks executing simultaneously.It contracts the temperature and humidity measurement module for data and instructions exchange through the general IO port, processing received data, storing it in the database, and uploading data or database files to the client according to the need.

Temperature and humidity measurement module
The

Realization of communication between client and Raspberry Pi server 3.1 Communication mode
Four communication modes are adopted in this system.1.Real time measurement mode.The client sends a command on behalf of the "real time measurement" to the server.After receiving the command, the server start the temperature and humidity measurement module connecting with it to continuously measure the temperature and humidity.When a value is measured, it is first stored in the database in a certain format, and then sent to the client.The process is carried out in a loop.2. Background measurement mode.The client sends a command on behalf of the "background measurement" to the server.After receiving the command, the server start the temperature and humidity measurement module connecting with it to continuously measure the temperature and humidity.When a value is measured, it is stored in the database in a certain format, but is not sent to the client.3. Access to historical data mode.The client sends a command on behalf of the "getting historical data" to the server.After receiving the command, the server starts the file transfer command, and sends the database file of the stored data to the client.4. Client exit mode.The client sends a command on behalf of the "client exit" to the server.After receiving the command, the server detects the current communication mode, and will stop the current mode and transferred to the background measurement mode if the current mode is "real time measurement" mode.If the current mode is "access to historical data" mode, the server will transfer to background measurement mode after the completion of file transmission, and if it is in "background measurement mode", then continues to keep on.

The function of the server side
The Raspberry Pi server has the following functions: -The server keep on listening signal from the client when it has started.
-Each time it receives an order or request, start a thread to process, and continue to listen to new requests at the same time.
-After receiving the command "real time measurement", the server start the temperature and humidity measurement module connecting with it to continuously measure the temperature and humidity.When a value is measured, it is first stored in the database in a certain format, and then sent to the client.The process is carried out in a loop.-After receiving the command "background measurement", the server starts the temperature and humidity measurement module connecting with it to continuously measure the temperature and humidity.When a value is measured, it is stored in the database in a certain format, but is not sent to the client.-After receiving the command "getting historical data", the server starts the file transfer command, and sends the database file of the stored data to the client.-After receiving the command "client exit", the server will detect the current communication mode, and transfer to the background measurement mode.

The function of the client side
The client has the following functions: -When the program is started, a control panel is open, and there are a server IP and port data frame, the state status bar, buttons of "connecting", "real-time measurement", "do not display / background measurement", "access to historical data" and "quit" on the panel.-Enter the Port and IP and press the "connection" button, the client is ready to connect to the server.-When "real-time measurement" button is pressed, the client commands the server to start measuring, and is prepared to accept the data of measuring.After obtaining the data, two rectangular boxes with different colors to display real-time temperature and humidity values appear in the center of the control interface, and a JfreeChart real-time window is open, showing the recent changes of the data within 300 seconds.-When "background measurement" button is pressed, the client sends the command to the server, the temperature and humidity value in the center of the control interface stop updating, and the real-time window of JfreeChart is close.-When "access to historical data" button is pressed, the client is ready to accept data document sent by the server.When the document is completely received, a new data display window, which shows all the data, is generated.
-When "exit" button is pressed, all the windows disappear, and the server is commanded to transfer to "background measurement" mode.

Achieving network communications based on the TCP/IP protocol using Java language
Because operating system of the Raspberry Pi server is Linux, and that of the client computer is Windows, so the TCP/IP protocol, which can realize the interconnection of different hardware, different operating systems, different network systems [6], is used.In the network program of Java language, "Socket" is abstracted into a class for connecting applications with the network port, so the Socket class can be used to write communication programs.The interaction between the server and the client using Socket is shown in Figure 2.

Figure 2. Interaction between client and Raspberry Pi server
There are three steps for the server and client to establish connection.Step 1, the server program creates a ServerSocket class, then call the accept () method waiting for a client to connect.Step 2, the client program create a socket to request to establish a connection with the server.Step 3, the server receives the client connection requests, and creates a new socket with the client to establish a connection, and in the same time the server continues to wait for new connections.
When the client and server have established a connection, they can carry on the exchange of instructions, data and data files, completing the corresponding communication tasks.

Key technology of remote measurement system 4.1 Start and setting of the Raspberry Pi server
To start the Raspberry Pi, the following is needed: (1) a Raspberry Pi (B type of second generation is used here); (2) USB or wireless mouse and keyboard; (3) display; (4) 16g SD card (the capacity can be increased according to the actual demand); (5) 5V DC power supply [7].
Before starting Raspberry Pi, the OS raspbian must be installed in it.There is a wide range of community resources for Raspberry Pi, providing lots of convenience for applications.The SD card should be formatted before installing operating system, and operating system installation file compression package NOOBS should be downloaded from the Raspberry Pi official website (URL: www.raspberrypi.org).After decompression, the NOOBS files are copied into the SD card by card-reading device.Then insert the SD card into the card slot of the Raspberry Pi, connect mouse, keyboard, display, and the 5V power supply.After that, the Raspberry Pi will automatically start the installation program of operation system.When the operating system installation completed, Raspberry Pi can start.
The Raspbian operating system comes with the Java programming environment, which makes Java programming possible.Since this program requires database, taking into account the limited resources of embedded system, the light SQLite3 database is chosen for database of the server in this system.In the case of networking, open the command line interface, enter the following statement one after another to obtain the latest update, and to install the SQLite3.
sudo apt-get update sudo apt-get upgrade sudo apt-get SQLite3 In addition, the SQLite database driver package is needed to download for Java programming (the latest version of this paper is "sqlitejdbc-v056.jar"),and should be placed under the Java project folder.

Hardware connection of temperature and humidity module
The type of temperature and humidity measurement module used in the system is DHT11.The module includes a resistive type humidity sensitive element and a NTC temperature sensor, and they are connected with a high-performance 8-bit microcontroller.The temperature and humidity sensors and the microcontroller are integrated together in a package, with high reliability and stability.Just providing the working power and control signal, then it can measure and output the temperature and humidity data of the digital signal.The B type of second generation Raspberry Pi has 40 pins.For a pull-up resistor have been integrated between the power supply and the data interface inside the DHT11 module, the data export can be connected directly to Raspberry Pi GPIO port pin.The VCC, DATA, GND pins of DHT11 module should be respectively connected with Raspberry Pi 5V power pin and the GPIO pin and GND pin, while NC port can be suspended, as shown in Figure 3.

Control program of temperature and humidity module
Since Python language for Raspberry Pi has a rich library, it is convenient to achieve control of Raspberry Pi pins using Python [8].In this paper, Python language is used to write the program of data exchange with DHT11 module, and when DHT11 measurements are needed by Raspberry Pi, Java language program is run to call the python scripts.The measurement principle of DHT11 is as follows: a) After power up, DHT11 waits for 1s to cross the unstable state, and then measures the ambient temperature and humidity, and records data, while the data line of it has been pull-up by resistance and maintained at a high level.At this time, the DATA pin is in the input state, constantly detecting external signals.b) The microprocessor I/O port connected with DHT11 is set as output state and outputs low level for more than 18ms, and then is set as input state.Due to the pull-up resistor, the microprocessor I/O which is the same level to DHT11 data line, becomes high, waiting for DHT11 answer signal.c) When DHT11 data pin detects the external sustained 18ms low level ends into a high level, it delays for a short time and turn as the output state, and immediately outputs low level signal for 80 microseconds as a response signal, followed by the another output of 80 microseconds high level, to notice peripheral ready to receive data.At the same time, the microprocessor I/O should be in the input state, and when it detects low level (DHT11 response signal), begin to receive data after waiting for 80 microseconds of high level.d) The DHT11 starts to output 40 bit data through the DATA pin, while the microprocessor starts to receive 40 bits of data according to the change of the I/O level.50us low level and 26~28us high level stand for data "0" and 50us low level plus 70us high level stand for data "1".The flow chart of the program according which microprocessor controls DHT11 is shown in Figure 4.

Java language calls Python program
Java language has excellent cross platform characteristics, so that it can call any program.The Java program can be simplified by calling the Python program which has been completely written.If the temperature and humidity control program is named dht11.py,and stored in the Java project file, the following Java statement section is used to achieve the call to the program, and the operation results is displayed on Java interface.
try{ After the Java program is compiled, enter "java dht11 (dht11 is the name of Java program of this system)" directly in the command line interface, the running results will be the same as the results of running Python program.

Java multi thread programming for Raspberry Pi server
The server works in a variety of modes.When there is no command from the client, the server will measure the temperature and humidity and store the result in database continuously.When the server receives the "real-time measurement" command, it begins to measure the temperature and humidity according to the timing of the client, and sends the data to the client.When the client receives the "access to historical data" command, the server will transmit the data file to the client at the same time of measurement.For the realization of these functions above, there is a prerequisite that the server can simultaneously carry on the work of the temperature and humidity measurement, the network port monitor, the data document transmission and so on.As a result, the server program needs to have the ability to perform multiple tasks at the same time.
Java language provides a multi-threaded concurrency mechanism, which makes it possible to execute multiple threads in the program, each thread to complete a function, and concurrent with other threads.
In this paper, the server program constructs the multi thread function of the server through the realization of the java.lang.Runnable interface.The specific process is as follows.First, create a "Runnable" object, and declare a class object named HandleATask through the statement "HandleATask implements Runnable{} class", in which the corresponding receiving instruction and the function of the corresponding instruction are set up to realize the specific communication mode.Then use the statement "new Thread(new HandleATask(socket)).start();" to create a thread instance and calls the "Start ()" method to start the thread, where "socket" is the server socket that has been started.The flow chart of the realization of the multi thread function of the server is shown in Figure 5.The main program thread realizes real-time monitoring the client's request through ServerSocket, if there is a connection, a new thread is started to process the connection, without affecting the following new connections, so the implementation of multiple tasks can be achieving at the same time.

Database design
In order to store the measured data of the temperature and humidity, and obtain historical data, it is necessary to establish a database.After setting the Raspberry Pi server as mentioned earlier, Java program in Raspberry Pi server can process database operation.First, refers to a SQLite database driven by using the statement "Class.forName("org.sqlite.JDBC");", then set up database files and data table, the data will be stored in the database file according to the format of the data table after every measurement.The format and content of each data in the data table can be set according to the actual needs.The data table of this paper includes four parts: server identification, serial number, temperature, humidity and the corresponding time.

Client program flow
The system client needs to communicate with human through visual GUI interface where control interface and measurement results are shown.There are, in Java language, rich GUI graphical user interface programming libraries which makes it capable to achieve displaying control interface and measurement results.The system Java program flow chart of client is shown in Figure 6.The communication model 1~4 in the program flow chart respectively indicates the real-time measurement model, the background measurement model, the access to historical data mode, the client exit mode, which are mentioned before.

Begin
Display GUI window, control buttons, etc.

Click button
Real time measurement?

Visual interface and dynamic display of measurement results
Using java GUI tool can realize the visualization window, and displaying real-time data update.The JavaFX stage, scene class and pane class can realize visualized GUI interface [9][10][11][12][13] and convenient control command input.Data dynamic update can be implemented through timeline class.In this paper, the size of the widths of rectangle frames filled with two different colors is used to mean temperature and humidity, and the digital results of temperature and humidity are displayed in the same time.

Clicking mouse to control temperature and humidity measurement
Issuing commands to control the temperature and humidity sensors to start or stop, is achieved through adding the mouse button driver event class to the button.For example, there is a Java statement blow which can be used for adding mouse button driver event class (where "animation" is an example of the Timeline class).In the statement, after adding mouse button driver event class to button named "btMeasure1" (real-time measurement), click the button by mouse, then the system displays prompt "Real-time Measuring…" and starts the dynamic display.

Design of JFreeChart graphic
JFreeChart is a widely-used chart library for Java, which can be easily used for Java applications to show the charts of professional quality.In this paper, JFreeChart is used to display real time measurement data and historical data.In this paper, the realization of real-time data displaying method is as follows.Firstly, construct a JfreeChart chart through the implementation of the Runnable interface, and join real-time data into the timeSeries sequence.Then secondly, create a JFrame object, and add the chart into the JFrame object, and finally start the instantiate Runnable object to realize real-time display.This thread is added in the communication mode 1.
The realization of historical data displaying method is as follows.Firstly, construct a JfreeChart chart through the implementation of the Runnable interface, and query the database files, add all the specified temperature and humidity data into the XYSeries sequence of the chart.Then secondly, create a JFrame object, and add the chart into the JFrame object, and finally start the instantiate Runnable object to realize real-time display.This thread is added in the communication mode 3.
Due to the chart for real-time and historical data display is achieved by starting the instantiate Runnable object, the chart can be shown simultaneously with the other functions of the Java program.

Operation results
Raspberry Pi server is connected to the Internet/Intranet by network cable and connected with DHT11 temperature and humidity module through the GPIO port.The physical map is shown in Figure7.Before work, Raspberry Pi server must be started, waiting for client connection requests.The client of this measurement system is a computer connected to the net.When the application is started, the client computer displays initial interface, and on the top the interface, there are four buttons on behalf of the four kinds of network model respectively.The left side is the server IP and port input box, and the intermediate region for the schematic real-time displaying, while the State bar below is for the prompt displaying.As shown in Figure 8. Input the server IP and port number, click the connection button, State bar will display the prompt language "Client has get the address, waiting for demand...".
When "Real-time measurement" button is pressed, program will start real-time measurement.The the middle part of control interface is blank., before starting measurement, and now the region display two rectangular boxes with different colors for sketching the size of temperature and humidity.Meanwhile, two JfreeChart windows are generated to display the real-time measurement graphs.The graphs show the recent 300s temperature and humidity real-time changes, as shown in Figure 9.When the "Background measurement" button is pressed, the program will start the server to automatically process measuring in the background and store data in the database.If real-time measurement have started before, the JfreeChart window will hide and the center temperature and humidity display area diagram will stop updating, being in the last state.As shown in Figure 10.When the "access to historical data" button is pressed, the program will start the server to upload the database file, which contains all measuring data in the Raspberry Pi server.A new JfreeChart window will be started to display all the data of temperature and humidity, and status bar will show the time period for the historical data.This button can be executed concurrently with other tasks, without affecting previous operations.Temperature and humidity changes of a secondary screen cabinet in 3 days time is shown in Figure 11.It can be seen in the Figure that the temperature change is little, and humidity change is great.The humidity was once more than 70%, indicating that the operation and maintenance personnel should take timely measures to control the over-limit humidity.When the "exit" button is pressed, all the displaying windows withdraw, and the program send commands to the server to get into the state of automatic continuous measurement, storing data in the database and waiting for a new connection in the same time.
Operation results show that the system has rapid response, complete data, clear graphics, reliable measurement results, which can greatly facilitate the operation and maintenance personnel to monitor the temperature and humidity of key places of substation.

Conclusion
In this paper, a temperature and humidity remote real-time measurement system is designed based on Raspberry Pi and Java language, and it realizes the data continuously measured and database generated on-site.Because the system uses Raspberry Pi, which can act as both on-site measurement controller and server, as a controller, so the database operation is assigned to each measurement server, thereby reducing the operation burden of client and thus improving the efficiency of the connection.This system is composed of client and server side, and can add multiple clients and servers according to need, so it has with great flexibility.The server uses multi thread programming, which can achieve communication between multiple clients and one server.While the client can also achieve communicate with multiple servers by inputting different IP and port.As a result, the system can realize the multidirectional flow of information.The client interface uses java visualization interface and JfreeChart for programming, and the interface can be changed according to need, with a strong scalability.
The system can also be used in other places where the temperature and humidity monitoring is needed.The communication architecture and programming method used in this system can also be used in other measurement and monitoring fields.

Figure 3 .
Figure 3. Wiring diagram between the DHT11 module and the PI pins

Figure 4 .
Figure 4. Program flow chart of DHT11 temperature and humidity measurement module

Figure 5 .
Figure 5. Multi-threaded program diagram of Raspberry Pi server

Figure 6 .
Figure 6.Java program flow chart of client

Figure 7 .
Figure 7. Picture of Raspberry Pi server and DHT11 module

Figure 8 .
Figure 8.The client initial interface of temperature and humidity measurement system

Figure 9 .
Figure 9.The client interface state of "real time measurement" mode

Figure 10 .
Figure 10.The client interface state of "background measurement" mode

Figure 11 .
Figure 11.The client interface state of "access to historical data" mode