At the time of writing this blog, this project needs the
patch for WiFiRN171 in order to work. The current unpatched version does not work due to incorrect baudrate settings in the driver.
The initialization of the module is quite easy: In ProgramStarted a one-time-timer is started that sets up the wifi_RN171 module to act as a HttpServer. After the initialization, the webserver listens at http://192.168.1.1 and requests are handled in wifi_RN171_HttpRequestReceived
void ProgramStarted()
{
string methodName = "ProgramStarted";
LogEnterMethod(methodName);
wifi_RN171.Reboot();
GT.Timer wifiInitializationTimer = new GT.Timer(1000);
wifiInitializationTimer.Behavior = GT.Timer.BehaviorType.RunOnce;
wifiInitializationTimer.Tick += initializeWifi;
wifiInitializationTimer.Start();
LogLeaveMethod(methodName);
}
void initializeWifi(GT.Timer timer)
{
string methodName = "initializeWifi";
LogEnterMethod(methodName);
wifi_RN171.SetDebugLevel(GTM.GHIElectronics.WiFi_RN171.DebugLevel.DebugAll);
wifi_RN171.Initialize(GTM.GHIElectronics.WiFi_RN171.SocketProtocol.TCP_Server);
wifi_RN171.EnableHttpServer(); //Enable HTTP Parsing
wifi_RN171.HttpRequestReceived += new GTM.GHIElectronics.WiFi_RN171.HttpRequestReceivedHandler(wifi_RN171_HttpRequestReceived);
LogLeaveMethod(methodName);
}
In wifi_RN171_HttpRequestReceived there's determined whether this is a normal request or a ajax request. All ajax request must be prefixed with '_'. The normal request returns html with buttons and mark-up:
private static void HandleHttpRequest(GTM.GHIElectronics.HttpStream request, string requestedURL)
{
string methodName = "HandleHttpRequest";
LogEnterMethod(methodName);
string action = "Unkown action";
if (requestedURL == "/index.html")
{
action = "Welcome";
}
else
{
action = "Unknown action";
}
request.Response.HeaderData["Content-type"] = "text/html; charset=utf-8";
request.Response.HeaderData["Connection"] = "close";
request.Response.HeaderData["Cache-Control"] = "no-cache";
request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;
request.Response.Send( HERE IS A LONG LINE OF HTML );
LogLeaveMethod(methodName);
}
private static void HandleAjaxHttpRequest(GTM.GHIElectronics.HttpStream request, string requestedURL)
{
string methodName = "HandleAjaxHttpRequest";
LogEnterMethod(methodName);
string action = "";
switch (requestedURL)
{
case "/_index.html":
action = "Welcome";
break;
case "/_northwest.html":
action += "north west";
break;
case "/_north.html":
action += "north";
break;
case "/_northeast.html":
action += "north east";
break;
case "/_west.html":
action += "west";
break;
case "/_center.html":
action += "home";
break;
case "/_east.html":
action += "east";
break;
case "/_southwest.html":
action += "south west";
break;
case "/_south.html":
action = "We're going down south";
break;
case "/_southeast.html":
action += "south east";
break;
case "/_start.html":
action = "Let's go";
break;
case "/_stop.html":
action = "Hold it, right there!";
break;
case "/_fire.html":
action = "Cease fire!";
break;
case "/_getposition.html":
action = "interval example position[x,y]";
break;
default:
action = "Unknown action";
break;
}
//request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;
request.Response.HeaderData["Content-type"] = "text/html; charset=utf-8";
request.Response.HeaderData["Connection"] = "close";
request.Response.HeaderData["Cache-Control"] = "no-cache";
request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;
request.Response.Send(action);
LogLeaveMethod(methodName);
}
The complete solution can be downloaded
here