Tuesday, November 28, 2017

RaspPi + SenseHat + Headless + What's my IP?

Camera box setup for PAN001
showing the arduino + hub +
DHT22 setup. Also a tangle of wires.
I'm playing with a little RaspberryPi3 + SenseHat. The ultimate goal for the setup is to replace the electronics in the camera box (see pic) with this simple setup. However, while testing I want this Pi plugged into our wired network which, unfortunately, doesn't have a static IP address. Since I want to measure power levels and generally don't want to deal with having an HDMI display around I wanted a quick way to get the IP should it change.

Quick solution consists of two components: one is a bash script that gets the IP address, the other is a python script that displays the IP address on the 8x8 led matrix provided by the SenseHat.

Code samples are listed below. Sorry about the lack of syntax highlighting and formatting. Blogger doesn't make that super easy for me and I don't have time to figure it out.

Video in action!




Added to $HOME/.profile:
/home/pi/get-ip.sh &
---

get-ip.sh:


#!/bin/bash
IP=`ifconfig eth0 | grep 'inet ' | cut -d ' ' -f 10`
/home/pi/show-ip.py $IP
---

show-ip.py:

#!/usr/bin/env python3

import os
import sys
from sense_hat import SenseHat

show_ip = True

fn = '/home/pi/show-ip'
# Don't want to run program twice
if os.path.exists(fn) is True:
    sys.exit()
else:
    with open(fn, 'w') as f:
        f.write('')

try:
    ip = sys.argv[1]
except IndexError:
    print("Must pass an IP")
else:
    sense = SenseHat()

    while show_ip:
        event = sense.stick.get_events()
        if len(event) > 0:
            break
        else:
            sense.show_message(ip)

    sense.clear()
    os.unlink(fn)

No comments:

Post a Comment