A Morse Code Application for MQTT

1878 Bunnell Telegraph Set

[Github Repo of the Code – see makermorse.py for this script.]

Telegraphy and Morse Code were the first instantaneous long distance form of communication.  Invented in 1844, for the first time, you could communicate in real time with someone you could neither see nor hear.

Telegraphy was huge.  It was the Internet of its day.  But the truth is, it was actually pretty simple:

A telegraph is something you can build in an afternoon.  The basic principle is electro-magnetism.  If you wrap wire around an iron nail, it becomes a magnet when you run electricity through it.

A telegraph sounder like the one below works by becoming magnetic electrically, which causes a spring-loaded tapper to click against it audibly.

1878 Bunnell Telegraph Set
1878 Bunnell Telegraph Set

In the age of Internet communications, this may all seem obsolete, but there are thousands of people around the world who still communicate via Morse code, primarily over radio.  These amateur radio operators, or HAMs, as they are called, don’t need Internet to communicate around the world, so they become of vital importance in times of natural disaster, or any time other forms of communication don’t work, both with voice radio and Morse Code.

In the photo is a pair of telegraph stations manufactured by J. H. Bunnell & Co., in 1878.

Now, many people would put something like that in a nice display case and never play with it, but where’s the fun in that?

Raspberry Pi to the rescue!

The Raspberry Pi has a bank of pins known as General Purpose Input/Output (GPIO) pins that we can use to send a bit of electricity to light up an LED.  It’s not much, only around 3 volts, but we thought it might be just enough to make the sounder click.  With careful adjustment of the sounder’s springs and set screws, we were able to get a good click.

With that first test out of the way, it’s time to write some code!

I started, as all good programmers do, by Googling for some code to steal borrow to save me some of the heavy lifting.  I came across a tutorial from Cambridge University in England.  Linked from that was a Python script that would get us started.

What this script does is ask you to type something and hit enter, then it blinks it out as code on an LED.  That’s close, but to make this interesting, let’s get it hooked up to the Internet, so we can receive Morse messages from anywhere in the world!

MQTT is a server protocol that’s a lot of fun to play with.  It’s a simple way to send messages from one device to any number of other devices.  You can also do a kind of chat with it, since it’s so fast and simple.

Let’s go get the code!  On your Pi, open the terminal.

Pi Terminal
Type the following commands:


This command takes you to your home directory. It’s a good place to work. Next, we’re going to go grab the code from Github:

git clone https://github.com/jimoconnell/electrobrain.git

You should see output that looks like this:

cd electrobrain

to change to the project’s directory. Next, type

idle ~/electrobrain/makermorse.py

to open the program in the Idle Python Editor:


Once you have the project open in Idle, try running it by hitting the F5 key on your keyboard, or by clicking “Run > Run Module” with your mouse.

You may get an error.  That’s OK, it’s probably just a missing library.  Probably the MQTT library we use.  To install Python libraries, we use a program called “pip”.  First, let’s make sure we have pip.  We ask Linux if it knows pip:

which pip

It should come back with:


If you just get a blank line when you hit enter, it means it isn’t installed.  Let’s install it!

sudo apt-get update
sudo apt-get install -y python-pip

OK, pip should be installed now.  We should only need to do that once.

OK, now type:

sudo pip install paho-mqtt

Now try running the script again from Idle. If everything is working, it will say:

Connected to:
with result code: 0

If you get that, pat yourself on the back.  (Seriously.  You’ve actually accomplished quite a bit.)


Now it’s time to hook something up to the Pi’s GPIO.  In the program, I’ve specified pin 25 as the one that will turn on.  Below is a diagram of the pins on a Raspberry Pi 3.  The ones on the right are on the outside edge of the board.


Get yourself your little breadboard, a resistor (220 Ohm) and an LED.  Hook it up like this diagram:


Now go back to Idle and run the program again.  It should be listening, ready to blink out your code, but it’s not likely that anyone is sending a message to that channel.  Let’s go send some data!

Open a new terminal and type:

sudo apt-get update
sudo apt-get install -y mosquitto-clients

When that’s finished, we’re ready to send!

mosquitto_pub -h "iot.eclipse.org" -t "test/abcd" -m "What Hath God Wrought!"

Why “What Hath God Wrought!”?  Well, as it turns out, that was the first message sent by inventor Samuel F.B. Morse on May 24, 1844.

Did it work? (If it didn’t, feel free to leave a comment below.)

If so, try this:

mosquitto_pub -h "iot.eclipse.org" -t "test/abcd" -l

Then type one message per line and hit enter.  Each line should be sent to your LED as Morse Code!