Howto get battery info (pi-top [1], raspi 4, pi-topOS - Bullseye)

Managed to get information about the battery with the actual pi-topOS.

Started with some small bash-scripts.

SoC.sh

#!/bin/bash
hexadezimal=$(i2cget -y 1 0x0b 0x0d w)
dezimal=$(printf "%d" "$hexadezimal")
#devided=$(echo "scale=3; $dezimal / 10000" | bc)
echo "State of Charge: $dezimal %"

runTimeToEmpty.sh

#!/bin/bash
hexadezimal=$(i2cget -y 1 0x0b 0x11 w)
dezimal=$(printf "%d" "$hexadezimal")
#devided=$(echo "scale=3; $dezimal / 100000" | bc)
if [ "$dezimal" -eq 65535 ]; then
  echo "Powercord connected"
else
  stunden=$((dezimal / 60))
  rest=$((dezimal % 60))
  #echo "RunTimeToEmpty: $dezimal min"
  echo "RunTimeToEmpty: $stunden h  $rest min"
fi

And ended up with a little hooked up i2c display to show the battery state:

Prerequisites

pip3 install board
pip3 install adafruit-circuitpython-ssd1306
sudo apt-get install python3-pil

SoC_Display.py
/home/pi/python/

#!/usr/bin/env python3
import smbus
import time

import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_ssd1306
import time

bus = smbus.SMBus(1)
address = 0x0b
register = 0x0d
timeRegister = 0x11

#Change these
#to the right size for your display!
WIDTH = 128
HEIGHT = 32  # Change to 64 if needed
BORDER = 1

#Use for I2C.
i2c = board.I2C()  # uses board.SCL and board.SDA

oled = adafruit_ssd1306.SSD1306_I2C(WIDTH, HEIGHT, i2c, addr=0x3C)

#Load default font.
font = ImageFont.load_default()

while True:

    hex_timeValue = bus.read_word_data(address, timeRegister)
    decimal_timeValue = hex_timeValue & 0xFFFF

    if decimal_timeValue == 65535:
        timeText = ("Laden")
    else:
        hours = decimal_timeValue // 60
        minutes = decimal_timeValue % 60
        timeText = (f"{hours}h {minutes}m")
    time.sleep(1)

    hex_value = bus.read_word_data(address, register)
    decimal_value = hex_value & 0xFFFF
    text = (f"{decimal_value}%")

    oled.fill(0)
    oled.show()
    
    #Create blank image for drawing.
    #Make sure to create image with mode '1' for 1-bit color.
    image = Image.new("1", (oled.width, oled.height))

    #Get drawing object to draw on image.
    draw = ImageDraw.Draw(image)

    #Draw a white background
    draw.rectangle((0, 0, oled.width, oled.height), outline=255, fill=255)

    #Draw a smaller inner rectangle
    draw.rectangle(
        (BORDER, BORDER, oled.width - BORDER - 1, oled.height - BORDER - 1),
        outline=0,
        fill=0,
    )

    combinedText = timeText + " / " + text

    draw.text(
        (4,4),
        combinedText,
        font=font,
        fill=255,
    )

battery.service
/etc/systemd/system/

[Unit]
Description=Gibt den SoC auf einem OLED Display aus

[Service]
ExecStart=/usr/bin/python3 /home/pi/python/SoC_Display.py
WorkingDirectory=/home/pi/python
Restart=always
User=pi

[Install]
WantedBy=multi-user.target

Does this mean that we can’t ever have the battery information and the speaker working with pitop os?

Asumed to my experience, not out of the box.

Well. I found an old bullseye image.

I installed it and the battery is showing and the speaker is working, but it is doing some updates now so it may break again.

Do you have a raspi 3 or 4?

It is a 4.

As suspected the battery monitor broke and doesn’t appear anymore.

The speaker is a weird one. It still works but only as snd_rpi_hifiberry_dac

It was working until the other day. I am not sure if it was because I hadn’t updated it or what.

The supposed hub fix doesn’t work. It needs some python3 common dependencies that can’t be installed.

I tried also adding the pitop devices on raspberry pi os 11 but it is the same.