A little while ago someone showed me how their Raspberry Pi emailed them the DHCP assigned IP address, which is really useful for when running headless in a environment where you have no control over DHCP. Now there are some alternatives, including running a Apple Zeroconf specification (like Bonjour), by installing “avahi-daemon”, but you need support on your client machine, which needs a agent installing or other apple software (on Windows).
The main problem I had with the email method, is that unless you can find a open email relay (not a good idea), you need to include your email password (I’m sure with a little more time I find away of securing this, or using some kind of token). So this set me thinking of what other presentation methods I could use to extract the IP address on boot. Here are some of my ideas (If you search the web you’ll probably find these are not original) :
- Take control of the Power LED and flash it according to the IP address.
- Post the IP address to a website
- Plug in a Serial cable into the pi to get a console and just read the IP
- Connect a Display of some kind
- Read the IP address out of the audio jack
If you search the web you should find the info on controlling the Power LED, which I did consider this a good approach, but I finally settled on using the Audio out via some headphones.
Here is how I did it:-
- Create a python script that reads the IP address:-
- I made sure the audio player “aplay” is installed and working (Make sure the audio is set to come out the audio jack!)
- Then I recorded a wav file for each number 0 – 9 and the decimal, ie 0.wav, 1.wav .. point.wav (had a little fun getting my Windows machine to record the right format; to to write raw wav)
- Then I wrote the following python to get and read the IP address (note I read the address used by the Ethernet port, as wireless would need you to connect to a access point first, but will work once the wireless config is established use ‘wlan0’ instead)
import socket import fcntl import struct import os Import time def get_ip_address(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) return socket.inet_ntoa(fcntl.ioctl( s.fileno(), 0x8915, # SIOCGIFADDR struct.pack('256s', ifname[:15]) )[20:24]) #print get_ip_address('eth0') myip = get_ip_address('eth0') #myip = '10.10.100.32' # test for i in myip: time.sleep(1) if i == '1': os.system('aplay /home/pi/readmyip/1.wav') elif i == '2': os.system('aplay /home/pi/readmyip/2.wav') elif i == '3': os.system('aplay /home/pi/readmyip/3.wav') elif i == '4': os.system('aplay /home/pi/readmyip/4.wav') elif i == '5': os.system('aplay /home/pi/readmyip/5.wav') elif i == '6': os.system('aplay /home/pi/readmyip/6.wav') elif i == '7': os.system('aplay /home/pi/readmyip/7.wav') elif i == '8': os.system('aplay /home/pi/readmyip/8.wav') elif i == '9': os.system('aplay /home/pi/readmyip/9.wav') elif i == '0': os.system('aplay /home/pi/readmyip/0.wav') elif i == '.': os.system('aplay /home/pi/readmyip/point.w$
- Call the Script at boot
- Add the following to “/etc/rc.local” file that which calls the python script on boot.
sudo python /<Python Script location>/<Python Script Name>.py
- Add the following to “/etc/rc.local” file that which calls the python script on boot.
- Connect your headphones and restart your Pi.
You can refine this a little, especially if the Pi doesn’t get a IP address.