Raspberry Pi CPU Temperature Log
If you have any questions or get stuck as you work through this in-class exercise, please ask the instructor for assistance. Enjoy!
Installing The Necessary Software
- Open up a terminal shell, this can be done by clicking on its icon (see right)
- Alternatively, a new shell can be opened by pressing
crtl+alt+t
- Alternatively, a new shell can be opened by pressing
- Next, ensure that matplotlib is installed by entering the command
sudo apt-get install python3-matplotlib
- If it installs or confirms that matplotlib is installed, proceed to the next step.
Creating The Python Script
- Enter
python3
to open the Python interpreter. - Enter these lines of code in order:
from gpiozero import CPUTemperature cpu = CPUTemperature() print(cpu.temperature)
This simply gets the temperature of the CPU and prints it.
- Press “ctrl” and “D” on your keyboard to exit the Python interpreter.
- Create a new Python file:
sudo nano cpu_temp.py
- Now let’s make the script more robust by having a log of the temperatures. To do this, enter the following lines:
from gpiozero import CPUTemperature from time import sleep, strftime, time from gpiozero import CPUTemperature cpu = CPUTemperature() with open(“cpu_temp.csv”, “a”) as log: while True: temp = cpu.temperature log.write(‘{0},{1}\n’.format(strftime(‘%Y-%m-%d %H:%M:%S’), str(temp)))
- Running this code will keep a log in the form of a CSV file. This file can be found in /home/pi and will be called cpu_temp.csv.
- Try running the code and finding the file.
- Now that a log is kept, visualization in the form of a realtime graph can be scripted.
- Enter this to draw a graph of the temperature:
from gpiozero import CPUTemperature from time import sleep, strftime, time import matplotlib.pyplot as plt cpu = CPUTemperature() x = [] y = [] def write_temp(temp): with open('cpu_temp.csv', 'a') as log: log.write('{0},{1}\n'.format(strftime('%Y-%m-%d %H:%M:%S'), str(temp))) def graph(temp): y.append(temp) x.append(time()) plt.clf() plt.scatter(x, y) plt.plot(x, y) plt.draw() while True: temp = cpu.temperature write_temp(temp) graph(temp) plt.pause(1)
Run on Startup
- This Python script can be made to run automatically on boot.
- Open a new terminal window and enter in
crontab -e
to open crontab. - If prompted, choose nano from the options given. - Scroll to the bottom of the opened file and enter in this line in a new line:
@reboot python3 /home/pi/temp.py
- Now, reboot the Raspberry Pi by entering the command:
sudo reboot
- Once the Raspberry Pi is rebooted and has run for a bit, enter the command:
cat cpu_temp.csv
- This will display a log of the recent CPU temperature.