raspberry pi boot long time – no boot / reboot issue fixed

I have raspberry pi 4 with raspbian os lite installed and the OS is directly installed on SSD which is connected with USB3.0 with connector USB-to-SATA. Whenever the power outage happens the pi some time boots and sometime it doesn’t and it also takes lots of time to boot 10min+

  • First I checked with below command what services are taking longer time and disabled docker and kubelet rpi-eeprom-update
root@lp-arm-3:~# systemd-analyze blame
1min 36.522s docker.service
     51.427s containerd.service
     50.609s rpi-eeprom-update.service
     50.590s user@1000.service
     50.221s ModemManager.service
     49.702s NetworkManager-wait-online.service
     38.108s apt-daily-upgrade.service
     32.277s dpkg-db-backup.service
      2.558s dev-sda2.device
      1.281s raspi-config.service
       670ms man-db.service
       613ms NetworkManager.service
       583ms keyboard-setup.service
       536ms systemd-logind.service
       512ms systemd-udev-trigger.service
       429ms systemd-journald.service


root@lp-arm-3:~# systemd-analyze
Startup finished in 36.419s (kernel) + 2min 33.375s (userspace) = 3min 9.794s 
multi-user.target reached after 2min 33.318s in userspace.



root@lp-arm-3:~# systemd-analyze critical-chain
The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.

multi-user.target @2min 33.318s
└─kubelet.service @1min 6.646s
  └─network-online.target @55.038s
    └─NetworkManager-wait-online.service @5.315s +49.702s
      └─NetworkManager.service @4.687s +613ms
        └─dbus.service @4.552s +97ms
          └─basic.target @4.509s
            └─sockets.target @4.509s
              └─docker.socket @4.485s +23ms
                └─sysinit.target @4.468s
                  └─systemd-timesyncd.service @4.091s +375ms
                    └─systemd-tmpfiles-setup.service @3.805s +208ms
                      └─local-fs.target @3.777s
                        └─run-user-1000.mount @5.576s
                          └─local-fs-pre.target @1.650s
                            └─keyboard-setup.service @1.065s +583ms
                              └─systemd-journald.socket @1.019s
                                └─-.mount @946ms
                                  └─-.slice @946ms
  • This did not fix issue.
  • Main issue was USB SATA driver speed.
  • I enabled permanent logs for boot
journalctl --list-boots
journalctl -b -1

show only error

