Newb fails to combine PUT and GET code samples from Gsiot book - Netduino Plus 2 (and Netduino Plus 1) - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


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

Newb fails to combine PUT and GET code samples from Gsiot book


  • Please log in to reply
1 reply to this topic

#1 gyre

gyre

    New Member

  • Members
  • Pip
  • 1 posts

Posted 03 December 2013 - 02:12 AM

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");   } }  



#2 SeBo

SeBo

    New Member

  • Members
  • Pip
  • 1 posts

Posted 05 December 2013 - 07:37 PM

Hi gyre,

 

i´m new in the world of Netduino too, but for a litte project, i have to use this for controlling a power switch with a website.

For this project I use the Server from GSIOT like you.

 

I see in your code no html-code, which should be resonsed when the GET request "[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]"GET /voltage/actual.html" is recieved by the Netduino.[/color]

 

i cite a littel of my code, and i hope, you can see and it helps you to see what i mean,

 

First my Request handler:

static RequestRouting Routings = new RequestRouting{                    { "GET /", GetIndex },              // IP/ -> return index page            { "GET /index.*", GetIndex },       // IP/index.* -> return index page            { "GET /status.htm", GetStatus },   // IP/status.htm -> return status page            { "GET /edit.htm", GetEdit },       // IP/edit.htm -> return edit page            { "GET /links.htm", GetLinks },     // IP/links.htm -> return link page            { "GET /cprogram.htm", GetCode },   // IP/cprogram.htm -> return code of main program            { "GET /*", GetError },             // IP/* -> return error page            { "POST /edit.htm", GetEdit },      // IP/edit.htm with post data -> return edit page with modified settings            { "POST /index.*", GetIndex },      // IP/index.* with post data -> return index page with event handler            { "POST /*", GetError }             // IP/* with post data -> return error page        };            webServer.RequestRouting = Routings;

__

I use POST instate of PUT, because my Website on Netduino is only html, and the pages includes forms to send data over html.
__
If any Request is send, there will be voids to handel the events, for Index the code is:
 
 static void GetIndex(RequestHandlerContext context) {            try {                string[] SendSettings = context.RequestContent.Split('&'); // make a array of the post-data                foreach (string s in SendSettings) {                    switch (s.Split('=')[0]) {   // split post keyword and post value                        case "pw1": InOut.bTRIAC1 = !InOut.bTRIAC1; break; // switch a output on Netduino                        case "pw2": InOut.bTRIAC2 = !InOut.bTRIAC2; break;                        case "pw3": InOut.bTRIAC3 = !InOut.bTRIAC3; break;                        case "pw4": InOut.bTRIAC4 = !InOut.bTRIAC4; break;                        case "pw5": InOut.bTRIAC5 = !InOut.bTRIAC5; break;                        case "pw6": InOut.bTRIAC6 = !InOut.bTRIAC6; break;                        case "pw7": InOut.bTRIAC7 = !InOut.bTRIAC7; break;                        case "pw8": InOut.bTRIAC8 = !InOut.bTRIAC8; break;                    }                }            }            catch (Exception) { } // if no Post data are include like the first call of index.htm, the Exeption comes up[...] // some not nessesary code            context.SetResponse(SD_IO.ReadOut(@"htmlheader.htm") + SD_IO.ReadOut(@"htmlindex.htm") +                 sA[2] + sL[2] + returnFooter(), "text/html");  // Send HTML-Code back to Device which requestet the index page with Post or Get data.        }// and as short exampel from my code: (read just the htm-files from SD-Card and send them back to request starter.        static void GetLinks(RequestHandlerContext context) {            context.SetResponse(SD_IO.ReadOut(@"htmlheader.htm") + SD_IO.ReadOut(@"htmllinks.htm") + returnFooter(), "text/html");        }
___________________________________________________

So, in your case, i think you must change the code of from 

"GET /voltage/actual.html",   new MeasuredVariable  {   FromSensor = voltageSensor.HandleGet   }.HandleRequest

to

"GET /voltage/actual.html", HandleVoltageTargetHtml

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]and add a new codeblock:[/color]

   static void HandleVoltageTargetHtml(RequestHandlerContext context){	    string requestUri = context.BuildRequestUri("voltage/actual");	    var script =		    @"<html>			    <head>"Value of Voltagesensor (Sorry, i havent used it bevor, i dont now how to handel it)                  "</body>			 </html>";	    context.SetResponse(script, "text/html");    }}

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]I hope, that this is helpfull for you. [/color]

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]Greetings[/color]

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]SeBo.[/color]

 

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]Ps. I know my code will not be the best, but for me it works fine :)[/color]

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]If you like, i can try to upload my code when you have future questions.[/color]






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.