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!
Cheers
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) { r.open('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(); } } } } }