This article refers to the following articles: Xukai871105 's csdn: Raspberry Pi Study notes-get Raspberry Pi CPU Temperature A brief book on the landscape: several ways to set up a Raspberry Pi boot
CPU is the core of Raspberry Pi hardware, is also the main source of heat. The official standard Raspberry Pi does not have thermal components, such as heat sinks, fans, etc., if the CPU temperature is too high will cause overheating protection, the life also has a certain impact. Usually buy some heatsink can improve this problem, but also need to write tools real-time query and display CPU temperature, play the role of the hint.
QT is a cross-platform C + + programming framework, known as "write once, compile everywhere." QT is also available in the Raspberry Pi Software Library, which shows that QT also has good support for Raspberry Pi. This blog post describes a tool that is written in QT with a real-time display of the CPU temperature of the Raspberry Pi. How to view the CPU temperature of Raspberry Pi
The Raspberry Pi temperature monitoring mechanism is such that the temperature of the CPU is detected and written into the system files, and timed refresh. The specific path to this file is/sys/class/thermal/thermal_zone0/temp, which is a read-only file. We only need to access this file to get the current CPU temperature. Viewing through the graphical interface is simple and only provides a way to view it using terminal commands.
Enter the following command at the terminal:
Cd/sys/class/thermal/thermal_zone0
After entering the directory where the temp file is located, enter the following command:
Cat Temp
System output (Take my Raspberry Pi as an example): 51540
Note: This value needs to be divided by 1000 to obtain the actual temperature, in degrees Celsius. Then my Raspberry Pi CPU current temperature is about 52 ℃. Software Design Ideas
The main function of this gadget is to display the CPU temperature of the Raspberry Pi, which should have the following characteristics: resident Desktop : Monitoring of CPU temperature is a continuous process. On the one hand, we should always know the CPU temperature of Raspberry Pi, on the other hand, tool window can not interfere with normal work. There are two implementations, the desktop widget/tray icon, and finally the way I chose the tray icon. Real-time display : The CPU temperature is constantly changing, the tool must constantly query the CPU temperature, and then display to the tool interface. Implementation mode: Set a timer, by reading the second way constantly access the temp file, and refresh the tool display. Boot start : Showing CPU temperature is an important requirement, it is necessary to start the tool every time using the Raspberry Pi, so it is imperative to set up the tool boot. The specific implementation method is described in the following article. Writing Programs
Note: Reading this section requires a certain C + + and QT Foundation. front-facing conditions
Assume that you have a raspbian system of Raspberry Pi and installed Qt creator and Qt5. Establish Engineering
Qt Creator is basically the same for each platform, not detailed here. I built the project named CPU and set the base class to be qdialog. Implement pallet Display
The first thing to do is hide the main window. The function of this program is through the tray icon, so the main window does not need to be displayed. Enter main.cpp and delete the following code:
W.show ();
Then find a tray icon of the material, this person likes to see. Since it is a tool for displaying CPU temperature, I have chosen this image:
It is important to note that because the CPU temperature is to be displayed, the center blank of the picture is good. The picture is then imported into the project's resource file. This step is relatively simple and no longer explained. In my project, the path to the picture is "://cpu.png". In this way, the footage is ready to complete.
In terms of programming, QT provides class Qsystemtrayicon related to pallet operations. We use this class to create a tray icon object icons. The Qpixmap class is required for the display of the graphic, and the icon object is an image. The following is the detailed code.
dialog.h
First introduce the header file:
#include <QSystemTrayIcon>
#include <QPixmap>
To define a pallet object:
Private:
Qsystemtrayicon *icon;
Qpixmap image;
Dialog.cpp
Initialize the pallet object and the icon object in the constructor, with the following code:
Image.load ("://cpu.png"); Read icon resource file
: Icon = new Qsystemtrayicon (this);
Icon->seticon (Qicon (image));
Icon->show (); Show tray icon
You can look at the effect of this step. On my Raspberry Pi, the result of the operation is this:
Because the main window is hidden, the program cannot be closed. As a program for monitoring CPU temperature, users are less likely to actively shut down, but it is still necessary to write this function. Therefore, add the following code to the dialog.cpp constructor:
Connect (icon,signal (activated (Qsystemtrayicon::activationreason)),
Qapp, SLOT (Quit ()));
The operation signal of the tray icon is connected to the exit slot of the program, i.e. the mouse clicks on the tray icon and the program exits. Implementing query Functionality
From the above Raspberry Pi CPU Temperature View method , the query process is relatively simple, does not involve hardware operations, just file access. So to use the Qfile class, we define a file pointer, temp, to access the text file that holds the CPU temperature.
dialog.h
Introduce header file:
#include <QFile>
#include <QTextStream> //Text stream class for reading and writing text files.
To define a file pointer:
Private:
QFile *temp;
Dialog.cpp
Initialize temp in the construction file, where the string is the path to the Raspberry Pi CPU temperature file:
temp = new QFile ("/sys/class/thermal/thermal_zone0/temp");
At this point you can test to see if you can output the temperature of the system, add the following code after the above:
/******************** Test Content ********************/
if (!temp->open (qiodevice::readonly | Qiodevice::text)) //Open the CPU temp file.
{
qdebug () << "failed to read file"; Need to introduce <QDebug> header files.
qapp->exit (); Program exits.
}
else
{
qtextstream stream (temp);
QString string = Stream.readall (); Read temperature.
temp->close (); Close the file.
long s = String.tolong ()/+; Converts the number of growth shaping, in degrees Celsius.
qdebug () << "CPU temperature is" <<s<< "℃";
}
/******************** Test Content ********************/
Run the program, normally output the following statement on the console: "CPU temperature is Xx℃". Set Timer
We need to set up a timer to ensure that the CPU temperature is constantly queried and displayed. In Qt, the use of the timer is relatively fixed, the basic follow these 4 steps: Define the timer pointer, write the slot function, the timer time exhausted signal and slot function connection, start timer. The following is the detailed code.
dialog.h
Introducing the Timer header file:
#include <QTimer>
Define timer pointer, claim slot function update ():
Private:
Qtimer *timer;
Private slots:
void update ();
Dialog.cpp
Add the following code to the constructor:
Timer = new Qtimer (this);
Connect (timer, SIGNAL (), this, SLOT (update ()));
Timer->start (3000);
The first line initializes the timer pointer, the second line is the timer's time drain signal and the update () slot function connection, the third line is the start timer. According to the description of the start (int) function, the parameter is in milliseconds, and 3000 milliseconds is 3 seconds.
Instructions for setting 3 seconds: I think monitoring CPU temperature is not necessary to update frequently, and the temp file that stores the CPU temperature of the Raspberry Pi is also relatively slow to update itself.
Of course, add the implementation of the update () function:
void Dialog::update ()
{
}
At this point, you can copy the above test code, without modification, to the function body of update () and run. Normally, the program will output the following statement at the console every 3 seconds: "CPU temperature is Xx℃".
Please see the next blog post for more information.
The Code of the tool and the executable program download address:
http://download.csdn.net/detail/u012952807/9825067