nanoFramework.NetWorkHelper 1.3.4-preview.28

The ID prefix of this package has been reserved for one of the owners of this package by NuGet.org. Prefix Reserved
Additional Details

NetworkHelpers are now available in nanoFramework.System.Net and nanoFramework.System.Device.WiFi

This is a prerelease version of nanoFramework.NetWorkHelper.
There is a newer prerelease version of this package available.
See the version list below for details.
dotnet add package nanoFramework.NetWorkHelper --version 1.3.4-preview.28
NuGet\Install-Package nanoFramework.NetWorkHelper -Version 1.3.4-preview.28
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="nanoFramework.NetWorkHelper" Version="1.3.4-preview.28" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add nanoFramework.NetWorkHelper --version 1.3.4-preview.28
#r "nuget: nanoFramework.NetWorkHelper, 1.3.4-preview.28"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install nanoFramework.NetWorkHelper as a Cake Addin
#addin nuget:?package=nanoFramework.NetWorkHelper&version=1.3.4-preview.28&prerelease

// Install nanoFramework.NetWorkHelper as a Cake Tool
#tool nuget:?package=nanoFramework.NetWorkHelper&version=1.3.4-preview.28&prerelease

Quality Gate Status Reliability Rating License NuGet #yourfirstpr Discord

nanoFramework logo


Welcome to the .NET nanoFramework Windows.Devices.WiFi Library repository and NetworkHelper

This repository contains nanoFramework Windows.Devices.Wifi and NetworkHelper.

Build status

Component Build Status NuGet Package
Windows.Devices.WiFi Build Status NuGet
Windows.Devices.WiFi (preview) Build Status NuGet

NetworkHelper usage

The NetworkHelper is mainly dedicated to help you connect automatically to WiFi networks. That said, it can be used as well to check if you have a valid IP address and a valid date on any interface including on ethernet.

Preferred usage

The following code will allow you to connect automatically, wait for a valid IP address and a valid date with specific credentials:

const string Ssid = "YourSSID";
const string Password = "YourWifiPassword";
// Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = NetworkHelper.ConnectWifiDhcp(Ssid, Password, setDateTime: true, token: cs.Token);
if (!success)
{
    // Something went wrong, you can get details with the ConnectionError property:
    Debug.WriteLine($"Can't connect to the network, error: {NetworkHelper.ConnectionError.Error}");
    if (NetworkHelper.ConnectionError.Exception != null)
    {
        Debug.WriteLine($"ex: { NetworkHelper.ConnectionError.Exception}");
    }
}
// Otherwise, you are connected and have a valid IP and date

Note that this function will store the network credentials on the device.

Using stored credentials

You can as well connect to a network with pre stored credentials on the device depending on the type of device you have. Please check for proper support with your device.

if (!NetworkHelper.IsConfigurationStored())
{
    Debug.WriteLine("No configuration stored in the device");
}
else
{
    // The wifi credentials are already stored on the device
    // Give 60 seconds to the wifi join to happen
    CancellationTokenSource cs = new(60000);
    var success = NetworkHelper.ReconnectWifi(setDateTime: true, token: cs.Token);
    if (!success)
    {
        // Something went wrong, you can get details with the ConnectionError property:
        Debug.WriteLine($"Can't connect to the network, error: {NetworkHelper.ConnectionError.Error}");
        if (NetworkHelper.ConnectionError.Exception != null)
        {
            Debug.WriteLine($"ex: { NetworkHelper.ConnectionError.Exception}");
        }
    }
    // Otherwise, you are connected and have a valid IP and date
}

Scan and join

You can force to scan the network and then join the network. This case can be useful in specific conditions. Be aware, that you may have to use this method with one of the previous method depending the support of rescanning while you are already connected.

const string Ssid = "YourSSID";
const string Password = "YourWifiPassword";
// Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = NetworkHelper.ScanAndConnectWifiDhcp(Ssid, Password, setDateTime: true, token: cs.Token);
if (!success)
{
    // Something went wrong, you can get details with the ConnectionError property:
    Debug.WriteLine($"Can't connect to the network, error: {NetworkHelper.ConnectionError.Error}");
    if (NetworkHelper.ConnectionError.Exception != null)
    {
        Debug.WriteLine($"ex: { NetworkHelper.ConnectionError.Exception}");
    }
}
// Otherwise, you are connected and have a valid IP and date

Note that this function will store the network credentials on the device.

Joining with a static IP address

You join a WiFi network with a static IP address:

const string Ssid = "YourSSID";
const string Password = "YourWifiPassword";
// Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = NetworkHelper.ConnectWifiFixAddress(Ssid, Password, new IPConfiguration("192.168.1.7", "255.255.255.0", "192.168.1.1"), setDateTime: true, token: cs.Token);

Checking valid IP address and date

The NetworkHelper offers couple of functions to check validity of your IP address, the DateTime and helping setting them up:

var success = IsValidDateTime();
// if success is true, you have a valid DateTime
success = IsValidIpAddress(NetworkInterfaceType.Wireless80211);
// if success is true, you have a valid IP address on the Wireless adapter
// Be aware that a local address like 127.0.0.1 is considered as a valid address for ethernet

You can as well wait for a valid IP address and date time:

// Give 60 seconds to check if we have a valid IP and a valid DateTime
CancellationTokenSource cs = new(60000);
var success = WaitForValidIPAndDate(true, NetworkInterfaceType.Ethernet, cs.Token);
// if success is true then you are connected

NetworkHelper usage

The NetworkHelper is mainly dedicated to help you connect automatically to WiFi networks. That said, it can be used as well to check if you have a valid IP address and a valid date on any interface including on ethernet.

Preferred usage

