Netduino home hardware projects downloads community

Jump to content


Photo

Silverlight Client and Server for Netduino Plus


  • Please log in to reply
20 replies to this topic

#1 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 915 posts
  • LocationNorth Yorkshire, UK

Posted 30 January 2011 - 06:11 PM

This (software only) project started out as a result of my experience hooking the N+ to a 16x2 LCD. At the time all I was trying to do was to get the temperature out of a TMP102 sensor. This got me thinking about using the network interface on the N+ to both feed data into the board and also get data out. This project is the result.

I have attached three zip files to this post in case you find this interesting. The first (Simple Web Server.zip) is a project which contains the code for the server which can be deployed on the N+. This is not complex and only serves a few file types, enough to generate a static web site using HTML, CSS, JavaScript, GIF and JPG's. It can also serve XAP files - that's where the Silverlight comes in. The code here is a hacked version of the Microsoft example in the SDK.

The second (SilverlightOnNetduino.zip) of the files contains a basic web project containing a web site for hosting a Silverlight application and the Silverlight application itself.

I split these into two projects in order to make debugging a little easier. There is a clientaccesspolicy.xml file in the web project to allow access to the N+ web server from a web site running on a PC - this allows debugging of the Silverlight code.

The final file (www.zip) contains the web site as deployed onto an SD card on the N+.

I decided to keep this simple in order to keep the size down. The system will serve few file types and uses a very primitive command mechanism. I decided to use a "virtual" file Command.html as the mechanism for detecting a command from the user. This takes commands and optional parameters from the QueryString. This of course places some restrictions on the types of data which can be passed but that's the price for simplicity. There is a benefit though, if you are only passing text data then you can simply point your browser at the N+ and issue commands using the URL.

For example, if your N+ is at IP 192.168.200.100 then the following will return valid HTML generated dynamically by the server application:

http://192.168.200.1...d.html?SayHello

Edited 1st Feb 2011: Updated the files as per posting below.

Hope you find this useful,
Mark

Attached Files


To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life


#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7118 posts
  • LocationNew York, NY

Posted 30 January 2011 - 06:24 PM

Nice! Do you have videos or screenshots of this in action, Mark? Chris

#3 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 915 posts
  • LocationNorth Yorkshire, UK

Posted 01 February 2011 - 07:59 PM

Nice! Do you have videos or screenshots of this in action, Mark?

Chris


