ctrygstad
Posts: 6
Joined: Wed Mar 01, 2017 5:15 pm

Determining Pi serial number

Fri Oct 11, 2019 10:05 pm

It looks like there are a number of ways to get it by going into the operating system, but is serial number printed somewhere on the board? To confirm, trying to working with the Raspberry Pi 3 B+

DavidCoder
Posts: 6
Joined: Wed Oct 09, 2019 1:12 am

Re: Determining Pi serial number

Fri Oct 11, 2019 10:12 pm

Sorry, but the serial number can only be found from the pi interface, not printed on the pi as far as I know. I'm sorry for the inconvenience. :(

Andyroo

Re: Determining Pi serial number

Fri Oct 11, 2019 10:15 pm

Just be aware they are not unique :shock:

Due to the number being made the chance of getting two the same is very very small...

trackseven
Posts: 2
Joined: Fri Oct 11, 2019 7:12 pm

Re: Determining Pi serial number

Fri Oct 11, 2019 11:18 pm

I found this in another post

Code: Select all

# revised 08-17-2019 
#!/usr/bin/python3
#--------------------------------------
#
#                mypi.py
#  Functions to display Pi properties
#
#  If called directly outputs :
#  - Pi Model
#  - Revision number
#  - Serial number
#  - Python version
#  - I2C,SPI and Bluetooth status
#  - Mac address
#  - IP address
#  - CPU temperature
#  - GPU temperature
#
# Author : Matt Hawkins
# Date   : 06/12/2017 Revised 08-17-2019 for my Pi Zero W (by kflmiami420)
#
# https://www.raspberrypi-spy.co.uk/
#
#--------------------------------------

import platform
import subprocess
import os
import socket

from uuid import getnode as get_mac

# Define functions

def getModel():
  # Extract Pi Model string
  try:
    mymodel = open('/proc/device-tree/model').readline()
  except:
    mymodel = "Error"

  return mymodel

def getSerial():
  # Extract serial from cpuinfo file
  mycpuserial = "Error"
  try:
    f = open('/proc/cpuinfo','r')
    for line in f:
      if line[0:6]=='Serial':
        mycpuserial = line[10:26]
    f.close()
  except:
    mycpuserial = "Error"

  return mycpuserial

def getRevision():
  # Extract board revision from cpuinfo file
  myrevision = "Error"
  try:
    f = open('/proc/cpuinfo','r')
    for line in f:
      if line[0:8]=='Revision':
        myrevision = line[11:-1]
    f.close()
  except:
    myrevision = "Error"

  return myrevision

def get_local_ip_address(target):
  ipaddr = ''
  try:
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.connect((target, 8000))
    ipaddr = s.getsockname()[0]
    s.close()
  except:
    pass

  return ipaddr

def getCPUtemp():
  # Extract CPU temp
  try:
    temp = subprocess.check_output(['vcgencmd','measure_temp'])
    temp = temp[5:-3]
  except:
    temp = '0.0'
  temp = '{0:.2f}'.format(float(temp))
  return str(temp)

def getGPUtemp():
  # Extract GPU temp
  try:
    temp = subprocess.check_output(['cat','/sys/class/thermal/thermal_zone0/temp'])
    temp = float(temp)/1000
  except:
    temp = 0.0
  temp = '{0:.2f}'.format(temp)
  return temp

def getRAM():
  # free -m
  output = subprocess.check_output(['free','-m'])
  lines = output.splitlines()
  line  = str(lines[1])
  ram = line.split()
  # total/free  
  return (ram[1],ram[3])

def getDisk():
  # df -h
  output = subprocess.check_output(['df','-h'])
  lines = output.splitlines()
  line  = str(lines[1])
  disk  = line.split()
  # total/free
  return (disk[1],disk[3])

def getCPUspeed():
  # Get CPU frequency
  try:
    output = subprocess.check_output(['vcgencmd','get_config','arm_freq'])
    output = output.decode()
    lines = output.splitlines()
    line  = lines[0]
    freq = line.split('=')
    freq = freq[1]
  except:
    freq = '0'
  return freq

def getUptime():
  # uptime
  # tupple uptime, 5 min load average
  return 0

def getPython():
  # Get current Python version
  # returns string
  pythonv = platform.python_version()
  return pythonv

def getSPI():
  # Check if SPI bus is enabled
  # by checking for spi_bcm2### modules
  # returns a string
  spi = "False"
  try:
    c=subprocess.Popen("lsmod",stdout=subprocess.PIPE)
    gr=subprocess.Popen(["grep" ,"spi_bcm2"],stdin=c.stdout,stdout=subprocess.PIPE)
    output = gr.communicate()[0]
    if output[:8]=='spi_bcm2':
      spi = "True"
  except:
    pass
  return spi

