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

NeonMika.Webserver

webserver web server neonmika internet web server http sd xml json

  • Please log in to reply
213 replies to this topic

#121 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 09 January 2013 - 06:07 PM

I'm working on getting the POST code functional right now, and it's coming down to how the available bytes are collected.

In the WaitingForRequest() method, it calculates the available bytes in a loop according to the socket.  The problem is that POST may be multi-part, so the available size should be pulled from the Content-Length header field instead of from the loop currently being used.

 

The other thing I'm seeing from that section of code is a lot of times the byte count is declared as 0, when the actual available bytes is much larger.

If(when) I figure it out, I'll update here.



#122 Joshua

Joshua

    Member

  • Members
  • PipPip
  • 20 posts

Posted 09 January 2013 - 06:12 PM

I'm working on getting the POST code functional right now, and it's coming down to how the available bytes are collected.

In the WaitingForRequest() method, it calculates the available bytes in a loop according to the socket.  The problem is that POST may be multi-part, so the available size should be pulled from the Content-Length header field instead of from the loop currently being used.

 

The other thing I'm seeing from that section of code is a lot of times the byte count is declared as 0, when the actual available bytes is much larger.

If(when) I figure it out, I'll update here.

I found a fix for that.  I changed the loop that listens for the incoming request.  Here's what i'm using.. It works a lot better.  and seems to get the full request.. but still no content from the post.

 

                    using (Socket clientSocket = _ListeningSocket.Accept())                    {                        Debug.Print("Client connected");                        int availableBytes = 0;                        int LoopCount = 0;                        Thread.Sleep(100);                        //if not all incoming bytes were received by the socket                        do                        {                            if (availableBytes < clientSocket.Available)                            {                                availableBytes = clientSocket.Available;                                LoopCount = 0;                            }                            else                                LoopCount += 1;                            Thread.Sleep(1);                        } while (availableBytes == 0 || LoopCount < 300);

 

And in the line below that

 

if (availableBytes < Settings.MAX_REQUESTSIZE && availableBytes > 0)


#123 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 09 January 2013 - 06:38 PM

Thanks Josh, I'll give that a shot.

When you're hitting the 'save' button on the front end, do you see the json in the raw byte buffer? Or does that not come through?

 

I just tried it and I'm seeing the clientSocket.Available listing at 998 bytes, but the availableBytes is coming in at 548.  My guess is that the FormData is not being pulled along with the header.  performing the POST with jquery is setting the content-type to x-www-form-urlencoded, which I believe means it's multi-part ( I could be wrong, going by fuzzy memory).

 

So I'm guessing that the tempRequest contains the header information, but we'd need to pull more data from clientSocket to get the actual json.  So maybe playing with the RequestReceivedEventArgs will help as it's still got access to the Socket, while tempRequest doesn't.



#124 Joshua

Joshua

    Member

  • Members
  • PipPip
  • 20 posts

Posted 09 January 2013 - 06:40 PM

Thanks Josh, I'll give that a shot.

When you're hitting the 'save' button on the front end, do you see the json in the raw byte buffer? Or does that not come through?

it never comes through the raw byte buffer.. but if you inspect element in chrome.. it says it sent it.  It's odd.. kinda feels like were missing something in the rfc.



#125 Joshua

Joshua

    Member

  • Members
  • PipPip
  • 20 posts

Posted 09 January 2013 - 06:59 PM

So I'm guessing that the tempRequest contains the header information, but we'd need to pull more data from clientSocket to get the actual json.  So maybe playing with the RequestReceivedEventArgs will help as it's still got access to the Socket, while tempRequest doesn't.

What i did was read the entire available bytes until the client stopped sending for more than 300ms and then printed the string it received.  All that was in the string was the header.. I did this right after the clientSocket.receive().. so I don't think its actually getting the json data in that request.



#126 H07R0D

H07R0D

    Advanced Member

  • Members
  • PipPipPip
  • 95 posts

Posted 09 January 2013 - 07:05 PM

What i did was read the entire available bytes until the client stopped sending for more than 300ms and then printed the string it received.  All that was in the string was the header.. I did this right after the clientSocket.receive().. so I don't think its actually getting the json data in that request.

That will get the header for sure, but I think there will still be more data on the socket after the first receive because of the multipart.  I'm seeing all of the json being sent in firebug, so it's sitting somewhere ....just got to find it.

Maybe doing another receive from the socket after tempRequest is created.  I'm still seeing a difference between availableBytes and clientSocket.Available when tempRequest is being created.



#127 Joshua

Joshua

    Member

  • Members
  • PipPip
  • 20 posts

Posted 09 January 2013 - 07:11 PM

That will get the header for sure, but I think there will still be more data on the socket after the first receive because of the multipart.  I'm seeing all of the json being sent in firebug, so it's sitting somewhere ....just got to find it.

Maybe doing another receive from the socket after tempRequest is created.  I'm still seeing a difference between availableBytes and clientSocket.Available when tempRequest is being created.

Maybe.  Give it a shot.  Maybe when the client receives the ack, it sends the content?  If you look at the latest version of NeonMika from git, it has code to do this.. and I think that's one of the pieces of code I had used when I was testing it out.. it receives the header and then does another receive loop to get the rest of the content but it just locks up waiting to receive the amount of bytes up to the content length because it never gets them.



#128 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 13 January 2013 - 05:04 PM

Hey guys!

 

First of all: You are awesome! So much interest in this webserver and the community helping each other. Thanks!

Second: Special thanks to Dave! I will sit down NOW and will include your N+2 version.

 

I'm pretty busy at university and work (yeah I had holidays, but I didn't think of my N+ :P)

 