I have updated the files in the original post. The files contain the following modifications:
  • The WebServer now returns a random "temperature" as though attached to a sensor (it's the number of milliseconds from the machine time divided by 100). This should allow some simulation of gathering data and returning it to a PC over the network.
  • The Silverlight application now uses MVVM to store and present the data to the user. The data is presented in a DataGrid - nothing too complex.
The following screen shot shows this:
Posted Image

Adding the Silverlight Toolkit charting controls allowed the data to be plotted:

Posted Image


Next step, hook it up to the TMP102.


Hope you find this useful,
Mark

EDIT: Updated links to images following server move.

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life


#4 Eivind

Eivind

    New Member

  • Members
  • Pip
  • 9 posts
  • LocationBodø, Norway

Posted 02 February 2011 - 10:28 PM

Very cool, and much the same type of project I'll be approaching - as soon as I get the hang of the basics :)

#5 kylin

kylin

    New Member

  • Members
  • Pip
  • 9 posts

Posted 10 June 2011 - 05:17 PM

There is a problem, I want to download the wmv file will fail, I try to add the code video / x-ms-wmv :rolleyes:

#6 kylin

kylin

    New Member

  • Members
  • Pip
  • 9 posts

Posted 11 June 2011 - 07:47 AM

The fileType Code add case "wmv": fileType = "video/x-ms-wmv"; break; Send(client, "HTTP/1.1 200 OK\r\nContent-Type: "+ fileType +"\r\nAccept-Ranges: bytes\r\nContent-Length: " + fs.Length.ToString() + "\r\n\r\n"); TEST OK

#7 Mark H

Mark H

    Advanced Member

  • Members
  • PipPipPip
  • 70 posts
  • LocationPerth, Western Australia

Posted 13 June 2011 - 07:30 AM

Nevyn, it appears you've deleted the images from your server? Do you have them hosted elsewhere?

#8 viktor

viktor

    Member

  • Members
  • PipPip
  • 14 posts

Posted 03 September 2011 - 01:28 AM

How do you go about adding clientaccesspolicy to n+? I want to be able to call it from a silverlight application on my pc, but that means i have to have clientaccesspolicy on the webserver end. I scanned your project but looks like you have it backwards, with silverlight on the n+. Thanks, Viktor.

#9 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 313 posts
  • LocationIndiana, USA

Posted 03 September 2011 - 01:08 PM

How do you go about adding clientaccesspolicy to n+? I want to be able to call it from a silverlight application on my pc, but that means i have to have clientaccesspolicy on the webserver end.


If the silverlight app is on the same server as the service it calls, I don't think you need a clientaccesspolicy.xml. Otherwise, you need to have a clientaccesspolicy.xml to be served up from the root of the web server.

-Valkyrie-MT

#10 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 915 posts
  • LocationNorth Yorkshire, UK

Posted 03 September 2011 - 04:33 PM

I scanned your project but looks like you have it backwards, with silverlight on the n+.

The Silverlight application is on the N+ in order to provide a self contained environment for the web server. The application never runs on the N+, it is simply served to the client and then exectuted within the clients web browser.

If you want the Silverlight application on your PC or on any internet server then you will need to look at implementing cross site policy. It should be easy to sort out putting the file on the N+ web server in the right place - a minor modification to the SendFile method should get the file out to the requester.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life


#11 viktor

viktor

    Member

  • Members
  • PipPip
  • 14 posts

Posted 09 September 2011 - 02:58 AM

The Silverlight application is on the N+ in order to provide a self contained environment for the web server. The application never runs on the N+, it is simply served to the client and then exectuted within the clients web browser.

If you want the Silverlight application on your PC or on any internet server then you will need to look at implementing cross site policy. It should be easy to sort out putting the file on the N+ web server in the right place - a minor modification to the SendFile method should get the file out to the requester.

Regards,
Mark


Thanks, I will have to take a look at SendFile method.

#12 seulater

seulater

    New Member

  • Members
  • Pip
  • 5 posts

Posted 11 November 2011 - 06:31 PM

Mark, i have scoured the net for hours with not luck until i saw your post here. I hope you would not mind shedding some light on things for me.

What i have done is created 2 listening sockets on the embedded board. One for port 943 and the other for 4503.
943 is the default port for requesting the policy file.
4503 is the socket to exchange data from the SL app to my board.

when i open my SL app it does open a connection to port 943 to my board and it does send it "<policy-file-request/>" to which i send back the policy file. But it still does not then open a socket connection on port 4503 to my board. In fact it never even fires the OnConnect method.

You are the only person i have found that is doing just about the same thing i want to do.
Also, i have done the same with Adobe Flex and that one works just fine. I just cannot get the Silver light to do the same.


I am just now learning Silverlight 4, so any advise would be greatly appreciated.

Here is my Silver light Code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Net.Sockets;
using System.Text;
using System.Threading;


namespace WebConnection
{

    public partial class MainPage : UserControl
    {
        public Socket socket;
        public DnsEndPoint endPoint;
        static AutoResetEvent autoEvent = new AutoResetEvent(false);

        public MainPage()
        {
            InitializeComponent();
            Debugwindow.Text = "Trying To Connect\n";

            try
            {

                endPoint = new DnsEndPoint("192.168.0.7", 4503);
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();
                args.SocketClientAccessPolicyProtocol = SocketClientAccessPolicyProtocol.Tcp;
                args.UserToken = socket;
                args.RemoteEndPoint = endPoint;
                args.Completed += new EventHandler<SocketAsyncEventArgs>(OnConnect);
                
                socket.ConnectAsync(args);

            }
            catch (Exception a)
            {
                Debugwindow.Text += "Exception\n";

                Debugwindow.Text += a.ToString();

            }

        }

        public void OnConnect(object sender, SocketAsyncEventArgs e)
        {

            if (e.SocketError != SocketError.Success)
            {
                // this is an error condition!
                // check what error happened and what it means:
                // http://msdn.microsoft.com/en-us/library/system.net.sockets.socketerror.aspx

                Debugwindow.Text += e.ToString();

                return;
            }

            byte[] response = new byte[255];
            e.SetBuffer(response, 0, response.Length);
            e.Completed -= new EventHandler<SocketAsyncEventArgs>(OnConnect);
            e.Completed += new EventHandler<SocketAsyncEventArgs>(OnReceive);
            socket.ReceiveAsync(e);
        }


        public void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            string s = Encoding.UTF8.GetString(e.Buffer, e.Offset, e.BytesTransferred);

            socket.ReceiveAsync(e);
        }

        public void sendbtn_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;

                SocketAsyncEventArgs args = new SocketAsyncEventArgs();

                string s = textBoxSend.Text;

                byte[] data = Encoding.UTF8.GetBytes(s);

                args.SetBuffer(data, 0, data.Length);
                socket.SendAsync(args);

            }
            catch (Exception a)
            {
                Debugwindow.Text += "Exception\n";

                Debugwindow.Text += a.ToString();

            }

        }
    }
}