def getI2C():
  # Check if I2C bus is enabled
  # by checking for i2c_bcm2### modules
  # returns a string
  i2c = "False"
  try:
    c=subprocess.Popen("lsmod",stdout=subprocess.PIPE)
    gr=subprocess.Popen(["grep" ,"i2c_bcm2"],stdin=c.stdout,stdout=subprocess.PIPE)
    output = gr.communicate()[0]
    if output[:8]=='i2c_bcm2':
      i2c = "True"
  except:
    pass
  return i2c

def getBT():
  # Check if Bluetooth module is enabled
  # returns a string
  bt = "False"
  try:
    c=subprocess.Popen("lsmod",stdout=subprocess.PIPE)
    gr=subprocess.Popen(["grep" ,"bluetooth"],stdin=c.stdout,stdout=subprocess.PIPE)
    output = gr.communicate()[0]
    if output[:9]==b'bluetooth':
      bt = "True"
  except:
    pass
  return bt

if __name__ == '__main__':
  # Script has been called directly

  myRAM = getRAM()
  myDisk = getDisk()
  host = socket.gethostname()
  mac = get_mac()

  macString = ':'.join(("%012X" % mac)[i:i+2] for i in range(0, 12, 2))


  print("----------------------------------------")
  print("Pi Model             : " + getModel())
  print("----------------------------------------")
  print("Host                 :", host)
  print("----------------------------------------")
  print("System               : " + platform.platform())
  print("Revision Number      : " + getRevision())
  print("Serial Number        : " + getSerial())
  print("Python version       : " + platform.python_version())
  print("----------------------------------------")
  print("I2C enabled          : " + getI2C())
  print("SPI enabled          : " + getSPI())
  print("Bluetooth enabled    : " + getBT())
  print("----------------------------------------")
  print("CPU Clock            : " + getCPUspeed() + "MHz")
  print("CPU Temperature      : " + getCPUtemp() + u"\u00b0" +"C")
  print("GPU Temperature      : " + getGPUtemp() + u"\u00b0" +"C")
  print("RAM (Available)      : " + myRAM[0] + "MB (" + myRAM[1] + "MB)")
  print("Disk (Available)     : " + myDisk[0] + " (" + myDisk[1] + ")")
  print("----------------------------------------")
  print("Wireless IP Address  : " + get_local_ip_address('10.0.1.1'))
  print("Wireless MAC Address : " + macString )

User avatar
B.Goode
Posts: 8987
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Determining Pi serial number

Sat Oct 12, 2019 7:42 am

trackseven wrote:
Fri Oct 11, 2019 11:18 pm
I found this in another post

Code: Select all

# sample code suppressed from this reply


So that is an example of "ways to get it by going into the operating system"



But the real question was " is serial number printed somewhere on the board?"


To which I believe, as with the first reply given, the answer is No.

User avatar
DougieLawson
Posts: 36578
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
Contact: Website Twitter

Re: Determining Pi serial number

Sat Oct 12, 2019 8:44 am

The answer is absolutely "no".

The serial is written to the OTP bits during manufacture (may be different on a 4B) and is only internal to the hardware, it is not externalised on any kind of sticker or ink jet printing.

Search Google there's a hundred and one ways to get the serial.

I like the three methods that Andrew Duncan has at: https://github.com/AndrewFromMelbourne/ ... rialnumber

There's even one that didn't exist before device tree /sys/firmware/devicetree/base/serial-number (that file has no new line, but it's a simple file read with no parsing needed).
Note: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

ctrygstad
Posts: 6
Joined: Wed Mar 01, 2017 5:15 pm

Re: Determining Pi serial number

Mon Oct 14, 2019 3:44 pm

Thanks, to confirm it was the Raspberry Pi 3 B+ model. All signs point to the fact that you have to login to the device to get the serial number, and there is a chance (although small), that you could have two devices with the same serial number.

jamesh
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 24191
Joined: Sat Jul 30, 2011 7:41 pm

Re: Determining Pi serial number

Mon Oct 14, 2019 4:27 pm

ctrygstad wrote:
Mon Oct 14, 2019 3:44 pm
Thanks, to confirm it was the Raspberry Pi 3 B+ model. All signs point to the fact that you have to login to the device to get the serial number, and there is a chance (although small), that you could have two devices with the same serial number.
Correct.(the chance is vanishingly small - they are very big pseudo random numbers)
Principal Software Engineer at Raspberry Pi (Trading) Ltd.
Contrary to popular belief, humorous signatures are allowed. Here's an example...
“I think it’s wrong that only one company makes the game Monopoly.” – Steven Wright

Return to “General discussion”