BOINC and a Raspberry Pi CPU usage LED

Lately I've been wanting better ways of burning electricity in my house to keep the basement above freezing in winter. The cost of electricity isn't high enough to justify investing in a heat pump, but I want to do something better with the electricity than just turning it into heat. Enter BOINC.

BOINC is a platform for distributing embarassingly parallel scientific computations. I already have a machine in the kitchen handling a webcam, and I have it contributing to PrimeGrid via BOINC. But I also have some Raspberry Pi's sitting around doing nothing. The Pi isn't supported by PrimeGrid. Instead I have chosen World Community Grid, mostly because of its low bandwidth usage.

To be able to tell whether a Pi is actually performing computations without attaching a display to it, a CPU usage indicator LED is useful. Pin 18 on the GPIO header can be used for this. I built some small LED modules using 2-pin socket headers, 1 kΩ resistors and red LEDs that connect nicely between pins 18 and 20 on the header. A small python script drives the LED on each Pi.

Setup

First you need psutil for python which can be installed by issuing the command sudo apt install python-psutil. Next you want to paste the following code into a file called cpuled.py. Save it to your home directory.

import psutil
import RPi.GPIO as IO

led = 18

IO.setwarnings(False)
IO.setmode(IO.BOARD)
IO.setup(led, IO.OUT)
pwm = IO.PWM(led, 100)
pwm.start(0)

while True:
 p = psutil.cpu_percent(interval=0.1)
 #print(p, p*p/100)
 pwm.ChangeDutyCycle(p*p/100)

The p*p/100 is to compensate for the eye's non-linear response to light intensity. It's crude, but it works. To get the script to start when the Raspberry Pi boots, edit the pi user's crontab with crontab -e and add the following line to the end of it:

@reboot nohup python cpuled.py

Connect a LED and a resistor between pins 18 and 20, reboot the Pi and the LED should indicate its CPU usage. It should look something like the following GIF, which shows a Pi 3 and a Pi 4 on top of each other, each with one LED pulsing:

Two Raspberry Pi's with a CPU usage LED each

Both Pi's are contributing to the World Community Grid. Specifically they are running calculations relating to the COVID-19 pandemic. From the pulsing it's possible to tell that the computations don't make the best use of each CPU. They're sitting idle around 25% of the time.

Closing remarks

(Un)fortunately even the Pi 4 uses only about 5 W of power, so getting larger amounts of heat requires quite a few of them. December and January typically needs 500 kWh of heat per month, or 700 W. Obviously I'm not going to buy hundreds of Pi's to generate that. What's more likely is running old-ish computers from the university.