N+2 and ASP.NET web api - 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

N+2 and ASP.NET web api


  • Please log in to reply
7 replies to this topic

#1 grey

grey

    Member

  • Members
  • PipPip
  • 12 posts

Posted 13 May 2013 - 01:11 PM

[font="calibri;"]Hello to all,[/font]

[font="calibri;"]I am new at netduino and need your support to realize my project.[/font]

[font="calibri;"]The challenge: I want to have a communication beetween netduino and a server (asp.net web api) in both directions. [/font]

 

[font="calibri;"]For example please find a script (attach files).[/font]

 

[font="calibri;"]Example to „SET“ (POST) data: send the temperatur of a sensor from the netduino by „http://server:port/api/temperature/22“[/font]

[font="calibri;"]Example to„GET“ data: get the status from a led number „458“ (true or false) by „http://server:port/api/led/458“.[/font]

 

[font="calibri;"]I know how I have to receive and return the data on the server for example by json but I don´t know how to send and receive data onto netduino. [/font]

[font="calibri;"]Could someone please give me an advice to do this in the right way and maybe post some code?[/font]

 

[font="calibri;"]Many thanky and greetings from Germany ;)[/font]

[font="calibri;"]grey[/font]

Attached Files



#2 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 13 May 2013 - 07:35 PM

The way i have done this in the past was to use as compact a serialisation as possible for the data you send to and from the ND.

 

In theroy you could use json and write a (very) minimal parser to run on the ND, but i found csv or other character delimited string format the easiest (and most effcient as json parsing requires lots of spits etc.) to work with.

 

Personally i would create lots of api actions on the server for each feature you wish the ND to perform and simply post or even get the data to the server using one of the many samples available on this forum for http comms.  As for dealing with responses i would just return a string (text/plain mime type), then split on your delimiter and parse out nd function to run and value.

 

I tend to use the Netmf tool box http client for all things HTTP

 

http://netmftoolbox....ailable classes

 

Hopefully the sample there will be enough to get you started.

 

Nak.



#3 grey

grey

    Member

  • Members
  • PipPip
  • 12 posts

Posted 14 May 2013 - 05:29 AM

Hi nak, thank you for the idea. It helps me a lot.

 

i try to realize it as followed:

HTTP_Client WebSession = new HTTP_Client(new IntegratedSocket("http://192.168.178.23", 80));// Requests the latest sourceHTTP_Client.HTTP_Response Response = WebSession.Get("/test/");// Did we get the expected response? (a "200 OK")if (Response.ResponseCode != 200)    throw new ApplicationException("Unexpected HTTP response code: " + Response.ResponseCode.ToString());// Fetches a response headerDebug.Print("Current date according to www.netmftoolbox.com: " + Response.ResponseHeader("date"));// Gets the response as a stringDebug.Print(Response.ToString());

But at the Get Method the Program returns the followed Exception:

 

"System.Net.Sockets.SocketException" ist in Microsoft.SPOT.Net.dll aufgetreten.

 

Strack Trace:

 

Microsoft.SPOT.Net.SocketNative::getaddrinfo
System.Net.Dns::GetHostEntry
Toolbox.NETMF.NET.IntegratedSocket::Connect
Toolbox.NETMF.NET.HTTP_Client::_DoRequest
Toolbox.NETMF.NET.HTTP_Client::Get
NetduinoApplication1.Program::Main

 

 

Do you have any ideas?

 

Grey



#4 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 14 May 2013 - 08:58 AM

Try using the machine name you are trying to connect to rather than IP address, IntegratedSocket does a DNS lookup to get the host IP address.

 

https://netmftoolbox...l/latest#354407

 

Have a look at the connection method.

 

The following should work:

new HTTP_Client(new IntegratedSocket("http://your-pc-name", 80))

Also ensure that port 80 is open on the firewall.

 

If that still doesnt work for you, i would create a new class which inherits from SimpleSocket and simply copy over the implementation of IntegratedSocket and modify the connect method to not do a DNS lookup and instead use an IP.

 

Nak.



#5 grey

grey

    Member

  • Members
  • PipPip
  • 12 posts

Posted 14 May 2013 - 12:41 PM

I tried it with my fritzbox ("http://fritz.box", 80). there is definitely port 80 open.

but same problem.

 

 

then i wrote my own connect method.

/// <summary>/// Connects to the remote host/// </summary>/// <param name="Protocol">The protocol to be used</param>public override void Connect(SocketProtocol Protocol = SocketProtocol.TcpStream){    // Creates a new socket object    if (Protocol == SocketProtocol.TcpStream)        this._Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);    else        this._Sock = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);    // Resolves the hostname to an IP address    //IPHostEntry address = Dns.GetHostEntry(this._Hostname);    IPAddress ipaddress = new IPAddress(new byte[] { 192, 168, 178, 23 });    // Creates the new IP end point    EndPoint Destination = new IPEndPoint(ipaddress, (int)this._Port);    // Connects to the socket    this._Sock.Connect(Destination);    this._Closed = false;}

but that did not work. 

it breaks at this line:

this._Sock.Connect(Destination);

then I tried to create a simple socket connection with connect and close.

 

 

but it braeks at the same line.

public static void Main()        {            Socket _Sock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);            EndPoint Destination = new IPEndPoint(new IPAddress(new byte[] { 192, 168, 178, 23 }), 80);            // Connects to the socket            _Sock.Connect(Destination);            _Sock.Close();                    }

port 80 is open at 192.168.178.23

 

 

grey



#6 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 14 May 2013 - 01:20 PM

have you tried not prefixing the address with http:// ?



#7 grey

grey

    Member

  • Members
  • PipPip
  • 12 posts

Posted 14 May 2013 - 02:00 PM

this solves the problem. thank you very much!!

it works without the prefix.

 

the problem with my own connect function was:

my mistake was the ip of the server. (i have a local webserver on my pc)

my fritzboy has only one ethernet port and i used this port to connect with my pc. when my pc is connected with cable the ip is [color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]192.168.178.23.[/color]

[color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]then i disconnected my pc (because i need this ethernet port for my netduino) and connected it with wlan to my network and the ip was [/color][color=rgb(40,40,40);font-family:helvetica, arial, sans-serif;]192.168.178.22.[/color]

 

 

when i use the ip address without the http prefix it also works great:

// wrongHTTP_Client WebSession = new HTTP_Client(new IntegratedSocket("http://192.168.178.23", 80));// rightHTTP_Client WebSession = new HTTP_Client(new IntegratedSocket("192.168.178.23", 80));

I realize I am a total beginner.

 

 

 

thank you!

 

grey



#8 nakchak

nakchak

    Advanced Member

  • Members
  • PipPipPip
  • 404 posts
  • LocationBristol, UK

Posted 14 May 2013 - 02:24 PM

Everyone has to start somewhere :)

 

Glad its working now.

 

You can give your PC a static IP if you wish it to remain the same regardless of whether you are connected to your LAN or not.






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.