Issue with ESPs talking to each other over UDP socket (solved)

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
woodat
Posts: 20
Joined: Wed Feb 17, 2016 9:24 pm

Issue with ESPs talking to each other over UDP socket (solved)

Post by woodat » Fri Oct 28, 2016 7:46 pm

Hello again,

Am currently experimenting with ESPs in a kind of tree topology network (where each ESP is both a client and an AP). As an early proof-of-concept I was trying to get them to talk to one another using simple UDP sockets. Unfortunately I've hit something of a snag and was wondering if it's a bug/limitation of the library or whether I'm not doing something properly.

The setup:

I have 1 ESP as an AP broadcasting a 192.168.4.x network. It's STA_IF is inactive. We'll call this the 'parent'
I have 1 ESP as a client connected to that network (its address is 192.168.4.2). We'll call this the 'child'

I created a socket on both the parent and the child with

Code: Select all

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
The issue:
While I can use s.sendto() to send FROM the child TO the parent, I cannot send from the parent to the child.
Child:

Code: Select all

s.sendto("hello!", ('192.168.4.1', 50000))
works just fine (the parent receives it with s.recvfrom(1024) ). the sendto() gives a return of 5. But,

Parent:

Code: Select all

s.sendto("hello!", ('192.168.4.2', 50000))
doesn't work. The packet is never received by the child. The sendto() returns a 2 and nothing ever appears on the child.

At this point I thought it best to post here, see what the experts suggest (especially as I'm not an expert in network programming in any way).


(As clarification, I did also test with the parent STA_IF active and disconnected and active and connected to another network)
Last edited by woodat on Fri Oct 28, 2016 7:57 pm, edited 1 time in total.

woodat
Posts: 20
Joined: Wed Feb 17, 2016 9:24 pm

Re: Issue with ESPs talking to each other over UDP socket

Post by woodat » Fri Oct 28, 2016 7:50 pm

Oh, and as a followup, there's no way to do a broadcast packet with UDP to the whole local wireless network is there?

woodat
Posts: 20
Joined: Wed Feb 17, 2016 9:24 pm

Re: Issue with ESPs talking to each other over UDP socket

Post by woodat » Fri Oct 28, 2016 8:22 pm

Just tried TCP and it seems to suffer even worse. While it can establish a connection in either direction (either side can be listener and the other can be the client), it cannot send a message either up or down regardless of which is which.

Lysenko
Posts: 62
Joined: Wed Aug 17, 2016 1:21 pm

Re: Issue with ESPs talking to each other over UDP socket

Post by Lysenko » Fri Oct 28, 2016 8:45 pm

woodat wrote:Oh, and as a followup, there's no way to do a broadcast packet with UDP to the whole local wireless network is there?
In a general sense you activate broadcast for a UDP socket by calling socket.setsockopt with the SO_BROADCAST flag and then sending to a broadcast address ending in 255. For example, 192.168.1.255 broadcasts to everything on the 192.168.1.x subnet.

usocket.py has setsockopt and usocket.c appears to pass those parameters essentially unchanged to the nic-> implementation, but I have no idea if that works on an ESP8266 because I haven't worked out the source code structure and naming conventions yet.

There's at least one interface to the lwip stack that only appears to implement SO_REUSEADDR and errors out everything else which seems a bit bizarre (or more likely I'm looking in the wrong place).

chrisgp
Posts: 41
Joined: Fri Apr 01, 2016 5:29 pm

Re: Issue with ESPs talking to each other over UDP socket

Post by chrisgp » Sat Oct 29, 2016 1:51 am

Which address are you binding the client to when you do the s.recvfrom? It sounds like your client might have two network interfaces active so it will have two IP addresses. You say the client's address on the parent's network is 192.168.4.2. What is the client's IP on the network it is broadcasting? Maybe you've considered all this, but make sure the client is binding to 192.168.4.2 (0.0.0.0 should work), and that it doesn't have an IP of 192.168.4.1 on the network it is broadcasting.

woodat
Posts: 20
Joined: Wed Feb 17, 2016 9:24 pm

Re: Issue with ESPs talking to each other over UDP socket

Post by woodat » Fri Nov 04, 2016 9:35 pm

Sorry for the late reply (busy week)
Thank you chrisgp, I'm definitely thinking I muddled the binding (which I didn't even document in the initial post, probably because I didn't have it straight). Have done that properly and it's now working properly. Or at least with UDP. Still have more testing to run.

woodat
Posts: 20
Joined: Wed Feb 17, 2016 9:24 pm

Re: Issue with ESPs talking to each other over UDP socket

Post by woodat » Sun Nov 06, 2016 4:53 am

Alrighty, finished testing. The big issue was indeed my devices both having 192.168.4.x as their AP lan. Guessing it was messing up the child more. Now have TCP and UDP working without incident after reconfiguring the child's network to a different IP range (192.168.5.x). Thank you again

Post Reply