nanoFramework.NetWorkHelper
1.3.4-preview.30
Prefix Reserved
NetworkHelpers are now available in nanoFramework.System.Net and nanoFramework.System.Device.WiFi
See the version list below for details.
dotnet add package nanoFramework.NetWorkHelper --version 1.3.4-preview.30
NuGet\Install-Package nanoFramework.NetWorkHelper -Version 1.3.4-preview.30
<PackageReference Include="nanoFramework.NetWorkHelper" Version="1.3.4-preview.30" />
paket add nanoFramework.NetWorkHelper --version 1.3.4-preview.30
#r "nuget: nanoFramework.NetWorkHelper, 1.3.4-preview.30"
// Install nanoFramework.NetWorkHelper as a Cake Addin #addin nuget:?package=nanoFramework.NetWorkHelper&version=1.3.4-preview.30&prerelease // Install nanoFramework.NetWorkHelper as a Cake Tool #tool nuget:?package=nanoFramework.NetWorkHelper&version=1.3.4-preview.30&prerelease
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 | ||
Windows.Devices.WiFi (preview) |
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 | Versions Compatible and additional computed target framework versions. |
---|---|
.NET Framework | net is compatible. |
-
- nanoFramework.CoreLibrary (>= 1.11.7)
- nanoFramework.Runtime.Events (>= 1.9.2)
- nanoFramework.System.Net (>= 1.7.1-preview.19)
- nanoFramework.System.Text (>= 1.1.2-preview.14)
- nanoFramework.System.Threading (>= 1.0.3-preview.13)
- nanoFramework.Windows.Devices.Wifi (>= 1.3.4-preview.26)
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 | 610 | 12/29/2021 | |
1.3.4-preview.36 | 263 | 12/16/2021 | |
1.3.4-preview.34 | 379 | 12/3/2021 | |
1.3.4-preview.32 | 198 | 12/3/2021 | |
1.3.4-preview.30 | 204 | 12/2/2021 | |
1.3.4-preview.28 | 198 | 12/2/2021 | |
1.3.4-preview.26 | 199 | 12/2/2021 | |
1.3.4-preview.21 | 617 | 11/13/2021 | |
1.3.4-preview.14 | 245 | 10/23/2021 | |
1.3.4-preview.12 | 199 | 10/22/2021 | |
1.3.4-preview.10 | 234 | 10/18/2021 | |
1.3.4-preview.8 | 245 | 10/18/2021 | |
1.3.4-preview.6 | 269 | 10/17/2021 | |
1.3.4-preview.3 | 881 | 9/16/2021 | |
1.3.3 | 1,625 | 9/16/2021 | |
1.3.3-preview.5 | 964 | 7/17/2021 | |
1.3.3-preview.3 | 215 | 7/16/2021 | |
1.3.2 | 363 | 7/15/2021 | |
1.3.2-preview.77 | 216 | 7/15/2021 | |
1.3.2-preview.75 | 236 | 7/14/2021 | |
1.3.2-preview.71 | 655 | 7/1/2021 | |
1.3.2-preview.68 | 226 | 6/21/2021 | |
1.3.2-preview.66 | 249 | 6/20/2021 | |
1.3.2-preview.60 | 213 | 6/19/2021 | |
1.3.2-preview.58 | 220 | 6/18/2021 | |
1.3.2-preview.56 | 251 | 6/16/2021 | |
1.3.2-preview.54 | 211 | 6/16/2021 | |
1.3.2-preview.50 | 258 | 6/7/2021 | |
1.3.2-preview.48 | 575 | 6/7/2021 | |
1.3.2-preview.46 | 267 | 6/6/2021 | |
1.3.2-preview.44 | 620 | 6/6/2021 | |
1.3.2-preview.42 | 277 | 6/5/2021 |