Establish Wifi Connection between Feather and Portal

Hello All,

My end goal here is to talk via MQTT between a FEZ Portal and FEZ Feather. I’m still working on establishing a WIFI connection between the two and having a bit of trouble.

On the Feather, I have created a WIFI Access Point “StableSwitch Net”. I’m able to see the WIFI network from my phone and establish a connection.

However, when I try to create a client connection from the Portal I get an Exception thrown when I try to enable the client network controller.


Enable the network controller.
    #### Exception System.ArgumentException - 0xfd000000 (1) ####
    #### Message: 
    #### GHIElectronics.TinyCLR.Devices.Network.Provider.NetworkControllerApiWrapper::Enable [IP: 0000] ####
    #### GHIElectronics.TinyCLR.Devices.Network.NetworkController::Enable [IP: 0007] ####
    #### StableCamper_Switcher_UI.MainWindow::.ctor [IP: 0043] ####
    #### StableCamper_Switcher_UI.Program::Main [IP: 001b] ####
Exception thrown: 'System.ArgumentException' in GHIElectronics.TinyCLR.Devices.Network.dll


I am able to connect the client on the Portal to my local WIFI network and reach the outside world with a ping.

I’m guessing that the issue with my Access Point on the Feather since the Portal client is able to work with the local WIFI network.

FEZ Feather Access Point code:

 public static GpioPin led;

 static void Main()
 {
     led = GpioController.GetDefault().OpenPin(SC20260.GpioPin.PE11);
     led.SetDriveMode(GpioPinDriveMode.Output);

     InitializeWifi();
 }

 

  public static void InitializeWifi()
 {
     var networkController = NetworkController.FromName(SC20100.NetworkController.ATWinc15x0);
     
     //Setup Pins
     var enablePinNumber = FEZBit.GpioPin.WiFiEnable;
     var chipSelectPinNumber = FEZBit.GpioPin.WiFiChipselect;
     var irqPinNumber = FEZBit.GpioPin.WiFiInterrupt;
     var resetPinNumber = FEZBit.GpioPin.WiFiReset;
     var spiControllerName = FEZBit.SpiBus.WiFi;
     var gpio = GpioController.GetDefault();
     var enablePin = gpio.OpenPin(enablePinNumber);
     enablePin.SetDriveMode(GpioPinDriveMode.Output);
     enablePin.Write(GpioPinValue.High);
     SpiNetworkCommunicationInterfaceSettings networkCommunicationInterfaceSettings = new SpiNetworkCommunicationInterfaceSettings();
     var settings = new SpiConnectionSettings()
     {
         ChipSelectLine = gpio.OpenPin(chipSelectPinNumber),
         ClockFrequency = 4000000,
         Mode = SpiMode.Mode0,
         ChipSelectType = SpiChipSelectType.Gpio,
         ChipSelectHoldTime = TimeSpan.FromTicks(10),
         ChipSelectSetupTime = TimeSpan.FromTicks(10)
     };
     networkCommunicationInterfaceSettings.SpiApiName = spiControllerName;
     networkCommunicationInterfaceSettings.GpioApiName = SC20260.GpioPin.Id;
     networkCommunicationInterfaceSettings.SpiSettings = settings;
     networkCommunicationInterfaceSettings.InterruptPin = gpio.OpenPin(irqPinNumber);
     networkCommunicationInterfaceSettings.InterruptEdge = GpioPinEdge.FallingEdge;
     networkCommunicationInterfaceSettings.InterruptDriveMode = GpioPinDriveMode.InputPullUp;
     networkCommunicationInterfaceSettings.ResetPin = gpio.OpenPin(resetPinNumber);
     networkCommunicationInterfaceSettings.ResetActiveState = GpioPinValue.Low;
     networkController.SetCommunicationInterfaceSettings(networkCommunicationInterfaceSettings);

     WiFiNetworkInterfaceSettings networkInterfaceSetting = new WiFiNetworkInterfaceSettings()
     {

         Ssid = "StableSwitch Net",
         Password = "0123456789",
         Mode = WiFiMode.AccessPoint,
         Channel = 6

     };

    
     networkInterfaceSetting.Address = new IPAddress(new byte[] { 192, 168, 0, 201 });
     networkInterfaceSetting.SubnetMask = new IPAddress(new byte[] { 255, 255, 255, 0 });
     networkInterfaceSetting.GatewayAddress = new IPAddress(new byte[] { 192, 168, 0, 1 });
     networkInterfaceSetting.DnsAddresses = new IPAddress[] { new IPAddress(new byte[] { 75, 75, 75, 75 }), new IPAddress(new byte[] { 75, 75, 75, 76 }) };

     networkInterfaceSetting.DhcpEnable = true;
     networkInterfaceSetting.DynamicDnsEnable = true;

     networkController.SetCommunicationInterfaceSettings(networkCommunicationInterfaceSettings);
     networkController.SetInterfaceSettings(networkInterfaceSetting);
     networkController.SetAsDefaultController();

     networkInterfaceSetting.AccessPointClientConnectionChanged += NetworkInterfaceSetting_AccessPointClientConnectionChanged;
     networkController.NetworkAddressChanged += NetController_NetworkAddressChanged;
     networkController.NetworkLinkConnectedChanged += NetController_NetworkLinkConnectedChanged;

     
     networkController.Enable();


     Thread.Sleep(-1);

 }

 //on connection
 private static void NetController_NetworkLinkConnectedChanged(NetworkController sender, NetworkLinkConnectedChangedEventArgs e)
 {
     //throw new NotImplementedException();
     Debug.WriteLine("EVENT:  NetController_NetworkLinkConnectedChanged");
     Debug.WriteLine(e.Connected.ToString ());
     led.Write(e.Connected? GpioPinValue.High: GpioPinValue.Low);
 }
 //on disconect
 private static void NetController_NetworkAddressChanged(NetworkController sender, NetworkAddressChangedEventArgs e)
 {
     //throw new NotImplementedException();
     Debug.WriteLine("EVENT:  NetController_NetworkAddressChange");
     Debug.WriteLine(e.ToString());
    // led.Write(GpioPinValue.Low);

 }

 //on connection
 private static void NetworkInterfaceSetting_AccessPointClientConnectionChanged(NetworkController sender, IPAddress clientAddress, string macAddress)
 {
     //throw new NotImplementedException();
     Debug.WriteLine("EVENT:  NetworkInterfaceSetting_AccessPointClientConnectionChanged");
     Debug.WriteLine(clientAddress.ToString());
    // led.Write(GpioPinValue.High);
 }


