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 29-March 23)


By content type

See this member's


Sort by                Order  

#50610 Serial LCD and Netduino Plus 2

Posted by don664 on 18 June 2013 - 10:12 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi Chuckles,

 

The supplier I use offers a 3.3V Serial LCD: http://www.netram.co...tml?keyword=lcd

 

Cheers,

Donovan




#44816 netduino and GPRS

Posted by don664 on 03 February 2013 - 07:52 AM in Netduino Plus 2 (and Netduino Plus 1)

Hi guys,

Does anyone know if the netduino plus 2 can support a gprs sheild like this one: http://www.netram.co...PRS-Shield.html ?

And if it does would we be able to use it for ftp file uploads and http requests?

Thanks,
Donovan



#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




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



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




#43911 Memory leak

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

Thanks for the suggestion but sending the files to a server share (not even sure where to begin with this) to process is not an option because it will mean having to install a machine on site to handle this and that defeats the object of wanting the devices to be "free standing" with their only requirements being power and a wifi network.

 

If we're having to install a machine on site to handle the processing we may as well install cabling and simply run the whole project over ethernet. Again though this increases the overheads and installation times and makes a simple mechanical counter and piece of paper the best solution.

 

It's looking more and more likely that the wifi capabilities of the Netduino are not yet up to the task we're asking of it  :(

 

I'm going to look at the Arduino today as I think there is a way for the Arduino to run the wifly module in CMD mode which in turn can handle FTP requests. I hate the Arduino interface though but may have no choice now.

 

Thanks again for looking into this... hopefully I'm missing something?




#43909 Memory leak

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

I don't think the HTTP delay has anything to do with the server, but rather the time the wifly module takes to process the request. I have changed to the remote host from the Toolbox example and it's still taking about a second per HTTP request.

 

Also since stripping out the try/catch statements the memory leak has gone from 684 bytes to a whopping 1728 bytes??!!

 

Here's the code:

 

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;        // SD Card file variables        static string strWriteFile = "datatwo";        static string strReadFile = "dataone";        // WiFi SSID and password        static string strSSID = "mySSID";        static string strPassword = "myPasssowrd";        // Remote host URL        static string strRemoteHost = "www.netmftoolbox.com";        // Counter variables        static uint intCounter = 0;        static ArrayList counterDataArray = new ArrayList();        public static SimpleSocket Socket;        public static HTTP_Client WebSession;                        public static void Main()        {            PowerManagment.SetPeripheralState(Peripheral.Ethernet, false);            Debug.EnableGCMessages(true);            connectToWifi();                                              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");            httpSendData();        }        public static void httpSendData()        {                foreach (string value in counterDataArray)                {                    HTTP_Client.HTTP_Response Response = WebSession.Get("/helloworld/");                    Debug.Print("value: " + value);                    Debug.GC(true);                }                cleanUp();                                }        public static void cleanUp()        {            if (strWriteFile == "dataone")            {                strWriteFile = "datatwo";                strReadFile = "dataone";            }            else            {                strWriteFile = "dataone";                strReadFile = "datatwo";            }            counterDataArray.Clear();            Debug.Print("Free Memory: " + Debug.GC(true).ToString());            Debug.Print("Record count: " + intCounter);            Debug.GC(true);            populateArray();        }        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);            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);            populateArray();        }    }}



#43868 Memory leak

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

Hi skobyjay,

 

Thanks for the suggestion...

 

I have moved the socket and websession out of the "send" routine and only create them once now when I first connect to the wifi network. This has helped reduce the memory leak per loop to just 684 bytes.

 

The problem is that something is still taking up memory which will mean the device cannot run all day as we'd like it to without a restart.

 

Further to that the HTTP request is very slow and the Netduino takes between 1-2 seconds to process the request. This means that we'll not be able to cycle all the requests fast enough when the count tally could be increasing by 1 a second.

 

The average querystring looks like this: deviceID=1&count=6&dateTimeStamp=12/7/2012%2013:31:34

 

I have to be able to get the FTP working over the wifi so that I can upload a text file with the querystrings in it and then have the server handle the processing. It's too much for the Netduino to do. It would be fine if the sensor was only collecting data every minute or so, but with it collecting a count every second it's going to overwhelm the Netduino.




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




#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




#43520 Ftp client socket

Posted by don664 on 16 January 2013 - 08:52 AM in Visual Studio

Hi there,

 

Has anyone used this with a wifi connection?

 

Thanks,

Donovan

 

EDIT- Have managed to sort of get this working over wifi, please check this topic: http://forums.netdui...7733-wifly-ftp/




#43113 Memory leak

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

I'm looking at how else I can re-do the code but I'm not sure what else to try...

 

...the querystring is generated (from the array) and then sent to the server via HTTP. That's already very simple.

 

Is there anyway to abort all threads and then start the "Main" thread again? I'm thinking of a sort of "soft" reset of the Netduino... this is a pain though as it means resetting the wifi connection as well as the counter.

 

I just can't understand what is hanging onto data and adding to it each loop. When a Thread gets to the end surely it releases any resources it was using?

 

I did notice when I did a Thread.count in the "Clean up" thread that is should 4 threads... how do I get it to drop a thread?




#43108 Memory leak

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

Hi NooM,

 

I have tried this in various places but still seem to end up losing 3528 bytes in the first loop... and then consistently 852 bytes for subsequent loops.

 

I even tried removing the counter variable (which is increased by 1 each loop after a successful HTTP request) but this does not affect the memory leak... so it's something else that is eating up the memory??




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




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




#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





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.