Taming the FLIR Lepton SPI Bus on a Headless Drone

September 24, 2026


Issue Summary

Integrating a FLIR Lepton 3.5 via a PureThermal breakout board onto a headless 1588g drone presents a critical failure point. If the Python radiometric script encounters a fatal error or is killed unexpectedly, it leaves the SPI file descriptor open. The sensor locks the bus, the stream dies, and the headless Pi cannot re-establish the connection without a physical power cycle mid-flight.

Hardware & Software

  1. Hardware: Raspberry Pi 3 A+, FLIR Lepton 3.5, Custom AeroTherm Chassis
  2. Software Stack: Python, spidev library, systemd, Linux

Fault Tolerance Protocol

  1. Initialize the Bus: Open the SPI port and explicitly define the max speed (16MHz) for the VoSPI stream.
  2. Isolate Execution: Wrap the core extraction logic inside a dedicated daemonized thread utilizing a strict try/except block.
  3. Enforce Hardware Release: Utilize a finally block to guarantee the spi.close() command executes, even on a fatal crash, releasing the bus for immediate systemd restart.

Example

This is the bare-metal logic used to ensure the sensor re-initializes successfully during a mid-flight daemon crash:

import spidev

def thermal_capture_thread():
    spi = spidev.SpiDev()
    spi.open(0, 0)
    spi.max_speed_hz = 16000000

    try:
        while True:
            frame = get_lepton_frame(spi)
            process_thermal_data(frame)
    except Exception as e:
        log_fatal_error(e)
    finally:
        # Guaranteed execution to release hardware
        spi.close()
        print("SPI bus released. Ready for safe restart.")