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.

don664's Content

There have been 77 items by don664 (Search limited from 28-April 23)


By content type

See this member's


Sort by                Order  

#44019 WiFly FTP

Posted by don664 on 23 January 2013 - 02:43 PM in Netduino Plus 2 (and Netduino Plus 1)

It's just like uploading and downloading files from a website.

 

Of course!! This project is making me stupid!!

 

It doesn't look like the wifi SimpleSocket can handle HTTP file posts though  :mellow:




#44006 WiFly FTP

Posted by don664 on 23 January 2013 - 11:50 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi Dave,

 

I never knew you could use HTTP for file transfer; I'll look into this over wifi and report back any findings.

 

Thanks for the lead!




#44000 WiFly FTP

Posted by don664 on 23 January 2013 - 07:41 AM in Netduino Plus 2 (and Netduino Plus 1)

how about using the single connection as the upload only and have the ND FTP the files to a file server with another type of trigger (ie: Menu system on the ND, or Timer, or Schedule, etc.)?  I think I read somewhere you can do this with WiFly you just can't do an full FTP server implementation.

 

Hi Dave,

 

Any luck with the wifi FTP?




#43647 WiFly FTP

Posted by don664 on 18 January 2013 - 08:51 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi skobyjay,

 

and upload the file contents

 

 

The problem is I don't know how to get the wifi SimpleSocket to upload the file via FTP. I already have a routine which alternates between two files so that one file is the "live" file which is having the counts stored on it and the other is the "upload" file. Once the "upload" file has been uploaded it's emptied and becomes the "live" file and the old "live" file waits in the queue for it's turn to be uploaded.
 
The problem is getting the files FTP'd up to the server. And HTTP is not an option because of the memory leak issue I detailed here: http://forums.netdui...75-memory-leak/
 
And as I mentioned there could be as many as 60 counts a minute, which would mean 60 HTTP requests a minute and these machines run in 8 hour shifts... so that's 28 800 HTTP requests per machine and we're hoping to have about 20 machines using these Netduino counters. This is why I'd like to use FTP to get the data to the server and then simply have a script on the server process the text file.
 
Hi Eric,

 

According to the WiFly User Manual, the WiFly module supports FTP out of the box.  Is that not the case?

 

This is true and I have used CoolTerm to connect to the wifi module via a UartBee sheild and in CMD mode I have been able to contact the Roving Networks FTP server and download a firmware update for the RN-XV.

 

The problem is that I don't know how you'd go about accessing CMD mode with the wifi module on the netduino board or even if the two will even talk to each other then.

 

There has to be a way to use simpleSocket to upload a file via FTP? I have been able to successfully connect to my FTP server and create an empty text file on the server (using the code in my first post on this thread) so surely that's half the battle won?

 

I'm hoping that Dave is onto something and we can simply use the single connection as the data connection and have one way FTP. I have no need for a full FTP server implementation at this stage and simply want to get those data files off the netduino and onto the server.




#43605 WiFly FTP

Posted by don664 on 17 January 2013 - 02:23 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi Dave,

 

I'm trying to build a data logger which uses a photo sensor to count actions (I've already got this working as well as writing the count to an LCD).

 

Basically every time the sensor beam is broken I have an On_interrupt increase a counter variable by one and then write the count and datetime stamp to a new line in a text file on the SD card.

 

I was then going to have a timer upload that file to my server every 5 minutes(for example). Once the file is uploaded I'll send an HTTP request to a .NET page which will in turn read the text file (on the server) and write the count values into an SQL DB.

 

So simply:

 

Main function{    connect to wifi    wait for interrupt}On_interrupt{    increase counter by 1    write count/dateTime to the SD card text file    trigger 5 minute timer to start upload (if timer not already running)}Upload{    upload file to FTP server    empty file for new values    send HTTP request to read file on server    reset timer}

 

Because I'm only looking to send the file up to the FTP server (no download or anything else from the server) I thought that a single connection wold be fine? The Wifi isn't doing anything else at the same time.

 

And the reason we'd like to use wifi is so that we can place the sensor/netduino anywhere without having to worry about cabling back to a router. We'd like to use it in a factory on a press to count components as they come out of the machine.

 

Thanks for any help!

 

Regards,

Donovan

 

