Device ID: use the serial number
Hostnames get cloned with SD card images, IP addresses change, and MAC addresses differ between Ethernet and Wi-Fi. Every Raspberry Pi has a unique serial number:
grep Serial /proc/cpuinfo
# Serial : 00000000765fc593
In Python:
def pi_serial() -> str | None:
with open("/proc/cpuinfo") as f:
for line in f:
if line.startswith("Serial"):
return line.split(":", 1)[1].strip()
return None
Community answers also read it from the device tree, which needs no parsing:
tr -d '\0' < /sys/firmware/devicetree/base/serial-number
Use it as the stable device ID when registering with your backend, MQTT client IDs, or certificates. Keep in mind it identifies the board: moving the SD card to another Pi changes it.
Model: device tree, not "BCM2835"
/proc/cpuinfo says Hardware : BCM2835 on every Raspberry Pi, even a Pi 5 with a BCM2712. Don't detect the processor from it. Across distributions, read the device tree:
tr '\0' '\n' < /proc/device-tree/compatible
# raspberrypi,5-model-b
# brcm,bcm2712
/sys/firmware/devicetree/base/model gives a human-readable name (e.g. for logs).
Revision codes: check fields, not a list
The Revision line is a bit-field (board type, memory, processor, manufacturer). Don't compare against a list of known codes: a new board revision or a different factory produces a new code, and your software would reject a compatible board. Check bit 23 (new-style code), then filter by board type or memory size, as Raspberry Pi's documentation recommends.
Sources: Stack Exchange (CC BY-SA 4.0) — see links.