ESP8266 UDP between AP and client not working? #10148
-
Hi: MicroPython v1.19.1 on 2022-06-18; ESP module with ESP8266 running on a pair of D1 minis. One is set as an AP: import socket
import select
import network
from time import sleep, time
import select
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.ifconfig(('192.168.4.1', '255.255.255.0', '192.168.4.1', '192.168.4.1'))
ap.config(essid = 'esp8266wifi', password = '123456789')
while ap.active() == False:
sleep(1)
print('starting...')
print('Connection successful')
print(ap.ifconfig())
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('192.168.4.1', 6060))
while True:
sleep(0.01)
readable, writeable, exceptional = select.select([sock], [sock], [sock], 1)
for s in readable:
print(s.recvfrom(8)) The other as a client: import socket
import network
from time import sleep, time
wlan = network.WLAN(network.STA_IF)
wlan.active(True)
wlan.ifconfig(('192.168.4.10', '255.255.255.0', '192.168.4.1', '192.168.4.1'))
wlan.connect('esp8266wifi', '123456789')
while not wlan.isconnected():
print('connecting to network...')
sleep(1)
print('network config:', wlan.ifconfig())
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
# sock.bind(('192.168.4.10', 12345))
while True:
sleep(1)
print(sock.sendto(b'1', ('192.168.4.1', 6060)))
print(wlan.ifconfig()) Which doesn't work. The client sends, but the AP doesn't receive the packet. If I uncomment the bind statement in the client script, an EINVAL error is thrown on sendto. The AP receives packets fine from my PC if I run the following: import socket
from time import sleep, time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', 6060))
while True:
sleep(1)
print(sock.sendto(b'from PC', ('192.168.4.1', 6060))) What am I doing wrong? Thank you. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 8 replies
-
I have found that setting static IPs on the ESP32 needed a short delay, say 0.5 seconds before it. Throwing some try/except blocks around might give you a clue. |
Beta Was this translation helpful? Give feedback.
I have found that setting static IPs on the ESP32 needed a short delay, say 0.5 seconds before it. Throwing some try/except blocks around might give you a clue.