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