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.

oofki's Content

There have been 11 items by oofki (Search limited from 19-April 23)


By content type

See this member's

Sort by                Order  

#17070 Problem With Basic Project

Posted by oofki on 25 August 2011 - 08:37 PM in Netduino Plus 2 (and Netduino Plus 1)

I'm using a static IP as well. Only thing I am left thinking is that it has something to do with the fact that we have 3 netduino's on the same LAN?
All different statically assigned IPs, all with their correct MACs set.



Whatttt!! How about using 4.2RC1? I think there was a GC bug in 4.1 from what I've read.



#17026 Problem With Basic Project

Posted by oofki on 24 August 2011 - 09:07 PM in Netduino Plus 2 (and Netduino Plus 1)

Running this exact code on the web server side, I get no more than 2 hours out, before becoming completely unresponsive.
I'm wandering if it's a RAM issue (I have ~30KB free at any time)


Hmm haven't checked mine in days until now and it still works :) I'm running 4.2RC1 all around. The only difference I can think of is I'm using a static ip, are you?



#16842 Problem With Basic Project

Posted by oofki on 19 August 2011 - 03:17 PM in Netduino Plus 2 (and Netduino Plus 1)

You got it! And yeah it's been running for days :)



#16824 Network Resetting Issue

Posted by oofki on 18 August 2011 - 08:44 PM in Netduino Plus 2 (and Netduino Plus 1)

Sure. It's basically:

main() { while(true) { Thread.sleep(5000); } }

I thought it was something to do with my code so simplified and simplified until I ended up with that. If you erase the app using mfdeploy so that there's no running program it still does the reset thing.

After reading this, http://forums.netdui...in-the-network/ it looks like I'm seeing the same issue - the link going up and down repeatedly.


I'm no expert, I'm just starting to learn too, but I think it's because you only have one thread and you are blocking it for 5 seconds at a time.

Try this: main() { while(true) { int i = 0; } }



#16801 Problem With Basic Project

Posted by oofki on 17 August 2011 - 08:47 PM in Netduino Plus 2 (and Netduino Plus 1)

oofki, do you mind posting the threaded code that is now working?


You got it!

Program.cs:
using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;
using System.IO.Ports;


namespace NetduinoPlusApplication1
{
    public class Program
    {
        public static OutputPort led;
        public static void Main()
        {

            led = new OutputPort(Pins.ONBOARD_LED, false);
             Listener webServer = new Listener(RequestReceived);
             webServer.StartListening();

        }
        private static void RequestReceived(Request request)
        {

            if (request.URL.IndexOf("START") > 0)
            {

                led.Write(true);
            }
            else if (request.URL.IndexOf("STOP") > 0)
            {
                led.Write(false);
            }      
  
            request.SendResponse("<html><body><p>Request from " + request.Client.ToString() + " received at " + DateTime.Now.ToString() + "</p><p>Method: " + request.Method + "<br />URL: " + request.URL +"</p></body></html>");
        }
    }
}

Listener.cs:
using System;
using Microsoft.SPOT;
using System.Net.Sockets;
using System.Net;
using System.Text;
using System.Threading;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;

namespace NetduinoPlusApplication1
{
    public delegate void RequestReceivedDelegate(Request request);

    public class Listener : IDisposable
    {
        const int maxRequestSize = 1024;
        readonly int portNumber = 80;

        private Socket listeningSocket = null;
        private RequestReceivedDelegate requestReceived;

        public Listener(RequestReceivedDelegate RequestReceived)
            : this(RequestReceived, 80) { }

        public Listener(RequestReceivedDelegate RequestReceived, int PortNumber)
        {
            portNumber = PortNumber;
            requestReceived = RequestReceived;
            listeningSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            listeningSocket.Bind(new IPEndPoint(IPAddress.Any, portNumber));
            listeningSocket.Listen(10);

            new Thread(StartListening).Start();

        }

        ~Listener()
        {
            Dispose();
        }

        public void StartListening()
        {

            while (true)
            {
                using (Socket clientSocket = listeningSocket.Accept())
                {
                    IPEndPoint clientIP = clientSocket.RemoteEndPoint as IPEndPoint;
                    Debug.Print("Received request from " + clientIP.ToString());
                    var x = clientSocket.RemoteEndPoint;

                    int availableBytes = clientSocket.Available;
                    Debug.Print(DateTime.Now.ToString() + " " + availableBytes.ToString() + " request bytes available");

                    int bytesReceived = (availableBytes > maxRequestSize ? maxRequestSize : availableBytes);
                    if (bytesReceived > 0)
                    {
                        byte[] buffer = new byte[bytesReceived]; // Buffer probably should be larger than this.
                        int readByteCount = clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);

                        using (Request r = new Request(clientSocket, Encoding.UTF8.GetChars(buffer)))
                        {
                            Debug.Print(DateTime.Now.ToString() + " " + r.URL);
                            if (requestReceived != null) requestReceived(r);

                        }
                    }
                    
                }

                // I always like to have this in a continuous loop. Helps prevent lock-ups
                Thread.Sleep(10);
            }

        }


