The release of 4.2.2 made the transferring of Greg's code easier for NOOBs. I've almost figured out this project, but I need to finish a few more pieces.
I can standup a standalone project with this code and it works for reading the output of a photo interrupter sensor (Photo Interrupter GP1A57HRJ00F https://www.sparkfun.com/products/9299).
namespace NetduinoApplication1{ public class Program { public static void Main() { const double maxVoltage = 3.3; const int maxAdcValue = 4096; // 10-bit Resolution (ADC) change to 4096 for ND+2 as it is 12-bit 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; Debug.Print(rawValue + " " + value.ToString("f")); Thread.Sleep(300); } } }}
The sensor debug:
1 0.00
1 0.00
1 0.00
2 0.00
1004 0.81
1004 0.81
1003 0.81
1005 0.81
1 0.00
1 0.00
2 0.00
1 0.00
1005 0.81
1003 0.81
1004 0.81
1004 0.81
- If the interrupter beam is blocked the reading is in the 1-3 range.
- If the interrupter beam is not blocked the reading in in the 1000 range.
- I am not sure what to do with the 0.00 or 0.81, since they are not needed.
- The main issue is that I need to convert these output ranges (~1-3 and ~1000-1004) from an integer to text (Open or Closed).
After converting them, this project requires pulling that text (Closed or Open) and placing it into the following line for the GET response. Right now, I have the text [color=rgb(178,34,34);]Closed [/color]manually populated and the an Droid app thinks the door is closed, so the green button on the app is active and the door can be opened or closed by pressing this button, but I have no idea if the door is really opened or closed.
Line 72-73 of solution:
string response1 = "Closed";
This pic is from when I manually entered Open, so the Droid Tablet thinks that the GET contains Open. Also notice that the red button is raised/enabled so I can click it to activate the garage door remote control.
I could be looking at this last piece wrong. Maybe I need to delete the string response1 line and insert the conversion somewhere else in this area (I bet there is more than one way to complete the task.)
case "checkdoor": IsGarageDoorOpen(); //Compose a response string response1 = "Closed"; //Manually entering Closed or Open makes the Droid app happy enough to activate the garage door bottom, the problem with this approach is that the actual state of the garage door is not known. 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;
So the challenge to convert the integer to text and then to populate the text in the GET response.
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;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); 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()) { case "activatedoor": ActivateGarageDoor(); //Compose a response string response = "I just opened or closed the garage!"; string 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; case "checkdoor": IsGarageDoorOpen(); //Compose a response string response1 = "Closed"; //Manually entering Closed or Open makes the Droid app happy enough to activate the garage door bottom, the problem with this approach is that the actual state of the garage door is not known. 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; } } } } } private void ActivateGarageDoor() { led.Write(true); //Light on-board LED for visual cue Garage2CarOpener.Write(true); //"Push" garage door button Thread.Sleep(1000); //For 1 second led.Write(false); //Turn off on-board LED Garage2CarOpener.Write(false); //Turn off garage door button } private void IsGarageDoorOpen() {//keep, this works for reading const double maxVoltage = 3.3;//keep, this works for reading const int maxAdcValue = 4096; // 10-bit Resolution (ADC) change to 4096 for ND+2 as it is 12-bit//keep, this works for reading //keep, this works for reading SecretLabs.NETMF.Hardware.AnalogInput garageSensor = new SecretLabs.NETMF.Hardware.AnalogInput(SecretLabs.NETMF.Hardware.NetduinoPlus.Pins.GPIO_PIN_A0);//keep, this works for reading //keep, this works for reading while (true)//keep, this works for reading {//keep, this works for reading int rawValue = garageSensor.Read();//keep, this works for reading double value = (rawValue * maxVoltage) / maxAdcValue;//keep, this works for reading Debug.Print(rawValue + " " + value.ToString("f"));//keep, this works for reading Thread.Sleep(300);//keep, this works for reading } } #region IDisposable Members ~WebServer() { Dispose(); } public void Dispose() { if (socket != null) socket.Close(); } #endregion }}
Dave VanderWekke, thanks for the AnalogInput code. http://forums.netdui...nput#entry47244 I used it as an update to Greg's original project.