FEZ Portal Station Code:


static void wifiConnectClient()
{

    var networkController = NetworkController.FromName(SC20100.NetworkController.ATWinc15x0);


    var enablePinNumber = SC20260.GpioPin.PA8;
    var chipSelectPinNumber = SC20260.GpioPin.PA6;
    var irqPinNumber = SC20260.GpioPin.PF10;
    var resetPinNumber = SC20260.GpioPin.PC3;
    var spiControllerName = SC20260.SpiBus.Spi3;
    var gpioControllerName = SC20260.GpioPin.Id;


    var gpio = GpioController.GetDefault();
    var enablePin = gpio.OpenPin(enablePinNumber);
    enablePin.SetDriveMode(GpioPinDriveMode.Output);
    enablePin.Write(GpioPinValue.High);
    SpiNetworkCommunicationInterfaceSettings networkCommunicationInterfaceSettings = new SpiNetworkCommunicationInterfaceSettings();
    var settings = new SpiConnectionSettings()
    {
        ChipSelectLine = gpio.OpenPin(chipSelectPinNumber),
        ClockFrequency = 4000000,
        Mode = SpiMode.Mode0,
        ChipSelectType = SpiChipSelectType.Gpio,
        ChipSelectHoldTime = TimeSpan.FromTicks(10),
        ChipSelectSetupTime = TimeSpan.FromTicks(10)
    };
    networkCommunicationInterfaceSettings.SpiApiName = spiControllerName;
    networkCommunicationInterfaceSettings.GpioApiName = gpioControllerName;
    networkCommunicationInterfaceSettings.SpiSettings = settings;
    networkCommunicationInterfaceSettings.InterruptPin = gpio.OpenPin(irqPinNumber);
    networkCommunicationInterfaceSettings.InterruptEdge = GpioPinEdge.FallingEdge;
    networkCommunicationInterfaceSettings.InterruptDriveMode = GpioPinDriveMode.InputPullUp;
    networkCommunicationInterfaceSettings.ResetPin = gpio.OpenPin(resetPinNumber);
    networkCommunicationInterfaceSettings.ResetActiveState = GpioPinValue.Low;
    networkController.SetCommunicationInterfaceSettings(networkCommunicationInterfaceSettings);


    string[] ssidList = Winc15x0Interface.Scan();
    foreach (var s in ssidList)
        Debug.WriteLine(s);


    WiFiNetworkInterfaceSettings networkSettings = new WiFiNetworkInterfaceSettings();
    networkSettings.Ssid = "StableSwitch Net";
    networkSettings.Password = "0123456789";
    networkSettings.Mode = WiFiMode.Station;
    networkSettings.Channel = 6;

    networkSettings.DhcpEnable = true;
    networkSettings.DynamicDnsEnable = true;
    networkController.SetInterfaceSettings(networkSettings);
    networkController.SetAsDefaultController();


   //this is where the exception is thrown....
    Debug.WriteLine("Enable the network controller.");
    try
    {
        networkController.Enable();
    }
    catch (Exception e) {
        Debug.WriteLine(e.ToString());
    }


    Debug.WriteLine("Network controller is enabled.");

    networkController.NetworkAddressChanged += NetworkController_NetworkAddressChanged;

    

	//just a pause for test in debug
    while (true)
    {
        Thread.Sleep(100);

    }

    IPHostEntry ip = Dns.GetHostEntry("GHIElectronics.com");
    Debug.WriteLine("GHI Electronics' IP = " + ip.AddressList[0].ToString());


}

private static void NetworkController_NetworkAddressChanged(NetworkController sender, NetworkAddressChangedEventArgs e)
{
    //throw new NotImplementedException();
    var ipProperties = sender.GetIPProperties();
    Debug.WriteLine("IP: " + ipProperties.Address);
    var address = ipProperties.Address.GetAddressBytes();
    
}

Any help would be so greatly appreciated!

Can you try set AP and client without password?

That worked, and cleared the exception when enabling the networkController. It’s interesting that the client received an IP address, however the event for AccessPointClientConnectionChanged did not fire on the Access Point. When I connect my phone the the Access point, the event does file…

 //on connection
 private static void NetworkInterfaceSetting_AccessPointClientConnectionChanged(NetworkController sender, IPAddress clientAddress, string macAddress)
 {
     //throw new NotImplementedException();
     Debug.WriteLine("EVENT:  NetworkInterfaceSetting_AccessPointClientConnectionChanged");
     Debug.WriteLine(clientAddress.ToString());
    // led.Write(GpioPinValue.High);
 }

Thanks, seems to me that connecting by phone with/without password is OK but SC to SC need no password.

Could you add issue to github?

Issue created: Wifi connection exception between to two FEZ boards (Wifi Access Point and Station) · Issue #1331 · ghi-electronics/TinyCLR-Libraries · GitHub