Skip to content

Instantly share code, notes, and snippets.

@C-REMO
Created February 3, 2018 19:38
Show Gist options
  • Save C-REMO/488f20584c402e14c7d57a585a75e70f to your computer and use it in GitHub Desktop.
Save C-REMO/488f20584c402e14c7d57a585a75e70f to your computer and use it in GitHub Desktop.
Arduino Morse code encoder build with Django
String data="";
int led = 12;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(13, LOW);
}
void loop() {
if(Serial.available() > 0) {
data = Serial.readString();
const char* source = data.c_str();
int length = (int)strlen(source);
for (int i = 0; i < length; i++) {
if(source[i]=='.')
{
digitalWrite(led, HIGH);
delay(300);
digitalWrite(led, LOW);
delay(300);
}
else if (source[i]=='-')
{
digitalWrite(led, HIGH);
delay(900);
digitalWrite(led, LOW);
delay(300);
}
else if(source[i]=='/')
{
delay(1800);
}
else
{
delay(600);
}
}
}
}
<!DOCTYPE html>{% load staticfiles %}
<html lang="{{ LANGUAGE_CODE }}">
<head>
<title>DIY Morse code encoder using Arduino/Django</title>
<link rel="stylesheet" href="{% static 'css/style.css' %}"/>
</head>
<body>
<div class="grid-wrapper">
<div class="alert-system">
<form action="/encode/data/" method="post">
{% csrf_token %}
<input type="submit" style="visibility: hidden; width:300px;" />
<input type="text" class='input-button' id='morse' name="msg" placeholder="Enter message ..." style="width:282px;">
<span class="help-block">Message to send as morse code.</span>
{% if serial_error %}<p class="alert-block">Problem with serial connection. Is cable connected?</p>{% endif %}
<div class="license">
<hr />
<b>Simple Python/Django Morse Encoder With Arduino</b><br />
Follow me on twitter <a href="https://twitter.com/sp_omer"> @sp_omer </a>
</div>
</form>
</div>
</div>
</body>
</html>
html, body{
color:#666;font-size:14px;
background:#fff;
overflow-x: hidden;
margin: 0;
}
.grid-wrapper {
padding: 10px;
display: grid;
grid-template-columns: 1fr 300px 1fr;
grid-template-rows: 1fr 300px 1fr;
grid-template-areas:
". alert-system ."
". alert-system ."
". alert-system ."
}
.alert-system {
margin-top: 10px;
grid-area: alert-system;}
.help-block {
font-size: 10px;
}
.alert-block {
font-size: 10px;
color: #FF0000;
}
.license {
padding-top: 50px;
font-size: 8px;
}
from django.contrib import admin
from django.urls import path
from . import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.index),
path('encode/data/', views.encode),
]
import serial
from django.shortcuts import render
def index(request):
return render(request, 'base.html', {})
def encode(request):
if request.method == 'POST':
msg_to_encode = request.POST.get('msg', '')
try:
'''Create serial connection, encode and send the message.
My serial con. PORT is /dev/ttyACM0, yours may differ.'''
serial_connection = serial.Serial('/dev/ttyACM0', 9600)
CODE = {
# English alphabet
'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': '--..',
# Numbers
'0': '-----', '1': '.----', '2': '..---', '3': '...--',
'4': '....-', '5': '.....', '6': '-....', '7': '--...',
'8': '---..', '9': '----.',
# Extended
' ': '/', '.': '.-.-.-', ',': '--..--', ':': '---...',
'?': '..--..', "'": '.----.', '-': '-....-', '/': '-..-.',
'@': '.--.-.', '=': '-...-', '(': '-.--.', ')': '-.--.-',
'+': '.-.-.'
}
encoded_msg = ' '.join(CODE.get(char.upper())
for char in msg_to_encode)
serial_connection.write(encoded_msg.encode())
serial_connection.close()
serial_error = False
except serial.SerialException:
serial_error = True
return render(request, 'base.html', {'serial_error': serial_error})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment