← index #6266Issue #4227
Related · high · value 2.249
QUERY · ISSUE

Socket.sendto() never transmitting on ESP32

openby j4magopened 2020-07-21updated 2026-03-22
port-esp32proposed-close

Hello all,

I'm writing some code in MicroPython for an ESP32-Devkitc-S1 that will need to send and receive UDP packets.

I've tested the code in Jupyter notebooks, and it's capable of both sending and receiving packets with my test socket.

On the ESP32, the same code is able to receive UDP packets just fine, but is incapable of sending packets (I've tried a few dozen times, so it doesn't seem like they're just dropped). The code runs without errors, and sock.sendto returns 17 as I'd expect, but the packets are never received.

I have an access point quite close to me, and the board is able to connect to the wifi without much hassle, so I don't think its an issue with the transmissions being too weak.

Please let me know if you have any suggestions, or if this is a known issue.

import network
import socket
import time

#I only run this on the SoC since my laptop is already connected to the wifi.
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.ifconfig(("192.168.100.102","255.255.255.0","192.168.1.1","8.8.8.8"))
wlan.connect("ap_ssid","ap_pass") #with my actual wifi credentials

def recv_packet(): #works on desktop and on SoC
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.bind(("",7777))
    print("Waiting...")
    data, addr = sock.recvfrom(2048)
    print("Received: ", data, "from", addr)
    sock.close()
    time.sleep(3) #the socket needs time to close or we run out of mem

def send_packet(): #works on desktop, not on SoC
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
    data = b'testing in Python'
    addr = ("192.168.39.136", 7778) #a UDP socket on my phone to test
    sock.sendto(data, addr)
    print("Sent: ", data, "to", addr)
    sock.close()
    time.sleep(3) #the socket needs time to close or we run out of mem
CANDIDATE · ISSUE

ESP32 cannot send and receive UDP packets to itself

openby goatchurchprimeopened 2018-10-10updated 2022-03-30
port-esp32

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 "<stdin>", line 16, in <module>
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.

Keyboard

j / / n
next pair
k / / p
previous pair
1 / / h
show query pane
2 / / l
show candidate pane
c
copy suggested comment
r
toggle reasoning
g i
go to index
?
show this help
esc
close overlays

press ? or esc to close

copied