P.S. I did originally have it setup so that on the interrupt an HTTP request was sent with the count and datetime (as querystring variables) but this caused issues with timeouts and such and also was a problem if the wifi connection was dropped. The other issue is that, depending on the press tool which is running, the machine can churn out over 60 items a minute... that's FAR too many HTTP requests to try and send and would result in the whole thing falling over.




#43600 WiFly FTP

Posted by don664 on 17 January 2013 - 12:34 PM in Netduino Plus 2 (and Netduino Plus 1)

I suppose the reason I was able to see an empty test text file on my FTP server was a result of the CMD connection working fine but obviously without the second connection there's no way to move the SD card's file to the server.

 

This was one of my last hopes to get this project off the ground as using an HTTP request for my live data logger isn't ideal either (As detailed here: http://forums.netdui...75-memory-leak/).

 

I've been working on this project for months now and am out of ideas about how to make it work  :(

 

Thanks for taking a look guys!




#43579 WiFly FTP

Posted by don664 on 17 January 2013 - 06:38 AM in Netduino Plus 2 (and Netduino Plus 1)

nope... that didn't work  -_-

 

I guess the file is going to have to be broken up into binary and sent as bytes or something?




#43578 WiFly FTP

Posted by don664 on 17 January 2013 - 06:15 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi skobyjay,

 

Thanks for the suggestion... I didn't think it would be that simple so I'll give it a try and let you know if it works.

 

Cheers,

Donovan




#43532 WiFly FTP

Posted by don664 on 16 January 2013 - 03:00 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi there,

 

I have managed to modify Jair's (Thanks Jair :D ) FTP socket code (from here: http://forums.netdui...-client-socket/) so that I am now able to use it with a wifi connection (Netduino Plus 2 and RN-XV module).

 

The question I have is how to upload a file from the SD card? At the moment the code simply creates a blank .txt file on the FTP server, how do upload a whole file?

 

Code below:

 

