FEZ Raptor and ENC28 – Ethernet Initialization for Gadgeteer Webserver fails

Hi,
my goal is, to use the FEZ Raptor as Gadgeteer webserver board, but it will not work.

I saw the example https://www.ghielectronics.com/community/codeshare/entry/367) and yes, I will use such easy code:



Today I got my FEZ Raptor and some Gadgeteer modules (*Yippi*). Now everything becomes so easy.

I followed all the preparation instruction including https://www.ghielectronics.com/docs/228/fez-raptor to change the "GadgeteerHardware.zip" 
First Test: One module is the simple led_Strip and it (and so the Raptor board) works fine (cool).

I connected the ENC28 module in the Gadgeteer Model Explorer to port 3 and tried to assign the IP address (as described in https://www.ghielectronics.com/community/codeshare/entry/367 ) by adding 

```cs]this.ethernet_ENC28.UseDHCP();[/code
 in the ProgramStarted. Sounds easy.
But I got the error message: 
"An unhandled exception of type 'System.NullReferenceException' occurred in Gadgeteer.dll" with debug output:

```cs
    #### Exception System.NullReferenceException - CLR_E_NULL_REFERENCE (1) ####
    #### Message: 
    #### Gadgeteer.Modules.Module+NetworkModule::UseStaticIP [IP: 000f] ####
    #### Gadgeteer.Modules.Module+NetworkModule::UseStaticIP [IP: 0008] ####
    #### GadgeteerApp1.Program::ProgramStarted [IP: 0028] ####
A first chance exception of type 'System.NullReferenceException' occurred in Gadgeteer.dll
An unhandled exception of type 'System.NullReferenceException' occurred in Gadgeteer.dll

Then I tried

, but the result stays the same.
At least I change the connection to gadgeteer socket 11, but still the same result 
And it seemed so easy :(

I stepped a bit into the code and saw, that neither the ethernet_ENC28 object nor the ethernet_ENC28.interface was null. But if I try to access the property IsCableConnected I get an error as well. And the this.ethernet_ENC28.NetworkSettings are null. That I do not understand.

Then I found the post https://www.ghielectronics.com/community/forum/topic?id=12445 and the solution was to use the interfaces the same was as without gadgeteer (as I had to do for G120HDR).Can it be:O? That I do not believe. The Raptor is a Gadgeteer board, isn't it?

Because th this.ethernet_ENC28.NetworkSettings are used as parameter if you start the Gadgeteer.Webserver, it doesn't work as well :(

Is there somebody who can help me to bring the Gadgeteer.Webserver up and running on the Raptor board. Otherwise I wasted a lot of money :(

Thanks to everybody!!!!

Hello andre.m,

thanks for your quite fast replay.
Yes, it is a 4.2 project.
I already tried and got the interface pingable by accessing the interface through the gadgeteer class:

this.ethernet_ENC28.Interface.Open();
NetworkInterfaceExtension.AssignNetworkingStackTo(this.ethernet_ENC28.Interface);
this.ethernet_ENC28.Interface.NetworkInterface.EnableStaticIP("192.168.1.14", "255.255.255.0", "192.168.1.1");

That works, but the required this.ethernet_ENC28.NetworkSettings are still null and cannot be used for the gadgeteer.webserver.

OK, I was blind and haven’t seen, that I have to handover the IPAddress instead the whole settings.
I change the code as in the sample:

Gadgeteer.Networking.WebEvent hello;
...
this.ethernet_ENC28.Interface.Open();
NetworkInterfaceExtension.AssignNetworkingStackTo(this.ethernet_ENC28.Interface);
this.ethernet_ENC28.Interface.NetworkInterface.EnableStaticIP("192.168.1.14", "255.255.255.0", "192.168.1.1");

Gadgeteer.Networking.WebServer.StartLocalServer(this.ethernet_ENC28.Interface.NetworkInterface.IPAddress, 80);
hello = Gadgeteer.Networking.WebServer.SetupWebEvent("hello");
hello.WebEventReceived += new WebEvent.ReceivedWebEventHandler(hello_WebEventReceived);
...
void hello_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            Debug.Print("Hello world");
            responder.Respond("Hello world");
        }

I get the debug output: Web server started at http://192.168.1.14:80/ ´(we get closer to the target :slight_smile: )
But if I enter the URL http://192.168.1.14/hello to the browser, there is no response.
Is there still something missing?

  1. What I still not understand is, why the ethernet_ENC28 cannot be used for Raptor as explained for other Gadgeteer boards. Can you explain? Is it my fault?

Thanks so far!

Hi,
I’m a bit disappoiunted, because I come from G120HDR with some problems with the ethernet connection and webserver stability. In addition I read the forum posts that sometimes network has to be reinitializes and so on. For me the implemented webserver, based on the samples here, stops running after a while. HttpListener hangs without any exception and switching to simple socket based webserver results in other problems. But that only to explain where I come from.

So I hoped to solve these problems by choosing a gadgeteer board, where I can use a build in Ethernet or the module based classes and the gadgeteer webserver.
Now it seems, that I investigated the money in the new hardware and I should use the same as I did with G120HDR, but it’s OK, if it will work.

  1. Can I expect, that the GHIElectronics.Ethernet_ENC28 will be full supported in the next SDK version?

  2. I modified the code from Gus you mentioned before by comparing the board schema and changed the interface initialization to:


Then I changed the event parameters to EthernetENC28J60 instead of EthernetBuiltIn and it works!!! The webserver responses.

3. Then I switched to the Gadgeteer webserver and it works, too. 

So the ENC28 Module is running on Raptor and I have to go back to my original Issue :( , to keep it up and running. Therefore I opened a new Post with the current state of code: https://www.ghielectronics.com/community/forum/topic?id=15966

4. I tried the other way around: initializing the Interface in a gadgeteer project:


```cs
Eth1 = this.ethernet_ENC28.Interface;
Eth1 = new EthernetENC28J60(SPI.SPI_module.SPI1, GHI.Hardware.G400.Pin.PA28, GHI.Hardware.G400.Pin.PB1, GHI.Hardware.G400.Pin.PC23, 5000);

This results in:

Exception GHI.Premium.Net.NetworkInterfaceExtensionException - 0x00000000 (1)

#### Message: 
#### GHI.Premium.Net.EthernetENC28J60::.ctor [IP: 0011] ####
#### GadgeteerApp1.Program::ProgramStarted [IP: 002a] ####
#### GadgeteerApp1.Program::Main [IP: 0015] ####

A first chance exception of type ‘GHI.Premium.Net.NetworkInterfaceExtensionException’ occurred in GHI.Premium.Net.dll
An unhandled exception of type ‘GHI.Premium.Net.NetworkInterfaceExtensionException’ occurred in GHI.Premium.Net.dll
So, is the conclusion, that the ENC28 cannot be uses as a gadgeteer module and can only be used through the promium library?

@ bin-blank - Can you post a complete and minimal program that throws that exception?

@ John:

Yes, I can.
Because the Interface is not to initialized as I would expect from a gadgeteer module, I tried to access the ethernet_ENC.Interface directly to use the Ethernet_ENC for the rest of the code.

using System;
using System.Collections;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Presentation.Shapes;
using Microsoft.SPOT.Touch;

using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.Net;
using Microsoft.SPOT.Hardware;

namespace EthernetTestGadgeteer
{
    public partial class Program
    {
  
        void ProgramStarted()
        {
            this.ethernet_ENC28.Interface = new EthernetENC28J60(SPI.SPI_module.SPI1, GHI.Hardware.G400.Pin.PA28, GHI.Hardware.G400.Pin.PB1, GHI.Hardware.G400.Pin.PC23, 5000);
        }
    }
}

But more or less I gave up to use the ENC28 and the Gadgeteer.Webserer (see other post and issues). I switch back to simple TCP socket, because they are much more stable especially on network reconnect.

@ bin-blank - If this.ethernet_ENC28 is the Gadgeteer module, you cannot set the Interface property like that. The constructor to the module initializes that field for you so when you reassign to it, it throws an exception because the hardware is already being used by the first instance. How is the interface not initialized as you would expect?

@ John
I do not really understand your question. We are back to my original post.
Because from the connection, defined in the module explore, everything is well known to initialize the module. Then I would expect that the one line code to get a DHCP address or set the addeess manually works.
But the feedback from andre.m is that I have to do it manually (see above).

Only in hope that the gadgeteer class will work, if I do the initialization manually was the reason, why I tried it.
But I understand, that the module is already doing some initialization; makes sense. If it would work as expected (see main post) make much more sense :slight_smile: .

I’m looking forward to the next SDK 4.3 version.

You seem to now understand what the Gadgeteer designer is doing for you (in InitializeModules, behind the scenes). So if you really wanted to initialize the module yourself like your last tests, you could omit it from the Gadgeteer designer and define it, but instead just let Gadgeteer invoke that for you.

From https://www.ghielectronics.com/community/forum/topic?id=12445 which was referred to earlier, there’s clear direction about what you need to do in your code, but I don’t see you’ve ever tried that? Besides the USING statement, add the following to the ProgramStarted:

using GHI.Premium.Net
  
 ethernet.Interface.CableConnectivityChanged += new EthernetBuiltIn.CableConnectivityChangedEventHandler(CableConnectivityChanged);
 ethernet.Interface.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(NetworkAddressChanged);
  
 ethernet.Interface.Open();
  
 NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet.Interface);
  
//if (!ethernet.Interface.NetworkInterface.IsDhcpEnabled)
//  ethernet.Interface.NetworkInterface.EnableDhcp();
 ethernet.Interface.NetworkInterface.EnableStaticIP(IPv4Address, SubnetMask, GatewayAddress);

In your case you wanted to use DHCP so you should omit the EnableStaticIP.

Hello GHI Team, thank you for all you invest and to as patient with me!
I’m using your hardware since FEZ Panda II and I’m very happy with it! I still know what the team is doing for me.

I will try to give a conclusion to explain where I am. And I have already a working socket http webserver and I currently verify if it make sense to switch to the gadgeteer ethernet class and webserver to reduce my code.
And if something is not working I hope this information will help you/others.

@ Bret: I already tried:

ethernet_ENC28.Interface.Open();
NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_ENC28.Interface);
ethernet_ENC28.Interface.NetworkInterface.EnableStaticIP("192.168.1.14", "255.255.255.0", "192.168.1.1");        Gadgeteer.Networking.WebServer.StartLocalServer(ethernet_ENC28.NetworkSettings.IPAddress, 80);

Issue 1:
If I access the network Interface.NetworkInterface (last line) it’s null. Later in the code (see below) the settings are still null.
That’s not critical because I can handover the address as string used in the previous line. Then the gatgeteer webserver becomes up and running and I get a response on the default web page.

Issue 2:
What I would need is a central web event method which will be entered for each site and sub site (e.g. one event for api as well as for api/json). Is it possible?

Issue 3:
The method NetworkDown is entered but NetworkUp never.

And here is my current test code:

using System;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
using GHI.Premium.Net;
using Microsoft.SPOT.Hardware;
using Microsoft.SPOT;

namespace EthernetTestGadgeteer
{
    public partial class Program
    {
        WebEvent sayHello;      
  
        void ProgramStarted()
        {
            ethernet_ENC28.NetworkUp += ethernet_ENC28_NetworkUp;
            ethernet_ENC28.NetworkDown += ethernet_ENC28_NetworkDown;
            ethernet_ENC28.Interface.CableConnectivityChanged += Interface_CableConnectivityChanged;
            ethernet_ENC28.Interface.NetworkAddressChanged += Interface_NetworkAddressChanged;

            ethernet_ENC28.Interface.Open();
            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_ENC28.Interface);
            ethernet_ENC28.Interface.NetworkInterface.EnableStaticIP("192.168.1.14", "255.255.255.0", "192.168.1.1");
            //Gadgeteer.Networking.WebServer.StartLocalServer(ethernet_ENC28.NetworkSettings.IPAddress, 80);
            Gadgeteer.Networking.WebServer.StartLocalServer("192.168.1.14", 80);

            sayHello = WebServer.SetupWebEvent("hello");
            sayHello.WebEventReceived += new WebEvent.ReceivedWebEventHandler(sayHello_WebEventReceived);
        }

        void Interface_NetworkAddressChanged(object sender, EventArgs e)
        {
            Debug.Print("Interface_NetworkAddressChanged");
        }

        void Interface_CableConnectivityChanged(object sender, EthernetENC28J60.CableConnectivityEventArgs e)
        {
            Debug.Print("Interface_CableConnectivityChanged");
        }

        void ethernet_ENC28_NetworkDown(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("ethernet_ENC28_NetworkDown");
        }

        void ethernet_ENC28_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            Debug.Print("ethernet_ENC28_NetworkUp");
        }

        void sayHello_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            // ethernet_ENC28.NetworkSettings are still null
            Debug.Print("sayHello_WebEventReceived");
            string content = "<html><body><h1>Hello World!!</h1></body></html>";
            byte[] bytes = new System.Text.UTF8Encoding().GetBytes(content);
            responder.Respond(bytes, "text/html");
        }
    }
}

@ bin-blank - Sorry for the delay. Can you comment out the line:

Gadgeteer.Networking.WebServer.StartLocalServer(“192.168.1.14”, 80);

and the two lines after it? Then just run the program and let it sit there for a few seconds to a minute. Are any of the events ever fired? If not, can you start with the cable unplugged and then plug it in once the program starts and see if any events are then fired?