Simple network discovery to find Netduinos from Windows

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest

With the advent of Internet-of-Things things, you’ll probably need a decent way to actually find all of the things on your network.

Fun fact: If you send a UDP packet to *.255 on your network, your router will then send that along to all the devices on your network. So if your local network is on 192.168.1.x, then send it to 192.168.1.255. Or if you want to send it to everything, then you can send to 255.255.255.255.

In my case, I’ve got this awesome little guy…

Netduino 3 WiFi

…setup with DHCP, so the IP occasionally changes. I’ve got a Windows 10 app that needs to connect to it, so we can use the way above to find the Netduino on the network.

 Universal Windows app discovering Netduino

Netduino bit

First create this class in the Netduino solution:

It is pretty simple stuff. We setup a UDP server, then constantly loop looking if we’ve received anything. If we receive “ping”, then send “pong” back to whatever sent it.

Side note: Initially I was checking the _socket.Available value to see if there is new data, then run _socket.ReceiveFrom. That had a weird behavior where it would see there were x bytes available, but then lock on the _socket.ReceiveFrom line until the next message you sent.
I contacted Chris Walker/Mr.Netduino and he found out the issue was that internally _socket.Available actually pulls the data into an internal buffer to give you a number of bytes, but that means that on the next line _socket.ReceiveFrom has no data left to read (it is all in the buffer, which only _socket.Receive uses). He’s filed this GHI discussing the issue. The simple workaround is to not check available bytes, but rather just see if the value returned from _socket.ReceiveFrom is > 0.

Then initialize it in the main method:

 

Universal Windows app bit

On the Windows app side, we have a super basic UI – literally just to check that the code is working. Please don’t ever publish something that looks like this.

And then this will send the actual UDP message to devices, and see which devices reply

Finally, call the above when the search button is pressed

 

 

Discover all the things

 

 

Tweet about this on TwitterShare on FacebookShare on RedditShare on Google+Email this to someonePin on Pinterest