journalctl -b -1 -p err
Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#9 uas_eh_abort_handler 0 uas-tag 15 inflight: CMD OUT Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#9 CDB: opcode=0x2a 2a 00 01 50 21 78 00 00 08 00 Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#4 uas_eh_abort_handler 0 uas-tag 6 inflight: CMD Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#4 CDB: opcode=0x35 35 00 00 00 00 00 00 00 00 00 Feb 07 20:40:53 lp-arm-3.home kernel: scsi host0: uas_eh_device_reset_handler start Feb 07 20:40:53 lp-arm-3.home kernel: usb 2-2: reset SuperSpeed USB device number 2 using xhci_hcd Feb 07 20:40:53 lp-arm-3.home kernel: scsi host0: uas_eh_device_reset_handler success Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#8 UNKNOWN(0x2003) Result: hostbyte=0x07 driverbyte=DRIVER_OK cmd_age=61s Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#8 CDB: opcode=0x28 28 00 00 5e dc f8 00 00 10 00 Feb 07 20:40:53 lp-arm-3.home kernel: I/O error, dev sda, sector 6216952 op 0x0:(READ) flags 0x80700 phys_seg 2 prio class 2 Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#2 UNKNOWN(0x2003) Result: hostbyte=0x07 driverbyte=DRIVER_OK cmd_age=61s Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#2 CDB: opcode=0x28 28 00 02 88 f5 e0 00 00 30 00 Feb 07 20:40:53 lp-arm-3.home kernel: I/O error, dev sda, sector 42530272 op 0x0:(READ) flags 0x80700 phys_seg 6 prio class 2 Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#1 UNKNOWN(0x2003) Result: hostbyte=0x07 driverbyte=DRIVER_OK cmd_age=61s Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#1 CDB: opcode=0x28 28 00 00 5e cb c8 00 01 00 00 Feb 07 20:40:53 lp-arm-3.home kernel: I/O error, dev sda, sector 6212552 op 0x0:(READ) flags 0x80700 phys_seg 32 prio class 2 Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#3 UNKNOWN(0x2003) Result: hostbyte=0x07 driverbyte=DRIVER_OK cmd_age=61s Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#3 CDB: opcode=0x28 28 00 02 84 ee 68 00 00 50 00 Feb 07 20:40:53 lp-arm-3.home kernel: I/O error, dev sda, sector 42266216 op 0x0:(READ) flags 0x80700 phys_seg 10 prio class 2 Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#6 UNKNOWN(0x2003) Result: hostbyte=0x07 driverbyte=DRIVER_OK cmd_age=55s Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#6 CDB: opcode=0x28 28 00 02 80 6e 38 00 00 90 00 Feb 07 20:40:53 lp-arm-3.home kernel: I/O error, dev sda, sector 41971256 op 0x0:(READ) flags 0x80700 phys_seg 18 prio class 2 Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#7 UNKNOWN(0x2003) Result: hostbyte=0x07 driverbyte=DRIVER_OK cmd_age=49s Feb 07 20:40:53 lp-arm-3.home kernel: sd 0:0:0:0: [sda] tag#7 CDB: opcode=0x28 28 00 02 80 3c d0 00 00 58 00 Feb 07 20:40:53 lp-arm-3.home kernel: I/O error, dev sda, sector 41958608 op 0x0:(READ) flags 0x80700 phys_seg 11 prio class 2
  • The kernel is resetting the USB
  • Disabled UAS for SSD – fixed the issue
lsusb

#get the USB ID eg 7825:a2a4

vi /boot/firmware/cmdline.txt

append this line at the end

usb-storage.quirks=7825:a2a4:u

reboot

root@lp-arm-3:~# lsusb -t
/:  Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
    |__ Port 2: Dev 2, If 0, Class=Mass Storage, Driver=usb-storage, 5000M
/:  Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
    |__ Port 1: Dev 2, If 0, Class=Hub, Driver=hub/4p, 480M

usb-storage (BOT):

sends one command at a time
waits for completion
has simpler recovery
tolerates retries and delays

Get indoor temperature from your LG AC WIFI

  • Install the pythinqconnect package
 pip3 install git+https://github.com/thinq-connect/pythinqconnect.git --break-system-packages
  • Get PAT token generated
https://connect-pat.lgthinq.com/tokens
  • Get Device id
import asyncio
from aiohttp import ClientSession
# Assuming a library structure like pythinqconnect
from thinqconnect.thinq_api import ThinQApi 

############## Get device ID ###############
async def test_devices_list():
    # Setup your credentials and personal access token (PAT)
    async with ClientSession() as session:
        thinq_api = ThinQApi(
            session=session, 
            access_token='PAT',
            country_code='',  # Example: US, KR, NL
            client_id='your_client_id'
        )
        # Fetch device list
        response = await thinq_api.async_get_device_list()
        print("device_list : %s", response)

# Run the async function
asyncio.run(test_devices_list())
  • Get temp
async def get_indoor_temperature_c():
    async with ClientSession() as session:
        api = ThinQApi(
            session=session,
            access_token="PAT",
            country_code="",
            client_id="YOUR_CLIENT_ID"
        )

        status = await api.async_get_device_status(DEVICE_ID)

        temp_c = status["temperature"]["currentTemperature"]

        print(temp_c)
        return temp_c

asyncio.run(get_indoor_temperature_c())
@home:~/Downloads/c$ python3 lg.py 
29.5

https://smartsolution.developer.lge.com/en/apiManage/device_profile?s=1734593490507#tag/overview

https://github.com/thinq-connect/pythinqconnect/tree/main

BME680 sensor permanent fix for 0x77 and 0x76 on rasp pi 5

  • After reboot the
root@pi5:~# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- 76 -- 
  • After removing the power pin and reattaching
