As a side-effect of this, your code will probably start executing before the Wi-Fi radio even finishes connecting to your Wi-Fi access point.
As standard procedure, we recommend that all apps wait for network connectivity before using System.Net classes or SslStream to access the network. Otherwise, if you try connecting to Internet hosts without a live connection, you will just get SocketExceptions (as expected).
Option 1: using DHCP, wait in a loop for a DHCP-allocated IP address
Most users will have their Netduino 3 Wi-Fi mainboards configured to obtain their IP address using DHCP. A really easy way to wait for connectivity is to wait for an IP address like this:
// wait for DHCP-allocated IP address while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any) ;
Option 2: using static IP, wait in a loop for network connectivity
If you are using static IP, add the NetduinoExtensions.dll assembly to your project. Then you can simply wait in a loop for the network connection to go live.
// wait for network connectivity while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) ;
Option 3: wait in a loop, regardless of whether DHCP or static IP is used
This is a good option for apps you're writing for public consumption. Just add the NetduinoExtensions.dll assembly to your project and then add this code at the top of your app.
if (!Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IsDhcpEnabled) { // using static IP while (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) ; // wait for network connectivity } else { // using DHCP while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any) ; // wait for DHCP-allocated IP address }
Option 4: event-driven code to wait for network connectivity and address allocation
This is my favorite option and is the most elegant. This is the recommended solution if your code is multi-threaded or you just want the most flexibility and lowest power consumption. Here we will use events and a WaitHandle to signal when connectivity is live.
Add the following code at the top of your class, as class-level fields:
static System.Threading.AutoResetEvent _networkAvailableEvent = new System.Threading.AutoResetEvent(false); static System.Threading.AutoResetEvent _networkAddressChangedEvent = new System.Threading.AutoResetEvent(false);Add the following code in your Main function to wire up the event handlers.
// wire up events to wait for network link to connect and address to be acquired Microsoft.SPOT.Net.NetworkInformation.NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged; Microsoft.SPOT.Net.NetworkInformation.NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; // if the network is already up or dhcp address is already set, pre-set those flags. if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable()) _networkAvailableEvent.Set(); if (Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IsDhcpEnabled && Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress != "0.0.0.0") _networkAddressChangedEvent.Set();Add the following code in your Main function to wait for the network connection:
_networkAvailableEvent.WaitOne(); _networkAddressChangedEvent.WaitOne();Finally, add these event handlers to your class:
static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e) { _networkAddressChangedEvent.Set(); } static void NetworkChange_NetworkAvailabilityChanged(object sender, Microsoft.SPOT.Net.NetworkInformation.NetworkAvailabilityEventArgs e) { if (e.IsAvailable) { _networkAvailableEvent.Set(); } }
Final notes
In all of the above cases, network connectivity is also usually indicated by a green ACT LED on your Netduino 3 Wi-Fi mainboard. There are some exceptions to that rule (such as the edge case where your router is all out of IP addresses to allocate) but it's a good general rule.
Chris