Hi all,
I'm working through Cuno Pfister's book Getting Started with the Internet of Things. The sample codes in the book all work for me. So I decided to try to combine Example 11-1 (VoltageMonitor) and Example 12-1 (LedController): I wanted to write a program that would use different RequestRoutings via Yaler to both show me the current voltage of a potentiometer AND to allow me to turn the onboard LED on and off. Basically I inserted chunks of Example 11-1 into Example 12-1, made sure all references are intact, etc. It compiles and deploys fine, and switching the LED on and off (the PUT request) works when I access try.yaler.net/my-domain/led/target.html, but reading the voltage (the GET request) yields only a blank page when I access try.yaler.net/my-domain/voltage/actual.html.
Any ideas? I've pasted my code below (with Yaler credentials X'd out). Any help greatly appreciated. I'm very new to programming (2/10 at C#, 0/10 at JavaScript) so probably just making a simple mistake.
Thanks!!
using Gsiot.Server; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; public class LedControllerHtml { public static void Main() { var ledActuator = new DigitalActuator { OutputPin = Pins.ONBOARD_LED }; var lowPort = new OutputPort(Pins.GPIO_PIN_A0, false); var highPort = new OutputPort(Pins.GPIO_PIN_A2, true); var voltageSensor = new AnalogSensor { InputPin = Pins.GPIO_PIN_A1, MinValue = 0.0, MaxValue = 3.3 }; var webServer = new HttpServer { RelayDomain = "XXXXXXXX", RelaySecretKey = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX", RequestRouting = { { "PUT /led/target", new ManipulatedVariable { FromHttpRequest = CSharpRepresentation.TryDeserializeBool, ToActuator = ledActuator.HandlePut }.HandleRequest }, { "GET /led/target.html", HandleLedTargetHtml }, { "GET /voltage/actual.html", new MeasuredVariable { FromSensor = voltageSensor.HandleGet }.HandleRequest } } }; webServer.Run(); } static void HandleLedTargetHtml(RequestHandlerContext context) { string requestUri = context.BuildRequestUri("/led/target"); var script = @"<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', '" + requestUri + @"'); 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')""/> <input type=""button"" value=""Bah"" onclick=""put('bah')""/> </p> </body> </html>"; context.SetResponse(script, "text/html"); } }