Regarding the missing connections: I didn't want to use the Thread.Sleep(100) because I wanted to keep to "as fast as possible".

But I think it is better that it works than it is light-fast :P

 

Without debugging through the code I can only tell that I had the following code working to upload data to the Netduino:

 

 

try		    {			    // Create a request using a URL that can receive a post.			    WebRequest request = WebRequest.Create(ServerTextBox.Text);			    // Set the Method property of the request to POST.			    request.Method = "POST";			    // Create POST data and convert it to a byte array.			    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);			    BinaryReader br = new BinaryReader(fs);			    byte[] byteArray = new byte[fs.Length];			    for (int i = 0; i < fs.Length; i++)				    byteArray[i] = br.ReadByte();			    // Set the ContentType property of the WebRequest.			    request.ContentType = "application/x-www-form-urlencoded";			    // Set the ContentLength property of the WebRequest.			    request.ContentLength = byteArray.Length;			    // Get the request stream.			    Stream dataStream = request.GetRequestStream();			    // Write the data to the request stream.			    dataStream.Write(byteArray, 0, byteArray.Length);			    // Close the Stream object.			    dataStream.Close();			    // Get the response.			    WebResponse response = request.GetResponse();			    // Display the status.			    System.Diagnostics.Debug.WriteLine(((HttpWebResponse)response).StatusDescription);			    // Get the stream containing content returned by the server.			    dataStream = response.GetResponseStream();			    // Open the stream using a StreamReader for easy access.			    StreamReader reader = new StreamReader(dataStream);			    // Read the content.			    string responseFromServer = reader.ReadToEnd();			    // Display the content.			    System.Diagnostics.Debug.WriteLine(responseFromServer);			    // Clean up the streams.			    reader.Close();			    dataStream.Close();			    response.Close();		    }		    catch (Exception ex)		    {			    MessageBox.Show(ex.ToString());		    }

I can imagine that a second (or even more) connection gets opened in the background. This only contains data or something like that. I don't exacly know how multi-part POST works. If I have a little more time, I will look into it, but at this point I can only provide this C# code.

 

Greats, Markus


NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#129 skobyjay

skobyjay

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 19 January 2013 - 09:23 PM

I've started with a clean version of NeonMika Webserver. I've made the appropriate changes to run on the Netduino Plus 2 and have deployed and tested it on the Netduino Plus 2.

You can download the modified project from this DropBox Link (sorry was too big to post here).

 

you're the man! thanks again Dave!



#130 visan

visan

    New Member

  • Members
  • Pip
  • 5 posts

Posted 21 January 2013 - 10:54 AM

-



#131 visan

visan

    New Member

  • Members
  • Pip
  • 5 posts

Posted 21 January 2013 - 10:54 AM

Hi Markus.

I am very new to this and I cannot think of a better way to learn about Netduino's networking capabilities and web-servers in general.

I succedded to de implement your web-server on a Netduino Plus 2.

Please help me with something: It responds very well in the LAN. But every time I try to access it from WAN (I forwarded a port to it) it seems like I can reach the web-server but there is no response from it (Error: "No data received. Unable to load the webpage because the server sent no data.)

 

Do you have any ideea?

 

Thank you very much for your work!

 

Bogdan



#132 Nevyn

Nevyn

    Advanced Member

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

Posted 21 January 2013 - 12:42 PM

I succedded to de implement your web-server on a Netduino Plus 2.

Please help me with something: It responds very well in the LAN. But every time I try to access it from WAN (I forwarded a port to it) it seems like I can reach the web-server but there is no response from it (Error: "No data received. Unable to load the webpage because the server sent no data.)

 

I had this problem when working with Markus's code and trying to get a web page from the N+2 using Chrome.  I found the small delay discussed in this post fixed the problem for me.

 

Regards,

Mark


To be or not to be = 0xFF

 

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

Follow @nevynuk on Twitter


#133 visan

visan

    New Member

  • Members
  • Pip
  • 5 posts

Posted 21 January 2013 - 08:53 PM

Mark, that single "Thread.Sleep(100)" solved my issue.

I can now access the Weserver using various browsers and I can acces it from WAN too.

Thank you for your help!

 

Bogdan



#134 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 22 January 2013 - 09:48 AM

Mark, that single "Thread.Sleep(100)" solved my issue.

I can now access the Weserver using various browsers and I can acces it from WAN too.

Thank you for your help!

 

Bogdan

 

Oops, I thougth I resolved this issue in the last version.

Will check it in again in the evening with the Thread.Sleep(100).

 

Hope you enjoy NeonMika.Webserver =)

Always wanted to see some project using it over WAN? Do you have a topic for your project or are you planning to open one?

 

Greets, Markus


NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#135 skobyjay

skobyjay

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 23 January 2013 - 02:47 AM

Does anyone have an example of sending and receiving Json? It looks like its implemented but I don't see any examples of how to fully implement.



#136 JerseyTechGuy

JerseyTechGuy

    Advanced Member

  • Members
  • PipPipPip
  • 870 posts

Posted 23 January 2013 - 11:47 AM

Does anyone have an example of sending and receiving Json? It looks like its implemented but I don't see any examples of how to fully implement.

 

Take a look at Coding Smackdown's Beerbrew Controller Project.  He uses a modified version of the NeonMika Webserver so it should be easy to understand if you've looked through the Webserver's code.  He is serving up a Webpage from the SD Card.  One of the good examples is how he is reading and saving the settings.

 

[color=#ff0000;]Edit:  The Github URL seems to be broken.  His blog article is at [/color][color=#ff0000;]http://diybrewery.com/2012/06/05/the-diy-brewery-temperature-logger-project/[/color][color=#ff0000;] and the code is on codeplex at [/color][color=#ff0000;]http://diybrewerytemplogger.codeplex.com/[/color]


Edited by Dave VanderWekke, 23 January 2013 - 11:53 AM.


#137 skobyjay

skobyjay

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 23 January 2013 - 12:44 PM

Thank Dave , I'll have a look this evening.

#138 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 23 January 2013 - 10:16 PM

Hey skobyjay,

 

Here is an example on how to SEND json:

 

 

	    /// <summary>	    /// Returns the responses added to the webserver	    /// </summary>	    /// <param name="e"></param>	    /// <param name="h"></param>	    /// <returns></returns>	    private void ResponseListJSON(Request e, JsonArray j)	    {		    JsonObject o;		    foreach ( Object k in _Responses.Keys )		    {			    if ( _Responses[k] as JSONResponse != null )			    {				    o = new JsonObject( );				    o.Add("methodURL", k);				    o.Add("methodInternalName", ( (Response)_Responses[k] ).URL);				    j.Add(o);			    }		    }	    }

As you can see, you just have to add some JsonObjects to the JsonArray j.

This JsonArray j will be sent to the client.

 

I you want to know a little more just have a look at JsonObject and JsonArray :)

 

On how to receive Json I cannot give you that much input, but I think it is not that hard to implement it.

You could work with some String.Split(), probably you also could use on of the frameworks mentioned here: http://www.json.org/

 

Greets, Markus

 

EDIT:

If it is enough to send some parameter to the webserver method, I would recommend you to send them in the URL.

You can use them this way:

 

 

if(e.GetArguments.Contains("pin")){   int pin = Convert.ToInt32(e.GetArguments["pin"]);   //Do something :P   //Add data to the JsonArray j}

NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#139 skobyjay

skobyjay

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 24 January 2013 - 01:44 AM

Cool Thanks. Markus. This should get me started.



#140 skobyjay

skobyjay

    Advanced Member

  • Members
  • PipPipPip
  • 53 posts

Posted 25 January 2013 - 12:46 AM

Okay, I have found that if I use NeonMika.WebServer.DLL everything launchs okay. If I use NeonMika.NETMF.Webserver.DLL I get the messages below and the project fails to start. What am I doing wrong or not doing? I have added my simple test project here.

 

http://betterbuiltco...MF) version.zip

 

 

 

Found debugger!



 

Create TS.



 

Loading start at 806a3f8, end 808450c



 

Assembly: mscorlib (4.2.0.0)



 

Assembly: Microsoft.SPOT.Native (4.2.0.0)



 

Assembly: Microsoft.SPOT.Hardware (4.2.0.0)



 

Assembly: Microsoft.SPOT.Net (4.2.0.0)



 

Assembly: System (4.2.0.0)



 

Assembly: Microsoft.SPOT.Hardware.SerialPort (4.2.0.0)



 

Assembly: Microsoft.SPOT.IO (4.2.0.0)



 

Assembly: System.IO (4.2.0.0)



 

Assembly: Microsoft.SPOT.Hardware.PWM (4.2.0.1)



 

Assembly: Microsoft.SPOT.Hardware.Usb (4.2.0.0)



 

Assembly: SecretLabs.NETMF.Diagnostics (4.2.0.0)



 

Assembly: SecretLabs.NETMF.Hardware.Netduino (4.2.1.0)



 

Assembly: Microsoft.SPOT.Hardware.OneWire (4.2.0.0)



 

Assembly: Microsoft.SPOT.Time (4.2.0.0)



 

Loading Deployment Assemblies.



 

Attaching deployed file.



 

Assembly: JSONLib (1.0.0.0)



 

Attaching deployed file.



 

Assembly: NeonMika.NETMF.Webserver (1.0.0.0)



 

Attaching deployed file.



 

Assembly: TestNeonMikaServer (1.0.0.0)



 

Attaching deployed file.



 

Assembly: SecretLabs.NETMF.Hardware (4.2.0.0)



 

Resolving.



 

Link failure: some assembly references cannot be resolved!!



 

?


 

Assembly: NeonMika.NETMF.Webserver (1.0.0.0) needs assembly 'Services' (1.0.0.0)



 

Assembly: TestNeonMikaServer (1.0.0.0) needs assembly 'NeonMika.NETMF.Webserver' (1.0.0.0)



 

Error: a3000000



 

Waiting for debug commands...



 

The program '[4] Micro Framework application: Managed' has exited with code 0 (0x0).


 







Also tagged with one or more of these keywords: webserver, web server, neonmika, internet, web, server, http, sd, xml, json

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.