1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

はじめに

ESP32同士でUDP通信を行いたい。

実装

送信側のコンソールで, 入力したメッセージを受信側に表示する

・送信側

udp_sender.py
import ubinascii as ub
from usocket import socket, AF_INET, SOCK_DGRAM

HOST = '192.168.100.213'
PORT = 8086
# ADDRESS = "127.0.0.1" # 自分に送信

s = socket(AF_INET, SOCK_DGRAM)
# ブロードキャストする場合は、ADDRESSを
# ブロードキャスト用に設定して、以下のコメントを外す
# s.setsockopt(SOL_SOCKET, SO_BROADCAST, 1)

while True:
    msg = input('> ')
    # 送信
    s.sendto( ub.hexlify(msg), (HOST, PORT))
    print(f"done")

s.close()

・受信側

udp_res.py
import ubinascii as ub
from usockBet import socket, AF_INET, SOCK_DGRAM

PORT = 8086

# ソケットを用意
s = socket(AF_INET, SOCK_DGRAM)
# バインドしておく
s.bind(('0.0.0.0', PORT))

while True:
    # 受信
    msg, address = s.recvfrom(1024)
    print(f"message: {ub.unhexlify(msg)}\nfrom: {address}")

s.close()

ポイント
1.microPythonを使っている場合, ライブラリ名の先頭に'u'が付くのを使うのを推奨されている
2.binary

1
1
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
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?