Just got my new RaspberryPi 4. I’d read about the CPU running hotter than previous models, so I decided to have a look for myself.


 

https://www.elinix.org/RPI_vcgencmd_usage gave details of how to do this.


 

I decided to wrap this up in a PyQt5 application so that I could monitor the temperature more easily.


 

So if anyone wants a copy here it is (to cut and paste) or download from:


 

http://www.concept-computing.co.uk

Login as Guest

 

Just got my new RaspberryPi 4. I’d read about the CPU running hotter than previous models, so I decided to have a look for myself.


 

https://www.elinix.org/RPI_vcgencmd_usage gave details of how to do this.


 

I decided to wrap this up in a PyQt5 application so that I could monitor the temperature more easily.


 

So if anyone wants a copy here it is (to cut and paste) or download from:


 

http://www.concept-computing.co.uk

 

 

Screenshot:


 


 


 


 


 


 


 


  Code:


 

#!/usr/bin/env python3

# -*- coding: utf-8 -*-

#

# cpu_temp.py

#

# Copyright 2019 alan.warden1@gmail.com

#

# This program is free software; you can redistribute it and/or modify

# it under the terms of the GNU General Public License as published by

# the Free Software Foundation; either version 2 of the License, or

# (at your option) any later version.

#

# This program is distributed in the hope that it will be useful,

# but WITHOUT ANY WARRANTY; without even the implied warranty of

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the

# GNU General Public License for more details.

#

# You should have received a copy of the GNU General Public License

# along with this program; if not, write to the Free Software

# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,

# MA 02110-1301, USA.

#

#


 

###################################

# see https://www.elinix.org/RPI_vcgencmd_usage

# for more info

###################################


 


 


 

import sys

import os


 

from PyQt5.QtWidgets import (QApplication, QDialog, QGridLayout,QLabel,

QLineEdit, QPushButton, QStatusBar, QMainWindow, QDesktopWidget)


 

from PyQt5.QtGui import (QGuiApplication, QIcon, QFont, QFontMetrics)

from PyQt5.QtCore import (Qt, QTimer)


 

class stats_dialog (QDialog):

  def __init__(self):

super(stats_dialog, self).__init__()

  # pastel yellow for the dialogue

self.setStyleSheet("background-color: rgb(250, 250, 210);")

  self.grid = QGridLayout ()

 

self.cpu_temp_lbl = QLabel ('CPU Temperature:')

self.cpu_temp_value = QLineEdit ('CPU')

self.cpu_temp_value.setReadOnly (True)

self.quit_btn = QPushButton ('&Quit')

self.shutdown_btn = QPushButton ('&Shutdown')

  self.tim = QTimer (self)

self.tim.setInterval (5000)

self.tim.start()

 

 

  # build the screen

self.grid.addWidget (self.cpu_temp_lbl,0,0)

self.grid.addWidget (self.cpu_temp_value,0,1)

self.grid.addWidget (self.quit_btn,1,0)

self.grid.addWidget (self.shutdown_btn,1,1)

self.setLayout (self.grid)

 

 

  # signals

self.tim.timeout.connect (self.tim_update)

self.quit_btn.clicked.connect (self.quit_prog)

self.shutdown_btn.clicked.connect (self.kill_computer)


 

 

  def tim_update (self):

os.system('vcgencmd measure_temp > cpu.txt')

  file_in = open ('cpu.txt')

  line = file_in.read ()

file_in.close ()

  line2 = line.split ('=')

self.cpu_temp_value.setText (line2[1])

 

  def quit_prog (self):

QGuiApplication.instance().quit()

 

  def kill_computer (self):

  os.system ('sudo shutdown -h now')

 

 

class main_win (QMainWindow):

  def __init__(self):

super(main_win, self).__init__()

self.setWindowTitle('System Stats')

  self.sb = QStatusBar ()

  self.tim = QTimer (self)

self.tim.setInterval (60000) # 1 minute

self.tim.start ()

self.setStatusBar (self.sb)

  self.dlg = stats_dialog ()

self.setCentralWidget (self.dlg)

 

  # signals

self.tim.timeout.connect(self.tim_update)

 

  def tim_update (self):

os.system('uptime > uptim.txt')

  file_in = open('uptim.txt')

  line = file_in.read ()

file_in.close ()

  line2 = line.split (',')

  msg = line2[0]

  if 'min' not in msg:

  msg = msg + ' hrs'

self.sb.showMessage ('Time: ' + msg)


 

 

def main(args):

  app = QApplication(sys.argv)

  scr = main_win()

scr.setWindowFlag (Qt.WindowStaysOnTopHint)

  scr_size = scr.geometry()

  screen = QDesktopWidget().screenGeometry()

  h_pos = (screen.width() - scr_size.width() ) / 2

  v_pos = (screen.height() - scr_size.height() ) / 2

  # Start roughly in the middle. Then move it to suit

scr.move(h_pos-h_pos/3, v_pos)

  scr.show ()

sys.exit(app.exec_())


 

if __name__ == '__main__':

sys.exit(main(sys.argv))


 


 


 

Notes:


 

This was written in Geany, you’ll need to set the build commands from python to python3 i.e

 

Notes:


 

This was written in Geany, you’ll need to set the build commands from python to python3 i.e


 

 

Notes:


 

This was written in Geany, you’ll need to set the build commands from python to python3 i.e


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Also set the Tab Type to Spaces

 

 

Regards

 

Alan