using System;using System.IO;using System.Net;using System.Net.Sockets;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.IO;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using SecretLabs.NETMF.Hardware.NetduinoPlus;using SecretLabs.NETMF.IO;using Toolbox.NETMF.Hardware;using Toolbox.NETMF.NET;using System.Text;namespace ftpTest{    public class Program    {        static WiFlyGSX WifiModule;        static string strSSID = "mySSID";        static string strPassword = "myPassword";        static string remoteHost = "FTP Server IP address";        static string remoteUser = "userName";        static string remotePass = "password";        private static SimpleSocket clientSocket;                public static void Main()        {            // write your code here                        connectToWifi();            Thread.Sleep(10000);            clientSocket = new WiFlySocket(remoteHost, 21, WifiModule);                        try            {                SimpleSocket.SocketProtocol Protocol = SimpleSocket.SocketProtocol.TcpStream;                clientSocket.Connect(Protocol);            }            catch (Exception e)            {                throw new IOException("Couldn't connect to remote server" + e);            }            sendCommand("USER " + remoteUser);            sendCommand("PASS " + remotePass);                        sendCommand("TYPE I");            sendCommand("PASV");            sendCommand("CWD www");            sendCommand("CWD other");            sendCommand("STOR testFile.txt");                        close();            clientSocket.Close();        }        private static void sendCommand(String command)        {            Byte[] cmdBytes = Encoding.UTF8.GetBytes((command + "rn"));            clientSocket.SendBinary(cmdBytes);        }        private static void close()        {            if (clientSocket != null)            {                sendCommand("QUIT");            }        }        public static void connectToWifi()        {            WifiModule = new WiFlyGSX();            WifiModule.EnableDHCP();            WifiModule.JoinNetwork(strSSID, 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, strPassword);            Thread.Sleep(3500);            // Showing some interesting output            Debug.Print("Local IP: " + WifiModule.LocalIP);            Debug.Print("MAC address: " + WifiModule.MacAddress);        }    }}

 

 

Thanks,

Donovan




#41388 Wifi timeout

Posted by don664 on 11 December 2012 - 02:31 PM in Netduino Plus 2 (and Netduino Plus 1)

I can't find a WiFlyGSX.cs file in the netmftoolbox-20618 zip. I have re-installed both MicroFrameworkSDK_NETMF42_QFE2 and netduinosdk_32bit_NETMF42 wiothout any success. This issue started yesterday when I copied "\Toolbox-20618\Release (4.2)\Toolbox.NETMF.Hardware.WiFlyGSX.dll" from the zip over the existing Toolbox.NETMF.Hardware.WiFlyGSX.dll I was using. Stefan, you suggested in another post (http://forums.netdui...error-a3000000/) to delete the OBJ and BIN folders and I have deleted those from my project and still it won't work. Do I need to uninstall Visual C# 2010 Express and start again?



#41529 Wifi timeout

Posted by don664 on 14 December 2012 - 06:06 AM in Netduino Plus 2 (and Netduino Plus 1)

Ok... so still no luck with this. Here's what else I have tried:

I put a try/catch around the "socket.connect()" in the hopes of finding out what's tripping it up.

socket = new WiFlySocket(strSocketHostname, 80, WifiModule);
Debug.Print("socket connect");
                try
                {
                    socket.Connect();
                }
                catch (System.ApplicationException errorCode)
                {
                    Debug.Print("Socket connect error: " + errorCode);
                }

The first time around it reveals a timeout as the problem:

Debug Output:

socket connect
A first chance exception of type 'System.ApplicationException' occurred in Toolbox.NETMF.Hardware.WiFlyGSX.dll
Socket connect error: System.ApplicationException: Connection timed out


The very next time it simply hangs at the connect, even the try/catch doesn't help:

Debug Output:

socket connect

....then nothing


Am I missing something obvious or is there a problem with the Toolbox.NETMF.Hardware.WiFlyGSX.dll and it's locking the shield after a timeout?

I have tried resetting the wifly shield by power cycling it(using Arron Chapman's PowerManagement.cs) and re-joining the wifi network but it still hangs on the next socket.connect().

I create a new socket each time (socket = new WiFlySocket(strSocketHostname, 80, WifiModule);) as well so surely that should reset it after a timeout?

Any suggestions?

Thanks,
Donovan



#41390 Wifi timeout

Posted by don664 on 11 December 2012 - 03:26 PM in Netduino Plus 2 (and Netduino Plus 1)

...ok, so it seems to have something to do with these assemblies being compiled for 4.1 or 4.2 I have gone back to using "Toolbox.NETMF.Hardware.WiFlyGSX.dll" from "Toolbox-17446" and it's compiling again. Is there an error in the "Toolbox-20618" release?



#41394 Wifi timeout

Posted by don664 on 11 December 2012 - 04:55 PM in Netduino Plus 2 (and Netduino Plus 1)

I don't know why you use:

public static WiFlyGSX connectToWifi()
private static SimpleSocket ConnectSocket()
Instead of:
public static void connectToWifi()
private static void ConnectSocket()

Since it's not the right type for normal code.

Do you currently get an error message by the way?


Hi Stefan,

Now that I can compile again I made these changes and my little application has not timed out. I have been running it for about an hour now and it's not frozen. I will run more tests in the morning and then tidy up the code and post for others.

Thank you very much for your help :D

Cheers,
Donovan



#41478 Wifi timeout

Posted by don664 on 13 December 2012 - 02:53 PM in Netduino Plus 2 (and Netduino Plus 1)

Have just upgraded my Firmware from 4.2.0.3 to 4.2.1.1 and it hasn't helped :(

Long shot I know... but I thought worth a try ;)



#41366 Wifi timeout

Posted by don664 on 11 December 2012 - 07:04 AM in Netduino Plus 2 (and Netduino Plus 1)

Now I'm back to this error when trying to debug :(

Link failure: some assembly references cannot be resolved!!


Assembly: Toolbox.NETMF (0.1.0.0) needs assembly 'mscorlib' (4.1.2821.0)

Assembly: Toolbox.NETMF (0.1.0.0) needs assembly 'Microsoft.SPOT.Hardware' (4.1.2821.0)

Assembly: wiflyTimeout (1.0.0.0) needs assembly 'Toolbox.NETMF.Hardware.WiFlyGSX' (1.0.0.0)

Assembly: wiflyTimeout (1.0.0.0) needs assembly 'Toolbox.NETMF.NET' (1.0.0.0)

Assembly: Toolbox.NETMF.Hardware.WiFlyGSX (1.0.0.0) needs assembly 'Toolbox.NETMF.NET' (1.0.0.0)

Assembly: Toolbox.NETMF.Hardware.WiFlyGSX (1.0.0.0) needs assembly 'Toolbox.NETMF' (0.1.0.0)

Assembly: Toolbox.NETMF.NET (1.0.0.0) needs assembly 'Toolbox.NETMF' (0.1.0.0)

Error: a3000000

Waiting for debug commands...

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


Where have those assemblies gone?



#41538 Wifi timeout

Posted by don664 on 14 December 2012 - 08:48 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi guys,

Thanks for taking a look...

I thought maybe it was my wifi network and have tried on a different network and it runs perfectly until the first timeout then can't connect to the socket again.

All I end up with is the little green LED (D1) blinking on the RN-XV and the PWR red LED blinking on the wifi shield.

:(



#41330 Wifi timeout

Posted by don664 on 10 December 2012 - 07:11 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi Stefan, Still a newbie so bound to mix some things up ;-) Thanks for taking a look, I'll make those amends in the morning and try again. As always thanks for the help! Donovan



#41316 Wifi timeout

Posted by don664 on 10 December 2012 - 02:04 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi guys,

I'm still working with the power management and even after "restarting" the wifly shield it's still unable to connect to the socket again.

It has no problem rejoining the wifi network but something is stopping it from connecting to the socket, is this something to do with a TX port or something on the N+2 itself... so even though the wifly has restarted it's the board that's hung up?


using System;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using Toolbox.NETMF;
using Toolbox.NETMF.NET;
using Toolbox.NETMF.Hardware;
using VariableLabs.PowerManagment;

namespace liveCountBeta003
{
    public class Program
    {
        public static string strSSID = "mySSID";
        public static string strPassword = "myPassword";
        public static string strSocketHostname = "myURL.com";
        
        const int updateInterval = 3000; 

        static WiFlyGSX WifiModule;

        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        public static void Main()
        {            
            Debug.Print("Turn off the Ethernet controller");
            //Turn off the Ethernet controller
            PowerManagment.SetPeripheralState(Peripheral.Ethernet, false);

            connectToWifi();
            
            while (true)
            {
                delayLoop(updateInterval);
                updateThingSpeak("deviceID=22664");                
            }
        }

        static void updateThingSpeak(string tsData)
        {
            Debug.Print("Connecting to server...\n");
            led.Write(true);

            String request = "POST /default.aspx?" + tsData + " HTTP/1.1\n";
            request += "Host: " + strSocketHostname + "\n";
            request += "Connection: Close\n";
            request += "Content-Type: application/x-www-form-urlencoded\n";
            request += "Content-Length: " + tsData.Length + "\n\n";
            Debug.Print(request);
            try
            {
                String tsReply = sendPOST(request);
                Debug.Print(tsReply);
                Debug.Print("...disconnected.\n");
                led.Write(false);
            }
            catch (SocketException se)
            {
                Debug.Print("Connection Failed.\n");
                Debug.Print("Socket Error Code: " + se.ErrorCode.ToString());
                Debug.Print(se.ToString());
                Debug.Print("\n");
                led.Write(false);
                connectToWifi();
                ConnectSocket();
                String tsReply = sendPOST(request);
                Debug.Print(tsReply);
                Debug.Print("...disconnected.\n");
                led.Write(false);
            }
        }

        private static String sendPOST(String request)
        {
            SimpleSocket serverSocket = ConnectSocket();

            while(serverSocket.IsConnected)
            {
                serverSocket.Send(request);                
            }
            Byte[] buffer = new Byte[1024];
            String page = String.Empty;
            page = page + new String(Encoding.UTF8.GetChars(buffer));
            return page;
        }

        public static WiFlyGSX connectToWifi()
        {
            try
            {
                //Turn off the wifly sheild
                Debug.Print("Turn off the wifly sheild");
                PowerManagment.SetPeripheralState(Peripheral.PowerHeaders, false);
                Debug.Print("waiting..");

                Thread.Sleep(10000);

                //Turn on the wifly sheild
                Debug.Print("Turn on the wifly sheild");
                PowerManagment.SetPeripheralState(Peripheral.PowerHeaders, true);
                // wait for power cycle to complete
                Thread.Sleep(6500);

                Debug.Print("Create new instance");
                WifiModule = new WiFlyGSX();
                // wait for wifly to start
                Thread.Sleep(4000);

                Debug.Print("EnableDHCP");
                WifiModule.EnableDHCP();

                Debug.Print("Join network");
                WifiModule.JoinNetwork(strSSID, 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, strPassword);
                // wait for DHCP
                Thread.Sleep(2500);

                // Get the Local IP to confirm connection to the wifi network
                string strLocalIP;
                strLocalIP = WifiModule.LocalIP;

                Debug.Print("wifi connected; IP address: " + strLocalIP);
            }

            catch (System.InvalidOperationException e)
            
            {
                Debug.Print("System.InvalidOperationException: " + e.StackTrace.ToString());
            }
            catch
            {
                Debug.Print("can't access wifi");
            }

            return WifiModule;
        }

        private static SimpleSocket ConnectSocket()
        {
            SimpleSocket socket = new WiFlySocket(strSocketHostname, 80, WifiModule);
            try
            {
                socket.Connect();
            }
            catch (System.ApplicationException e)
            {
                Debug.Print("************************************************************");
                Debug.Print("Code: " + e);
                Debug.Print("************************************************************");

                Debug.Print("module dispose");
                WifiModule.Dispose();
                if (socket.IsConnected)
                {
                    Debug.Print("close socket");
                    socket.Close();
                }
                Debug.Print("connect to wifi()");
                connectToWifi();
                Debug.Print("socketConnect()");
                socket.Connect();
            }
            return socket;
        }

        static void delayLoop(int interval)
        {
            long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            int offset = (int)(now % interval);
            int delay = interval - offset;
            Thread.Sleep(delay);
        }
    }
}



Any ideas?

Thanks,
Donovan



#41315 Wifi timeout

Posted by don664 on 10 December 2012 - 01:14 PM in Netduino Plus 2 (and Netduino Plus 1)

I think I'm going to start again... ...will let you know how it goes :)



#41313 Wifi timeout

Posted by don664 on 10 December 2012 - 12:31 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi Stefan,

I've replaced "Toolbox.NETMF.Hardware.WiFlyGSX.dll" with the latest downloaded version and now my application won't deploy.

I just took the new one from the zip file and overwrote the old one; is that the wrong way to do it?

All I get is this and then the "output" window closes and the debug stops running:

Assembly: wiflyTimeout (1.0.0.0) needs assembly 'Toolbox.NETMF.NET' (1.0.0.0)

Assembly: Toolbox.NETMF.Hardware.WiFlyGSX (1.0.0.0) needs assembly 'Toolbox.NETMF.NET' (1.0.0.0)

Assembly: Toolbox.NETMF.Hardware.WiFlyGSX (1.0.0.0) needs assembly 'Toolbox.NETMF' (0.1.0.0)

Assembly: Toolbox.NETMF.NET (1.0.0.0) needs assembly 'Toolbox.NETMF' (0.1.0.0)

Error: a3000000

Waiting for debug commands...

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


Thanks,
Donovan



#41302 Wifi timeout

Posted by don664 on 10 December 2012 - 05:34 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi Chuck, Thanks for the response. I'll take a look and post my feedback. Is this a problem with the RN-XV module or the wiflyGSX.dll that requires a power reset when it times out? Thanks, Donovan



#41175 Wifi timeout

Posted by don664 on 07 December 2012 - 07:38 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi Chris ,

It's hooked up via the Wifi sheild.

Posted Image

Is there a way to cycle the power to the wifly without having to physically use the switch? Is there really no other way to reset the wifly once it hangs?

Thanks,
Donovan



#41143 Wifi timeout

Posted by don664 on 07 December 2012 - 02:42 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi there,

I am creating a data logger which uses a photo sensor, a Sparkfun RN-XV WifiModule and a netduino Plus 2.

Basically what happens is that when the photo sensor is interrupted it initiates an HTTP Post to a server which then logs the activity to a database via a querystring.

The problem I am having is that if there is a connection timeout the WifiModule simply locks up and I don't know how to reset it to try the request again.

To try and solve this timeout issue I modified an example from ThingSpeak.com; without the photo sensor and created a loop which sends the HTTP request.

I can sometimes send 100 requests without a timeout and then sometimes only about 20... either way once the connection times out the whole application stops.

How should I handle the timeout and how do I reset the WifiModule to try again?

using System;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using Toolbox.NETMF;
using Toolbox.NETMF.NET;
using Toolbox.NETMF.Hardware;

namespace timeoutTest
{
    public class Program
    {
        const int updateInterval = 3000; 

        static WiFlyGSX WifiModule;

        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        public static void Main()
        {

            WifiModule = new WiFlyGSX();
            // wait for wifly to start
            Thread.Sleep(2000);

            WifiModule.EnableDHCP();
            WifiModule.JoinNetwork("mySSID", 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, "myPassword");

            // Get the Local IP to confirm connection to the wifi network
            string strLocalIP;
            strLocalIP = WifiModule.LocalIP;

            Debug.Print(strLocalIP);


            while (true)
            {
                delayLoop(updateInterval);
                updateThingSpeak("variable=664");                
            }
        }

        static void updateThingSpeak(string tsData)
        {
            Debug.Print("Connecting to server...\n");
            led.Write(true);

            String request = "POST /default.aspx?" + tsData + " HTTP/1.1\n";
            request += "Host: some.hostname.here.com\n";
            request += "Connection: Close\n";
            request += "Content-Type: application/x-www-form-urlencoded\n";
            request += "Content-Length: " + tsData.Length + "\n\n";
            Debug.Print(request);
            try
            {
                String tsReply = sendPOST(request);
                Debug.Print(tsReply);
                Debug.Print("...disconnected.\n");
                led.Write(false);
            }
            catch (SocketException se)
            {
                Debug.Print("Connection Failed.\n");
                Debug.Print("Socket Error Code: " + se.ErrorCode.ToString());
                Debug.Print(se.ToString());
                Debug.Print("\n");
                led.Write(false);
            }
        }

        private static String sendPOST(String request)
        {
            SimpleSocket serverSocket = ConnectSocket();

            while(serverSocket.IsConnected)
            {
                serverSocket.Send(request);                
            }
            Byte[] buffer = new Byte[1024];
            String page = String.Empty;
            page = page + new String(Encoding.UTF8.GetChars(buffer));
            return page;
        }

        private static SimpleSocket ConnectSocket()
        {
            SimpleSocket socket = new WiFlySocket("some.hostname.here.com", 80, WifiModule);
            try
            {
                socket.Connect();
            }
            catch (System.ApplicationException e)
            {
                Debug.Print("Code: " + e);
                ConnectSocket();
            }
            return socket;
        }

        static void delayLoop(int interval)
        {
            long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            int offset = (int)(now % interval);
            int delay = interval - offset;
            Thread.Sleep(delay);
        }
    }
}

Thanks,
Donovan



#41421 Wifi timeout

Posted by don664 on 12 December 2012 - 08:17 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi Stefan,

So it seems the reason it never timed out last night was because I'd forgotten to include the HTTP request; so it was just connecting to the socket, closing that connection and then re-opening it again in a loop... no chance of that timing out ;)

This morning I have added in the HTTP request and setup a catch for the timeout and it's working well.

The application loops through and sends the querystring to my aspx page, which then adds a record to my database. To simulate the counter I have just created a variable called intCount which increases by 1 after each successful HTTP request.

When a timeout occurs it is caught by the "catch" and the wifi module is reset and it attempts the HTTP request again. It does this successfully but then freezes on "socket.connect()" the next time around.

I thought maybe it was something to do with the N+2 running out of memory (because of the loop running in the background while the wifi reset is happening) but that doesn't seem to be the case.

Any ideas?

Here's the code:

using System;
using System.Collections;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading;
using System.IO;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;
using Toolbox.NETMF;
using Toolbox.NETMF.NET;
using Toolbox.NETMF.Hardware;
using VariableLabs.PowerManagment;

namespace liveCountBeta003
{
    public class Program
    {
        public static string strSSID = "mySSID";
        public static string strPassword = "myPassword";
        public static string strSocketHostname = "my.url.com";
        public static uint intCount = 0;

        const int updateInterval = 3000;

        static WiFlyGSX WifiModule;
        static SimpleSocket socket;

        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);

        public static void Main()
        {
            //Turn off the Ethernet controller
            Debug.Print("Turn off the Ethernet controller");
            PowerManagment.SetPeripheralState(Peripheral.Ethernet, false);

            //Connect to wifi network
            Debug.Print("connect to Wifi");
            connectToWifi();

            while (true)
            {
                delayLoop(updateInterval);
                updateDatabase("deviceID=6640&count=" + intCount);
            }
        }

        static void updateDatabase(string tsData)
        {
            Debug.Print("start updateDatabase - freemem: " + Debug.GC(true) + "\n");
            led.Write(true);
            
            String request = "GET /default.aspx?" + tsData + " HTTP/1.1\r\n";
            request += "Host: " + strSocketHostname + "\r\n";
            request += "Connection: Close\r\n";
            request += "\r\n";

            sendHTTP_Request(request);
            led.Write(false);           
        }

        private static void sendHTTP_Request(String request)
        {
             try
             {
                socket = new WiFlySocket(strSocketHostname, 80, WifiModule);
                Debug.Print("IsConnected: " + socket.IsConnected); 
                Debug.Print("socket connect");
                socket.Connect();

                while (socket.IsConnected)
                {
                    Debug.Print("Send request: " + intCount);
                    socket.Send(request);
                    Debug.Print("close socket");             
                    socket.Close();
                    Debug.Print("increase counter by 1");
                    intCount++;
                    Debug.Print("send complete\n");
                }
            }

             catch (System.ApplicationException e)
             {
                 Debug.Print("************************************************************");
                 Debug.Print("Code: " + e);
                 Debug.Print("************************************************************");
                 
                 Debug.Print("reset wifi and reconnect");

                 WifiModule.Dispose();
                 connectToWifi();

                 Debug.Print("try socket connect - after reconnecting wifi");

                 socket = new WiFlySocket(strSocketHostname, 80, WifiModule);
                 socket.Connect();

                 while (socket.IsConnected)
                 {
                     Debug.Print("Send request: " + intCount);
                     socket.Send(request);
                     Debug.Print("close socket");
                     socket.Close();
                     Debug.Print("increase counter by 1");
                     intCount++;
                     Debug.Print("send complete\n");
                 }
             }

             Debug.Print("leave sendHTTP_Request()\n");

        }

        public static void connectToWifi()
        {
            try
            {
                Debug.Print("Create new instance");
                WifiModule = new WiFlyGSX();
                // wait for wifly to start
                Thread.Sleep(4000);

                Debug.Print("EnableDHCP");
                WifiModule.EnableDHCP();

                Debug.Print("Join network");
                WifiModule.JoinNetwork(strSSID, 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, strPassword);
                // wait for DHCP
                Thread.Sleep(2500);

                // Get the Local IP to confirm connection to the wifi network
                string strLocalIP;
                strLocalIP = WifiModule.LocalIP;

                Debug.Print("wifi connected; IP address: " + strLocalIP);
                Thread.Sleep(1500);
            }

            catch (System.InvalidOperationException e)
            {
                Debug.Print("System.InvalidOperationException: " + e.StackTrace.ToString());
            }

            catch
            {
                Debug.Print("can't access wifi");
            }
        }

        static void delayLoop(int interval)
        {
            long now = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            int offset = (int)(now % interval);
            int delay = interval - offset;
            Thread.Sleep(delay);
        }
    }
}


Here's the Debug output before and after the timeout:

start updateDatabase - freemem: 104640

IsConnected: False
socket connect
Send request: 115
close socket
increase counter by 1
send complete

leave sendHTTP_Request()

start updateDatabase - freemem: 104640

IsConnected: False
socket connect
A first chance exception of type 'System.ApplicationException' occurred in Toolbox.NETMF.Hardware.WiFlyGSX.dll
************************************************************
Code: System.ApplicationException: Connection timed out
************************************************************
reset wifi and reconnect
Create new instance
EnableDHCP
Join network
wifi connected; IP address: 192.168.0.9
try socket connect - after reconnecting wifi
Send request: 116
close socket
increase counter by 1
send complete

leave sendHTTP_Request()

start updateDatabase - freemem: 100596

IsConnected: False
socket connect

*** it just hangs here now ***


Thanks,
Donovan



#41473 Wifi timeout

Posted by don664 on 13 December 2012 - 01:51 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi guys, Does anyone have any ideas about why the wifly module would hang after a timeout? I've tried restarting the module after the timeout as well as calling the send request from an array, instead of the loop, and none of it seems to help. Today the application gets to the timeout, restarts the wifi and then does about another two sends before it hangs at the socket.connect... ...so I'm not sure what to try next?? Thanks, Donovan




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.