Netduino home hardware projects downloads community

Jump to content

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

Reading Content from Javascript Question

Javascript Content Socket

  • Please log in to reply
2 replies to this topic

#1 Mark1


    New Member

  • Members
  • Pip
  • 2 posts

Posted 21 September 2013 - 04:43 AM

Hi all !


I am having trouble reading "Content" from JavaScript... Could anyone tell me how to  parse true and false Content into my Netduino Code (in simple language for a beginner)?


Am using Netduino +2 to host a simple webserver; javascript is being embedded in webpage stored on the Netduino to be access by client pc. Am using javascript to enable simple PUT messages to be delivered to the Netduino... to blink an LED.


Code is derived from both "Getting Started with Netduino" and "Getting Started with the Internet of Things". There is a great piece of Code in the back of GSIOT which does what I want, but as you can tell I am trying to do this without using the GSIOT. Server library (am taking small steps here).


Code is as follows.  Problem code lies in:


if ((request.IndexOf("PUT /led/target") >= 0) && (request.IndexOf("False") >= 0))


and similarly,


if ((request.IndexOf("PUT /led/target") >= 0) && (request.IndexOf("True") >= 0))


A follow up question would be how do I add a second set of buttons (and associated javascript) to control a second device on the netduino (say, a servo).


Any suggestions welcome - in addition any good textbook ideas to direct my reading would be appreciated!




    using System;   using System.Net;   using System.Net.Sockets;   using System.Threading;   using Microsoft.SPOT;   using Microsoft.SPOT.Hardware;   using SecretLabs.NETMF.Hardware;   using SecretLabs.NETMF.Hardware.Netduino;     namespace Connecting_to_the_Internet_1   {   public class Program   {   public static void Main()   {   //setup the LED and turn it off by default   OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);     //configure the port # (the standard web server port is 80)   int port = 9000;     //wait a few seconds for the netduino to get a network address   Thread.Sleep(5000);     //display the IP address   Microsoft.SPOT.Net.NetworkInformation.NetworkInterface networkInterface =   Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];     Debug.Print("my ip address: " + networkInterface.IPAddress.ToString());     //create a socket to listen for incoming connections   Socket listenerSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);   IPEndPoint listenerEndPoint = new IPEndPoint(IPAddress.Any, port);     //bind to the listening socket   listenerSocket.Bind(listenerEndPoint);     //and start listening for incoming connections   listenerSocket.Listen(1);     //listen for and process incoming requests   while (true)   {   //wait for socket to connect   Socket clientSocket = listenerSocket.Accept();     //wait for data to arrive   bool dataReady = clientSocket.Poll(5000000, SelectMode.SelectRead);     //if dataReady is true and there are bytes available to read,   //then you have a good connection     if (dataReady && clientSocket.Available > 0)   {   byte[] buffer = new byte[clientSocket.Available];   int bytesRead = clientSocket.Receive(buffer);     string request = new string(System.Text.Encoding.UTF8.GetChars(buffer));   //string testF = "false";   //string testT = "true";   if ((request.IndexOf("PUT /led/target") >= 0) && (request.IndexOf("False") >= 0))   {   led.Write(false);   }     if ((request.IndexOf("PUT /led/target") >= 0) && (request.IndexOf("True") >= 0))   {   led.Write(true);   }     if (request.IndexOf("GET /led/target.html") >= 0)   {   string statusText = "LED is " + (led.Read() ? "ON" : "OFF") + ".";     //return a message to the client   var response =   @"<html>   <head>   <script type=""text/javascript"">   var r;   try {   r = new XMLHttpRequest();   } catch (e) {   r = new ActiveXObject('Microsoft.XMLHTTP');   }   function put (content) {'PUT', '" + "/led/target" + @"');   r.setRequestHeader('Content-Type', 'text/plain');   r.send(content);   }   </script>   </head>   <body>   <p>   <input type=""button"" value=""Switch LED on""    onclick=""put('true')""/>   <input type=""button"" value=""Switch LED off""   onclick=""put('false')""/>   </p>   </body> </html>";   clientSocket.Send(System.Text.Encoding.UTF8.GetBytes(response));     }   //important: close the client socket   clientSocket.Close();   }   }   }   }   }

#2 SteveAndrews



  • Members
  • PipPip
  • 17 posts
  • LocationOrange County, CA

Posted 27 September 2013 - 09:17 AM

What I would do, just to keep things simple, is to first parse the HTTP verb (GET, POST, PUT, etc), which is from char index 0 to the first whitespace. Then, grab the URL from that point until the second bit of whitespace. That way you can decide what action to take based on both pieces of information.


As to your question, I might recommend using a simpler URL naming convention, such as "/on" and "/off", which would be considerably easier to parse.


Lastly, I once tried to server jQuery from the Netduino, only to find it took quite a bit too long to load. There are, however, a number of really tiny JavaScript libraries available that I have used with success, like MinifiedJS (selectors, ajax, etc.). Of course, you'd need to handle requests for those resources as well. (I did that by checking if the URL ended with ".js" and passing that straight through to pull a file off an SD card.)


Hope this helps.

#3 Mark1


    New Member

  • Members
  • Pip
  • 2 posts

Posted 28 September 2013 - 06:57 AM

Firstly, I really appreciate the response. Thanks for taking the time!


I had thought about doing it using parsing of the browser URI, but I believe that this requires the user to type a PUT request into the browser; this approach is fine for me but not so user-friendly for others. While I certainly have a lot to learn even under this approach, this time around I am nonetheless trying to do it slightly differently. 


I am keen to use more than one button on a webpage to initiate different PUT request (is it correct to say this is the "JavaScript approach"?). To be clear , this is where one of several buttons on a page may be pressed to initiate different actuator requests (not simply cycling LED ON / LED OFF - which I think is perhaps only one request and is simpler to program).


I believe this multi-button approach can be done without an additional library, and just requires a fairly minor modification of my current code (though I confess I know absolutely nothing about JQuery and how it may even be applicable here).


If you or others have view on how I may do it I'd be grateful.


Cheers, Mark

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

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.