LoginSignup
3
6

More than 5 years have passed since last update.

ラズパイの温度を表示できるプログラムを作ってみた

Last updated at Posted at 2018-09-19

はじめに

ラズパイはsshで接続し操作したりと遠隔で操作する事が多いと思いますが、様々な人が温度問題で苦しんでいると思います。そこでXserver等で外部からグラフィカルにCPUとGPUの温度を表示させることのできるプログラムを作りました。

このプログラムを動かした環境

  • python2.7
  • Tkinter
  • RaspberryPi3 model B
  • raspbian

使い方

プログラムを実行すると  
window0.png  
このようなウィンドウが出てくるので後はボタンをクリックするだけです。

window1.png
 クリックした結果

実際に作成したコード

python3系を使用する際はTkinterをすべてtkinterに置換すれば動くと思われます。
検証はしていないので必要な人はやってみてください。
Githubのリンク貼っておきますので是非cloneして使ってください。

raspi_cpu_gpu_temp.py

    # coding:utf-8
    import os
    import Tkinter

    #make window
    root = Tkinter.Tk()
    root.title(u"RasPi_temp")
    root.geometry("350x50")

    #button push command
    def DeleteEntryValue(event):
        cpu_temp,gpu_temp=Temp_get()
        EditBox.delete(0, Tkinter.END)
        EditBox.insert(Tkinter.END,"    CPU_temp="+str(cpu_temp)+"℃  "+"GPU_temp="+gpu_temp+"℃")

    #get temp
    def Temp_get():

        cpu_file="/opt/vc/bin/vcgencmd measure_temp"
        gpu_file="cat /sys/class/thermal/thermal_zone0/temp"

        cpu_line = os.popen(cpu_file).readline().strip()
        gpu_line = os.popen(gpu_file).readline().strip()
        cpu_temp = cpu_line.split('=')[1].split("'")[0]
        gpu_temp = float(gpu_line)/1000

        return cpu_temp,str(gpu_temp)

    #make empty box
    EditBox = Tkinter.Entry(width=35)
    EditBox.insert(Tkinter.END,"")
    EditBox.pack()

    #button setting
    Button = Tkinter.Button(text=u'raspi_temp', width=25)
    Button.bind("<Button-1>",DeleteEntryValue) 

    Button.pack()
    root.mainloop()

さいごに

個人的には初めて使ったTkiterで、初めて作るラズパイのプログラムの割には面白い物が作れたかなと思います。

今後はいちいちXserverを使用するのは面倒なので、端末やターミナル、コマンドプロンプト等に温度を表示し続けるプログラムも作ってみようかなと思います。

3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6