root@pi5:~# i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:                         -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- 77 
  • ValueError: No I2C device at address: 0x77
  • Solutions:
  • Some BME680 boards default to 0x76 so set it as default.
  • Update the code to try both 0x76 and 0x77

camera raspberry pi

raspistill -o image_$(date +%s).jpg -w 480 -h 360
root@lp-arm-4:~# raspistill -o image_$(date +%s).jpg -w 480 -h 360
mmal: Cannot read camera info, keeping the defaults for OV5647
mmal: mmal_vc_component_create: failed to create component 'vc.ril.camera' (1:ENOMEM)
mmal: mmal_component_create_core: could not create component 'vc.ril.camera' (1)
mmal: Failed to create camera component
mmal: main: Failed to create camera component
mmal: Only 76M of gpu_mem is configured. Try running "sudo raspi-config" and ensure that "memory_split" has a value of 128 or greater

/boot/firmware/config.txt add gpu_mem=128

boot order – Raspbery pi

  • Edit boot order(read from right to left)
1. 4 = Read from USB
2. 1 = Read from SD Card
3. 0xf = Restart boot order

rpi-eeprom-config -e
ValueModeDescription
0x0SD CARD DETECTTry SD then wait for card-detect to indicate that the card has changed – deprecated now that 0xf (RESTART) is available.
0x1SD CARDSD card (or eMMC on Compute Module 4).
0x2NETWORKNetwork boot – See Network boot server tutorial
0x3RPIBOOTRPIBOOT – See usbboot
0x4USB-MSDUSB mass storage boot – See USB mass storage boot
0x5BCM-USB-MSDUSB 2.0 boot from USB Type C socket (CM4: USB type A socket on CM4IO board). Not available on Raspberry Pi 5.
0x6NVMECM4 and Pi 5 only: boot from an NVMe SSD connected to the PCIe interface. See NVMe boot for more details.
0x7HTTPHTTP boot over ethernet. See HTTP boot for more details.
0xeSTOPStop and display error pattern. A power cycle is required to exit this state.
0xfRESTARTRestart from the first boot-mode in the BOOT_ORDER field i.e. loop

boot order: https://www.raspberrypi.com/documentation/computers/raspberry-pi.html#BOOT_ORDER

Raspberry Pi Max current with GPIO for LED

Note: withdrawing more current(~15mA) from GPIO can affect the Rpi
https://raspberrypi.stackexchange.com/questions/9298/what-is-the-maximum-current-the-gpio-pins-can-output

  • Use 5V Pin (2,4)
  • Use GPIO as Negative volt
  • Max current withdrawn was 23mA for my use case
import RPi.GPIO as GPIO
from time import sleep 
import sys

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup(21, GPIO.OUT, initial=GPIO.HIGH)

if sys.argv[1] == "on":
    GPIO.output(21, GPIO.LOW)
    #print("on")
    exit()

Fore More brightness:

  • Add multiple GPIO as GROUND
  • After adding GPIO20 as ground it was taking ~18mA from GPIO20
  • And Overall of ~36mA from both GPIO 20 and 21

rapsberry pi camera on ubuntu

  • see if camera attached to raspberry pi yo will see something like bcm2835-v4l2: V4L2 device registered as video0 – stills mode > 1280×720
root@lp-arm-4:~# dmesg  | grep -i vid
--More--
[   13.071843] bcm2835-isp bcm2835-isp: Device node output[0] registered as /dev/video13
[   13.615235] bcm2835-isp bcm2835-isp: Device node capture[0] registered as /dev/video14
[   13.615709] bcm2835-isp bcm2835-isp: Device node capture[1] registered as /dev/video15
[   13.616053] bcm2835-isp bcm2835-isp: Device node stats[2] registered as /dev/video16
[   13.626826] bcm2835-codec bcm2835-codec: Device registered as /dev/video10
[   13.631504] bcm2835-codec bcm2835-codec: Device registered as /dev/video11
[   13.667772] : bcm2835_codec_get_supported_fmts: port has more encoding than we provided space for. Some are dropped.
[   13.702795] bcm2835-v4l2: V4L2 device registered as video0 - stills mode > 1280x720
[   13.708226] bcm2835-v4l2: Broadcom 2835 MMAL video capture ver 0.0.2 loaded.
[   13.744213] bcm2835-codec bcm2835-codec: Device registered as /dev/video12

