RN171 Send response not working?

Hi

I was trying to get the sample code for RN171 working but I did not succeed.
So I made my own implementation of the http server. What works is getting the request. If I try to request an html page from my browser RN171 perfectly establishes a connection and gets the request (OPENGET /index.html …)
The problem seems to be the response:
When I try

string response =  "HTTP/1.1 200 OK\n" +
"Date: Sun, 18 Oct 2009 08:56:53 GMT\n" +
"Server: Apache/2.2.14 (Win32)\n" +
"Last-Modified: Sat, 20 Nov 2004 07:16:26 GMT\n" +
"ETag: \"10000000565a5-2c-3e94b66c2e680\"\n" +
"Accept-Ranges: bytes\n" +
"Content-Length: 44\n" +
"Connection: close\n" +
"Content-Type: text/html\n" +
"X-Pad: avoid browser bug\n" +
"\n" +
"<html><body><h1>It works!</h1></body></html>\n";
wifi.Send(ref response);

It just does nothing (also no exception) and my Browser waits forever.

First I was thinking about a wrong baudrate (because some guys mentioned this here). But for me this doesn’t make sense, because I get the request. So communication between the boards is working. (I even tried to set baudrate to 9600 and the result was I wasn’t getting requests anymore, so the baudrate of 115200 is correct).

Does anyone have some suggestions for me?

I really start to get crazy with this Gadgeteer platform. Every time I get something to run another thing appears that just should work but doesn’t.

My Bad, thanks.
But this didn’t solve my problem.
The send methode does not send (maybe the problem lies within the Serial class). I really don’t know…

Wow, the tipp with Fiddler was THE BOMB! Thank you so much!

I now see, that I get responses and they look as I expect them to be (Exept the HELLO at the beginning).
However this is what Fiddler says:

[Fiddler] ReadResponse() failed: The server did not return a response for this request.Server returned 364 bytes.[Fiddler] Response Header parsing failed.
Response Data:
<plaintext>
2A 48 45 4C 4C 4F 2A 48 54 54 50 2F 31 2E 31 20 32 30 30 20 4F 4B 0D 0A  *HELLO*HTTP/1.1 200 OK..
44 61 74 65 3A 20 53 75 6E 2C 20 31 38 20 4F 63 74 20 32 30 30 39 20 30  Date: Sun, 18 Oct 2009 0
38 3A 35 36 3A 35 33 20 47 4D 54 0D 0A 53 65 72 76 65 72 3A 20 41 70 61  8:56:53 GMT..Server: Apa
63 68 65 2F 32 2E 32 2E 31 34 20 28 57 69 6E 33 32 29 0D 0A 4C 61 73 74  che/2.2.14 (Win32)..Last
2D 4D 6F 64 69 66 69 65 64 3A 20 53 61 74 2C 20 32 30 20 4E 6F 76 20 32  -Modified: Sat, 20 Nov 2
30 30 34 20 30 37 3A 31 36 3A 32 36 20 47 4D 54 0D 0A 45 54 61 67 3A 20  004 07:16:26 GMT..ETag: 
22 31 30 30 30 30 30 30 30 35 36 35 61 35 2D 32 63 2D 33 65 39 34 62 36  "10000000565a5-2c-3e94b6
36 63 32 65 36 38 30 22 0D 0A 41 63 63 65 70 74 2D 52 61 6E 67 65 73 3A  6c2e680"..Accept-Ranges:
20 62 79 74 65 73 0D 0A 43 6F 6E 74 65 6E 74 2D 4C 65 6E 67 74 68 3A 20   bytes..Content-Length: 
34 34 0D 0A 43 6F 6E 6E 65 63 74 69 6F 6E 3A 20 63 6C 6F 73 65 0D 0A 43  44..Connection: close..C
6F 6E 74 65 6E 74 2D 54 79 70 65 3A 20 74 65 78 74 2F 68 74 6D 6C 0D 0A  ontent-Type: text/html..
58 2D 50 61 64 3A 20 61 76 6F 69 64 20 62 72 6F 77 73 65 72 20 62 75 67  X-Pad: avoid browser bug
0D 0A 0D 0A 3C 68 74 6D 6C 3E 3C 62 6F 64 79 3E 3C 68 31 3E 49 74 20 77  ....<html><body><h1>It w
6F 72 6B 73 21 3C 2F 68 31 3E 3C 2F 62 6F 64 79 3E 3C 2F 68 74 6D 6C 3E  orks!</h1></body></html>
0D 0A 0D 0A 43 6F 6E 6E 65 63 74 69 6F 6E 20 74 6F 20 68 6F 73 74 20 6C  ....Connection to host l
6F 73 74 2E                                                              ost.    

The question is: How do I prevent RN171 from sending this HELLO- garbage at the beginning? It definitely doesn’t come from my code…

I use the 4.2 R1u1 code and it’s true: There is no HELLO in it. So I googled a bit and found

