Evan Rust
Published © GPL3+

Email to Morse Code

Convert the body of an email to Morse code and have an LED or speaker signal it to you!

BeginnerFull instructions provided2 hours2,242
Email to Morse Code

Things used in this project

Hardware components

Raspberry Pi 2 Model B
Raspberry Pi 2 Model B
×1
LED (generic)
LED (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
Optional
×1

Software apps and online services

IDLE
Gmail

Story

Read more

Schematics

Schematic

Code

Python Code

Python
Run on Raspberry Pi
import gmail
import time
import RPi.GPIO as GPIO

username = "gmail email name"
password = "password for your account"

g = gmail.login(username,password)

led_pin = 4 #BCM pin 4
GPIO.setmode(GPIO.BCM)
GPIO.setup(led_pin,GPIO.OUT)
GPIO.output(led_pin,GPIO.LOW)

morseAlphabet = {
       "A" : ".-",
        "B" : "-...",
        "C" : "-.-.",
        "D" : "-..",
        "E" : ".",
        "F" : "..-.",
        "G" : "--.",
        "H" : "....",
        "I" : "..",
        "J" : ".---",
        "K" : "-.-",
        "L" : ".-..",
        "M" : "--",
        "N" : "-.",
        "O" : "---",
        "P" : ".--.",
        "Q" : "--.-",
        "R" : ".-.",
        "S" : "...",
        "T" : "-",
        "U" : "..-",
        "V" : "...-",
        "W" : ".--",
        "X" : "-..-",
        "Y" : "-.--",
        "Z" : "--..",
        " " : "/"
}
messages = []
last_message = ""
current_message = ""

def getMessage():
    global messages
    messages = g.inbox().mail(subject="Morse")
    size = len(g.inbox().mail(subject="Morse"))
    message = g.inbox().mail(subject="Morse")[size - 1]
    message.fetch()
    message = message.body
    return message

def encode_morse(text):
        encodedMessage = ""
        for char in text:
            if char.upper() in morseAlphabet:
                encodedMessage += morseAlphabet[char.upper()] + " "             
        return encodedMessage
    
def dot():
    print "dot"
    GPIO.output(led_pin,GPIO.LOW)
    time.sleep(.1)
    GPIO.output(led_pin,GPIO.HIGH)
    time.sleep(.1)
    GPIO.output(led_pin,GPIO.LOW)
def dash():
    print "dash"
    GPIO.output(led_pin,GPIO.LOW)
    time.sleep(.3)
    GPIO.output(led_pin,GPIO.HIGH)
    time.sleep(.3)
    GPIO.output(led_pin,GPIO.LOW)
def check():
    global current_message
    global last_message
    current_message = getMessage()
    if current_message != last_message:
        last_message = current_message
        morse_encoded = encode_morse(current_message)
        print morse_encoded
        for char in morse_encoded:
            if char == '.':
                dot()
            elif char == '-':
                dash()
            elif char == ' ':
                time.sleep(.2)
            elif char == '/':
                time.sleep(.6)
            else:
                print "null"
    else:
        print "done already"
while(1):
    try:
        check()
        g.logout()
        time.sleep(10)
        g = gmail.login(username,password)
    except Exception:   
        g.logout()
        GPIO.cleanup()

Credits

Evan Rust

Evan Rust

120 projects • 1053 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments