In this project I take a BBC micro:bit and a Pimoroni scroll:bit and use them, along with some Python code, to display the Raspberry Pi 4’s CPU temperature.
Here’s a taste of how it looks in operation.
Basically, the micro:bit is plugged into the scroll:bit, and then the micro:bit is attached to the Raspberry Pi 4 with a micro USB cable.
NOTE: You will need the Python psutil module for this to work. If it’s not installed already then execute at the command line: sudo apt install python-psutil -y
The following bit of Python is what provides the data to, and control of, the scroll:bit.
#! /usr/bin/env python3import serial, psutil, time, signal, sysdef sigint_handler(signum, frame):print()sys.exit(0)signal.signal(signal.SIGINT, sigint_handler)ser = serial.Serial("/dev/ttyACM0", 115200, timeout=1)ser.close()ser.open()print("Started monitoring system temperature for scroll:bit display on micro:bit")ser.write("import scrollbit \r".encode())time.sleep(0.1)while True:sysTemp = "%d C" % int(psutil.sensors_temperatures()['cpu-thermal'][0].current)ser.write("scrollbit.scroll(\"%s\") \r".encode() % sysTemp.encode())time.sleep(5.0)
Only 21 lines total to display the RPi’s CPU temperature on the scroll:bit.
The real work is done on lines 19 and 20. Line 19 reads the temperature and formats it into a string, and then line 20 uses that string to create the REPL command that will be sent to the micro:bit to scroll the temperature. I used scrolling because it’s so simple to use, it automatically shows the complete string which is longer than the display can show, and it automatically cleans up after itself. It would take three separate scroll:bit commands (write, show, then clear) to do what the scroll command does. The five second sleep allows the string to scroll all the way across before the next one is sent. This keeps memory usage on the micro:bit to a minimum.
Note that when you set this up, you need to have dropped scrollbit.py onto the micro:bit. You can follow the directions for how to do that here: https://github.com/pimoroni/micropython-scrollbit
I wrote this to see if I could learn how to use these three separate devices. This project allowed me to tie them all together and learned not just how they work individually, but how to integrate them into something interesting, and possibly useful. I also wanted to create a reminder that the processor temperature is very high, perhaps too high.
Update 28 July
I updated the original Python script. I added a Control C handler to make exiting via Control C clean, and moved the script from Python 2 to Python 3. Finally the script has a she-bang at the top; change the script to executable (chmod +x [scriptname.py]) and you can start it at the command line without prefacing it with python3.
How does knowing the temperature assist you?
LikeLike
First of all it’s not supposed to be this high, especially under idle (i.e. no load). Second, I want to keep an eye on the temperature as I work with this and see how higher it gets. If it gets high enough then the CPU will throttle back. Finally, temperatures this high in shirt-sleeve ambient temperatures are never a good sign, especially if you want to use this as a base for an unattended system performing some continuous task.
These temperatures are all in Centigrade. The typical idle temperature on this board’s CPU of 75 C is 167 F.
LikeLiked by 1 person
Sounds like an interesting pastime.
LikeLike
It keeps me off the streets.
As another data point, I moved the code and hardware over to my Raspberry Pi 3 Model B Plus Rev 1.3. Idle temperature is 48 C. That’s 27 C lower, and this is with the same OS (Raspbian Buster) running on both. Yes, the 3B+ is slower, but I contend the temperature should be higher on the 4, it shouldn’t be that hot on the 4.
LikeLike
[…] USB. I’ve been using it with my Raspberry Pi for as a simple CPU temperature readout, as you can read in my earlier post. Working on that temperature readout utility taught me how to communicate with the micro:bit and to […]
LikeLike