FEZ Duino WIFI

I modified the WIFI example for FEZ Portal to work with FEZ Duino but get an exception when routine networkController.Enable() is called. Don’t know how to proceed.

An unhandled exception of type ‘System.ArgumentException’ occurred in GHIElectronics.TinyCLR.Devices.Network.dll

// my code
using GHIElectronics.TinyCLR.Devices.Gpio;
using GHIElectronics.TinyCLR.Devices.Network;
using GHIElectronics.TinyCLR.Devices.Spi;
using GHIElectronics.TinyCLR.Pins;
using System;
using System.Diagnostics;
using System.Net;
using System.Threading;

namespace WIFI_Example
{
    
        class Program
        {
            static void Main()

            {
                var enablePin = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PA8);
                enablePin.SetDriveMode(GpioPinDriveMode.Output);
                enablePin.Write(GpioPinValue.High);

                SpiNetworkCommunicationInterfaceSettings netInterfaceSettings =
                    new SpiNetworkCommunicationInterfaceSettings();

                var cs = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PD15);

                var settings = new SpiConnectionSettings()
                {
                    ChipSelectLine = cs,
                    ClockFrequency = 4000000,
                    Mode = SpiMode.Mode0,
                    ChipSelectType = SpiChipSelectType.Gpio,
                    ChipSelectHoldTime = TimeSpan.FromTicks(10),
                    ChipSelectSetupTime = TimeSpan.FromTicks(10)
                };

                netInterfaceSettings.SpiApiName = SC20100.SpiBus.Spi3;

                netInterfaceSettings.GpioApiName = SC20100.GpioPin.Id;

                netInterfaceSettings.SpiSettings = settings;
                netInterfaceSettings.InterruptPin = GpioController.GetDefault().
                    OpenPin(SC20100.GpioPin.PB12);

                netInterfaceSettings.InterruptEdge = GpioPinEdge.FallingEdge;
                netInterfaceSettings.InterruptDriveMode = GpioPinDriveMode.InputPullUp;
                netInterfaceSettings.ResetPin = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PB13);
                netInterfaceSettings.ResetActiveState = GpioPinValue.Low;

                var networkController = NetworkController.FromName
                    ("GHIElectronics.TinyCLR.NativeApis.ATWINC15xx.NetworkController");


                WiFiNetworkInterfaceSettings wifiSettings = new WiFiNetworkInterfaceSettings()
                {
                    Ssid = "WIRE802",
                    Password = "7187946977",
                };

                wifiSettings.Address = new IPAddress(new byte[] { 192, 168, 1, 122 });
                wifiSettings.SubnetMask = new IPAddress(new byte[] { 255, 255, 255, 0 });
                wifiSettings.GatewayAddress = new IPAddress(new byte[] { 192, 168, 1, 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.DhcpEnable = true;
                wifiSettings.DynamicDnsEnable = true;

                networkController.SetInterfaceSettings(wifiSettings);
                networkController.SetCommunicationInterfaceSettings(netInterfaceSettings);
                networkController.SetAsDefaultController();

                networkController.NetworkAddressChanged += NetworkController_NetworkAddressChanged;

                networkController.NetworkLinkConnectedChanged +=
                    NetworkController_NetworkLinkConnectedChanged;

                networkController.Enable();

                // Network is ready to use
                Thread.Sleep(Timeout.Infinite);
            }
            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]);
            }
        }

}

In my experience, this has meant one of these things:

EDITED: Added the real reason as the first bullet

Either

  • The SSID or password are wrong
  • You have the pins wrong (but to me, they look correct for FezDuino. They match the silkscreen on the back of the unit I have)
  • You have used a pin opened somewhere else in your program which also maps to SPI3, but if that’s your whole program, it does not look like that’s the problem.
  • There is a minor firmware mismatch, but then your program should not start, though you might want to make sure you have matching firmware and libraries. I only saw this once when I copied in the wifi driver source code instead of using the nuget.

I will try your code later this afternoon… but the above is what has caused this for me in the past.

We ran the same code here with no issues. Do you have a different WiFi network you can try it on? I assume that the version of the FEZ Duino you’re using is REV B correct?

Verify that the SSID and Password are correct. I get the same exception when those aren’t set correctly.

