Sorry if I butcher the terminology here or the code in general. I am not looking for someone to write the code (although that would help), but I am interested in the steps to get text into a response.
The setup is a sensor that feeds either 0V or 3.3V into A0, on a N+2.
- In a "Private Void" I am able to receive the sensor's input and convert it to a text.
- I have a remote app that comes into the Ethernet interface with a HTTP Get request for status and the status response needs to be the text that is produced in the "Private Void".
With debug I see the text being generated by the Private Void. The problem that I am having is getting the text response out of the Ethernet interface.
I think the problem is around lines 65-68, that is about half way down at " IsGarageDoorOpen();" Yes, that old project...
using System;using Microsoft.SPOT;using System.Net.Sockets;using System.Net;using System.Threading;using System.Text;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware.NetduinoPlus;using System.IO;namespace NetduinoGarageDoorOpener{ public class WebServer : IDisposable { private Socket socket = null; //open connection to onbaord led so we can blink it with every request private OutputPort led = new OutputPort(Pins.ONBOARD_LED, false); private OutputPort Garage2CarOpener = new OutputPort(Pins.GPIO_PIN_D9, false); private SecretLabs.NETMF.Hardware.AnalogInput garageSensor = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A0); public WebServer() { //Initialize Socket class socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); //Request and bind to an IP from DHCP server socket.Bind(new IPEndPoint(IPAddress.Any, 80)); //Debug print our IP address Debug.Print(Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress); //Start listen for web requests socket.Listen(10); ListenForRequest(); } public void ListenForRequest() { while (true) { using (Socket clientSocket = socket.Accept()) { //Get clients IP IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint; EndPoint clientEndPoint = clientSocket.RemoteEndPoint; //int byteCount = cSocket.Available; int bytesReceived = clientSocket.Available; if (bytesReceived > 0) { //Get request byte[] buffer = new byte[bytesReceived]; int byteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None); string request = new string(Encoding.UTF8.GetChars(buffer)); string firstLine = request.Substring(0, request.IndexOf('n')); //Example "GET /activatedoor HTTP/1.1" string[] words = firstLine.Split(' '); //Split line into words string command = string.Empty; if (words.Length > 2) { string method = words[0]; //First word should be GET command = words[1].TrimStart('/'); //Second word is our command - remove the forward slash // } switch (command.ToLower()) { // I am trying to figure out how to respond to // the "checkdoor" request with the text that is produced in the Private Void case "checkdoor": IsGarageDoorOpen(); string.response1 = textValue(); string header1 = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response1.Length.ToString() + "rnConnection: closernrn"; clientSocket.Send(Encoding.UTF8.GetBytes(header1), header1.Length, SocketFlags.None); clientSocket.Send(Encoding.UTF8.GetBytes(response1), response1.Length, SocketFlags.None); break; default: //Did not recognize command response = "Bad commando"; header = "HTTP/1.0 200 OKrnContent-Type: text; charset=utf-8rnContent-Length: " + response.Length.ToString() + "rnConnection: closernrn"; clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None); clientSocket.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None); break; } } } } } //tried to make this a public void, but that did not fix it. private void IsGarageDoorOpen() { const double maxVoltage = 3.3; const int maxAdcValue = 4095; SecretLabs.NETMF.Hardware.AnalogInput garageSensor = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A0); while (true) { int rawValue = garageSensor.Read(); double value = (rawValue * maxVoltage) / maxAdcValue; { String textValue = ""; { if (rawValue > 50) textValue = "Open"; else textValue = "Closed"; } Debug.Print(textValue); } Thread.Sleep(2500); } } #region IDisposable Members ~WebServer() { Dispose(); } public void Dispose() { if (socket != null) socket.Close(); } #endregion }}