Make sure that you use Wifi_RN171_42 code with the following:
using System;
using System.Collections;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Presentation;
using Microsoft.SPOT.Presentation.Controls;
using Microsoft.SPOT.Presentation.Media;
using Microsoft.SPOT.Touch;
using Gadgeteer.Networking;
using GT = Gadgeteer;
using GTM = Gadgeteer.Modules;
using Gadgeteer.Modules.GHIElectronics;
namespace Waugh.Cerbot.Robot
{
public partial class Program
{
private const byte R_SPEED = 100;
private const byte L_SPEED = 100;
private enum Direction
{
Unknown,
Forward,
Reverse,
Left,
Right
}
private Direction[] _directions = new Direction[20];
string rootDirectory;
// This method is run when the mainboard is powered up or reset.
void ProgramStarted()
{
string methodName = "ProgramStarted";
LogMessage(methodName, MessageType.Start);
Debug.Print("Free mem : " + Debug.GC(false).ToString());
// set up camera -----------------------
// set up camera -----------------------
//// set up wifi -------------------------
// set up wifi -------------------------
LogMessage("Wifi Started", MessageType.Message);
new Thread(InitializeWifi).Start();
// done -------------------------
LogMessage(methodName, MessageType.End);
}
// Wifi Cerbot -------------------------
// Wifi Cerbot -------------------------
// Wifi Cerbot -------------------------
void InitializeWifi() {
string methodName = "InitializeWifi";
LogMessage(methodName, MessageType.Start);
wifi_RN171.Initialize(GTM.GHIElectronics.WiFi_RN171.SocketProtocol.TCP_Server); //Init as TCP Server
wifi_RN171._Command_Mode_Start();
wifi_RN171._Command_Execute("get ip");
wifi_RN171._Command_Execute("set comm remote 0");
wifi_RN171._Command_Mode_Exit();
wifi_RN171.EnableHttpServer(); //Enable HTTP Parsing
wifi_RN171.HttpRequestReceived += new GTM.GHIElectronics.WiFi_RN171.HttpRequestReceivedHandler(wifi_RN171_HttpRequestReceived);
wifi_RN171.SetListenPort(80);
LogMessage(methodName, MessageType.End);
}
void wifi_RN171_HttpRequestReceived(GTM.GHIElectronics.HttpStream request)
{
string methodName = "wifi_RN171_HttpRequestReceived";
LogMessage(methodName, MessageType.Start);
string requestedURL = request.Request.URL;
// unfortunately /move.html?direction='north' is not working
if (requestedURL.IndexOf("/_") == 0)
{
HandleAjaxHttpRequest(request, requestedURL);
}
else
{
HandleHttpRequest(request, requestedURL);
}
LogMessage(methodName, MessageType.End);
}
private void HandleHttpRequest(GTM.GHIElectronics.HttpStream request, string requestedURL)
{
string methodName = "HandleHttpRequest";
LogMessage(methodName, MessageType.Start);
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;
string HTML = IndexHTML(action);
request.Response.Send(System.Text.Encoding.UTF8.GetBytes(HTML));
LogLeaveMethod(methodName);
}
private void HandleAjaxHttpRequest(GTM.GHIElectronics.HttpStream request, string requestedURL)
{
string methodName = "HandleAjaxHttpRequest";
LogMessage(methodName, MessageType.Start);
string action = "";
switch (requestedURL)
{
case "/_index.html":
action = "Welcome";
break;
case "/_northwest.html":
action += "north west";
break;
case "/_north.html":
{
action += "north";
Move(Direction.Forward);
break;
}
case "/_northeast.html":
action += "north east";
break;
case "/_west.html":
{
action += "west";
Move(Direction.Left);
break;
}
case "/_center.html":
action += "home";
break;
case "/_east.html":
{
action += "east";
Move(Direction.Right);
break;
}
case "/_southwest.html":
action += "south west";
break;
case "/_south.html":
{
action = "We're going down south";
Move(Direction.Reverse);
break;
}
case "/_southeast.html":
action += "south east";
break;
case "/_start.html":
action = "Let's go";
break;
case "/_stop.html":
{
action = "Hold it, right there!";
Move(Direction.Unknown);
break;
}
case "/_fire.html":
{
action = "Cease fire!";
//serCam.TakePicture();
break;
}
case "/_getposition.html":
action = "interval example position[x,y]";
break;
default:
action = "Unknown action";
break;
}
LogMessage(action, MessageType.Start);
//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(System.Text.Encoding.UTF8.GetBytes(action));
LogLeaveMethod(methodName);
}
// Move Cerbot -------------------------
// Move Cerbot -------------------------
// Move Cerbot -------------------------
private string DirectionToString(Direction direction)
{
switch (direction)
{
case Direction.Forward:
return "Forward";
case Direction.Reverse:
return "Reverse";
case Direction.Left:
return "Left";
case Direction.Right:
return "Right";
default:
return "Unknown";
}
}
private void Move(Direction direction)
{
LogMessage("Move", MessageType.Start);
switch (direction)
{
case Direction.Forward:
cerbotController.SetMotorSpeed(L_SPEED, R_SPEED);
break;
case Direction.Reverse:
cerbotController.SetMotorSpeed(-L_SPEED, -R_SPEED);
break;
case Direction.Left:
TurnLeft();
Move(Direction.Forward);
return;
case Direction.Right:
TurnRight();
Move(Direction.Forward);
return;
}
Thread.Sleep(1000);
cerbotController.SetMotorSpeed(0, 0);
LogMessage("Move", MessageType.End);
}
private void TurnLeft()
{
LogMessage("TurnLeft", MessageType.Start);
cerbotController.SetMotorSpeed(-L_SPEED, R_SPEED);
Thread.Sleep(350);
cerbotController.SetMotorSpeed(0, 0);
}
private void TurnRight()
{
LogMessage("TurnRight", MessageType.Start);
cerbotController.SetMotorSpeed(L_SPEED, -R_SPEED);
Thread.Sleep(315);
cerbotController.SetMotorSpeed(0, 0);
}
// General Cerbot -------------------------
// General Cerbot -------------------------
// General Cerbot -------------------------
private void LogMessage(string msgText, MessageType msgType)
{
switch (msgType)
{
case MessageType.Message:
msgText = "Message [" + msgText + "]";
break;
case MessageType.Start:
msgText = "--> [" + msgText + "]";
break;
case MessageType.End:
msgText = "<-- [" + msgText + "]";
break;
case MessageType.Error:
msgText = "Error: [" + msgText + "]";
break;
default:
break;
}
msgText = msgText + " " + DateTime.Now.Ticks.ToString();
Debug.Print(msgText);
}
private enum MessageType {
Message = 0,
Start = 1,
End = 2,
Error = 3,
}
private void LogLeaveMethod(string methodName)
{
Debug.Print("<--- " + methodName);
}
private string IndexHTML(string action)
{
string HTML = String.Empty;
HTML = HTML + "<!DOCTYPE html>\n";
HTML = HTML + "<html lang=\"en\" xmlns=\"http://www.w3.org/1999/xhtml\">\n";
HTML = HTML + "<head>\n";
HTML = HTML + " <meta charset=\"utf-8\" />\n";
HTML = HTML + " <title></title>\n";
HTML = HTML + "</head>\n";
HTML = HTML + " \n";
HTML = HTML + "<body>\n";
HTML = HTML + " <style>\n";
HTML = HTML + " td {\n";
HTML = HTML + " width:160px;\n";
HTML = HTML + " vertical-align:central\n";
HTML = HTML + " }\n";
HTML = HTML + " input[type=\"button\"] {\n";
HTML = HTML + " font-size:144px;\n";
HTML = HTML + " background-color:beige;\n";
HTML = HTML + " text-align:center;\n";
HTML = HTML + " color:black;\n";
HTML = HTML + " font-weight:bold;\n";
HTML = HTML + " width:150px;\n";
HTML = HTML + " height:150px;\n";
HTML = HTML + " vertical-align:central\n";
HTML = HTML + "\n";
HTML = HTML + " }\n";
HTML = HTML + "\n";
HTML = HTML + " input[type=\"button\"].x {\n";
HTML = HTML + " font-size:12px;\n";
HTML = HTML + " }\n";
HTML = HTML + "\n";
HTML = HTML + " #btnStop {\n";
HTML = HTML + " background-color:red;\n";
HTML = HTML + " font-size:32px;\n";
HTML = HTML + " }\n";
HTML = HTML + "\n";
HTML = HTML + " #btnStart {\n";
HTML = HTML + " background-color:green;\n";
HTML = HTML + " font-size:32px;\n";
HTML = HTML + " }\n";
HTML = HTML + " #lbInfo {\n";
HTML = HTML + " display: block;\n";
HTML = HTML + " float: right;\n";
HTML = HTML + " width:300px;\n";
HTML = HTML + " background-color:white;\n";
HTML = HTML + " font-size:24px;\n";
HTML = HTML + " text-align:center\n";
HTML = HTML + " }\n";
HTML = HTML + "\n";
HTML = HTML + " </style>\n";
HTML = HTML + " <script type=\"text/javascript\">\n";
HTML = HTML + " var xmlhttp;\n";
HTML = HTML + " if (window.XMLHttpRequest) {\n";
HTML = HTML + " xmlhttp = new XMLHttpRequest();\n";
HTML = HTML + " }\n";
HTML = HTML + " else {\n";
HTML = HTML + " xmlhttp = new ActiveXObject(\"Microsoft.XMLHTTP\");\n";
HTML = HTML + " }\n";
HTML = HTML + "\n";
HTML = HTML + " window.setInterval(submitAction('getposition()'), 1000);\n";
HTML = HTML + "\n";
HTML = HTML + " function submitAction(action) {\n";
HTML = HTML + "\n";
HTML = HTML + " xmlhttp.onreadystatechange = function () {\n";
HTML = HTML + " if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {\n";
HTML = HTML + " document.getElementById(\"lbInfo\").innerHTML = xmlhttp.responseText;\n";
HTML = HTML + " }\n";
HTML = HTML + " }\n";
HTML = HTML + " xmlhttp.open(\"GET\", \"http://192.168.1.1/_\" + action + \".html\", true);\n";
HTML = HTML + " xmlhttp.send();\n";
HTML = HTML + " }\n";
HTML = HTML + "\n";
HTML = HTML + "\n";
HTML = HTML + "\n";
HTML = HTML + "\n";
HTML = HTML + " </script>\n";
HTML = HTML + " <table>\n";
HTML = HTML + " <tr>\n";
HTML = HTML + " <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnNorthWest\" onclick=\"submitAction('northwest');\"/></td>\n";
HTML = HTML + " <td><input type=\"button\" value=\"/\\\" id=\"btnUp\" onclick=\"submitAction('north');\"/></td>\n";
HTML = HTML + " <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnNorthEast\" onclick=\"submitAction('northeast');\"/></td>\n";
HTML = HTML + " <td></td>\n";
HTML = HTML + " <td><input type=\"button\" value=\"STOP\" id=\"btnStop\" onclick=\"submitAction('stop');\"/></td>\n";
HTML = HTML + " </tr>\n";
HTML = HTML + " <tr>\n";
HTML = HTML + " <td><input type=\"button\" value=\"<\" id=\"btnWest\" onclick=\"submitAction('west');\"/></td>\n";
HTML = HTML + " <td><input type=\"button\" class=\"x\" value=\"click\" id=\"btnCenter\" onclick=\"submitAction('fire');\"/></td>\n";
HTML = HTML + " <td><input type=\"button\" value=\">\" id=\"btnEast\" onclick=\"submitAction('east');\"/></td>\n";
HTML = HTML + " <td colspan=\"2\"><label id=\"lbInfo\">We'are going nowhere</label></td>\n";
HTML = HTML + " </tr>\n";
HTML = HTML + " <tr>\n";
HTML = HTML + " <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnSouthWest\" onclick=\"submitAction('southwest');\"/></td>\n";
HTML = HTML + " <td><input type=\"button\" value=\"\\/\" id=\"btnSouth\" onclick=\"submitAction('south');\"/></td>\n";
HTML = HTML + " <td><input type=\"button\" class=\"x\" value=\"x\" id=\"btnSouthEast\" onclick=\"ssubmitAction('southeast');\"/></td>\n";
HTML = HTML + " <td></td>\n";
HTML = HTML + " <td><input type=\"button\" value=\"START\" id=\"btnStart\" onclick=\"submitAction('start');\"/></td>\n";
HTML = HTML + " </tr>\n";
HTML = HTML + " </table>\n";
HTML = HTML + " <br/>\n";
HTML = HTML + " <div>" + DateTime.Now.TimeOfDay.ToString() + "</div>\n";
HTML = HTML + "</body>\n";
HTML = HTML + "</html>\n";
return HTML;
}
}
}