--More--
  • install raspistill binary
apt install libraspberrypi-bin
  • check camera status
vcgencmd get_camera
  • Removing and adding back sunny connector(the yellow thingy below the camera in the board) worked. (very strange)

https://stackoverflow.com/questions/31354280/raspberry-camera-error-mmal-no-data-received-from-sensor

32 GB SD card issue

One of the issue that i face with my raspberry pi 4 SD card. I removed it when it was in the pi case. Which broke the SD card chip in half internally.

Lesson : Always remove the SD carefully.

broke my 32 GB SD card while removing from Pi. I tried formatting with fdisk, mkfs.ext4 and Windows but none of them worked.

I have attached screenshot of fdisk /dev/sda where I have created 1 partition but when i try to delete the same it’s giving error No partition is defined yet!

https://i.stack.imgur.com/rRVNJ.png

Here is the lsblk output:

root@lp-arm-1:~# lsblk
NAME        MAJ:MIN RM  SIZE RO TYPE MOUNTPOINT
loop0         7:0    0 88.1M  1 loop /snap/core/11803
loop1         7:1    0 48.9M  1 loop /snap/core18/2127
loop2         7:2    0   49M  1 loop /snap/core18/2248
loop3         7:3    0 57.4M  1 loop /snap/core20/1171
loop4         7:4    0 88.1M  1 loop /snap/core/11996
loop5         7:5    0 60.4M  1 loop /snap/lxd/21544
loop6         7:6    0 28.2M  1 loop /snap/snapd/13269
loop7         7:7    0 57.4M  1 loop /snap/core20/1084
loop8         7:8    0   62M  1 loop /snap/lxd/21032
loop9         7:9    0 28.2M  1 loop /snap/snapd/13643
sda           8:0    1 30.6M  0 disk
mmcblk0     179:0    0 59.7G  0 disk
├─mmcblk0p1 179:1    0  256M  0 part /boot/firmware
└─mmcblk0p2 179:2    0 59.4G  0 part /

Note: mmcblk0 is another working SD card

When try to format it with widows OS.

windows

Raspberry pi 4 to measure temperature and humidity with DHT11/DHT22 sensor

pip3 install adafruit-circuitpython-dht
apt-get install libgpiod2

temp.py

import Adafruit_DHT
import time
 
DHT_SENSOR = Adafruit_DHT.DHT11
DHT_PIN = 4

#while loop is there because sometime it does not get the temp.
while True:
    humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
    if humidity is not None and temperature is not None:
        print("Temp={0:0.1f}C Humidity={1:0.1f}%".format(temperature, humidity))
    else:
        print("Sensor failure. Check wiring.");
    time.sleep(1);

run:

python3 temp.py

For DHT22 sensor:

temp-dht22.py

import Adafruit_DHT
import time

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4

humidity, temperature = Adafruit_DHT.read(DHT_SENSOR, DHT_PIN)
if humidity is not None and temperature is not None:
    #print("Temp={0:0.1f}C Humidity={1:0.1f}%".format(temperature, humidity))
    print("{0},{1}".format(temperature, humidity))
  • shell script to send over pushgateway
root@lp-arm-1:~# cat /opt/cron/get_temp_dht22.sh
#!/bin/bash

t_and_h=$(python3 /opt/cron/get_temp_dht22.py)

temprerature=$(echo $t_and_h | awk -F ',' '{print $1}')
humidity=$(echo $t_and_h | awk -F ',' '{print $2}')
echo "room_temp $temprerature" | curl --data-binary @- http://192.168.0.183:30008/metrics/job/room_temp/instance/lp-arm-1
echo "room_humidity $humidity" | curl --data-binary @- http://192.168.0.183:30008/metrics/job/room_humidity/instance/lp-arm-1