Maple Server now Supports Automatic UDP Broadcasts!
Hey folks! We added automatic User Datagram Protocol (UDP) broadcast support to Maple Server that makes discovering and connecting to your Netduino-powered connected things a breeze. UDP allows you to broadcast a name and IP of your Netduino on a local network, so that connecting applications, like mobile apps can automatically retrieve the IP address of your connected thing and remove the need for users to enter it in manually. For instance, consider the RgbRemote client app:
When the app launches, it checks for UDP broadcasts and will populate a picker control with all the servers found on the network, meaning you could have multiple connected things running Maple server, and you can automatically list them, or even filter on the name to show specific ones to connect to.
Configuring Automatic UDP Broadcast
It’s super easy to configure the Netduino to UDP broadcast, simply pass the name and IP to Maple Server’s Start()
method. For example, the following code works in conjunction with the `Netduino.Foundation.Network.Initializer` class to retrieve the current IP:
MapleServer server = new MapleServer();
Initializer.NetworkConnected += (s, e) =>
{
// start maple server and send name broadcast address
server.Start(“my server”, Initializer.CurrentNetworkInterface.IPAddress);
};
Initializer.InitializeNetwork();
For a full example, check out the Maple_Sample.
Maple Server will begin broadcasting the name and IP when it finishes starting the service.
Enumerating Broadcasters
To get the IP address on your client application, simply listen for the broadcasted message over UDP. If your client application is written in C#, the code looks like the following:
UdpClient udpClient = new UdpClient(MAPLE_SERVER_BROADCASTPORT);
IPEndPoint RemoteIpEndPoint = new IPEndPoint(IPAddress.Any, 0);
while (true)
{
Byte[] receiveBytes = udpClient.Receive(ref RemoteIpEndPoint);
string returnData = Encoding.ASCII.GetString(receiveBytes);
Console.WriteLine(returnData);
}
For a full sample check out the Maple_Sample_Listener .Net console app source.
Xamarin Client Nuget Package
If you’re connecting via a Xamarin app, we’ve made it even easier by publishing a NuGet package of the Maple client code so your only focus is getting the server(s) and send requests. Simply add the MapleClient nuget, instantiate an instance of MapleClient derived class, and call `FindMapleServersAsync` to find all broadcasting Netduino devices on your network. To send data, call `SendCommandAsync` with your text data and the IP address of the Netduino.
MapleClient is compatible with .NET Standard 1.3 and up, so you can use it with almost any modern .NET platform including WPF, UWP, and Xamarin. The example code below shows how to use MapleClient in Xamarin.Forms:
public class RgbLedClient : MapleClient
{
public async Task<bool> TurnOnAsync(ServerItem server)
{
return (await SendCommandAsync(“TurnOn”, server.IpAddress));
}
public async Task<bool> TurnOffAsync(ServerItem server)
{
return (await SendCommandAsync(“TurnOff”, server.IpAddress));
}
…
}
public partial class MainPage : ContentPage
{
List<ServerItem> serverList { get; set; }
ServerItem connectedServer;
RgbLedClient rgbClient = new RgbLedClient();
async Task FindAServer()
{
serverList = await rgbClient.FindMapleServers();
connectedServer = serverList.FirstOrDefault();
}
void TurnLedOn()
{
if (connectedServer != null)
rgbClient.TurnOn(connectedServer);
}
void TurnLedOff()
{
if (connectedServer != null)
rgbClient.TurnOff(connectedServer);
}
…
}
For a full sample, check out the source for the RbgLedRemote.
Maple server discovery via UDP is a simple and reliable way to set up communications with client apps. If you want to learn more about handling HTTP requests from your device, check out how we added connectivity to Connected Coffee Maker.
If you’re a Maple veteran and have suggestions, please leave us comment or a pull request.