Wifi RN 171

Has anyone been able to get it to work?

I am beyond myself and am very frustrated with it. I have gone through every example that I could find with no luck. If anyone can point me to one sample that works (please do not suggest the GHI default one - been there done that) I would be beyond overjoyed to see this work.

I have struggled through every module so far including the wifi and sercam. I am wondering if I should pack it in and go raspberry.

Any help would be very appreciated.

I can’t say I have tried the WiFi module, so can’t directly help you.

But there are others that DO report that the GHI code works. Can you tell us about your router? And do you have a smartphone with Wifi Access Point that you can also try

Hi your tricky part lies in the header that you are sending to the browser. I got it working(also after a long struggle!) on Internet Expolrer. I am still tying to get it to work on Safari / Mozilla
etc. :’(

void wifi_RN171_HttpRequestReceived(GTM.GHIElectronics.HttpStream request)
{
string requestedURL = request.Request.URL;
Debug.Print(requestedURL);

        if (requestedURL == "/")
        {
            string document = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">";
            document  +="<html><head>\n";
            document += "<title>Transtech R&D</title></head>\n";
            document += "<body style=background-Color:#CCFF66><h1><em>Hallo, from  Cobra II - G120 embedded .NETMF system !</em></h1>\n";
            document += "<hr><h2>RN 171</h2></hr>";
            document += "<hr>Sotware development by: Ulf<hr></hr>\n";

            document += "</body></html>";
            //HeaderData["Content-Length"] = document.Length.ToString();
            request.Response.HeaderData["Content-type"] = "text/html";
            request.Response.HeaderData["Content-Length"] = document.Length.ToString();
            request.Response.HeaderData["Connection"] = "close";
            request.Response.HeaderData["Cache-Control"] = "no-cache";
            request.Response.StatusCode = GTM.GHIElectronics.HttpResponse.ResponseStatus.OK;

             //Header data is automatically applied for you when you chose to send.
            request.Response.Send(System.Text.Encoding.UTF8.GetBytes(document));
            }
  }

You may be having issues in other browsers because they are more secure. Instead of using \n (Windows only new line feed) you should use \r\n (Universal new line feed) for anything HTML.

What is your host board? Also, which set of example code are you using, and, in the most detail possible, what and how exactly is not working? Is it only partially serving the request? Does it respond with any data at all, or is it timing out?

After battling with it for over a week, it is finally running. My goal is to provide the code.

LF: Multics, Unix and Unix-like systems (GNU/Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS and others.
CR+LF: Microsoft Windows, DEC TOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPC
LF+CR: Acorn BBC and RISC OS spooled text output.
CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9 and OS-9
RS: QNX pre-POSIX implementation.

Also, the HTTP spec says that protocol elements (headers etc) should use \r\n as a line ending, but that in the interest of interoperability, browsers should actually use the \n and ignore the \r if it precedes the \n.

For the body, the spec doesn’t specify, and any line ending style will work in any browser, modern or ancient.

1 Like

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;
    }


}

}