Cobra II ECO and ENC28

I can not get network on Cobra II with ENC28 module. First of all in the ENC28 documentation ( https://www.ghielectronics.com/catalog/product/333 ) does not write that it support Cobra II (I then found this thread: https://www.ghielectronics.com/community/forum/topic?id=10718 )

Then I try with several sources, but I can not get it to work. Same configuration (IP, DNS) works with spider, Cerberus.

net.EnableStaticIP(ShareConst.IP, ShareConst.SubnetMask, ShareConst.IPRouter);
net.EnableStaticDns(ShareConst.DNS);
net.Open();

I tried:

private static readonly EthernetENC28J60 net = new EthernetENC28J60(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, G120.P1_10, G120.P2_11, G120.P1_9);
private static readonly EthernetENC28J60 net = new EthernetENC28J60(Microsoft.SPOT.Hardware.SPI.SPI_module.SPI2, G120.P1_17, G120.P2_21, G120.P1_14);
private static readonly EthernetENC28J60 net = new EthernetENC28J60(SPI.SPI_module.SPI1, Cpu.Pin.GPIO_Pin1, Cpu.Pin.GPIO_Pin2, Cpu.Pin.GPIO_Pin3);
private EthernetBuiltIn net = new EthernetBuiltIn();

Non of them work. I even try with adding ENC28 module to the Program.gadgeteer.

I try to find the answer on the following locations (without success):
https://www.ghielectronics.com/docs/318/fez-cobra-ii-developers-guide
https://www.ghielectronics.com/docs/30/networking
https://www.ghielectronics.com/community/forum/topic?id=15320

In some cases the NetworkChange_NetworkAvailabilityChanged event is called and e.IsAvailable is true, but I can not get any html not even from local network (works with Spider, Cerberus).
If I copy requested URL to the Chrome the page opens (it only prints current time).

I connected ENC28 to the Socket 6 (The only one with S socket).
What is the correct configuration?

@ Makla - The example on https://www.ghielectronics.com/docs/318/fez-cobra-ii-developers-guide is known to work. Using that as a starting point, can you create a minimal, but complete, program that shows your issue? Can you then try the exact same code on a different board, changing only the pins?

I tried with new project and the code:

using GHI.Pins;
using GHI.Networking;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT;

namespace CobraIIECONetTest
{
    public partial class Program
    {
        private static EthernetENC28J60 netif;
        // This method is run when the mainboard is powered up or reset. 
  
        public void ProgramStarted()
        {
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;

            netif = new EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_10, G120.P2_11, G120.P1_9);
            netif.Open();
            netif.EnableDhcp();
            netif.EnableDynamicDns();
        }

        private static void NetworkChange_NetworkAddressChanged(object sender, Microsoft.SPOT.EventArgs e)
        {
            Debug.Print("Network address changed");

            if (netif.IPAddress != "0.0.0.0")
            {
                Debug.Print("Network address " + netif.IPAddress);
            }
        }

        private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            Debug.Print("Network availability: " + e.IsAvailable.ToString());
        }
    }
}

Output is:

[quote]The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZ Cobra II Eco version Rev B
Network availability: True
The thread ‘’ (0x5) has exited with code 0 (0x0).
The program ‘[8] Micro Framework application: Managed’ has exited with code 0 (0x0).[/quote]
AddressChanged is never called. My DHCP is working. I tried with mobile, and laptop.

Of course I am running the latest firmware. I also tried to erase everything and load fresh firmware and TinyBooter.
The cable is working.
The orange led on ENC28 is blinking. 1 to 5 times per second.

I also try with static IP/DNS:

using GHI.Pins;
using GHI.Networking;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT;
using Gadgeteer.Networking;

namespace CobraIIECONetTest
{
    public partial class Program
    {
        private static EthernetENC28J60 netif;
        // This method is run when the mainboard is powered up or reset. 
  
        public void ProgramStarted()
        {
            NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
            NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;

            netif = new EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_10, G120.P2_11, G120.P1_9);
            netif.Open();
            netif.EnableStaticIP("192.168.1.207", "255.255.255.0", "192.168.1.1");
            netif.EnableStaticDns(new string[] { "193.189.160.13", "193.189.160.23" });
       }

        private static void NetworkChange_NetworkAddressChanged(object sender, Microsoft.SPOT.EventArgs e)
        {
            Debug.Print("Network address changed");

            if (netif.IPAddress != "0.0.0.0")
            {
                Debug.Print("Network address " + netif.IPAddress);
            }
        }

