Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.
Photo

Monitor Internet Connectivity


  • Please log in to reply
16 replies to this topic

#1 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 14 June 2011 - 07:21 AM

I just received my Netduino Plus yesterday - cool product by the way - and though I had a different project in mind when I ordered it I needed to change my plans due to necessity. Let me explain what I wish to do. The modem on my 3G router dies intermittently. Annoying as it is, it also does not start on its own afterwards. The only way to make it work again is to unplug the router. I have thought of overheating as the culprit but pointing fans at the modem led to nothing but disappointment. My plan is to use the Netduino plus to monitor my internet connection by trying to access a webpage every few minutes. If it receives error 404 for a consecutive number of times it will drive a 12V relay for a few seconds to cut power to the router effectively resetting it. Driving the relay would not be a problem as I've already seen various tips and suggestions around and outside the forum about how to do that. The one I need information about is programming the Netduino to access a specific webpage and check the response. In other words, the Netduino Plus will work as a client. I've tried searching but I couldn't find any topics about it - or I may be using wrong keywords for searching. Please point me to the right direction. Another solution to my problem is to buy a new 3G router/modem but those are quite expensive compared to a a few electrical parts such as transistors and relays. Also I think the above is a good starter project for me that would be useful until I save enough money for a router/modem replacement or upgrade. Thanks.

#2 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 15 June 2011 - 11:18 AM

I got it working now! Or I think it's working, at least when the URL is existing. When the URL does not exists it hangs up - a problem I'm trying to debug at the moment. Still, if anyone's got working code already please hint me on it. Will appreciate it very much. It seems I had to reference System.Http to make WebRequest work... my bad. This was the first time I worked with C# to connect to the web so I'm really ignorant about it. Hehehe! Also, I noticed that the Netduino Plus hangs up and reboots when I unplug/plug an Ethernet cable or switch off the router. Is this normal behavior? I have updated to the latest beta firmware by the way, if that's important to know. Alternatively, how do I send a Ping command with the Netduino?

#3 ColinR

ColinR

    Advanced Member

  • Members
  • PipPipPip
  • 142 posts
  • LocationCape Town, South Africa

Posted 15 June 2011 - 12:25 PM

Also, I noticed that the Netduino Plus hangs up and reboots when I unplug/plug an Ethernet cable or switch off the router. Is this normal behavior? I have updated to the latest beta firmware by the way, if that's important to know.

Are you handling?
NetworkChange_NetworkAvailabilityChanged

Alternatively, how do I send a Ping command with the Netduino?

Please let me know if you work it out!

#4 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 15 June 2011 - 01:05 PM

Are you handling?

NetworkChange_NetworkAvailabilityChanged


That I wasn't doing! I didn't know I had to. I thought it was all automatic. Thanks for the heads up.

Please let me know if you work it out!


Sure thing. I hope I do find how to do that though.

#5 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 21 June 2011 - 03:51 PM

I now have managed to make an Internet connection monitoring app that seem to work along with the router resetting relay that I mentioned earlier... until it freezes. I still couldn't find the reason why it freezes but I added the part of the code which causes it to freeze below which references System.Http:

try
{
  HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);

  request.Timeout = 5000;

  Debug.Print("Accessing preset URL: " + uri);

  // This is where the world ends... well at least for this app!

  using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
  {
    Debug.Print("Waiting for response...");
    Debug.Print(response.ContentLength.ToString());
    Debug.Print(response.ContentType);
    Debug.Print("Status code: " + response.StatusCode.ToString());    

    if (response.StatusCode == HttpStatusCode.OK) 
    {
      Debug.Print("Internet connection is available");

      isConnectedToInternet = true;
      internetConnectedLed.Write(true);
      break;
    }
    else
    {
      Debug.Print("Status code: " + response.StatusCode.ToString() + " received.");

      isConnectedToInternet = false;
      internetConnectedLed.Write(false);
    }
  }
}
catch
{
  Debug.Print("Resolving URL failed. The network or Internet connection is down.");

  isConnectedToInternet = false;
  internetConnectedLed.Write(false);
}

