RPi Zero W at 100% CPU running Python3 Script on SenseHAT

General Information

I installed the RPi SenseHAT on a RPi Zero W running DietPi. I created a Python Script to control the Joystick and the LED Matrix Output (code and screenshot below). I added a Service file to have the script start at boot, however the Service is causing the CPU to run at 100% all the time - unless an event is triggered by the Joystick, which brings the CPU usage down. When the event has finished, the CPU usage returns to 100%.

Is this normal? Did I set up the Python Script incorrectly? Did I set up the Service file incorrectly? Is there a better way to have this script run at boot?

Required Information

  • DietPi Version:
    • G_DIETPI_VERSION_CORE=8
    • G_DIETPI_VERSION_SUB=6
    • G_DIETPI_VERSION_RC=1
    • G_GITBRANCH=‘master’
    • G_GITOWNER=‘MichaIng’
    • G_LIVE_PATCH_STATUS[0]=‘applied’
    • G_LIVE_PATCH_STATUS[1]=‘applied’
  • Distro Version | bullseye 1
  • Kernel Version | Linux SensePi 5.15.32+ #1538 Thu Mar 31 19:37:58 BST 2022 armv6l GNU/Linux
  • SBC Model | RPi Zero W (armv6l)
  • Power Supply Used | 5V 2.1A Anker Power Block
  • SD Card Used | MicroCenter 16GB SD Card

Steps to Reproduce

  1. Add Python Script to ‘systemd’ folder (/lib/systemd/system).
  2. Use DietPi-Services to add missing Service (Controlled by DietPi) to run Service at Boot.
  3. Reboot.
  4. Review htop Stats.

Expected Behaviour

The Pi should NOT be running at 100% CPU Usage while Idle

Actual Behaviour

The Pi is running at 100% CPU Usage while Idle, and drops to 5%-20% when SenseHAT Joystick is used.

Extra Details

systemd Service File - sensehat.service
[Unit]
Description=SenseHAT Temp Display
After=multi-user.target

[Service]
Type=idle
ExecStart=/usr/bin/python3 /root/tempctrl.py
User=root

[Install]
WantedBy=multi-user.target
Python Script File
#!/usr/bin/python
from sense_hat import SenseHat
import time
from time import sleep

sense = SenseHat()
sense.set_rotation(180)
sense.clear()

red = (255,0,0)
green = (0,255,0)
blue = (0,0,255)

def tempcolor():
    temp = round(sense.get_temperature()*1.8 +32)
    sense.low_light = True
    if temp > 78:
        sense.clear(red)
    elif temp < 78 and temp > 65:
        sense.clear(green)
    elif temp < 65 and temp > 0:
        sense.clear(blue)
    sleep(5)
    sense.clear()
    sense.low_light = False

def temperaturecolor():
    temp = round(sense.get_temperature()*1.8 +32)
    sense.low_light = True
    if temp > 78:
        sense.clear(red)
    elif temp < 78 and temp > 65:
        sense.clear(green)
    elif temp < 65 and temp > 0:
        sense.clear(blue)
    sleep(5)
    sense.show_message((str(temp) + "F "), scroll_speed=(0.15), back_colour= [0,0,0])
    sense.clear()
    sense.low_light = False

def humiditycolor():
    humidity = round(sense.get_humidity())
    sense.low_light = True
    if humidity > 55:
        sense.clear(red)
    elif humidity < 55 and humidity > 30:
        sense.clear(green)
    elif humidity < 30 and humidity > 0:
        sense.clear(blue)
    sleep(5)
    sense.show_message((str(humidity) + "%"), scroll_speed=(0.15), back_colour= [0,0,0])
    sense.clear()
    sense.low_light = False

def temperature():
    temp = round(sense.get_temperature()*1.8 +32)
    sense.show_message((str(temp) + "F "), scroll_speed=(0.15), back_colour= [0,0,0])

def humidity():
    humidity = round(sense.get_humidity())
    sense.show_message((str(humidity) + "%"), scroll_speed=(0.15), back_colour= [0,0,0])

while True:
    for event in sense.stick.get_events():
        if event.action == "pressed":
            if event.direction == "down":
                temperaturecolor()
            elif event.direction == "up":
                humiditycolor()
            elif event.direction == "left":
                temperature()
            elif event.direction == "right":
                humidity()
            elif event.direction == "middle":
                tempcolor()
            sleep(0.5)
Screenshots


The Script is running via a .service File added to Diet Pi Services. At idle, the CPU stays at 100%.

SenseHAT
Using the SenseHAT Joystick to trigger an event…


When pressing the SenseHAT Joystick to trigger an event on the Sense HAT LED Matrix, CPU Usage drops to around 15% while the script is running an event.


Screenshot of the .service file.


Screenshot of the added Service on the DietPi-Services screen.

how does it looks like if you start the script manually? Theoretically you could start your script using crontab as well Crontab Reboot: Execute a Job Automatically at Boot | phoenixNAP.

I stopped the ‘Service’ and ran the script on it’s own - it’s running at 100% just like before. Which I suppose means that there is a problem with my Python code? See attached screenshots.

I thought about using a crontab @reboot, but I figured adding it as a System Service would be easier to manager plus it gives me the ability to give it a low boot priority.

Thoughts on the coding?


Sorry I’m not able to assist further as I’m not a python export and I’m not able to analyse your script.

No problem at all. I’ll close this thread since it is no longer relevant to Diet Pi.