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.
You must be logged in to post a comment.