        private static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
        {
            Debug.Print("Network availability: " + e.IsAvailable.ToString());

            HttpRequest request = WebClient.GetFromWeb(@ "http://192.168.1.27/Microcontroler/GetDateTime.php");

            request.ResponseReceived += (HttpRequest sender2, HttpResponse response) =>
            {
                if (response.StatusCode == "200")
                {
                }
            };
        }
    }
}

Output:

[quote]The thread ‘’ (0x2) has exited with code 0 (0x0).
Using mainboard GHI Electronics FEZ Cobra II Eco version Rev B
Network availability: True
The thread ‘’ (0x5) has exited with code 0 (0x0).
Network address changed
Network address 192.168.1.207
#### Exception System.Net.Sockets.SocketException - CLR_E_FAIL (6) ####
#### Message:
#### Microsoft.SPOT.Net.SocketNative::send [IP: 0000] ####
#### System.Net.Sockets.Socket::Send [IP: 0018] ####
#### System.Net.Sockets.NetworkStream::Write [IP: 0051] ####
#### System.Net.InputNetworkStreamWrapper::Write [IP: 000a] ####
#### System.Net.HttpWebRequest::SubmitRequest [IP: 007d] ####
#### System.Net.HttpWebRequest::GetResponse [IP: 000c] ####
#### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 01a8] ####
#### SocketException ErrorCode = 10053
#### SocketException ErrorCode = 10053
A first chance exception of type ‘System.Net.Sockets.SocketException’ occurred in Microsoft.SPOT.Net.dll
#### SocketException ErrorCode = 10053
#### SocketException ErrorCode = 10053
#### Exception System.Net.WebException - 0x00000000 (6) ####
#### Message:
#### System.Net.HttpWebRequest::GetResponse [IP: 00c8] ####
#### Gadgeteer.Networking.HttpRequest::HandleRequestSync [IP: 01a8] ####
A first chance exception of type ‘System.Net.WebException’ occurred in System.Http.dll

An exception occured while connecting to the Internet. Please, make sure that a valid URL is used and a network connection is up.

The thread ‘’ (0x6) has exited with code 0 (0x0).[/quote]

I use external power supply and ENC is the only module connected to Cobra II.

When using DHCP, my router does not find any device that could be Cobra II.

I tried with Cobra II wifi and it works. The same code (with DHCP and with static).

I also turn of Firewall, try to download google.si, inspect trafic with WireShark.
No request is made when using Cobra II Eco.

@ Makla - You are assuming that the NetworkChange_NetworkAddressChanged event occurs before NetworkChange_NetworkAvailabilityChanged event. This might not be the case. Do a little research.

I would wait to insure that an IP address is available before doing anything.

I also recommend that you do not do any processing in an event handler. Issuing the web request will hold the event handler thread which often results in unexpected things happening.

The exception you are seeing may be caused by the IP address not being available yet.

I know. I send http request in NetworkChange_NetworkAvailabilityChanged event only when I use static IP/DNS. (Second case.)
When I use dynamic IP/DNS I don’t have any code. Only some Debug.Print.

Same code works on Spider, CobraII WIFI and Cerberus.

I move get request outside NET event handler:

using GHI.Pins;
using GHI.Networking;
using Microsoft.SPOT.Net.NetworkInformation;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT;
using Gadgeteer.Networking;
using System.Threading;

namespace Net
{
    public partial class Program
    {
        private EthernetENC28J60 netif;

        public void ProgramStarted()
        {
            netif = new EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_10, G120.P2_11, G120.P1_9);
            //InitDynamic();
            InitStatic();
        }

        private void InitDynamic()
        {
            NetworkChange.NetworkAvailabilityChanged += (object sender, NetworkAvailabilityEventArgs e) =>
                {
                    Debug.Print("Network availability: " + e.IsAvailable.ToString());
                };
            NetworkChange.NetworkAddressChanged += (object sender, Microsoft.SPOT.EventArgs e) =>
                {
                    Debug.Print("Network address changed");

                    if (netif.IPAddress != "0.0.0.0")
                    {
                        Debug.Print("Network address " + netif.IPAddress);
                        GetRequestSO();
                    }
                };

            netif.Open();
            netif.EnableDhcp();
            netif.EnableDynamicDns();
        }

