We are in the process of doing major veterinary surgery on our Petoi Robot Dog to add organs to it, namely LIDAR rangefinding, a camera, foot sensors so it knows when it’s stepped in something, and recharging when it seeks out, then stands on, a charging mat. These are all controlled by a Raspberry Pi Zero with WiFi via a custom PCB that we designed. You can keep track of all this here on Github. (Wires will be tidied soon…)Petoi Dog Robot on a wooden stand with additional electronics added.

 

But one thing we did that should have much wider application is this. When you have a Raspberry Pi that’s just sitting on your network and accessed by SSH it’s not immediately obvious what its IP address is, especially if, like us, you have over 100 devices on your local network. So we wrote a little Python program to display the IP address on boot using I2C on one of these small, cheap Waveshare OLED displays:

OLED display showing IP address

Now you don’t have to log in to your network hub and search out what address the Pi has been dynamically allocated every time you boot it up (yes – we do know we can reserve a static address on the hub for it, but the more addresses you reserve the fewer are available when the reserved devices are not being used).

See the Waveshare link above to install the OLED library you need, put this Python program somewhere on your Pi and call it display-ip.py:

import socket
import sys
import os
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic')
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib')
if os.path.exists(libdir):
    sys.path.append(libdir)

import traceback
from waveshare_OLED import OLED_0in91
from PIL import Image,ImageDraw,ImageFont

try:
    disp = OLED_0in91.OLED_0in91()
    # Initialize library.
    disp.Init()
        
    # Clear display.
    disp.clear()

    # Create blank image for drawing.
    image1 = Image.new('1', (disp.width, disp.height), "WHITE")
    draw = ImageDraw.Draw(image1)
    font1 = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 12)
    #Get IP
    host_name = socket.gethostname()
    ip = "IP: " + socket.gethostbyname(host_name + ".local")
    draw.text((20,0), ip, font = font1, fill = 0)

    image1=image1.rotate(0) 
    disp.ShowImage(disp.getbuffer(image1))

except IOError as e:
    logging.info(e)
    
except KeyboardInterrupt:    
    OLED_0in91.config.module_exit()
    exit()

Then add this at the bottom of /etc/rc.local:

sudo python3 /path/to/where/that/program/is/display-ip.py > /dev/null &

And you’ll be able to SSH straight into the Pi every time it boots.

 

Every Dog Has Its (Day)x IP Address
Tagged on:                         

Leave a Reply

Your email address will not be published. Required fields are marked *