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 07-May 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 :-)



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




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




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




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




#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




#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




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




#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



#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



#41313 Wifi timeout

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

Hi Stefan,

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

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

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

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

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

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

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

Error: a3000000

Waiting for debug commands...

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


Thanks,
Donovan



#41315 Wifi timeout

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

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



#41478 Wifi timeout

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

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

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



#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



#41529 Wifi timeout

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

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

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

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

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

Debug Output:

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


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

Debug Output:

socket connect

....then nothing


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

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

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

Any suggestions?

Thanks,
Donovan




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.