ghi-electronics / TinyCLR-Libraries

Official Libraries supporting TinyCLR OS
https://www.ghielectronics.com/tinyclr/
17 stars 16 forks source link

Wifi connection exception between to two FEZ boards (Wifi Access Point and Station) #1331

Closed jaredsund closed 6 months ago

jaredsund commented 8 months ago

When trying to connect a two FEZ boards (Feather and Portal) via wifi, an exception is thrown.

Setup:

Feather (setup as Access Point) Wifi 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);
 }

Portal (setup as Station) Wifi 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();

}

When trying to connect the Portal as a client to the Feather, the following exception is thrown when enabling the netWorkController on the Portal.


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

Note, I am able to connect my phone to the Feather Access Point and I am able to connect the Portal to my house wifi without trouble.

I remove the wifi password from both the Feather and Portal and the systems connect and provides an IP address to the client. However, the AccessPointClientConnectionChanged event does not fire. When I connect via my phone, the event does fire.

Palomino34 commented 6 months ago

AP mode is no longer supported. Removed from 2.2.0.7000