Skip to content

Instantly share code, notes, and snippets.

@bauland bauland/testWifi1 Secret
Last active Aug 6, 2018

Embed
What would you like to do?
Test Http
using System;
using System.Diagnostics;
using System.Net;
using System.Net.NetworkInterface;
using System.Runtime.InteropServices;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading;
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Spi;
using GHIElectronics.TinyCLR.Networking.SPWF04Sx;
using GHIElectronics.TinyCLR.Pins;
namespace testWifi
{
internal class App
{
private static GpioPin _led1;
private static GpioPin _btn1;
private static SPWF04SxInterface _wifi;
private static bool _bInitialized = false;
public App()
{
Init();
}
internal void Init()
{
Debug.WriteLine("App - Init");
var cont = GpioController.GetDefault();
_led1 = cont.OpenPin(FEZ.GpioPin.Led1);
_btn1 = cont.OpenPin(FEZ.GpioPin.Btn1);
_led1.SetDriveMode(GpioPinDriveMode.Output);
_btn1.SetDriveMode(GpioPinDriveMode.InputPullUp);
var reset = cont.OpenPin(FEZ.GpioPin.WiFiReset);
var irq = cont.OpenPin(FEZ.GpioPin.WiFiInterrupt);
var spi = SpiDevice.FromId(FEZ.SpiBus.WiFi,
SPWF04SxInterface.GetConnectionSettings(FEZ.GpioPin.WiFiChipSelect));
_wifi = new SPWF04SxInterface(spi, irq, reset);
_wifi.IndicationReceived +=
(s, e) =>
{
Debug.WriteLine($"WIND: {WindToName(e.Indication)} {e.Message}");
if (e.Indication == SPWF04SxIndication.WiFiUp)
{
}
else if (e.Indication == SPWF04SxIndication.NtpServerDelivery)
{
SetTime(e.Message);
}
};
_wifi.ErrorReceived += (s, e) => Debug.WriteLine($"ERROR: {e.Error} {e.Message}");
WaitForButton("Press button to turn wifi on...");
_wifi.TurnOn();
NetworkInterface.ActiveNetworkInterface = _wifi;
Thread.Sleep(2000);
// WaitForButton("Press button to Join...");
//You only need to do this once, it'll get saved to the Wi-Fi internal config to be reused on reboot.
_wifi.JoinNetwork("Livebox-Code", "password");
//WaitForButton(" Press button to Certificate...");
//_wifi.ClearTlsServerRootCertificate();
//You'll need to download and use the correct root certificates for the site you want to connect to.
//Debug.WriteLine("After JoinNetwork - Press button...");
// WaitForButton();
//wifi.SetTlsServerRootCertificate(Resources.GetBytes(Resources.BinaryResources.Digicert___GHI));
//wifi.SetTlsServerRootCertificate(Resources.GetBytes(Resources.BinaryResources.DigiCert_High_Assurance___StackOverflow));
while (true)
{
WaitForButton();
Debug.WriteLine("Test");
if ((_wifi.OperationalStatus == OperationalStatus.Up) && (_wifi.State == SPWF04SxWiFiState.ReadyToTransmit))
{
Debug.WriteLine("## Start test...");
try
{
TestHttp("http://srv-backup.bauland.fr", "/index.html");
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
}
}
}
}
private static void TestHttp(string host, string url, string commonName = null)
{
if (commonName != null)
{
_wifi.ForceSocketsTls = true;
_wifi.ForceSocketsTlsCommonName = commonName;
}
var buffer = new byte[512];
var start = DateTime.UtcNow;
var req = (HttpWebRequest)HttpWebRequest.Create(host + url);
req.HttpsAuthentCerts = new[] { new X509Certificate() };
var res = (HttpWebResponse)req.GetResponse();
var str = res.GetResponseStream();
Debug.WriteLine($"HTTP {res.StatusCode}");
var total = 0;
while (str.Read(buffer, 0, buffer.Length) is var read && read > 0)
{
total += read;
try
{
Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read));
}
catch
{
Debugger.Log(0, "", Encoding.UTF8.GetString(buffer, 0, read - 1));
}
Thread.Sleep(100);
}
Debug.WriteLine($"\r\nRead: {total:N0} in {(DateTime.UtcNow - start).TotalMilliseconds:N0}ms");
}
private void SetTime(string eMessage)
{
int year = 0, month = 0, day = 0;
int hour = 0, minute = 0, second = 0;
var index = eMessage.IndexOf(":");
var lastIndex = eMessage.LastIndexOf(":");
var date = eMessage.Substring(0, index);
var time = eMessage.Substring(lastIndex + 1, eMessage.Length - lastIndex - 2 - 1);
var infoDate = date.Split('.');
var infoTime = time.Split('.');
if (Int32.TryParse(infoDate[0], out year) && Int32.TryParse(infoDate[1], out month) && Int32.TryParse(infoDate[2], out day)
&& Int32.TryParse(infoTime[0], out hour) && Int32.TryParse(infoTime[1], out minute) && Int32.TryParse(infoTime[2], out second))
{
DateTime dt = new DateTime(year, month, day, hour, minute, second);
SystemTime.SetSystemTime((ulong)dt.Ticks, 120);
Debug.WriteLine($"Date/Time: {DateTime.Now}");
}
}
public void Loop()
{
Thread.Sleep(50);
}
private static void WaitForButton(string msg = "")
{
if (!String.IsNullOrEmpty(msg))
Debug.WriteLine(msg);
while (_btn1.Read() == GpioPinValue.High)
{
_led1.Write(_led1.Read() == GpioPinValue.High ? GpioPinValue.Low : GpioPinValue.High);
Thread.Sleep(50);
}
while (_btn1.Read() == GpioPinValue.Low)
Thread.Sleep(50);
}
private static string WindToName(SPWF04SxIndication wind)
{
switch (wind)
{
case SPWF04SxIndication.ConsoleActive: return nameof(SPWF04SxIndication.ConsoleActive);
case SPWF04SxIndication.PowerOn: return nameof(SPWF04SxIndication.PowerOn);
case SPWF04SxIndication.Reset: return nameof(SPWF04SxIndication.Reset);
case SPWF04SxIndication.WatchdogRunning: return nameof(SPWF04SxIndication.WatchdogRunning);
case SPWF04SxIndication.LowMemory: return nameof(SPWF04SxIndication.LowMemory);
case SPWF04SxIndication.WiFiHardwareFailure: return nameof(SPWF04SxIndication.WiFiHardwareFailure);
case SPWF04SxIndication.ConfigurationFailure: return nameof(SPWF04SxIndication.ConfigurationFailure);
case SPWF04SxIndication.HardFault: return nameof(SPWF04SxIndication.HardFault);
case SPWF04SxIndication.StackOverflow: return nameof(SPWF04SxIndication.StackOverflow);
case SPWF04SxIndication.MallocFailed: return nameof(SPWF04SxIndication.MallocFailed);
case SPWF04SxIndication.RadioStartup: return nameof(SPWF04SxIndication.RadioStartup);
case SPWF04SxIndication.WiFiPSMode: return nameof(SPWF04SxIndication.WiFiPSMode);
case SPWF04SxIndication.Copyright: return nameof(SPWF04SxIndication.Copyright);
case SPWF04SxIndication.WiFiBssRegained: return nameof(SPWF04SxIndication.WiFiBssRegained);
case SPWF04SxIndication.WiFiSignalLow: return nameof(SPWF04SxIndication.WiFiSignalLow);
case SPWF04SxIndication.WiFiSignalOk: return nameof(SPWF04SxIndication.WiFiSignalOk);
case SPWF04SxIndication.BootMessages: return nameof(SPWF04SxIndication.BootMessages);
case SPWF04SxIndication.KeytypeNotImplemented: return nameof(SPWF04SxIndication.KeytypeNotImplemented);
case SPWF04SxIndication.WiFiJoin: return nameof(SPWF04SxIndication.WiFiJoin);
case SPWF04SxIndication.WiFiJoinFailed: return nameof(SPWF04SxIndication.WiFiJoinFailed);
case SPWF04SxIndication.WiFiScanning: return nameof(SPWF04SxIndication.WiFiScanning);
case SPWF04SxIndication.ScanBlewUp: return nameof(SPWF04SxIndication.ScanBlewUp);
case SPWF04SxIndication.ScanFailed: return nameof(SPWF04SxIndication.ScanFailed);
case SPWF04SxIndication.WiFiUp: return nameof(SPWF04SxIndication.WiFiUp);
case SPWF04SxIndication.WiFiAssociationSuccessful:
return nameof(SPWF04SxIndication.WiFiAssociationSuccessful);
case SPWF04SxIndication.StartedAP: return nameof(SPWF04SxIndication.StartedAP);
case SPWF04SxIndication.APStartFailed: return nameof(SPWF04SxIndication.APStartFailed);
case SPWF04SxIndication.StationAssociated: return nameof(SPWF04SxIndication.StationAssociated);
case SPWF04SxIndication.DhcpReply: return nameof(SPWF04SxIndication.DhcpReply);
case SPWF04SxIndication.WiFiBssLost: return nameof(SPWF04SxIndication.WiFiBssLost);
case SPWF04SxIndication.WiFiException: return nameof(SPWF04SxIndication.WiFiException);
case SPWF04SxIndication.WiFiHardwareStarted: return nameof(SPWF04SxIndication.WiFiHardwareStarted);
case SPWF04SxIndication.WiFiNetwork: return nameof(SPWF04SxIndication.WiFiNetwork);
case SPWF04SxIndication.WiFiUnhandledEvent: return nameof(SPWF04SxIndication.WiFiUnhandledEvent);
case SPWF04SxIndication.WiFiScan: return nameof(SPWF04SxIndication.WiFiScan);
case SPWF04SxIndication.WiFiUnhandledIndication:
return nameof(SPWF04SxIndication.WiFiUnhandledIndication);
case SPWF04SxIndication.WiFiPoweredDown: return nameof(SPWF04SxIndication.WiFiPoweredDown);
case SPWF04SxIndication.HWInMiniAPMode: return nameof(SPWF04SxIndication.HWInMiniAPMode);
case SPWF04SxIndication.WiFiDeauthentication: return nameof(SPWF04SxIndication.WiFiDeauthentication);
case SPWF04SxIndication.WiFiDisassociation: return nameof(SPWF04SxIndication.WiFiDisassociation);
case SPWF04SxIndication.WiFiUnhandledManagement:
return nameof(SPWF04SxIndication.WiFiUnhandledManagement);
case SPWF04SxIndication.WiFiUnhandledData: return nameof(SPWF04SxIndication.WiFiUnhandledData);
case SPWF04SxIndication.WiFiUnknownFrame: return nameof(SPWF04SxIndication.WiFiUnknownFrame);
case SPWF04SxIndication.Dot11Illegal: return nameof(SPWF04SxIndication.Dot11Illegal);
case SPWF04SxIndication.WpaCrunchingPsk: return nameof(SPWF04SxIndication.WpaCrunchingPsk);
case SPWF04SxIndication.WpaTerminated: return nameof(SPWF04SxIndication.WpaTerminated);
case SPWF04SxIndication.WpaStartFailed: return nameof(SPWF04SxIndication.WpaStartFailed);
case SPWF04SxIndication.WpaHandshakeComplete: return nameof(SPWF04SxIndication.WpaHandshakeComplete);
case SPWF04SxIndication.GpioInterrupt: return nameof(SPWF04SxIndication.GpioInterrupt);
case SPWF04SxIndication.Wakeup: return nameof(SPWF04SxIndication.Wakeup);
case SPWF04SxIndication.PendingData: return nameof(SPWF04SxIndication.PendingData);
case SPWF04SxIndication.InputToRemote: return nameof(SPWF04SxIndication.InputToRemote);
case SPWF04SxIndication.OutputFromRemote: return nameof(SPWF04SxIndication.OutputFromRemote);
case SPWF04SxIndication.SocketClosed: return nameof(SPWF04SxIndication.SocketClosed);
case SPWF04SxIndication.IncomingSocketClient: return nameof(SPWF04SxIndication.IncomingSocketClient);
case SPWF04SxIndication.SocketClientGone: return nameof(SPWF04SxIndication.SocketClientGone);
case SPWF04SxIndication.SocketDroppingData: return nameof(SPWF04SxIndication.SocketDroppingData);
case SPWF04SxIndication.RemoteConfiguration: return nameof(SPWF04SxIndication.RemoteConfiguration);
case SPWF04SxIndication.FactoryReset: return nameof(SPWF04SxIndication.FactoryReset);
case SPWF04SxIndication.LowPowerMode: return nameof(SPWF04SxIndication.LowPowerMode);
case SPWF04SxIndication.GoingIntoStandby: return nameof(SPWF04SxIndication.GoingIntoStandby);
case SPWF04SxIndication.ResumingFromStandby: return nameof(SPWF04SxIndication.ResumingFromStandby);
case SPWF04SxIndication.GoingIntoDeepSleep: return nameof(SPWF04SxIndication.GoingIntoDeepSleep);
case SPWF04SxIndication.ResumingFromDeepSleep: return nameof(SPWF04SxIndication.ResumingFromDeepSleep);
case SPWF04SxIndication.StationDisassociated: return nameof(SPWF04SxIndication.StationDisassociated);
case SPWF04SxIndication.SystemConfigurationUpdated:return nameof(SPWF04SxIndication.SystemConfigurationUpdated);
case SPWF04SxIndication.RejectedFoundNetwork: return nameof(SPWF04SxIndication.RejectedFoundNetwork);
case SPWF04SxIndication.RejectedAssociation: return nameof(SPWF04SxIndication.RejectedAssociation);
case SPWF04SxIndication.WiFiAuthenticationTimedOut:return nameof(SPWF04SxIndication.WiFiAuthenticationTimedOut);
case SPWF04SxIndication.WiFiAssociationTimedOut:return nameof(SPWF04SxIndication.WiFiAssociationTimedOut);
case SPWF04SxIndication.MicFailure: return nameof(SPWF04SxIndication.MicFailure);
case SPWF04SxIndication.UdpBroadcast: return nameof(SPWF04SxIndication.UdpBroadcast);
case SPWF04SxIndication.WpsGeneratedDhKeyset: return nameof(SPWF04SxIndication.WpsGeneratedDhKeyset);
case SPWF04SxIndication.WpsEnrollmentAttemptTimedOut:
return nameof(SPWF04SxIndication.WpsEnrollmentAttemptTimedOut);
case SPWF04SxIndication.SockdDroppingClient: return nameof(SPWF04SxIndication.SockdDroppingClient);
case SPWF04SxIndication.NtpServerDelivery: return nameof(SPWF04SxIndication.NtpServerDelivery);
case SPWF04SxIndication.DhcpFailedToGetLease: return nameof(SPWF04SxIndication.DhcpFailedToGetLease);
case SPWF04SxIndication.MqttPublished: return nameof(SPWF04SxIndication.MqttPublished);
case SPWF04SxIndication.MqttClosed: return nameof(SPWF04SxIndication.MqttClosed);
case SPWF04SxIndication.WebSocketData: return nameof(SPWF04SxIndication.WebSocketData);
case SPWF04SxIndication.WebSocketClosed: return nameof(SPWF04SxIndication.WebSocketClosed);
case SPWF04SxIndication.FileReceived: return nameof(SPWF04SxIndication.FileReceived);
default: return "Other";
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.