The following code will allow you to connect automatically, wait for a valid IP address and a valid date with specific credentials:

const string Ssid = "YourSSID";
const string Password = "YourWifiPassword";
// Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = NetworkHelper.ConnectWifiDhcp(Ssid, Password, setDateTime: true, token: cs.Token);
if (!success)
{
    // Something went wrong, you can get details with the ConnectionError property:
    Debug.WriteLine($"Can't connect to the network, error: {NetworkHelper.ConnectionError.Error}");
    if (NetworkHelper.ConnectionError.Exception != null)
    {
        Debug.WriteLine($"ex: { NetworkHelper.ConnectionError.Exception}");
    }
}
// Otherwise, you are connected and have a valid IP and date

Note that this function will store the network credentials on the device.

Using stored credentials

You can as well connect to a network with pre stored credentials on the device depending on the type of device you have. Please check for proper support with your device.

// The wifi credentials are already stored on the device
 // Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = NetworkHelper.ReconnectWifi(setDateTime: true, token: cs.Token);
if (!success)
{
    // Something went wrong, you can get details with the ConnectionError property:
    Debug.WriteLine($"Can't connect to the network, error: {NetworkHelper.ConnectionError.Error}");
    if (NetworkHelper.ConnectionError.Exception != null)
    {
        Debug.WriteLine($"ex: { NetworkHelper.ConnectionError.Exception}");
    }
}
// Otherwise, you are connected and have a valid IP and date

Scan and join

You can force to scan the network and then join the network. This case can be useful in specific conditions. Be aware, that you may have to use this method with one of the previous method depending the support of rescanning while you are already connected.

const string Ssid = "YourSSID";
const string Password = "YourWifiPassword";
// Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = NetworkHelper.ScanAndConnectWifiDhcp(Ssid, Password, setDateTime: true, token: cs.Token);
if (!success)
{
    // Something went wrong, you can get details with the ConnectionError property:
    Debug.WriteLine($"Can't connect to the network, error: {NetworkHelper.ConnectionError.Error}");
    if (NetworkHelper.ConnectionError.Exception != null)
    {
        Debug.WriteLine($"ex: { NetworkHelper.ConnectionError.Exception}");
    }
}
// Otherwise, you are connected and have a valid IP and date

Note that this function will store the network credentials on the device.

Joining with a static IP address

You join a WiFi network with a static IP address:

const string Ssid = "YourSSID";
const string Password = "YourWifiPassword";
// Give 60 seconds to the wifi join to happen
CancellationTokenSource cs = new(60000);
var success = NetworkHelper.ConnectWifiFixAddress(Ssid, Password, new IPConfiguration("192.168.1.7", "255.255.255.0", "192.168.1.1"), setDateTime: true, token: cs.Token);

Checking valid IP address and date

The NetworkHelper offers couple of functions to check validity of your IP address, the DateTime and helping setting them up:

var success = IsValidDateTime();
// if success is true, you have a valid DateTime
success = IsValidIpAddress(NetworkInterfaceType.Wireless80211);
// if success is true, you have a valid IP address on the Wireless adapter
// Be aware that a local address like 127.0.0.1 is considered as a valid address for ethernet

You can as well wait for a valid IP address and date time:

// Give 60 seconds to check if we have a valid IP and a valid DateTime
CancellationTokenSource cs = new(60000);
var success = WaitForValidIPAndDate(true, NetworkInterfaceType.Ethernet, cs.Token);
// if success is true then you are connected

Feedback and documentation

For documentation, providing feedback, issues and finding out how to contribute please refer to the Home repo.

Join our Discord community here.

Credits

The list of contributors to this project can be found at CONTRIBUTORS.

License

The nanoFramework Class Libraries are licensed under the MIT license.

Code of Conduct

This project has adopted the code of conduct defined by the Contributor Covenant to clarify expected behaviour in our community. For more information see the .NET Foundation Code of Conduct.

.NET Foundation

This project is supported by the .NET Foundation.

Product Compatible and additional computed target framework versions.
.NET Framework net is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on nanoFramework.NetWorkHelper:

Repository Stars
dotnet/samples
Sample code referenced by the .NET documentation
Version Downloads Last updated
1.3.4-preview.40 581 12/29/2021
1.3.4-preview.36 241 12/16/2021
1.3.4-preview.34 361 12/3/2021
1.3.4-preview.32 180 12/3/2021
1.3.4-preview.30 182 12/2/2021
1.3.4-preview.28 176 12/2/2021
1.3.4-preview.26 182 12/2/2021
1.3.4-preview.21 602 11/13/2021
1.3.4-preview.14 224 10/23/2021
1.3.4-preview.12 184 10/22/2021
1.3.4-preview.10 217 10/18/2021
1.3.4-preview.8 229 10/18/2021
1.3.4-preview.6 253 10/17/2021
1.3.4-preview.3 866 9/16/2021
1.3.3 1,545 9/16/2021
1.3.3-preview.5 802 7/17/2021
1.3.3-preview.3 199 7/16/2021
1.3.2 330 7/15/2021
1.3.2-preview.77 200 7/15/2021
1.3.2-preview.75 212 7/14/2021
1.3.2-preview.71 560 7/1/2021
1.3.2-preview.68 209 6/21/2021
1.3.2-preview.66 227 6/20/2021
1.3.2-preview.60 195 6/19/2021
1.3.2-preview.58 203 6/18/2021
1.3.2-preview.56 235 6/16/2021
1.3.2-preview.54 188 6/16/2021
1.3.2-preview.50 239 6/7/2021
1.3.2-preview.48 557 6/7/2021
1.3.2-preview.46 244 6/6/2021
1.3.2-preview.44 603 6/6/2021
1.3.2-preview.42 256 6/5/2021