Http WebServer

Hello I’m going to start a simple webserver on my raptor this is a simple code


public partial class Program
    {
        string DeviceIP = "172.16.50.44";
        string SubnetMask = "255.255.255.0";
        string DefultGateway = "172.16.50.254";
       
        void ProgramStarted()
        {
     InitEthernet();
            try
            {
Gadgeteer.Networking.WebServer.StartLocalServer(ethernet_ENC28.NetworkSettings.IPAddress, 80);
               WebEvent webEv = WebServer.SetupWebEvent("index.html");
               webEv.WebEventReceived += new WebEvent.ReceivedWebEventHandler(webEv_WebEventReceived);
            }
            catch (Exception e)
            { }
            
        }

        void webEv_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
        {
            string content = "<html><body><h1>This is now my custom page</h1></body></html>";
            byte[] bytes = new System.Text.UTF8Encoding().GetBytes(content);
            responder.Respond(bytes, "text/html");
}
 private void InitEthernet()
        {
            if (!ethernet_ENC28.Interface.IsOpen)
            {
                ethernet_ENC28.Interface.Open();
            }
            NetworkInterfaceExtension.AssignNetworkingStackTo(ethernet_ENC28.Interface);
            ethernet_ENC28.Interface.NetworkInterface.EnableStaticIP(DeviceIP, SubnetMask, DefultGateway);
            ethernet_ENC28.DebugPrintEnabled = true;
 }
    }


but the problem is I have a error on this line


Gadgeteer.Networking.WebServer.StartLocalServer(ethernet_ENC28.NetworkSettings.IPAddress, 80);

The error is:

Thanks for help… :slight_smile:

@ Ehsan Ansari - Can you post a stacktrace and the contents of InitEthernet?

@ John - Sorry, I didn’t understand what you mean… InitEthernet seems to be OK as after completion of it I can ping DeviceIP on my laptop…

You should check the NetworkInterface object associated with the ENC28 object to determine when the IP address changed from “0.0.0.0”. I believe there is an issue with the IP address being set in the ENC28 object.

There is a lot going on behind the scene. You have to wait for the IP address to be set. There is a short delay. Could be as long as a few seconds.

@ Mike - I know about delay… Actually I used the following event before:


void ProgramStarted()
        {
            InitEthernet();
            ethernet_ENC28.NetworkUp += ethernet_ENC28_NetworkUp;
        }

        void ethernet_ENC28_NetworkUp(GTM.Module.NetworkModule sender, GTM.Module.NetworkModule.NetworkState state)
        {
            try
            {

                Gadgeteer.Networking.WebServer.StartLocalServer(ethernet_ENC28.NetworkSettings.IPAddress, 80);
                WebEvent webEv = WebServer.SetupWebEvent("index.html");
                webEv.WebEventReceived += new WebEvent.ReceivedWebEventHandler(webEv_WebEventReceived);
            }
            catch (Exception e)
            { }
        }

But it never raised…(Although NetwokInterface IP was changed and as I said I could ping my raptor on network) So I used the second code without that event…

@ Ehsan Ansari - have you tried what I have suggested?

Maybe this helps…
https://www.ghielectronics.com/community/codeshare/entry/1008
or
https://www.ghielectronics.com/community/codeshare/entry/1009

Try this:

void ProgramStarted()
         {
          Thread.Sleep(2000);

   InitEthernet();
             ethernet_ENC28.NetworkUp += ethernet_ENC28_NetworkUp;
         }

cheers,
Jay

Thanks everybody…
I tried your suggestions and finally when I switched to netmf 4.3 It worked!
;D

Although yet patching of responder.cs in gadgeteer core is not sufficient and I have to add lifr Webserver project to my solution following this thread: