using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Network;
using GHIElectronics.TinyCLR.Devices.Spi;
using GHIElectronics.TinyCLR.Drivers.Microchip.Winc15x0;
using GHIElectronics.TinyCLR.Networking.Mqtt;
using GHIElectronics.TinyCLR.Pins;
using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
namespace SitCoreDev
{
class Program
{
static void Main()
{
var enablePin = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PI0);
enablePin.SetDriveMode(GpioPinDriveMode.Output);
enablePin.Write(GpioPinValue.High);
SpiNetworkCommunicationInterfaceSettings netInterfaceSettings =
new SpiNetworkCommunicationInterfaceSettings();
var cs = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PG12);
var settings = new SpiConnectionSettings()
{
ChipSelectLine = cs,
ClockFrequency = 4000000,
Mode = SpiMode.Mode0,
ChipSelectType = SpiChipSelectType.Gpio,
ChipSelectHoldTime = TimeSpan.FromTicks(10),
ChipSelectSetupTime = TimeSpan.FromTicks(10)
};
netInterfaceSettings.SpiApiName = SC20260.SpiBus.Spi3;
netInterfaceSettings.GpioApiName = SC20260.GpioPin.Id;
netInterfaceSettings.SpiSettings = settings;
netInterfaceSettings.InterruptPin = GpioController.GetDefault().
OpenPin(SC20260.GpioPin.PG6);
netInterfaceSettings.InterruptEdge = GpioPinEdge.FallingEdge;
netInterfaceSettings.InterruptDriveMode = GpioPinDriveMode.InputPullUp;
netInterfaceSettings.ResetPin = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PI8);
netInterfaceSettings.ResetActiveState = GpioPinValue.Low;
var networkController = NetworkController.FromName
("GHIElectronics.TinyCLR.NativeApis.ATWINC15xx.NetworkController");
WiFiNetworkInterfaceSettings wifiSettings = new WiFiNetworkInterfaceSettings()
{
Ssid = "PecnikVip",
Password = "PecnikVip",
};
// wifiSettings.Address = new IPAddress(new byte[] { 192, 168, 0, 122 });
// wifiSettings.SubnetMask = new IPAddress(new byte[] { 255, 255, 255, 0 });
// wifiSettings.GatewayAddress = new IPAddress(new byte[] { 192, 168, 0, 1 });
// wifiSettings.DnsAddresses = new IPAddress[] { new IPAddress(new byte[]
//{ 75, 75, 75, 75 }), new IPAddress(new byte[] { 75, 75, 75, 76 }) };
wifiSettings.MacAddress = new byte[] { 0x00, 0x4, 0x00, 0x00, 0x00, 0x00 };
wifiSettings.IsDhcpEnabled = true;
wifiSettings.IsDynamicDnsEnabled = true;
wifiSettings.TlsEntropy = new byte[] { 0, 1, 2, 3 };
networkController.SetInterfaceSettings(wifiSettings);
networkController.SetCommunicationInterfaceSettings(netInterfaceSettings);
networkController.SetAsDefaultController();
networkController.NetworkAddressChanged += NetworkController_NetworkAddressChanged;
networkController.NetworkLinkConnectedChanged += NetworkController_NetworkLinkConnectedChanged;
networkController.Enable();
Thread.Sleep(2000);
//Scan for WiFi access points:
string[] ssidList = Winc15x0Interface.Scan();
foreach (string item in ssidList)
{
Debug.WriteLine(item);
}
//Get Relative Signal Strength Indicator for the connected access point:
int signalStrength = Winc15x0Interface.GetRssi();
Debug.WriteLine(signalStrength.ToString());
//Get the WiFi module's MAC address:
byte[] macAddress = Winc15x0Interface.GetMacAddress();
Debug.WriteLine(macAddress.ToString());
TestHttp();
// Network is ready to used
Thread.Sleep(Timeout.Infinite);
}
static void TestHttp()
{
var url = "http://www.bing.com/robots.txt";
int read = 0, total = 0;
byte[] result = new byte[512];
try
{
using (var req = HttpWebRequest.Create(url) as HttpWebRequest)
{
req.KeepAlive = false;
req.ReadWriteTimeout = 2000;
using (var res = req.GetResponse() as HttpWebResponse)
{
using (var stream = res.GetResponseStream())
{
do
{
read = stream.Read(result, 0, result.Length);
total += read;
System.Diagnostics.Debug.WriteLine("read : " + read);
System.Diagnostics.Debug.WriteLine("total : " + total);
String page = "";
page = new String(System.Text.Encoding.UTF8.GetChars
(result, 0, read));
System.Diagnostics.Debug.WriteLine("Response : " + page);
}
while (read != 0);
}
}
}
}
catch
{
}
}
private static void NetworkController_NetworkLinkConnectedChanged
(NetworkController sender, NetworkLinkConnectedChangedEventArgs e)
{
// Raise event connect/disconnect
}
private static void NetworkController_NetworkAddressChanged
(NetworkController sender, NetworkAddressChangedEventArgs e)
{
var ipProperties = sender.GetIPProperties();
var address = ipProperties.Address.GetAddressBytes();
Debug.WriteLine("IP: " + address[0] + "." + address[1] + "." + address[2] +
"." + address[3]);
}
}
}
exception:
The thread '<No Name>' (0x2) has exited with code 0 (0x0).
IP: 0.0.0.0
IP: 192.168.0.113
PecnikVip
76
System.Byte[]
#### Exception System.InvalidOperationException - CLR_E_INVALID_OPERATION (1) ####
#### Message:
#### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::GetHostByName [IP: 0000] ####
#### System.Net.Dns::GetHostEntry [IP: 000b] ####
#### System.Net.HttpWebRequest::EstablishConnection [IP: 00ed] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 0019] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### SitCoreDev.Program::TestHttp [IP: 0034] ####
#### SitCoreDev.Program::Main [IP: 0178] ####
#### Exception System.Net.WebException - 0x00000000 (1) ####
#### Message:
#### System.Net.HttpWebRequest::GetResponse [IP: 00d3] ####
#### SitCoreDev.Program::TestHttp [IP: 0034] ####
#### SitCoreDev.Program::Main [IP: 0178] ####
Exception thrown: 'System.Net.WebException' in GHIElectronics.TinyCLR.Networking.Http.dll
As you can see it finds PecnikVip WIFI, signal strength is 76, IP address etc…
And i can confirm in my router that the device has connected.