        #region IDisposable Members

        public void Dispose()
        {
            if (listeningSocket != null) listeningSocket.Close();

        }

        #endregion
    }
}



Request.cs:
using System;
using Microsoft.SPOT;
using System.Net.Sockets;
using System.Text;
using System.Net;
using System.IO;

namespace NetduinoPlusApplication1

{
    /// <summary>
    /// Holds information about a web request
    /// </summary>
    /// <remarks>
    /// Will expand as required, but stay simple until needed.
    /// </remarks>
    public class Request : IDisposable
    {
        private string method;
        private string url;
        private Socket client;

        const int fileBufferSize = 256;

        internal Request(Socket Client, char[] Data)
        {
            client = Client;
            ProcessRequest(Data);
        }

        /// <summary>
        /// Request method
        /// </summary>
        public string Method
        {
            get { return method; }
        }

        /// <summary>
        /// URL of request
        /// </summary>
        public string URL
        {
            get { return url; }
        }

        /// <summary>
        /// Client IP address
        /// </summary>
        public IPAddress Client
        {
            get
            {
                IPEndPoint ip = client.RemoteEndPoint as IPEndPoint;
                if (ip != null) return ip.Address;
                return null;
            }
        }

        /// <summary>
        /// Send a response back to the client
        /// </summary>
        /// <param name="response"></param>
        public void SendResponse(string response, string type = "text/html")
        {
            if (client != null)
            {
                string header = "HTTP/1.0 200 OK\r\nContent-Type: " + type + "; charset=utf-8\r\nContent-Length: " + response.Length.ToString() + "\r\nConnection: close\r\n\r\n";

                client.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                client.Send(Encoding.UTF8.GetBytes(response), response.Length, SocketFlags.None);

                Debug.Print("Response of " + response.Length.ToString() + " sent.");
            }

        }

        /// <summary>
        /// Sends a file back to the client
        /// </summary>
        /// <remarks>
        /// Assumes the application using this has checked whether it exists
        /// </remarks>
        /// <param name="filePath"></param>
        public void SendFile(string filePath)
        {
            // Map the file extension to a mime type
            string type = "";
            int dot = filePath.LastIndexOf('.');
            if (dot != 0)
                switch (filePath.Substring(dot + 1))
                {
                    case "css":
                        type = "text/css";
                        break;
                    case "xml":
                    case "xsl":
                        type = "text/xml";
                        break;
                    case "jpg":
                    case "jpeg":
                        type = "image/jpeg";
                        break;
                    case "gif":
                        type = "image/gif";
                        break;
                    // Not exhaustive. Extend this list as required.
                }


            using (FileStream inputStream = new FileStream(filePath, FileMode.Open))
            {
                // Send the header
                string header = "HTTP/1.0 200 OK\r\nContent-Type: " + type + "; charset=utf-8\r\nContent-Length: " + inputStream.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                client.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);

                byte[] readBuffer = new byte[fileBufferSize];
                while (true)
                {
                    // Send the file a few bytes at a time
                    int bytesRead = inputStream.Read(readBuffer, 0, readBuffer.Length);
                    if (bytesRead == 0)
                        break;

                    client.Send(readBuffer, bytesRead, SocketFlags.None);
                    Debug.Print("Sending " + readBuffer.Length.ToString() + "bytes...");
                }
            }

            Debug.Print("Sent file " + filePath);
        }

        /// <summary>
        /// Send a Not Found response
        /// </summary>
        public void Send404()
        {
            string header = "HTTP/1.1 404 Not Found\r\nContent-Length: 0\r\nConnection: close\r\n\r\n";
            if (client != null)
                client.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
            Debug.Print("Sent 404 Not Found");
        }
        /// <summary>
        /// Process the request header
        /// </summary>
        /// <param name="data"></param>
        private void ProcessRequest(char[] data)
        {
            string content = new string(data);
            string firstLine = content.Substring(0, content.IndexOf('\n'));

            // Parse the first line of the request: "GET /path/ HTTP/1.1"
            string[] words = firstLine.Split(' ');
            method = words[0];
            url = words[1];

            // Could look for any further headers in other lines of the request if required (e.g. User-Agent, Cookie)
        }


        #region IDisposable Members

        public void Dispose()
        {
            if (client != null)
            {
                client.Close();
                client = null;
            }
        }

        #endregion
    }
}



#16800 Network Resetting Issue

Posted by oofki on 17 August 2011 - 08:44 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi,

I've been playing with N+ at home plugged into my ADSL router/modem (Billion 7800G) and it works just fine. However, today I took the board into work and had a strange issue when connected to our network.

Plugging the N+ into our network - which I believe is some flavour of a cisco gigabit switch - causes the network adapter on the N+ to appear to continually reset. By this I mean the three n/w leds turn on, one blinks a few times then all three turn off for a couple of seconds. This repeats.

Originally I was using HttpWebRequest to retrieve some data from a webserver on my PC. This code wass hanging on the GetResponse call. After simplifying the code, I've ended up with a main that only calls Thread.sleep in a loop. The network still appears to reset endlessly.

