RaspberryPi/IoT Morse Code Keyer
From charlesreid1
This page will show you how to turn your Raspberry Pi into an Internet of Things-enabled morse code keyer that can be remotely controlled with a RESTful web API.
The first part is the hello world LED circuit. This circuit connects to the Raspberry Pi (see RaspberryPi/Blink) via the GPIO pins. These are pins that enable you to turn voltages on and off from the Raspberry Pi, and therefore control circuits. The GPIO pins, in turn, can be controlled from a Python script on the Raspberry Pi.
The second part is the Flask API, which turns URL requests like http://myraspberrypi/hello
into instructions to send morse code for "hello" via the LED circuit.
Contents
Part 1: Hello World LED Circuit
We've already covered how to get a basic Hello World circuit, independent of any code on the Raspberry Pi, on the RaspberryPi/Hello World page.
We then covered how to use the GPIO cable and a Python script to control voltage to certain pins, enabling us to control circuits with the Raspberry Pi, over on the RaspberryPi/Blink page.
These pieces cover part 1 of building your IoT Morse Code Keyer with the Raspberry Pi.
Part 2: RESTful API on the Raspberry Pi
The second step is getting a RESTful API up and running on the Raspberry Pi. We can do this by using Flask to define URL endpoints (like /hello
or /on
), and then define the "verbs" associated with those URLs.
Basic information on running a web server on the Pi here: RaspberryPi/Web_Server
However, this page doesn't go into much detail, so we'll go into greater detail here.
Planning Out the API
We'll pick four URLs and four associated verbs for our remote control morse code keyer, and create four different urls for each of these actions:
Action | Endpoint URL |
---|---|
Turn the LED on | /on
|
Turn the LED off | /off
|
Send message "SOS" with the LED | /sos
|
Send message "Hello world" with the LED | /hello
|
The API Script
Setting Up GPIO
First, our script should set up which GPIO pins we want to use, and define how we turn the LED on and off (or, whatever we want to do with our morse code keyer).
import time
# -----------------------
# RPi stuff
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
# get ready to use gpio from pi
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# use channel 15, i.e., P22
channel = 16
# gpio is used for output
GPIO.setup(channel, GPIO.OUT)
def LED_on():
GPIO.output(channel,GPIO.HIGH)
def LED_off():
GPIO.output(channel,GPIO.LOW)
# -----------------------
Setting Up Morse Code
Now we can use the michael-morse library (http://github.com/charlesreid1/michael-morse) to set up a morse code generator:
# -----------------------
# morse code stuff
from MichaelMorse import MichaelMorse
m = MichaelMorse(20,on=LED_on,off=LED_off)
# -----------------------
The Flask API
Finally, we define some Flask endpoints, then run the Flask app on host "0.0.0.0" (which means, listen publicly for any request on the network).
# ------------------------
# flask routes
from flask import Flask
app = Flask(__name__)
@app.route('/on', methods=['GET'])
def rpi_on():
LED_on()
@app.route('/hello', methods=['GET'])
def rpi_hello():
while True:
m.send("hello world de kc7dbu")
time.sleep(3)
@app.route('/sos', methods=['GET'])
def rpi_sos():
while True:
m.send("SOS")
time.sleep(3)
@app.route('/off', methods=['GET'])
def rpi_off():
LED_off()
# ------------------------
# ------------------------
# run it
if __name__ == '__main__':
LED_off()
app.run(debug=True,host='0.0.0.0')
GPIO.cleanup()
# ------------------------
Final Code for REST API
# Use Flask with michael-morse to make
# an internet of things Raspberry Pi
# that can be remotely controlled to send
# morse code with a blinking LED
import time
# -----------------------
# RPi stuff
try:
import RPi.GPIO as GPIO
except RuntimeError:
print("Error importing RPi.GPIO! This is probably because you need superuser privileges. You can achieve this by using 'sudo' to run your script")
# get ready to use gpio from pi
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
# use channel 15, i.e., P22
channel = 16
# gpio is used for output
GPIO.setup(channel, GPIO.OUT)
def LED_on():
GPIO.output(channel,GPIO.HIGH)
def LED_off():
GPIO.output(channel,GPIO.LOW)
# -----------------------
# -----------------------
# morse code stuff
from MichaelMorse import MichaelMorse
m = MichaelMorse(20,on=LED_on,off=LED_off)
# -----------------------
# ------------------------
# flask routes
from flask import Flask
app = Flask(__name__)
@app.route('/on', methods=['GET'])
def rpi_on():
LED_on()
@app.route('/hello', methods=['GET'])
def rpi_hello():
while True:
m.send("hello world de kc7dbu")
time.sleep(3)
@app.route('/sos', methods=['GET'])
def rpi_sos():
while True:
m.send("SOS")
time.sleep(3)
@app.route('/off', methods=['GET'])
def rpi_off():
LED_off()
# ------------------------
# ------------------------
# run it
if __name__ == '__main__':
LED_off()
app.run(debug=True,host='0.0.0.0')
GPIO.cleanup()
# ------------------------
Run It
Now that we've got the hardware (LED circuit) and software (Python script) ready, we can run our Internet of Things Morse Code Keyer.
Start Flask API Server
First, we ssh into the Pi:
$ ssh pi@169.254.113.200
Next, we start screen, so that the Flask server will continue to run even if we disconnect from the Pi:
$ screen -S flask
Now we run the Flask server:
$ cd /path/to/code $ sudo python run.py
Note that both the GPIO and the Flask app will require the script to be run as sudo.
Now you should see your Pi listening on port 5000. We have our RESTful API, and we can access it at http://169.254.113.200/
.
Send a Request with Curl
Now that the RESTful API is listening, let's send it a request with curl.
From your laptop that's SSHed into the Pi, run:
$ curl -i http://169.254.113.200/on
and you should see some activity in the Flask window, as well as the LED switch on.
Now test out the rest:
$ curl -i http://169.254.113.200/on $ curl -i http://169.254.113.200/off $ curl -i http://169.254.113.200/sos $ curl -i http://169.254.113.200/hello
What It Looks Like
Here's what the finished product looks like: