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

Wifi timeout


Best Answer don664, 09 January 2013 - 06:56 AM

Hi guys,

 

Thanks for the advice.

 

After splitting into various threads (like one for connecting to the wifi and another for handling the data send), as well as now using the Toolbox.HTTP_Client reference, I seem to have resolved a timeout causing the Netduino/wifly module to hang  :D

 

I have been able to run a test with over 700 HTTP Get requests and although there were a number of timeouts none of them caused the Netduino to hang/crash!!

 

Only problem I have now though is that I run out of memory... and I can't understand why, but I think that's for another thread!

 

Please find my code below (please ignore all the "Debug.GC(true);" lines as it's part of my efforts to figure out the memory issue) , I hope it helps someone else. Thanks to everyone for their input!!

 

using System;using System.Collections;using System.IO;using System.Net;using System.Net.Sockets;using System.Text;using System.Threading;using Microsoft.SPOT;using Microsoft.SPOT.Hardware;using SecretLabs.NETMF.Hardware;using SecretLabs.NETMF.Hardware.Netduino;using Toolbox.NETMF.Hardware;using Toolbox.NETMF.NET;namespace testToolbox21240{    public class Program    {        static uint intCounter = 0;        static uint intTimeoutCounter = 0;        static WiFlyGSX WifiModule;        static string strWriteFile = "dataone";        static string strReadFile = "datatwo";        static ArrayList counterDataArray = new ArrayList();        public static void Main()        {            connectToWifi();            populateArray();            //httpSendData();                                    Thread.Sleep(Timeout.Infinite);        }                public static void populateArray()        {            Debug.Print("Populate array from: " + strReadFile);            using (var filestream = new FileStream(@"SD" + strReadFile + ".txt", FileMode.Open))            {                string strSendLine;                StreamReader sr = new StreamReader(filestream);                while ((strSendLine = sr.ReadLine()) != null)                {                    counterDataArray.Add(strSendLine);                }                sr.Close();                sr.Dispose();                Debug.GC(true);            }            Debug.Print("send data from array");                        foreach (string value in counterDataArray)            {                httpSendData(value);                Debug.EnableGCMessages(true);            }                        counterDataArray.Clear();            Debug.GC(true);            cleanUp();                    }        public static void cleanUp()        {            if (strWriteFile == "dataone")            {                strWriteFile = "datatwo";                strReadFile = "dataone";            }            else            {                strWriteFile = "dataone";                strReadFile = "datatwo";            }            Debug.GC(true);            Debug.Print("Free Memory: " + Debug.GC(true).ToString());            Debug.Print("Record count: " + intCounter);            Debug.GC(true);            populateArray();        }        public static void connectToWifi()        {            //SimpleSocket Socket = new WiFlySocket("myURL.com", 80, WifiModule);            //httpSendData(Socket);            WifiModule = new WiFlyGSX();            WifiModule.EnableDHCP();            WifiModule.JoinNetwork("mySSID", 0, WiFlyGSX.AuthMode.MixedWPA1_WPA2, "myPassword");            Thread.Sleep(3500);            // Showing some interesting output            Debug.Print("Local IP: " + WifiModule.LocalIP);            Debug.Print("MAC address: " + WifiModule.MacAddress);            SNTP_Client TimeClient = new SNTP_Client(new WiFlySocket("ntp2.is.co.za", 123, WifiModule));            Thread.Sleep(500);            TimeClient.Synchronize();            Thread.Sleep(500);            Debug.Print("DateTime.Now: " + DateTime.Now.AddHours(2));            Thread.Sleep(3500);            Debug.GC(true);        }        public static void httpSendData(string queryStringData)        {                        SimpleSocket Socket = new WiFlySocket("myURL.com", 80, WifiModule);            HTTP_Client WebSession = new HTTP_Client(Socket);            try            {                HTTP_Client.HTTP_Response Response = WebSession.Get("/default.aspx?" + queryStringData);                // Fetches a response header                Debug.Print("value: " + queryStringData);                Debug.GC(true);            }            catch (System.ApplicationException e)            {                Debug.Print("Error Code: " + e);                intTimeoutCounter++;                Debug.Print("intTimeoutCounter: " + intTimeoutCounter);                using (var filestream = new FileStream(@"SDlog.txt", FileMode.Append))                {                    StreamWriter sw = new StreamWriter(filestream);                    sw.WriteLine("DateTime: " + DateTime.Now.AddHours(2) + "rnError Code: " + e + "rnintTimeoutCounter: " + intTimeoutCounter+ "rn");                    sw.Flush();                    sw.Close();                    Debug.GC(true);                }                Thread.Sleep(1500);                Debug.GC(true);                httpSendData(queryStringData);               }            finally            {                Socket.Close();                //intCounter++;                Debug.GC(true);            }                     }    }}
Go to the full post


  • Please log in to reply
29 replies to this topic

#1 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 07 December 2012 - 02:42 PM

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

#2 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 07 December 2012 - 04:12 PM

Hi Donovan, How does the WiFi module get its power? Via the VIN header or the 5V/3.3V headers? If it gets power via the 5V/3.3V headers, Netduino Plus 2 has a power switch on those you can use to power down and then re-power the shield. Of course finding a software-based solution may be an even better solution...but I thought I'd throw that out there. :) Chris

#3 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 07 December 2012 - 07:38 PM

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

#4 carb

carb

    Advanced Member

  • Members
  • PipPipPip
  • 352 posts
  • LocationCrystal River, Florida

Posted 07 December 2012 - 08:44 PM

Donovan,

Have you looked at Arron's Power Management Class. Power Management

It allows you to cycle the 3.3 & 5.0vdc headers (and other things). This would allow you to remove power temporarily from the shield to reset it, as needed.

Chuck

#5 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 10 December 2012 - 05:34 AM

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

#6 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 10 December 2012 - 08:52 AM

Is this a problem with the RN-XV module or the wiflyGSX.dll that requires a power reset when it times out?

Could be, I fixed a locking bug yesterday. Please download the latest version and try again :)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#7 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 10 December 2012 - 12:31 PM

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

#8 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 10 December 2012 - 01:14 PM

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

#9 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 10 December 2012 - 02:04 PM

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

#10 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 10 December 2012 - 04:54 PM

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?
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#11 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 10 December 2012 - 07:11 PM

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

#12 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 11 December 2012 - 07:04 AM

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?

#13 Stefan

Stefan

    Moderator

  • Members
  • PipPipPip
  • 1965 posts
  • LocationBreda, the Netherlands

Posted 11 December 2012 - 07:39 AM

Interesting, I have no clue :) If you just embed the .cs files with your project, you won't need the references at all!
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#14 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 11 December 2012 - 02:31 PM

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?

#15 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 11 December 2012 - 03:26 PM

...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?

#16 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 11 December 2012 - 04:55 PM

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

#17 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 12 December 2012 - 08:17 AM

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

#18 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 13 December 2012 - 01:51 PM

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

#19 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 13 December 2012 - 02:53 PM

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 ;)

#20 don664

don664

    Advanced Member

  • Members
  • PipPipPip
  • 77 posts
  • LocationHillcrest, KZN, South Africa

Posted 14 December 2012 - 06:06 AM

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




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.