Socket.sendto() never transmitting on ESP32
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
OSError: [Errno 113] EHOSTUNREACH while listen for connection
Hello,
I was using so far older micropython (1.10 official ) release, but I was testing some stuff and switched to one of the latest:
MicroPython v1.10-380-g6cf4e9675 on 2019-05-23; ESP32 module with ESP32
problem is that I got a simple web server using usocket to listen for connections:
my code is large i just gonna show how I use socket:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(3)
conn, addr = s.accept()
with older version of micropython (1.10 official) I never got a problem, but with this one from time to time i got:
OSError: [Errno 113] EHOSTUNREACH
failing on line with:
conn, addr = s.accept()
google it and it may be dns related, but there is no dns involved when you listen for connections.
P.S.
It happen at random, I use ajax to reload browser page every 3 seconds, sometime it happen on first minute, some time 30 min need to pass. Good part is I have modified my code and place it in try: except: statement with 1 second time out on failing, so it works now.
P.S.1
After some testing, another problem that happen from time to time appear. When tcp connection is established (listening mode) sometimes it receive zero data. Here is part of the code I use:
print('[ WEB ] Incoming connection from: %s' % str(addr))
poller = uselect.poll()
poller.register(conn, uselect.POLLIN)
res = poller.poll(10000) # time in milliseconds
if not res:
conn.close()
print('[ WEB ] INITIAL CONN TIMEOUT')
poller.unregister(conn)
continue
print ("[ WEB ] INITIAL CONN RECV")
poller.unregister(conn)
request = conn.recv(512)
from time to time conn.recv receives 0 bytes. None of that happen on older version. I'm already modified my code to handle this, but for sure that is not the way.