On page 9 there is a HELLO. I think this behaviour is on the firmware of the RN171 chip.

Nothing too fancy, really:

public class WebServer
    {
        public void Start()
        {
            Thread webServerThread = new Thread(new ThreadStart(startThread));
            webServerThread.Start();
            startThread();
        }

        private void startThread()
        {
            Device.wifi.DataReceived += wifi_DataReceived;
            Device.wifi.ConnectionEstablished += wifi_ConnectionEstablished;
            Device.wifi.ConnectionClosed += wifi_ConnectionClosed;
            Device.wifi.LineReceived += wifi_LineReceived;
            Device.wifi.Initialize(GTM.GHIElectronics.WiFi_RN171.SocketProtocol.TCP_Server); //Init as TCP Server
            Device.wifi.SetListenPort(80);
        }

        void wifi_LineReceived(string line)
        {
            Device.displayMessage("line received: " + line);
        }

        void wifi_ConnectionClosed()
        {
            Device.displayMessage("connection closed");
        }

        void wifi_ConnectionEstablished()
        {
            Device.displayMessage("connection established");
        }

        void wifi_DataReceived(string data)
        {
            Device.displayMessage("data received: " + data);
            if(StringUtil.Contains(data.ToLower(), "get"))
            {
                //http seite angefordert
                string response = 
"HTTP/1.1 200 OK\r\n" + 
"Date: Mon, 07 Oct 2013 11:14:51 GMT\r\n" +
"Server: Apache\r\n" +
"X-Powered-By: PHP/5.3.27\r\n" +
"Vary: Accept-Encoding\r\n" +
"Content-Length: 30\r\n" +
"Connection: close\r\n" +
"Content-Type: text/html\r\n" +
"\r\n" +
"<html><body>test</body></html>";
                Device.displayMessage("sending response: " + response);

                try
                {
                    Device.wifi.Send(ref response);
                    Device.displayMessage("response sent: " + response);
                }
                catch (Exception ex)
                {
                    Device.displayMessage("Exception thrown");
                    Device.displayMessage(ex.Message);
                    Device.displayMessage(ex.StackTrace);
                }
            }
        }
    }

[quote=“D. Helfenstein”]
Hi

I was trying to get the sample code for RN171 working but I did not succeed…[/quote]

That’s what I did initialy. I only tried to make it by myself to find out which part is not working…

[quote=“D. Helfenstein”]

This is a parameter in the firmware that is configurable. You will need to send the following command to the module:

set comm remote 0
save

@ James - Is there a document that describes the command and any other possible commands?

Ah, I see. You do that in the Initialize- Method.
But I called that method as well!

As I tried to send those parameters by myself I found out, that I can’t get into start mode (_Command_Mode_Start() always returns false). I assume the same thing happens in the Initialize method. But because I never checked the return value (the example code didn’t do that as well) I never found out.

This will be added to the documentation, however, if _Command_Mode_Start() returns false, there are a few possible reasons for this to happen:

  1. Device is already in command mode, which will cause the string to enter command mode to be parsed as a command, returning ERR.
  2. Baud-rate mismatch
  3. The command mode string was changed and this information was not passed to initialize().

Case 3 will only ever be true if you set up configuration for this module manually.

I will take a look at this as soon as possible because, as mentioned, the comm remote configuration is currently set to 0.

The above is the PDF we will be adding to the resources for this product.

1 Like

it works now! (At least my own program).
What I did:

I started an acceess point on the RN171. Then I connected to it.
After that I connected via telnet to the RN171:

telnet 192.168.1.1 80

Then I entered command mode:

$$$

I used the two commands from James:

set comm remote 0
save

And now it’s possible for me to return valid http headers.
And what’s even cooler: the command seems to be stored in a config file on the RN171. So I don’t have to repeat the telnet procedure!

1 Like

And here we go again (with another problem):

Is it possible to use System.Net.Sockets.Socket with the RN171?
If I do this:

Socket listenSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
IPAddress localIP = new IPAddress(new byte[] { 192, 168, 1, 1 });
EndPoint localEndPoint = new IPEndPoint(localIP, 21);
listenSocket.Bind(localEndPoint);

I get an exception on the last line (bind). Do I have to prepare the RN171 for the use of Sockets or is just not possible?

I was hoping this was not the case…

My real problem seems to be that I can’t get into command mode and perform configuration changes to the RN171 during runtime.

  1. I think this is not possible because I allways call _Command_Mode_Exit() first and then _Command_Mode_Start()
  2. I don’t think so. I mean, I receive data and I can send too.
  3. No, because if I connect to the RN171 via telnet I’m able to go to command mode via $$$

→ But I’m still not able to successfully call _Command_Mode_Start() and _Command_Execute(…)

And as long as I’m not able to do so it’s also not possible for me to realize another server (in my case I’d like to create an ftp server).