Skip to main content

Networking

Endpoint provides full networking through .NET's standard System.Net stack (sockets, HTTP/HTTPS, WebSockets, TLS). The Endpoint-specific GHIElectronics.Endpoint.Devices.Network package is only for bringing the network interface up and configuring it. The same package is shipped for Python and C/C++, with the same class and method names.

Two interface types are supported:

  • Ethernet: the board's built-in Ethernet, or any USB Ethernet adapter.
  • Wi-Fi: a built-in Wi-Fi module, or a USB Wi-Fi dongle.

Settings persist in /config/network.json. Nothing starts automatically at boot; the application calls Enable() on the interfaces it wants up.

Wi-Fi​

using GHIElectronics.Endpoint.Devices.Network;

var wifi = NetworkController.FromType(NetworkType.Wifi);

var settings = new WiFiNetworkInterfaceSettings {
Ssid = "MyNetwork",
Password = "secret123",
Security = WiFiSecurityType.Wpa2,
IsDhcpEnabled = true,
};

wifi.SetInterfaceSettings(settings); // saves to /config/network.json
wifi.Enable(); // brings the interface up, runs DHCP

Console.WriteLine($"Connected: {wifi.IsConnected}");
Console.WriteLine($"Interface: {wifi.Name}");

// Standard .NET info is also available
var nic = wifi.NetworkInterface;
if (nic != null) {
foreach (var ip in nic.GetIPProperties().UnicastAddresses)
Console.WriteLine(ip.Address);
}

Ethernet​

Static IP:

using System.Net;
using GHIElectronics.Endpoint.Devices.Network;

var eth = NetworkController.FromType(NetworkType.Ethernet);

var settings = new NetworkInterfaceSettings {
IsDhcpEnabled = false,
Address = IPAddress.Parse("192.168.1.50"),
SubnetMask = IPAddress.Parse("255.255.255.0"),
GatewayAddress = IPAddress.Parse("192.168.1.1"),
DnsAddresses = new[] { IPAddress.Parse("8.8.8.8") },
};

eth.SetInterfaceSettings(settings);
eth.Enable();

DHCP (the common case):

var eth = NetworkController.FromType(NetworkType.Ethernet);
eth.SetInterfaceSettings(new NetworkInterfaceSettings { IsDhcpEnabled = true });
eth.Enable();

Using the network​

Once an interface is enabled, use the standard networking surface of your language (HTTP, sockets, WebSockets, TLS) exactly as you would on a PC:

using var http = new HttpClient();
var json = await http.GetStringAsync("https://api.example.com/status");
Console.WriteLine(json);

TLS certificates​

HTTPS clients on Endpoint trust the public CA bundle out of the box, same as any desktop. If your server uses a self-signed certificate or a private CA, ship the CA certificate (PEM file) with your app and tell the HTTP client to trust it.

using System.IO;
using System.Net.Http;
using System.Security.Cryptography.X509Certificates;

var ca = new X509Certificate2(Path.Combine(AppContext.BaseDirectory, "ca.crt"));

var handler = new HttpClientHandler {
ServerCertificateCustomValidationCallback = (msg, chain, errors) => {
chain.ChainPolicy.CustomTrustStore.Add(ca);
chain.ChainPolicy.TrustMode = X509ChainTrustMode.CustomRootTrust;
return chain.Build(chain.ChainElements[0].Certificate);
},
};

using var http = new HttpClient(handler);
var body = await http.GetStringAsync("https://my-server.local/");

For mutual TLS (client certificate too), add handler.ClientCertificates.Add(new X509Certificate2("client.pfx", password));.

Higher-level protocols​

For pub/sub messaging and edge-gateway patterns see MQTT and Endpoint as gateway under Cloud, IoT and Industrial.