Hello everybody,
after switching from G120HDR to FEZ Raptor I had to learn that prepare using the gadgeteer webserver with the new board. You can find the post here: https://www.ghielectronics.com/community/forum/topic?id=15961 But there are still issues to keep the webserver up and running and I opened this new post.
At the G120HDR board I didn’t get it stable, neither for HttpListener nor for simple socket webserver implementations. That’s why I’m looking forward to the gadgeteer webserver implementation. With the new hardware I can exclude hardware problems.
First the current sample code based on: https://www.ghielectronics.com/community/forum/topic?id=12445:
using System;
using Microsoft.SPOT;
using System.Net;
using System.Text;
using System.Threading;
using GHI.Premium.Net;
using Microsoft.SPOT.Hardware;
using Gadgeteer.Networking;
namespace SimpleHttpServer
{
public class Program
{
static EthernetENC28J60 Eth1 = new EthernetENC28J60(SPI.SPI_module.SPI1, GHI.Hardware.G400.Pin.PA28, GHI.Hardware.G400.Pin.PB1, GHI.Hardware.G400.Pin.PC23, 5000);
static byte[] outBuffer;
static public ManualResetEvent NetworkAvailablityBlocking = null;
static string myIP = "192.168.1.14";
static Gadgeteer.Networking.WebEvent hello;
public static void Main()
{
Eth1.Open();
NetworkInterfaceExtension.AssignNetworkingStackTo(Eth1);
Eth1.CableConnectivityChanged += new EthernetENC28J60.CableConnectivityChangedEventHandler(Eth1_CableConnectivityChanged);
Eth1.NetworkAddressChanged += new NetworkInterfaceExtension.NetworkAddressChangedEventHandler(Eth1_NetworkAddressChanged);
Eth1.NetworkInterface.EnableStaticIP(myIP, "255.255.255.0", "192.168.1.1");
if (!Eth1.IsCableConnected)
{
NetworkAvailablityBlocking = new ManualResetEvent(false);
do
{
if (!Eth1.IsCableConnected)
{
Debug.Print("Ethernet cable is not connected yet.");
}
else
break;
}
while (!NetworkAvailablityBlocking.WaitOne(5000, false));
}
while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any)
{
Debug.Print("IP address is not set yet.");
}
Debug.Print("IP address is set");
Gadgeteer.Networking.WebServer.StartLocalServer(Eth1.NetworkInterface.IPAddress, 80);
hello = Gadgeteer.Networking.WebServer.SetupWebEvent("hello");
hello.WebEventReceived += new WebEvent.ReceivedWebEventHandler(hello_WebEventReceived);
}
static void hello_WebEventReceived(string path, WebServer.HttpMethod method, Responder responder)
{
Debug.Print("Hello world ");
responder.Respond("Hello world");
}
static void Eth1_CableConnectivityChanged(object sender, EthernetENC28J60.CableConnectivityEventArgs e)
{
Debug.Print("Built-in Ethernet Cable is " + (e.IsConnected ? "Connected!" : "Disconneced!"));
if (e.IsConnected)
NetworkAvailablityBlocking.Set();
}
static void Eth1_NetworkAddressChanged(object sender, EventArgs e)
{
Debug.Print("New address for The built-in Ethernet Network Interface ");
Debug.Print("Is DhCp enabled: " + Eth1.NetworkInterface.IsDhcpEnabled);
Debug.Print("Is DynamicDnsEnabled enabled: " + Eth1.NetworkInterface.IsDynamicDnsEnabled);
Debug.Print("NetworkInterfaceType " + Eth1.NetworkInterface.NetworkInterfaceType);
Debug.Print("Network settings:");
Debug.Print("IP Address: " + Eth1.NetworkInterface.IPAddress);
Debug.Print("Subnet Mask: " + Eth1.NetworkInterface.SubnetMask);
Debug.Print("Default Getway: " + Eth1.NetworkInterface.GatewayAddress);
Debug.Print("Number of DNS servers:" + Eth1.NetworkInterface.DnsAddresses.Length);
for (int i = 0; i < Eth1.NetworkInterface.DnsAddresses.Length; i++)
Debug.Print("DNS Server " + i.ToString() + ":" + Eth1.NetworkInterface.DnsAddresses[i]);
Debug.Print("------------------------------------------------------");
}
}
}
And here are my current issues:
1. Issue: Network Reconnection
My current issue is, that I do not know how to handle a network reconnection. At reconnection the code stops working at
if (e.IsConnected)
NetworkAvailablityBlocking.Set();
with a ‘System.NullReferenceException’.
I do not realy understand what the NetworkAvailablityBlocking stays for and why it becomes null if it is instantiated before.
Does somebody knows how to handle a network reconnection?
2. Unstable - no Response
2a. Starting with root page
If I start the Webserver, I get a response in the browser for the root URL 192.168.1.14. I can do many refresh and I get still a response. For each request I get two such lines in the debug output window:
The thread ‘’ (0x8) has exited with code 0 (0x0).
If I change the URL to 192.168.1.14/hello, I get one response, but then there is a timeout at page refresh, that means no response. The hello_WebEventReceived is not entered.
In this case in the debug output only one such line is created.
But the webserver doesn’t stop working. If I go back to the root, I get a response again. But the sub page is not accessible.
2b. Starting with sub page hello
If the first page I request from the webserver is the sub page there is one response before timeout.
Is there something I’m doing wrong?
Thanks for all replies.