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  

#40291 RN-XV WiFly Module driver

Posted by don664 on 27 November 2012 - 08:21 AM in Project Showcase

ok, as a work around, I am simply adding two hours when I call for the date and time:

DateTime.Now.AddHours(2)

Seems to work just fine for the moment ;)

Thanks,
Donovan



#40013 RN-XV WiFly Module driver

Posted by don664 on 23 November 2012 - 02:56 PM in Project Showcase


By the way, for those who know that netmftoolbox thingy, I also added an SNTP client today


When was this added and is there an example of it's simple usage to set the dateTime on the Netduino?

Thanks,
Donovan



#40220 RN-XV WiFly Module driver

Posted by don664 on 26 November 2012 - 11:38 AM in Project Showcase

Ah ha ;) Perfect! Thanks for the help Stefan!



#40239 RN-XV WiFly Module driver

Posted by don664 on 26 November 2012 - 05:05 PM in Project Showcase

Is there a way to set the timezone just before the synchronize; as I need the times to be GMT +2? Thanks :)



#40212 RN-XV WiFly Module driver

Posted by don664 on 26 November 2012 - 05:41 AM in Project Showcase

You can swap the integratedsocket for the wifi socket;
http://netmftoolbox....ailable classes


Hi Stefan,

Thanks... but now I don't know how to set the time as "Synchronize" is not part of SimpleSocket:

'Toolbox.NETMF.NET.SimpleSocket' does not contain a definition for 'Synchronize' and no extension method 'Synchronize' accepting a first argument of type 'Toolbox.NETMF.NET.SimpleSocket' could be found (are you missing a using directive or an assembly reference?)


I've tried looking through the IntelliSense options for TimeClient but can't figure out which to use.

// Initializes the time client
            SimpleSocket TimeClient = new WiFlySocket("time-a.nist.gov", 123, WifiModule);
            // Displays the time in three ways:
            Debug.Print("Amount of seconds since 1 jan. 1900: " + TimeClient.NtpLookup());
            //Debug.Print("UTC time: " + TimeClient.UTCDate.ToString());
            //Debug.Print("Local time: " + TimeClient.LocalDate.ToString());
            // Synchronizes the internal clock
            TimeClient.Synchronize();



#40165 RN-XV WiFly Module driver

Posted by don664 on 25 November 2012 - 02:49 PM in Project Showcase

Hi Stefan,

Thanks for the link... which reference do I need to add for "IntegratedSocket"?

I keep getting the following error:

The type or namespace name 'IntegratedSocket' could not be found (are you missing a using directive or an assembly reference?)


I'm running a Netduino Plus 2.



#40095 RN-XV WiFly Module driver

Posted by don664 on 24 November 2012 - 04:51 AM in Project Showcase

I have the toolbox.... will look again for an ntpvexample Thanks :-)



#43104 Memory leak

Posted by don664 on 10 January 2013 - 01:21 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi Cuno,

 

I noticed this after printing out free RAM in various places and then removing a lot of these "Debug.Print" statements... I even stopped printing out the querystring and now am only losing about 852 bytes per cycle... still seems strange to be losing so much.

 

I've also thought about adding some delay's into the project hoping that this will result in threads "dying" naturally so the next time around it creates them fresh?




#43095 Memory leak

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

Hi there,

 

I am building a live data logger (basically a counter) which sends counts (collected by a photo sensor) to a webpage in a querystring so the data can be added to a SQL DB.

 

To make sure that I have a redundancy, in the event of power failure for example, I have two files on an SD card which the counter writes to on the photo sensor interrupt. Periodically the SD card is read and the data put into an array which is then looped through to send the HTTP requests. This prevents any issues of trying to read from the SD card at the same time as a photo interrupt is trying to write to the card.

 

I'm using a Netduino Plus 2.

 

At the moment I am testing a part of the final code and have encountered a "memory leak" issue.

 

Basically I am simply looping through the "SD Card read" and "array send threads" to test them. the problem I am having is that I eventually run out of memory and I can't understand why.

 