I'm using a static IP and firmware 4.2.

Curiously, about 1 in 100 resets, the board functions fine and I can ping it and make socket connections back to the server running on my PC. But for the other 99 times, the board is not pingable.


I've brought the board home again tonight, changed the static IP to one on my network and the board works fine. So I'm wondering is it something to do with the networking hardware at work?


Thanks



Can you post the code you are using?



#16728 Problem With Basic Project

Posted by oofki on 15 August 2011 - 01:45 AM in Netduino Plus 2 (and Netduino Plus 1)

The download for the manual is at: http://www.lg.com/co...ategoryIdCms=3# I *think* I can just use it as it, I will probably buy a break out if I can instead of a shield. If you see at the END, there is R232 stuff, and one it with a 3 pin connection...



#16724 Problem With Basic Project

Posted by oofki on 15 August 2011 - 12:00 AM in Netduino Plus 2 (and Netduino Plus 1)

I thing its do someting width buffer size.

http://forums.netduino.com/index.php?/topic/2081-socketreceive-issue-with-receive-buffer-512-bytes-on-netduino-plus-411-beta-1/page__p__14938__hl__web__fromsearch__1#entry14938


I was only sending it a VERY small amount of data so I don't think applies. But I did switch to a threaded model and it fixed my issue. I don't think anyone will know this, but my LG tv can use only 3 pins in RS232 (RX, TX, and GRND) and it says it is UART, can I just use pins d0 + d1 as com1 and go right in without a shield?



#16716 Problem With Basic Project

Posted by oofki on 14 August 2011 - 05:09 PM in Netduino Plus 2 (and Netduino Plus 1)

I will try that! I think that was from the example code. Just some other notes, it fails only about 1 out of 10 times when I have it attached to the debugger and I am using a static IP.


No luck. I took it out of the constructor and I get the same results. It fails 3/4 times or so, it just does not give back a response. I'd like to fix this before continuing forward with my project.



#16712 Problem With Basic Project

Posted by oofki on 14 August 2011 - 02:12 PM in Netduino Plus 2 (and Netduino Plus 1)

You are calling the ListenForRequest method twice.
Since that method halts forever by a while-true loop, I would not call within a constructor. Any constructor should contain a quick initialization code, without heavy work (and event firing).
The:

webServer.ListenForRequest();
pattern is correct.
Cheers


I will try that! I think that was from the example code. Just some other notes, it fails only about 1 out of 10 times when I have it attached to the debugger and I am using a static IP.



#16698 Problem With Basic Project

Posted by oofki on 14 August 2011 - 01:16 AM in Netduino Plus 2 (and Netduino Plus 1)

I used code that was in an example and modified it some to do some tests. It's just a basic web server. Well id say 2/3 requests I sent get back a 503 error. I've attached the code, it doesn't make sense to me. I tried changing to 4.2 as well and that did not fix it:

using System;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.NetduinoPlus;
using System.Text;


namespace NetduinoPlusApplication1
{
    public class Program
    {
        public static OutputPort led;
        public static WebServer webServer;
        public static void Main()
        {
             led = new OutputPort(Pins.ONBOARD_LED, false);
             webServer = new WebServer();
             webServer.ListenForRequest();
        }
        public class WebServer : IDisposable
        {
            private Socket socket = null;
            public WebServer()
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.Bind(new IPEndPoint(IPAddress.Any, 80));
                socket.Listen(10);
                ListenForRequest();
            }
            public void ListenForRequest()
            {
                while (true)
                {
                    using (Socket clientSocket = socket.Accept())
                    {
                        int bytesReceived = clientSocket.Available;
                        if (bytesReceived > 0)
                        {
                            byte[] buffer = new byte[bytesReceived];
                            clientSocket.Receive(buffer, bytesReceived, SocketFlags.None);
                            string request = new string(Encoding.UTF8.GetChars(buffer));
                            int start = request.IndexOf("cmd=");
                            string OMG = "";
                            if (start > 0)
                            {
                                    start += 4;
                                    int stop = request.IndexOf(" ", start);
                                    OMG = request.Substring(start, stop - start);
                            }     
                            switch(OMG)
                            {
                                case"START":
                                    led.Write(true);
                                    break;
                                case "STOP":
                                    led.Write(false);
                                    break;
                                default:
                                    break;
                            }
                            string header = "HTTP/1.0 200 OK\r\nContent-Type: text; charset=utf-8\r\nContent-Length: " + OMG.Length.ToString() + "\r\nConnection: close\r\n\r\n";
                            clientSocket.Send(Encoding.UTF8.GetBytes(header), header.Length, SocketFlags.None);
                            clientSocket.Send(Encoding.UTF8.GetBytes(OMG), OMG.Length, SocketFlags.None);
                        }
                    }
                }
            }
            #region IDisposable Members
            ~WebServer()
            {
                Dispose();
            }
            public void Dispose()
            {
                if (socket != null)
                    socket.Close();
            }
            #endregion
        }
    }
}




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.