Srivz S
Published © GPL3+

Swift Morse Code Sender

Send a secret message to your friend encrypted using this Swift Morse Code Sender. Let him take the fun of decoding it.

BeginnerFull instructions provided1 hour470
Swift Morse Code Sender

Things used in this project

Hardware components

Buzzer
Buzzer
×1
Bolt WiFi Module
Bolt IoT Bolt WiFi Module
×1
LED (generic)
LED (generic)
×1
Bolt IoT Resistor 320 ohms
×1
Breadboard (generic)
Breadboard (generic)
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Bolt Cloud
Bolt IoT Bolt Cloud
SMS Messaging API
Twilio SMS Messaging API
Mailgun

Story

Read more

Schematics

My Morse_coder circuit

Morse_coder circuit

Schematics diagram

Code

Morse_coder

Python
It is the main program. This will create an application, get the message as input from users, convert the message into morse code, and at last operates the buzzer and led connected to the bolt module.It also notifies the receiver about the transmission 1 minute prior
import json
import time
from boltiot import Sms, Bolt, Email
from kivy.app import App
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.widget import *
import conf  # Make sure conf.py is stored in the same folder.

# The below code is to store data from the conf.py file the credentials needed to connect to the {bolt wifi module, twilio account, mailgun account} in a local variable.
mybolt = Bolt(conf.bolt_api_key, conf.device_id)
sms = Sms(conf.SID, conf.AUTH_TOKEN, conf.TO_NUMBER, conf.FROM_NUMBER)
mailer = Email(conf.MAILGUN_API_KEY, conf.SANDBOX_URL, conf.SENDER_EMAIL, conf.RECIPIENT_EMAIL)

# The below code is to create the GUI of the kivy application that we are creating.
Builder.load_string('''<MyGrid>
    name:"main"
    secret:secret
    lb:lb
    GridLayout:
        cols:1
        size:root.width-100,root.height-100
        pos:50,50
        Label:
            id:lb
            text:"MORSE CODE"
            size:100,10
        TextInput:
            multiline: True
            id:secret
        Button:
            text:"SEND"
            on_press:root.btn()
''')

# THE BELOW FUNCTION IS USED TO CONVERT THE USER INPUT TO MORSE CODE.
def tomorse(text):
    a = text.lower()
    morse_dict = {"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": "--.. ", " ": "/ ", ",": "--..-- ",  ".": ".-.-.- ", "/": "-..-. ", ":": "---... ", "!": "-.-.-- ", "@": ".--.-. ", "1": ".---- ",  "2": "..--- ", "3": "...-- ", "4": "....- ", "5": "..... ", "6": "-.... ", "7": "--... ",  "8": "---.. ", "9": "----. ", "0": "----- ", "&": ".-... ", "(": "-.--. ", ")": "-.--.- ",  "+": ".-.-. ", "=": "-...- ", "?": "..--.. ", "-": "-....- ", "\n": "\n", "\t": "\t"}  
    final = ""
    for i in a:
        final = final + morse_dict[i]
    return final
    
# THE BELOW CODE IS TO CREATE A WIDGET USING KIVY.
class MyGrid(Widget):
    secret = ObjectProperty(None)
    # The below code is called when the button in pressed in the app.
    def btn(self):
        code = tomorse(self.secret.text)  # Calls tomorse() function.
        print(code)
        try:
            # The below code will send SMS to the receiver.
            response = sms.send_sms("The morse code will be transmitted in a minute")
            print("Response received from Twilio is: " + str(response))
            print("Status of SMS at Twilio is :" + str(response.status))
            # The below code will send EMAIL to the receiver.
            response = mailer.send_email("Alert", "The morse code will be transmitted in a minute")
            response_text = json.loads(response.text)
            print("Response received from Mailgun is: " + str(response_text['message']))
        except Exception as e:
            print("Error occurred: Below are the details")
            print(e)
        time.sleep(60)  # Wait for 60 seconds for the receiver to notice the morse code.
        response = 0
        # Transmits every character in the morse code to the bolt wifi module.
        for j in code:
            time.sleep(0.03)
            if j == ' ':
                time.sleep(0.2)  # The gap between letters is 1 unit tine.
            elif j == '/':
                time.sleep(1)  # The gap between words is 7 units time.
            else:
                if j == '-':
                    mybolt.digitalWrite('0', 'HIGH')  # Turns on the buzzer and LED.
                    time.sleep(0.3)  # dash lasts for 3 units time.
                    response = mybolt.digitalWrite('0', 'LOW')  # Turns off the buzzer and LED.
                elif j == '.':
                    mybolt.analogWrite('0', 240)  # Turns on the buzzer and LED and reduces its intensity to 240.(for further differentiation between dot and dash.)!!
                    time.sleep(0.001)  # dot lasts for 1 unit time.
                    response = mybolt.digitalWrite('0', 'LOW')  # Turns off the buzzer and LED.
        print("Message transmission is " + str(response))
        if response[-2] == 0:
            time.sleep(5)  # If message is not transmitted it waits for 5 seconds and closes the kivy application.
            exit()
class morse(App):
    def build(self):
        return MyGrid()  # This open/start the kivy App.
if __name__ == "__main__":
    morse().run()
    

conf file

Python
This file contains the credentials of your bolt account, mailgun account(for MAIL), twilio account(for SMS).
# Below is twilio website code
SID = 'You SID that is given in twilio website'
AUTH_TOKEN = 'Your API key as in the twilio website'
FROM_NUMBER = "seder's mobile number as given in the twilio website"
TO_NUMBER = "reciever's phone that is registered in twilio website"
# Below is mailgun website code
MAILGUN_API_KEY = 'private api'
SANDBOX_URL = 'sandbox<>.mailgun.org'
SENDER_EMAIL = 'test@'+SANDBOX_URL
RECIPIENT_EMAIL = "receiver's email that is registered in mailgun website"
# Below is common bolt iot code
bolt_api_key = "This is your Bolt Cloud API Key"
device_id = "BOLT<>"#This is your device id

Credits

Srivz S

Srivz S

1 project • 0 followers

Comments