I would have thought that the only thing taking up memory would be the counter variable (which is the only thing that changes with each request... it's increased by one) but for whatever reason on each loop I lose about 3500 bytes.

 

I have made sure that I flush/close/dispose both the stream reader and stream writer (used for writing errors to a log file) when I am finished using them. I also clear the array when i am done with it.

 

Surely increasing the counter variable by an increment of 1 is not 3500 bytes worth of memory?

 

Do I need to abort/close threads as I leave them? Is it something to do with the "Socket" or "WebSession"... do I need to somehow terminate/clear these before creating new ones?

 

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;using VariableLabs.PowerManagment;namespace testToolbox21240{    public class Program    {        static WiFlyGSX WifiModule;        static ArrayList counterDataArray = new ArrayList();        // SD Card file variables        static string strWriteFile = "datatwo";        static string strReadFile = "dataone";        // WiFi SSID and password        static string strSSID = "mySSID";        static string strPassword = "myPassword";        // Remote host URL        static string strRemoteHost = "myUrl.com";        // Counter variables        static uint intCounter = 0;        static uint intTimeoutCounter = 0;                        public static void Main()        {            Debug.Print("Turn off the Ethernet controller");            PowerManagment.SetPeripheralState(Peripheral.Ethernet, false);            Debug.EnableGCMessages(true);            connectToWifi();            populateArray();                                  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.Print("send data from array");                        foreach (string value in counterDataArray)            {                httpSendData(value);            }                        cleanUp();                    }        public static void httpSendData(string queryStringData)        {            SimpleSocket Socket = new WiFlySocket(strRemoteHost, 80, WifiModule);            HTTP_Client WebSession = new HTTP_Client(Socket);            try            {                HTTP_Client.HTTP_Response Response = WebSession.Get("/default.aspx?" + queryStringData);                Debug.Print("value: " + queryStringData);            }            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();                    sw.Dispose();                }                Thread.Sleep(1500);                httpSendData(queryStringData);            }            catch (System.NullReferenceException f)            {                Debug.Print("Error Code: " + f);                using (var filestream = new FileStream(@"SDlog.txt", FileMode.Append))                {                    StreamWriter sw = new StreamWriter(filestream);                    sw.WriteLine("DateTime: " + DateTime.Now.AddHours(2) + "rnError Code: " + f + "rnintTimeoutCounter: " + intTimeoutCounter + "rn");                    sw.Flush();                    sw.Close();                    sw.Dispose();                }                        }            finally            {                intCounter++;            }                     }        public static void cleanUp()        {            if (strWriteFile == "dataone")            {                strWriteFile = "datatwo";                strReadFile = "dataone";            }            else            {                strWriteFile = "dataone";                strReadFile = "datatwo";            }            counterDataArray.Clear();            Debug.GC(true);            Debug.Print("Free Memory: " + Debug.GC(true).ToString());            Debug.Print("Record count: " + intCounter);            Debug.Print("running thread count: " + ThreadState.Background.ToString());            populateArray();        }        public static void connectToWifi()        {            Debug.Print("start wifi");            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);            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);        }    }}

 

A snippet of the debug output:

 

 

The thread '<No Name>' (0x2) has exited with code 0 (0x0).
Turn off the Ethernet controller
start wifi
Local IP: 192.168.0.8
MAC address: 00:60:06:80:88:55
DateTime.Now: 01/10/2013 12:02:16
Populate array from: dataone
send data from array
value: deviceID=1&count=0&dateTimeStamp=12/5/2012%2017:8:5
value: deviceID=1&count=1&dateTimeStamp=12/5/2012%2017:8:6
value: deviceID=1&count=2&dateTimeStamp=12/5/2012%2017:8:7
value: deviceID=1&count=3&dateTimeStamp=12/5/2012%2017:8:7
value: deviceID=1&count=4&dateTimeStamp=12/5/2012%2017:8:8
[color=#ff0000;]Free Memory: 99072[/color]
Record count: 5
running thread count: 4
Populate array from: datatwo
send data from array
value: deviceID=1&count=5&dateTimeStamp=12/7/2012%2013:31:33
value: deviceID=1&count=6&dateTimeStamp=12/7/2012%2013:31:34
value: deviceID=1&count=7&dateTimeStamp=12/7/2012%2013:32:24
value: deviceID=1&count=8&dateTimeStamp=12/7/2012%2013:32:24
value: deviceID=1&count=9&dateTimeStamp=12/7/2012%2013:32:24
value: deviceID=1&count=10&dateTimeStamp=12/7/2012%2013:32:24
value: deviceID=1&count=11&dateTimeStamp=12/7/2012%2013:32:24
value: deviceID=1&count=12&dateTimeStamp=12/7/2012%2013:32:25
value: deviceID=1&count=13&dateTimeStamp=12/7/2012%2013:32:25
value: deviceID=1&count=14&dateTimeStamp=12/7/2012%2013:32:25
value: deviceID=1&count=15&dateTimeStamp=12/7/2012%2013:32:25
value: deviceID=1&count=16&dateTimeStamp=12/7/2012%2013:32:25
[color=#ff0000;]Free Memory: 95544[/color]
Record count: 17
running thread count: 4

 

 

Thanks for any advice!

 

Donovan




#43667 Memory leak

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

as far as i know you have to use simpleSocket in conjunction with a wifi connection?




#43098 Memory leak

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

d'oh!!  :blink:

 

What a good idea, I'll give that a try and let you know where it swallows up the RAM.

 

Thanks Chris!!




#44165 Memory leak

Posted by don664 on 25 January 2013 - 01:49 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi emg,

 

Thanks for the lead. I have spoken with OpenPicus and ordered a board which is on it's way for me to test. Hopefully their wifi module can handle FTP (which they claim) and will help my project along.

 

Cheers,

Donovan




#43086 Wifi timeout

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

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



#44164 Memory leak

Posted by don664 on 25 January 2013 - 01:46 PM in Netduino Plus 2 (and Netduino Plus 1)

Hi skobyJay,

 

I wasn't sure how to modify the  array as you suggested, or implement the iDisposable using method so I thought the best thing was to simply try and put the whole project together including the sensor.and this seems to have helped with the memory leak.

 

Previously I was setting up a loop to simply read from a SD card file, populate the array and send via HTTP then read from a second file, send and start at the first file again. This loop seems to have been the real issue as it never "released" any of the threads?

 

Anyway now that the SD card read/array populate/http send are triggered by the sensor count the memory loss has stopped (apart from a few bytes for the counter variables obviously). So i can only imagine it's because that now there isn't a loop the garbage collector can release resources held by the various threads.

 

I'm still having timeout issues with the wifi module but at least it feels like another step forward.

 

The most recent version of the code below:

 

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;using VariableLabs.PowerManagment;namespace testToolbox21240{    public class Program    {        // Hardware: wifly, sensor and LCD        static WiFlyGSX WifiModule;        static InterruptPort mySensor;        // SD Card file variables        static string strWriteFile = "dataone";        static string strReadFile = "datatwo";        static int intDeviceID = 1;        // WiFi SSID and password        static string strSSID = "mySSID";        static string strPassword = "myPassword";        // Remote host URL        static string strRemoteHost = "myRemoteHost";        // Counter variables        static uint intCounter = 0;        static int intSendCounter = 0;        static int intArrayCounter = 0;        static uint intTimeoutCounter = 0;        static ArrayList counterDataArray = new ArrayList();        static OutputPort led = new OutputPort(Pins.ONBOARD_LED, false);        public static SimpleSocket Socket;        public static HTTP_Client WebSession;                public static void Main()        {            PowerManagment.SetPeripheralState(Peripheral.Ethernet, false);            Debug.EnableGCMessages(true);            connectToWifi();            // setup sensor interrupt            mySensor = new InterruptPort(Pins.GPIO_PIN_D7, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);            mySensor.OnInterrupt += new NativeEventHandler(mySensor_OnInterrupt);            Thread.Sleep(Timeout.Infinite);        }        public static void mySensor_OnInterrupt(uint port, uint state, DateTime time)        {            string strDateTime = (DateTime.Now.AddHours(2).Month.ToString() + "/" + DateTime.Now.AddHours(2).Day.ToString() + "/" + DateTime.Now.AddHours(2).Year.ToString() + "%20" + DateTime.Now.AddHours(2).Hour.ToString() + ":" + DateTime.Now.AddHours(2).Minute.ToString() + ":" + DateTime.Now.AddHours(2).Second.ToString());            using (var filestream = new FileStream(@"SD" + strWriteFile + ".txt", FileMode.Append))            {                StreamWriter sw = new StreamWriter(filestream);                sw.WriteLine("deviceID=" + intDeviceID + "&count=" + intCounter + "&dateTimeStamp=" + strDateTime);                sw.Flush();                sw.Close();            }            intCounter++;            intSendCounter++;            Debug.Print("intCounter: " + intCounter + "intSendCounter: " + intSendCounter);            Thread.Sleep(250);            if (intSendCounter == 5)            {                intSendCounter++;                populateArray();            }        }        public static void populateArray()        {            if (strWriteFile == "dataone")            {                strWriteFile = "datatwo";                strReadFile = "dataone";            }            else            {                strWriteFile = "dataone";                strReadFile = "datatwo";            }            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();            }            Thread httpSendThread = new Thread(httpSendData);            httpSendThread.Start();                   }        public static void httpSendData()        {            try            {                foreach (string value in counterDataArray)                {                    HTTP_Client.HTTP_Response Response = WebSession.Get("/default.aspx?" + value);                }            }            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();                    sw.Dispose();                }                Thread.Sleep(1500);                httpSendData();            }            catch (System.NullReferenceException f)            {                Debug.Print("Error Code: " + f);                using (var filestream = new FileStream(@"SDlog.txt", FileMode.Append))                {                    StreamWriter sw = new StreamWriter(filestream);                    sw.WriteLine("DateTime: " + DateTime.Now.AddHours(2) + "rnError Code: " + f + "rnintTimeoutCounter: " + intTimeoutCounter + "rn");                    sw.Flush();                    sw.Close();                    sw.Dispose();                }            }            finally            {                cleanUp();            }        }        public static void cleanUp()        {            File.Delete(@"SD" + strReadFile + ".txt");                        counterDataArray.Clear();                        Debug.Print("Free Memory: " + Debug.GC(true).ToString());            intSendCounter = 0;                        Debug.GC(true);                    }        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);            // Set the RTC for GMT + 2            SNTP_Client TimeClient = new SNTP_Client(new WiFlySocket("ntp2.is.co.za", 123, WifiModule));            Thread.Sleep(1000);            TimeClient.Synchronize();            Debug.Print("DateTime.Now: " + DateTime.Now.AddHours(2));            Socket = new WiFlySocket(strRemoteHost, 80, WifiModule);            WebSession = new HTTP_Client(Socket);        }    }} 

 

Thanks for the help... I'll keep you posted on any further progress!

 

Have a great weekend!




#44025 Memory leak

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

Thanks emg,This looks very promising and seems to be what I'm looking for......from what I've read briefly this is a stand alone product and won't simply replace the wifi module on my Netduino?



#44021 Memory leak

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

I know Get requests are typically lighter than Postbut have you tried to use Post to populate your database? I don't know how large your files are but it might be worth a shot. If the files are large then you could create smaller files.

 

The files are only a couple of kb each... very small but it doesn't seem that the wifi SimpleSocket allows HTTP Posts for transferring files.

 

Was really hoping that could be a solution to replace FTP or the need for an HTTP request for each count; but it looks like that is the only option at the moment  :mellow:

 

As for the memory leak I'm considering just restarting the Netduino (using PowerState.RebootDevice(true);) as soon as the memory gets too low. This isn't a solution though as you'll invariably lose a few counts while the Netduino restarts.

 

Is there a way to simply restart the threads? I've tried Thread.Abort(); but this simply throws an exception when I try and abort a thread. I had hoped that by aborting a thread once I was done with it, it would release any memory resources it was using.

 

To do this I tried (just an outline of the program):

 

class program{     // Define the thread    public static Thread populateArrayThread = new Thread(populateArray);    Main    {        connectToWifi();    }    connectToWifi()    {        // connect to wifi stuff here        populateArrayThread.Start();    }    populateArray()    {        //populate array from SD card file        httpDataSend();    }    httpSendData()    {        populateArrayThread.Abort();        // send HTTP querystring requests from array variables        cleanUp();    } }

 

When I try and abort the thread I get:

 

A first chance exception of type 'System.ArgumentException' occurred in mscorlib.dll
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
 
Also the wifi response does not include "Idisposable"... not that I'd know how to use it anyway  :huh:
 
To clean up the array, as it's being used, I'm removing each value after it's been set via HTTP... still not helping much though.
 
 
 



#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



#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



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



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



#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



#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



#42964 Wifi timeout

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

Hi Eric,

 

Thanks for the sample... I will work with it later and let you know what I am able to come up with.





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.