#13 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 915 posts
  • LocationNorth Yorkshire, UK

Posted 13 November 2011 - 09:52 AM

What i have done is created 2 listening sockets on the embedded board. One for port 943 and the other for 4503.
943 is the default port for requesting the policy file.
4503 is the socket to exchange data from the SL app to my board.

when i open my SL app it does open a connection to port 943 to my board and it does send it "<policy-file-request/>" to which i send back the policy file. But it still does not then open a socket connection on port 4503 to my board. In fact it never even fires the OnConnect method.

If memory serves I had the same problem when I had the Silverlight application open in the debugger and I was trying to get the app to talk to the web server on the N+. The problem was that the policy file was not being served correctly or that the file was incorrect.

Are you using the web application attached to this thread?
Does the default web application as supplied here work?
How do you know the policy file is being served? I use Fiddler and Wire Shark to snoop on the traffic. I'd start with Fiddler first as it is easier to use but if that does not provide any useful information then I break out Wire Shark.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life


#14 seulater

seulater

    New Member

  • Members
  • Pip
  • 5 posts

Posted 13 November 2011 - 08:40 PM

If memory serves I had the same problem when I had the Silverlight application open in the debugger and I was trying to get the app to talk to the web server on the N+. The problem was that the policy file was not being served correctly or that the file was incorrect.

Are you using the web application attached to this thread?
Does the default web application as supplied here work?
How do you know the policy file is being served? I use Fiddler and Wire Shark to snoop on the traffic. I'd start with Fiddler first as it is easier to use but if that does not provide any useful information then I break out Wire Shark.

Regards,
Mark


I am using the SL application that i posted.
I am using wireshark and i see the request and i also see that my board is responding with it.

#15 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 915 posts
  • LocationNorth Yorkshire, UK

Posted 13 November 2011 - 09:06 PM

I am using the SL application that i posted.
I am using wireshark and i see the request and i also see that my board is responding with it.

Can you post the contents of the policy file?

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life


#16 seulater

seulater

    New Member

  • Members
  • Pip
  • 5 posts

Posted 13 November 2011 - 09:27 PM

Mark, Thanks for your time i got it working now. The deal was that when i wrote my website using Adobe Flex it needed the policy file terminated with /0 for it to work. Yet with Silverlight it will not work when its termed that way. I just assumed that they both work work the same silly me. So i removed the /0 off the end of the policy file and now it works just fine. Again, thank you for your time.

#17 durangoflyer

durangoflyer

    New Member

  • Members
  • Pip
  • 5 posts

Posted 10 February 2012 - 02:27 AM

Subscribed.

#18 Giuliano

Giuliano

    Advanced Member

  • Members
  • PipPipPip
  • 340 posts
  • LocationSimi Valley, CA

Posted 11 June 2012 - 04:50 PM

I am going to try this out as well. Great work Nevyn!

#19 poschi8

poschi8

    Member

  • Members
  • PipPip
  • 13 posts

Posted 10 August 2012 - 12:19 PM

Hello!

I am trying to modify your code, so that the onboard Led is going on when I am sending the gettemperature.

I added the following code in the ProcessComand Method in the Webserver Class:
case "gettemperature":
                        double temperature;
                        OutputPort LED = new OutputPort(Pins.ONBOARD_LED, true);  // This is the line i added
                        temperature = ((double) Utility.GetMachineTime().Milliseconds) / 100;
                        Send(client, "Temperature:" + temperature.ToString());
                        break;

When i go to the Url http://192.168.1.104...?gettemperature, it always stops at the line where the Led should be activated with the following exception: "System.Exception" in Microsoft.SPOT.Hardware.dll

Could you please help me with this error.

#20 Nevyn

Nevyn

    Advanced Member

  • Members
  • PipPipPip
  • 915 posts
  • LocationNorth Yorkshire, UK

Posted 10 August 2012 - 12:31 PM

OutputPort LED = new OutputPort(Pins.ONBOARD_LED, true);  // This is the line i added

When i go to the Url http://192.168.1.104...?gettemperature, it always stops at the line where the Led should be activated with the following exception: "System.Exception" in Microsoft.SPOT.Hardware.dll


The on board LED is already in use in the main program - see program.cs. You cannot have two input or output port objects referring to the same pin.

Regards,
Mark

To be or not to be = 0xFF

 

Blogging about Netduino, .NET, STM8S and STM32 and generally waffling on about life





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2010-2014 Secret Labs LLC  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.