ESP32 cannot send and receive UDP packets to itself
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.
Socket datas (UDP, TCP, MQTT) are received discontinuously - expected realtime
Hi, for games and robotics projects, I send data from my computer to my ESP32 over wifi every 100ms (for TCP / MQTT and 50ms for UDP). The ESP32 shows me all the data sent (with no lost with TCP / MQTT and almost no lost with UDP). So the ESP32 correcty receives all data sent from my computer. The data payload is : {"x": 1, "y": 2, "r": 102, "g": 0, "b": 0}.
Sadly, the ESP32 doesn't show me the data in real-time. It shows me up to 5 datas in a row instead of one data every 100ms. Results : the actions performed by the ESP32 are not fluid, like a lag in a video game. And I found no solutions on the internet.
I checked if the issue comes from my computer, its not. Other devices (other computers and smartphones in my local network) correctly shows me datas in real-time (so every 100ms with TCP / MQTT, or 50ms with UDP). I also tried to program my ESP32 with Arduino IDE and C++, no problem at all. So the issue seems to be related with MicroPython.
Could you help me to resolve this issue ?
Do you know how to get realtime data with micropython ?
You can see below the simpliest UDP code I use for my tests.
import network
import socket
import time
port_dest = 6000
ip_dest = "0.0.0.0"
session=socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
session.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
try:
session.bind((ip_dest, port_dest))
print('waiting...')
while True:
data,addr=session.recvfrom(128)
print('received:',data,'from',addr)
print('sending:',data,'from',addr)
except :
print("quit")
session.close()
(btw now I can receive UDP data inside a thread for hours without crashes, it wasn't possible some years ago. Great thanks for your hard work during all those years)