micropython / micropython/micropython

ESP32 cannot send and receive UDP packets to itself

Open
#4,227 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

port-esp32
Dominant language
C
Stars
22.1k
Forks
9k
Avg merge
6d 4h
Merged PRs (30d)
16

Description

It seems you can a UDP packet on a PC to itself, but you can't send a UDP packet from an ESP32 to itself:

import network, socket

# Create access point for the phone to log onto
si = network.WLAN(network.AP_IF) 
si.active(True)       
print("Wifi is: ESP_%X%X%X" % tuple(si.config("mac")[-3:]))
print(si.ifconfig())

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(3)
port = 9099
ipnum = "192.168.4.1"
s.bind((ipnum, port))   

s.sendto(b"An ESP32 UDP Packet", (ipnum, port))
print(s.recvfrom(100))  # <--- this times out

response is:

Wifi is: ESP_8C1FB2
('192.168.4.1', '255.255.255.0', '192.168.4.1', '0.0.0.0')

Traceback (most recent call last):
File "", line 16, in
OSError: [Errno 110] ETIMEDOUT

However, when I connect my PC to the ESP_8C1FB2 hotspot then I can run the following:

import socket

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.settimeout(3)
port = 9099
ipnum = "192.168.4.2"
s.bind((ipnum, port))   

s.sendto(b"A PC UDP Packet", (ipnum, port))
print(s.recvfrom(100))

response is:

(b'A PC UDP Packet', ('192.168.4.2', 9099))

I am able to send and receive UDP packets between the PC and the ESP32

  • PC: s.sendto(b"A PC UDP Packet", ("192.168.4.1", port)); print(s.recvfrom(100)))
  • ESP32: s.sendto(b"An ESP UDP Packet", ("192.168.4.2", port)); print(s.recvfrom(100)))

The socket.bind function seems to have to be to the device's own IPnumber. Fortunately this is quite predictable when working from the ESP's hotspot.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The report provides a reproducible ESP32 access-point script using network.WLAN and UDP socket calls, plus PC comparison cases. Start by reproducing the self-send timeout and compare it with the PC and PC-to-ESP cases; done means an ESP32 can send a datagram to its own bound address and receive it.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
embedded-iot, networking
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.