The freezing is quite random. Sometimes it will run for around an hour or so. Sometimes it will take a few minutes. The randomness makes it hard for me to pinpoint the problem. I'm more or less a beginner with .NETMF and C#. Any tips on how I could optimize the above code or an alternative over it?

I also noticed that the Netduino seems to switch back to a static IP randomly when the N+ is set to use DHCP with MFDeploy. It could have reset itself when it crashed. Anyone have an idea what could cause it? I'm using 4.2.0 Beta 1 firmware by the way.

#6 Chris Walker

Chris Walker

    Secret Labs Staff

  • Moderators
  • 7767 posts
  • LocationNew York, NY

Posted 21 June 2011 - 09:52 PM

Hi devilfisch,

I also noticed that the Netduino seems to switch back to a static IP randomly when the N+ is set to use DHCP with MFDeploy. It could have reset itself when it crashed. Anyone have an idea what could cause it? I'm using 4.2.0 Beta 1 firmware by the way.

Hmm, very interesting. If you have a sample app which reproduces this consistently, we'd love to test this out. I belieave that there are some networking bugfixes in the .NET 4.2 codebase...hopefully they haven't accidentally introduced any glitches :) Either way, we'd love to look into this.

Chris

#7 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 22 June 2011 - 07:01 AM

Hi devilfisch,

Hmm, very interesting. If you have a sample app which reproduces this consistently, we'd love to test this out. I belieave that there are some networking bugfixes in the .NET 4.2 codebase...hopefully they haven't accidentally introduced any glitches :) Either way, we'd love to look into this.

Chris


Yes, there could be a glitch somewhere on the networking code as I noticed that sometimes the EnableDHCP method doesn't work which forces me to restart my Netduino Plus. It normally works after the restart. I could tell even without using Debug as I have wired an LED to tell me if it has a valid DHCP address.

The freezing of the code I posted above seem to trigger the problem but it takes time and is very random and usually happens after a forced restart (pulling of the USB cable) following a freeze. The above code also freezes when it tries to access a local IP address that isn't in the Netduino's subnet which happens when the N+ fails to grab a DHCP address but I think this is more of an effect than a cause. Although I said "freeze", the N+ does seem to respond to other events like the InterruptPort that I wrote for the onboard switch so it's very likely that the problem has something to do with just the networking side of the N+.

I'll try reflashing it again with firmware 4.2.0 and older to see if the results are the same to pinpoint if this is a firmware problem or it could have been introduced by a slightly corrupted flashing. I'll let you know immediately what I find.

#8 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 22 June 2011 - 07:33 AM

By the way, when I flashed my N+ I wasn't able to flash TinyBooterDecompressor as I didn't notice the file when I flashed the new 4.2.0 firmware. Could this be what's causing me headaches? I just saw the file now after I downloaded the archive again. At the moment I don't have a 32-bit Windows installation to flash the bootloader. I could install Windows XP via VirtualBox but would SAM-BA work with VBox?

#9 Stefan

Stefan

    Moderator

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

Posted 22 June 2011 - 07:53 AM

By the way, when I flashed my N+ I wasn't able to flash TinyBooterDecompressor as I didn't notice the file when I flashed the new 4.2.0 firmware. Could this be what's causing me headaches? I just saw the file now after I downloaded the archive again.

At the moment I don't have a 32-bit Windows installation to flash the bootloader. I could install Windows XP via VirtualBox but would SAM-BA work with VBox?

Quite possible that the wrong tinybooter could give a headache ;)

I haven't tried SAM-BA in a virtual environment, but I flashed my netduinos on Win7x64 without any problems.
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#10 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 22 June 2011 - 08:11 AM

Quite possible that the wrong tinybooter could give a headache ;)

I haven't tried SAM-BA in a virtual environment, but I flashed my netduinos on Win7x64 without any problems.


Thanks Stefan. Just installed SAM-BA on my Win7x64 system. Will do the reflash after the requested restart. I'm crossing my fingers that this fixes my problem. :)

#11 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 22 June 2011 - 08:31 AM