        private void InitStatic()
        {
            NetworkChange.NetworkAvailabilityChanged += (object sender, NetworkAvailabilityEventArgs e) =>
            {
                Debug.Print("Network availability: " + e.IsAvailable.ToString());
                GetRequestSO();
            };
            NetworkChange.NetworkAddressChanged += (object sender, Microsoft.SPOT.EventArgs e) =>
            {
                Debug.Print("Network address changed");

                if (netif.IPAddress != "0.0.0.0")
                {
                    Debug.Print("Network address " + netif.IPAddress);

                }
            };

            netif.EnableStaticIP("192.168.1.207", "255.255.255.0", "192.168.1.1");
            netif.EnableStaticDns(new string[] { "193.189.160.13", "193.189.160.23" });
            netif.Open();
        }

        private void GetRequest()
        {
            Debug.Print("Get request");

            Thread thread = new Thread(() =>
            {
                HttpRequest request = WebClient.GetFromWeb(@ "http://192.168.1.27/Smarthouse3/Microcontroler/GetDateTime.php");

                request.ResponseReceived += (HttpRequest sender2, HttpResponse response) =>
                {
                    Debug.Print("Status code " + response.StatusCode.ToString());
                };
            });
            thread.Start();
        }

        private void GetRequestSO()
        {
            Debug.Print("Get request");

            Thread thread = new Thread(() =>
            {
                HttpRequest request = WebClient.GetFromWeb(@ "http://stackoverflow.com/");

                request.ResponseReceived += (HttpRequest sender2, HttpResponse response) =>
                {
                    Debug.Print("Status code " + response.StatusCode.ToString());
                };
            });
            thread.Start();
        }


    }
}

Strange is when I request page from localhost I get socket error code: 10053 - Software caused connection abort,
but when I request StackOverflow I get error code: 10060 - Connection timed out.

I also try to connect LAN cable to different router.

@ Makla - Can you run the below code and see what happens? Make sure the ENC28 is in socket 6 and you have nothing else connected to the board. Update the loader and firmware before running it as well.

The code in the document that I linked you was actually for the Cobra II Net which I mistakenly thought you were using. The pins in the below code are correct for the Cobra II Eco on socket 6.


using GHI.Networking;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT.Net.NetworkInformation;
using System.Net;
using System.Threading;

namespace Networking
{
	public class Program
	{
		public static void Main()
		{
			NetworkChange.NetworkAvailabilityChanged += (a, b) => Debug.Print("SPOT NAVAC " + b.IsAvailable.ToString());
			NetworkChange.NetworkAddressChanged += (a, b) => Debug.Print("SPOT NADRC");

			using (var netif = new EthernetENC28J60(SPI.SPI_module.SPI2, (Cpu.Pin)(32 + 17), (Cpu.Pin)(64 + 21), (Cpu.Pin)(32 + 14), 8000))
			{
				netif.Open();
				netif.EnableDhcp();
				netif.EnableDynamicDns();

				while (netif.IPAddress == "0.0.0.0")
				{
					Debug.Print("Waiting DHCP");
					Thread.Sleep(250);
				}

				Debug.Print(netif.IPAddress);

				byte[] result = new byte[4096 * 4];
				int read = 0, total = 0;

				using (var req = HttpWebRequest.Create("http://www.ghielectronics.com/downloads/Gadgeteer/.NET%20Gadgeteer%20Ultimate%20Guide%20(discontinued).pdf") as HttpWebRequest)
				{
					using (var res = req.GetResponse() as HttpWebResponse)
					{
						using (var stream = res.GetResponseStream())
						{
							do
							{
								read = stream.Read(result, 0, result.Length);

								total += read;

								Thread.Sleep(20);
							} while (read != 0);

							Debug.Print("Downloaded " + total.ToString("N2") + " bytes.");
						}
					}
				}
			}
		}
	}
}

I didn’t refresh loader or firmware, just change 2 lines:

//netif = new EthernetENC28J60(SPI.SPI_module.SPI2, G120.P1_10, G120.P2_11, G120.P1_9);
netif = new EthernetENC28J60(SPI.SPI_module.SPI2, (Cpu.Pin)(32 + 17), (Cpu.Pin)(64 + 21), (Cpu.Pin)(32 + 14), 8000);

and it works. It would be good idea to update some documentation, because I didn’t find which pins I should use. Maybe here: https://www.ghielectronics.com/docs/318/fez-cobra-ii-developers-guide

I am really glad that it works. ;D I couldn’t sleep because of this. :wall: