The Signal - Part 2
Again, this is for Raspberry Pi, and I think a much simpler option than the Fldigi route. A dude named GerryK wrote this program, but it's a bit dated now, and I don't know how well it is maintained. Reference here:
http://gerryk.com/posts/morse_code_beacon_for_the_raspberry_pi/https://github.com/gerryk/cwbeaconBecause it was written a while back, I had to make a few syntax changes to the code. I also learned the hard way that
Gerry's code references the actual pin number, not the GPIO number. Once I got that figured out, I was able to make it work. With that in mind, here's what you do:
Step 1 - GroundworkOpen your Raspberry Pi, and navigate to your home directory (/home/pi). Create a new folder called "cwbeacon". Inside that folder, create a new file called "beacon.py" Then, copy/paste the code below into that file, save, and exit
import time
import RPi.GPIO as GPIO
class CWString(object):
letters = { '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' : '-----',
'.' : '.-.-.-',
'/' : '-..-.',
',' : '--..--',
'?' : '..--..'
}
cw = ""
def encode(self, m):
self.cw = ""
new_word = True
for i in m:
if i in self.letters:
if not new_word:
self.cw = self.cw + " "
self.cw = self.cw + self.letters[i]
new_word = False
elif i == " ":
self.cw = self.cw + "/"
new_word = True
else: # Unrecognised character... what now?
pass
return self.cw
class CWSender(object):
cps = None
timer = None
def __init__(self, cps):
self.cps = cps
self.dit_length = 1.0/cps
self.dah_length = self.dit_length*3
def send(self, m):
for i in m:
if i == '.':
self.dit()
self.pause()
if i == '-':
self.dah()
self.pause()
if i == ' ':
self.cspace()
if i == '/':
self.wspace()
def dit(self):
GPIO.output(37,True)
time.sleep(self.dit_length)
GPIO.output(37,False)
def dah(self):
GPIO.output(37,True)
time.sleep(self.dah_length)
GPIO.output(37,False)
def wspace(self):
time.sleep(self.dah_length * 3)
def cspace(self):
time.sleep(self.dah_length)
def pause(self):
time.sleep(self.dit_length)
def main():
import argparse
GPIO.setmode(GPIO.BOARD)
GPIO.setup(37, GPIO.OUT)
GPIO.output(37,False)
parser = argparse.ArgumentParser('CW Beacon Sender - uses GPIO pin')
parser.add_argument('-c', '--cps', type=int, default='15', help='Characters per second to send')
parser.add_argument('message', nargs='+', help='Text string to send as CW')
args = parser.parse_args()
cw = CWString()
message = ' '.join(args.message)
message = ' '.join(args.message).lower()
encoded = cw.encode(message)
sender = CWSender(args.cps)
sender.send(encoded)
if __name__ == "__main__":
main()
Again, all credit goes to GerryK and his original code here https://github.com/gerryk/cwbeacon I've made a couple of syntax fixes, and re-routed the output signal to GPIO26Step 2 - Create a MessageSo this part is easy. Open a terminal, and type
python /home/pi/cwbeacon/beacon.py -c 20 hello world
What you've done is the following:
-- You're sending "hello world" as The Signal.
-- It's being sent in 20 words per minute morse code.
-- The code will be
tapped out via the RPi's GPIO 26 pinNote the above bold text: My changes to Gerry's code make it so that GPIO 26 is your huckleberry. Use that pin. Only that pin. The pin shall be GPIO 26, nothing more, nothing less.Step 3 - Automate the beaconSo if you follow the above, you'll generate a one-time message of "hello world." That's not helpful. Because this is for a beacon, we want the code to send out nonstop forever. So to do that, you need to run a "task scheduler." In linux/Rpi, this is called crontab.
So, do the following. Open a terminal on your Rpi, and type
crontab -e
Next, copy/paste or type in the following, save, and exit.
5,10,15,20,25,30,35,40,45,50,55,0 * * * * python /home/pi/cwbeacon/beacon.py -c 20 hello world
You've just created a crontab schedule.
What the above means is that every 5 minutes (on the 5, then the 10, then the 15, and so on) the RPi will tap a CW message of "hello world" out to GPIO26. And it will do this indefinitely.
So for your final version, you'll obviously replace "hello world" with your callsign and all that stuff.
***
To wrap up, I really like this one. It's a proper, clean, decent way to create The Signal with a raspberry pi. It's also extremely easy on resources, meaning you can get away with using a bare-bones and cheap Raspberry Pi zero for this.