I just erased my N+ but I can't use SAM-BA because of missing drivers. How did you get SAM-BA to work on Win7x64 Stefan? Edit: Oh! Don't mind anymore. Someone asked me to fix a computer which is running XP. I'll just use that. What luck! :D

#12 Stefan

Stefan

    Moderator

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

Posted 22 June 2011 - 09:03 AM

I just erased my N+ but I can't use SAM-BA because of missing drivers. How did you get SAM-BA to work on Win7x64 Stefan?

Edit:
Oh! Don't mind anymore. Someone asked me to fix a computer which is running XP. I'll just use that. What luck! :D

LOL!

Windows Update provided me of a proper driver. Requires a legitime version of Windows with the latest service pack though. But WinXP is a solution as well I suppose :D
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#13 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 22 June 2011 - 09:11 AM

LOL!

Windows Update provided me of a proper driver. Requires a legitime version of Windows with the latest service pack though. But WinXP is a solution as well I suppose :D


I see. I should have waited but I cancelled the installation from Windows Update since it was taking forever and I was thinking that the driver could be in the ATMEL folder. Silly me. I'll just do that next time. :P

Anyway, I've flashed the bootloader and the firmware. Will test my code and hopefully it will be glitch free now. :)

#14 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 22 June 2011 - 03:33 PM

I finally see the problem in my code:

A first chance exception of type 'System.OutOfMemoryException' occurred in mscorlib.dll

I was expecting that this was the case but I couldn't see it on the output before. The wrong bootloader might have been the cause for that.

The OutOfMemoryException was thrown when the N+ reached the following code after running it several times:

using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())

My code is probably quite inefficient - I'm a beginner after all - or there's a memory leak on GetResponse(). I printed out the memory usage before running GetResponse() and noticed a pattern. At first memory is around 16000 then rises to 17000 or 19000. From then on it starts dropping rapidly deducting more than 2000 on every iteration until memory dwindles to nothingness inevitably inducing major head trauma to me... well, ignore the last part... I've been losing hair (not literally, or course ;)) this past few days because of this code so I was trying to amuse myself. Hehehe! :P

Additionally, to better isolate the problem I completely removed the try/catch statement from my code to see if getting the GetResponse() out would equal to no problems. As expected the freezing disappeared and I confirmed that the try/catch statement containing my HTTP web request code is the culprit.

I'm now thinking of discarding HttpWebRequest so I'm looking for better alternatives. Any suggestions? Would sockets work for this? I'm a total noob about sockets though - currently reading information about it. If you think sockets would be better please hint me.

Another idea I have was to use events instead of using (almost) infinite loops (which I thought was a bad idea but I had no knowledge how to get rid of them). Are there any timers I could use with the N+?

Would love to hear your inputs. Thanks!

#15 Stefan

Stefan

    Moderator

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

Posted 22 June 2011 - 03:38 PM

Hi devilfisch,

The ethernet code in the firmware is quite heavy. A beta with saving some memory exists, see this thread for more details:
http://forums.netdui...te-50-more-ram/
Not 4.2 yet though :)

Are there any timers I could use with the N+?

Yes, there are. See also: http://msdn.microsof...ercallback.aspx
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#16 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 22 June 2011 - 05:15 PM

Oh no! I think my stress got to me and I think I made a big mistake! I think I bricked my N+. Since it is not related to this thread I'm posting a new topic about it. :(

#17 devilfisch

devilfisch

    Member

  • Members
  • PipPip
  • 26 posts

Posted 26 June 2011 - 08:20 AM

With my N+ running again after a stupid mistake I started working on this app again. Unmodified, it still freezes even with the "50% more RAM" firmware. It's what I expected it would do although this time it lasted longer thanks to more RAM. I did some research and it seems like HttpWebRequest.GetResponse() is quite a memory hog so I thought of ditching it entirely. I haven't implemented it yet but one of my ideas is to use Twitter instead. If I can't connect to Twitter then I'm disconnected from the Internet and should switch the relay to reset the router. If it's connected I can use Twitter to post messages and maybe even send simple commands to it using Twitter posts. I'm thinking of using MicroTweet but it's currently send only. Does anyone know of a good way to retrieve tweets using the Netduino?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users

home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.