0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

モールス符号を使用した通信内容の確認手段の構築

Last updated at Posted at 2025-08-31

モールス符号を使用した通信内容の確認手段の構築

ZigBeeなどの無線通信では、正しくデータが送られているか確認するのが難しいことがある。
そこで今回は「LEDをモールス符号で点滅させる」ことで、目視でも通信内容を確認できる環境を作ってみた。

本記事ではXbeeとの組み合わせによる通信確認デモを行う。

テストのために、簡単な環境を作成してみよう。

送信側はパソコンにつなぎ、コンソールから送る。
受信側は送信側の内容に応じてLチカを行い、目視で受信内容を確認する。

図にして以下のようなものを作成する考えである。

morse_comu_1on1.png

ライブラリ

以下のコードをライブラリとして使用する。

# 入力文字列を英文モールス方式にて、LEDを使って出力するプログラム。

import utime
import xbee

DOT_MS = 100
DASH_MS = DOT_MS * 3
INTRA_LETTER_MS = DOT_MS
INTER_LETTER_MS = DOT_MS * 3
INTER_WORD_MS = DOT_MS * 7

def morse_code(code):
    table = {
        32: " ", # Space
        48: "-----", # 0
        49: ".----", # 1
        50: "..---", # 2
        51: "...--", # 3
        52: "....-", # 4
        53: ".....", # 5
        54: "-....", # 6
        55: "--...", # 7
        56: "---..", # 8
        57: "----.", # 9
        65: ".-",    # A
        66: "-...",  # B
        67: "-.-.",  # C
        68: "-..",   # D
        69: ".",     # E
        70: "..-.",  # F
        71: "--.",   # G
        72: "....",  # H
        73: "..",    # I
        74: ".---",  # J
        75: "-.-",   # K
        76: ".-..",  # L
        77: "--",    # M
        78: "-.",    # N
        79: "---",   # O
        80: ".--.",  # P
        81: "--.-",  # Q
        82: ".-.",   # R
        83: "...",   # S
        84: "-",     # T
        85: "..-",   # U
        86: "...-",  # V
        87: ".--",   # W
        88: "-..-",  # X
        89: "-.--",  # Y
        90: "--.."   # Z
    }
    return table.get(code, "Err")

def play_click(input_text,pin='P0'):
    codes = []
    for ch in input_text:
        code = ord(ch.upper())
        if code in range(32, 127):
            morse = morse_code(code)
            if morse == "Err":
                return
            codes.append(morse)
        else:
            return

    for morse in codes:
        for symbol in morse:
            if symbol == ".":
                xbee.atcmd(pin,0x05)
                utime.sleep_ms(DOT_MS)
                xbee.atcmd(pin,0x04)
            elif symbol == "-":
                xbee.atcmd(pin,0x05)
                utime.sleep_ms(DASH_MS)
                xbee.atcmd(pin,0x04)
            elif symbol == " ":
                utime.sleep_ms(INTER_WORD_MS)
                continue
            utime.sleep_ms(INTRA_LETTER_MS)
        utime.sleep_ms(INTER_LETTER_MS)

1対1 通信でのテスト

親機側(受信)

# MicroPython XBee3 ZigBee
'''
親機
receive命令を使ってXBee ZigBeeパケットに含まれる文字列の受信を行う
(送信元アドレスとペイロードを表示)
'''
import binascii
import utime
import xbee
from lib import morse_xbee_lib 

#ネットワークへの参加状態確認&参加
while True:
    status = xbee.atcmd('AI')# ネットワーク参加状態を確認する
    print('.',end='')
    if status == 0x00:# 参加状態の時にループを抜ける
        break
    xbee.atcmd('CB',0x01)# コミッショニング(ネットワーク参加)
    utime.sleep_ms(2000)# 待ち時間処理
print('\nJoined')

# 参加した後、受信待機状態⇒受信時の処理に移動
while True:
    packet = xbee.receive()# パケットの受信を行う
    if packet:# 受信データがある時
        addr = str(binascii.hexlify(packet['sender_eui64']).decode('utf-8'))
        addr = addr[:8] + ' ' + addr[8:]    # 送信元アドレスを表示用(8+8文字)に分離
        payload = str(packet['payload'].decode('utf-8'))    # 受信データを抽出
        print(addr + ', ' + payload)# アドレスと受信データを表示する(直接影響はないので残す)
        morse_xbee_lib.play_click(payload)

子機側(送信)

検証したデータの送り方が複数あるので、それぞれ書き残しておく。

1. スイッチをトリガーにした指定のメッセージを送信

# スイッチを押したらハローを送信するプログラム
from machine import Pin
import utime
import xbee
# 正しいピン指定方法
button_pin = Pin(Pin.board.D0, Pin.IN, Pin.PULL_UP)
last_state = button_pin.value()
send_content = "hello"
while True:
    current_state = button_pin.value()
    if current_state != last_state:
        if current_state == 0:
            #デバッグ用のLED
            xbee.atcmd('D1',0x05)
            utime.sleep_ms(300)
            xbee.atcmd('D1',0x04)
            utime.sleep_ms(100) 
            xbee.transmit(xbee.ADDR_COORDINATOR,send_content)
        last_state = current_state
    utime.sleep(0.05)

2. Send Frameから任意のメッセージを送信

比較的ノンプログラミングな手段としては、

送信側のAP を [2] にした後、Console working mode(Alt+C)に移動して、

Send Frame からメッセージを送る

作ってない場合の作り方としては、
Frame type0x10 にしたものを作って 簡単のために送信先アドレスとBroadcast にして好きな文字列のパッケージ作って送ればよい。

一例を以下に:

xbee_morse_example.png

検証動画

検証動画を以下に

まとめ

LEDによるモールス符号は、簡易的ながらも通信内容を目で追える強力なデバッグ手法であるように思う。

既存のPRを確認したが、XBeeと組み合わせる事例は少ないように感じたため、今回の取り組みを共有した。

参考

ASCIIコード表

似たようなPR

検索してみたところ、以下の場所にて上記の同様のプロジェクトを確認できた。

slouchd/morse4pico: Very simple Morse code script in MicroPython for the Raspberry Pi Pico.

mampersat/micropython-morsecode: MicroPython Morse Code

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?