Have you ever connected your Raspberry Pi to a new Ethernet cable but don't have a monitor to see its new IP address? Or maybe you don't want to connect a monitor, keyboard, and mouse to search for the IP address when you don't plan on using any of the three?
This project will run a Python script on startup that grabs the IP address of your Raspberry Pi and outputs it to a GPIO port through Morse code. Morse code is a method of transmitting text, numbers, and punctuation through on-off tones, lights, or 1's and 0's. They are transmitted as short and long "
dits" and "
dahs" or "
dots" and "
NOTE: You will need a monitor and/or ssh capabilities to load the code and configure your Pi to run it on startup.
Download and/or modify the
findIpMorse.py code. Line 51 and 56 has the delays between each dot and dash. You can change these lines if the delay is too short or too long. Place the file somewhere on your Pi.
The way the program runs is it flashes an LED three times to let the user know that it is running. After the three flashes the Pi then starts transmitting the IP address in Morse code. This includes periods! See video below for an example. The program will run for five minutes continuously searching for an IP address, if none is found then the program terminates.
Next you need to create a launcher script that will run every time you turn your Pi on. In this launcher script there is a command that is running the python code. Open terminal in your Pi and type
cd to go to your home directory. Next type:
In your newly created file type the following:
#!/bin/sh cd cd /home/pathToFile/ sudo python findIpMorse.py cd
pathToFile" to the correct path that leads to the directory where
findIpMorse.py program is located. For example, my
findIpMorse.py file is in a folder on my homepage called "
IPFolder" so I would do:
Now exit and save the file with CTRL-X, y, then ENTER. Use this command to make the file executable:
chmod +x launcher.sh
Next you need to configure your Pi to run
findIpMorse.py on startup. Go to the terminal on your Pi.
Before you do that, in terminal navigate to your Desktop (or anywhere) and create a folder called "
logs" and a file inside that folder called "
mkdir logs touch cronlog
This will help you figure out what goes wrong if the program doesn't run when you turn on your Pi.
sudo crontab -e
Next add this line to the bottom of the file and save it (CTRL-X, y, ENTER):
@reboot sh /pathToFile/launcher.sh >/pathToDesktop/logs/cronlog 2>&1
pathToFile" with the correct path to the directory where your launcher script is and also replace "
pathToDesktop" with the correct path to the directory where your logs directory is located. This line runs the launcher script on startup and also outputs any errors to the cronlog which you can open and view to see if there are any errors or messages.
The Pi should be ready to run the code now. This project doesn't require a lot of setup for the hardware. Just one LED, a breadboard, a resistor, wires, and your Pi.
Connect the positive leg of the LED to pin 40 (bottom right pin in the picture above) and the negative leg to ground (Bottom left pin in the picture above).
The program pulses the LEDs in either a short pulse or a long pulse. A short pulse represents a dot and a long pulse represents a dash.
- For example the number 1 is dot-dash- dash-dash-dash
- With the LED this would be short pulse, long pulse, long pulse, long pulse, long pulse
The following video demonstrates reading an IP address.
Now when you connect your Pi to an Ethernet cable you can read the IP through Morse code. No monitor necessary.
#!/usr/bin/env python # Purpose: To toggle a GPIO port that shows IP address of your Raspberry Pi in morse code # Useful for if you do not have a monitor to use with Pi import RPi.GPIO as GPIO import os import subprocess import time import sys GPIO.setmode(GPIO.BCM) GPIO.setwarnings(False) LED = 21 GPIO.setup(LED, GPIO.OUT) GPIO.output(LED, GPIO.LOW) def main(): try: ip = '' time_start = time.time() # Constantly searching for valid IP address. Quits if not IP found within 5 minutes while ip == '': ip = subprocess.check_output("ifconfig | grep \"inet \" | grep Bcast | \ cut -f2 -d\':\' | cut -f1 -d\' \'", shell=True) time_now = time.time() time_since_start = time.strftime('%M', time.localtime(time_now - time_start)) if int(time_since_start) >= 5: sys.exit() # Initialize morse code variable and strip the ip of all newline characters and spaces morse = '' ip = ip.strip() for num in ip: morse += str2morse(num) # IP address is flashed in morse code after 3 preliminary flashes # IMPORTANT! Short HIGH = Dot (1), Long HIGH = Dash (0) # Example: 127 = 10000 11000 00111 for i in range(3): GPIO.output(LED, GPIO.HIGH) time.sleep(2) GPIO.output(LED, GPIO.LOW) time.sleep(2) for value in morse: if value == '1': GPIO.output(LED, GPIO.HIGH) time.sleep(0.3) GPIO.output(LED, GPIO.LOW) time.sleep(1) else: GPIO.output(LED, GPIO.HIGH) time.sleep(1) GPIO.output(LED, GPIO.LOW) time.sleep(1) # If the user presses CTRL-C except KeyboardInterrupt: GPIO.output(LED, GPIO.LOW) finally: GPIO.cleanup() def str2morse(number): ''' This function takes a string input called number (which will only be numbers and a period and translate it to morse code Dot = 1 Dash = 0 ''' if number == '0': return '00000' elif number == '1': return '10000' elif number == '2': return '11000' elif number == '3': return '11100' elif number == '4': return '11110' elif number == '5': return '11111' elif number == '6': return '01111' elif number == '7': return '00111' elif number == '8': return '00011' elif number == '9': return '00001' elif number == '.': return '10101' if __name__ == '__main__': main()
Did you replicate this project? Share it!I made one
Love this project? Think it could be improved? Tell us what you think!