Thanks Greg, I had missed typed the SSID. Also where do you find the “Winc15x0Interface class” that’s in the WINC1500 Utilities section?

1 Like

I too am experiencing a similar error whenever the enable or disable method is called, but mine states “An unhandled exception of type ‘System.InvalidOperationException’ occurred in GHIElectronics.TinyCLR.Devices.Network.dll”.

I am using preview 4 and a Feather Rev B which has the same chipset as the Fezduino.

I even started with new project and using the sample on the sample of the website. I do not have a portal but I am speculating this is isolated to the SC20100 type chips.

packages.config

<?xml version="1.0" encoding="utf-8"?>
<packages>
  <package id="GHIElectronics.TinyCLR.Core" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.Devices.Gpio" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.Devices.Network" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.Devices.Spi" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.Devices.Uart" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.IO" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.Native" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.Networking" version="2.1.0-preview4" targetFramework="net452" />
  <package id="GHIElectronics.TinyCLR.Pins" version="2.1.0-preview4" targetFramework="net452" />
</packages>

program.cs

    class Program
    {
        static void Main()
        {
            Wifi_Example();
            Thread.Sleep(-1);
        }

        static void Wifi_Example()
        {
            var enablePin = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PA8);
            enablePin.SetDriveMode(GpioPinDriveMode.Output);
            enablePin.Write(GpioPinValue.High);

            var cs = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PD15);

            SpiNetworkCommunicationInterfaceSettings netInterfaceSettings = new SpiNetworkCommunicationInterfaceSettings
            {
                SpiApiName = SC20100.SpiBus.Spi3,
                GpioApiName = SC20100.GpioPin.Id,
                SpiSettings = new SpiConnectionSettings()
                {
                    ChipSelectLine = cs,
                    ClockFrequency = 4000000,
                    Mode = SpiMode.Mode0,
                    ChipSelectType = SpiChipSelectType.Gpio,
                    ChipSelectHoldTime = TimeSpan.FromTicks(10),
                    ChipSelectSetupTime = TimeSpan.FromTicks(10)
                },
                InterruptPin = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PB12),

                InterruptEdge = GpioPinEdge.FallingEdge,
                InterruptDriveMode = GpioPinDriveMode.InputPullUp,
                ResetPin = GpioController.GetDefault().OpenPin(SC20100.GpioPin.PB13),
                ResetActiveState = GpioPinValue.Low
            };

            var networkController = NetworkController.FromName("GHIElectronics.TinyCLR.NativeApis.ATWINC15xx.NetworkController");

            WiFiNetworkInterfaceSettings wifiSettings = new WiFiNetworkInterfaceSettings()
            {

                Ssid = "MOTOROLA-3258C",
                Password = "0661c66a03810b23b",
                Mode = WiFiMode.Station, TlsEntropy = new byte[] {1,2 }
            };

            wifiSettings.Address = new IPAddress(new byte[] { 192, 168, 1, 122 });
            wifiSettings.SubnetMask = new IPAddress(new byte[] { 255, 255, 255, 0 });
            wifiSettings.GatewayAddress = new IPAddress(new byte[] { 192, 168, 1, 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.DhcpEnable = true;
            wifiSettings.DynamicDnsEnable = true;

            networkController.SetInterfaceSettings(wifiSettings);
            networkController.SetCommunicationInterfaceSettings(netInterfaceSettings);
            networkController.SetAsDefaultController();

            networkController.NetworkAddressChanged += NetworkController_NetworkAddressChanged;

            networkController.NetworkLinkConnectedChanged += NetworkController_NetworkLinkConnectedChanged;

            networkController.Enable();

            // Network is ready to use
            Thread.Sleep(Timeout.Infinite);
        }

        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]);
        }
    }

Mighty neighborly of you to share that SSID and password… :wink:

Settings look ok, comparing to the schematic. Like @StanSchultz, are you sure they are correct?

Add the nuget : GHIElectronics.TinyCLR.Drivers.Microchip.Winc15x0

I use this as my test network, i maybe the one “testing” my neighbor’s wifi. :smile:

Documented code sample works when both SSID and password are correct. Tried on multiple wireless networks and it works as expected.

2 Likes