Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

TheBrian

Member Since 08 Feb 2012
Offline Last Active Private
-----

Topics I've Started

Web server, socket, reading buffer data?

10 February 2016 - 06:25 PM

How do I get at the data put into the buffer and sent to the web server? When using the web server code found on the netduino forums, I only seem to be able to read the header info from the buffer received. I need to see the degrees of rotation I'm trying to send. 
 
Sending code from MVC app:
 public ActionResult PostDegrees(string degree)
        {
            WebRequest request = WebRequest.Create("http://10.0.1.240"); // my N3 static IP
            request.Method = "POST";
            string postData = degree; //Degree to turn servo.
            byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            request.ContentType = "application/x-www-form-urlencoded";
            request.ContentLength = byteArray.Length;
            Stream dataStream = request.GetRequestStream();
            dataStream.Write(byteArray, 0, byteArray.Length);
            dataStream.Close();
            return null; //Probably should be something else.
        }

Receiving Code on Netduino 3:

  public void ListenForRequest(Servo servo)
        {
            while (true)
            {
                using (Socket clientSocket = socket.Accept())
                {
                    //Get clients IP
                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                    EndPoint clientEndPoint = clientSocket.RemoteEndPoint;
                    int bytesReceived = clientSocket.Available;
                    if (bytesReceived > 0)
                    {
                        //Get request
                        byte[] buffer = new byte[bytesReceived];
                        int byteCount = clientSocket.Receive(buffer);
                        string request = new string(Encoding.UTF8.GetChars(buffer));
                        Debug.Print(request);
                        //Servo code to rotate motor follows...
                    }
                }
            }
        }

Output from Debug.Print statement:
POST / HTTP/1.1
Content-Type: application/x-www-form-urlencoded
Host: 10.0.1.240
Content-Length: 3
Expect: 100-continue
Connection: Keep-Alive

I'm moving a slider on a web page with a numeric range of 0-180, which is the range of motion of my servo connected to my Netduino 3. I just seem to be missing some important concept in reading the degree value that I believe I'm sending. At the moment my workaround is changing the sending code from WebRequest to HTTPWebRequest and stashing the degrees in the user agent property. This works great but just feels oh so wrong. Any insight/suggestions would really be appreciated.

 

 


home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.