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

Getting the local IP address


  • Please log in to reply
12 replies to this topic

#1 Stefan

Stefan

    Moderator

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

Posted 28 May 2011 - 08:31 PM

I'm toying a bit with the Netduino and all works great! But one thing I haven't figured out. I can configure a static IP address or turn on DHCP with mfdeploy. Works fine. I prefer to use DHCP since my network is built that way. I have a couple of MAC addresses configured in my router to a specific IP so I have a list of all IP addresses and ranges in my router. But I can't find a way to read the current IP address from my netduino with code. Is that actually possible? I want to display the DHCP-provided IP on a LCD so my project works in every network without need of mfdeploy.exe.
"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

#2 ColinR

ColinR

    Advanced Member

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

Posted 28 May 2011 - 08:42 PM

Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress


#3 Stefan

Stefan

    Moderator

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

Posted 28 May 2011 - 08:45 PM

Neat! Just one line, now I really feel like a newb! :D Thanks ;)
"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

#4 Stefan

Stefan

    Moderator

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

Posted 29 May 2011 - 09:50 AM

Hmm doesn't seem to work for DHCP. My Netduino gets an IP address from my router (Edimax BR-6104, latest firmware), I can see it in my router logs and I can actually use that IP to connect to my Netduino (for example when I've installed a webserver on my netduino), but it displays 0.0.0.0 on my Netduino.

I tried this simple sample-code:
// Fetches the first network interface
            Microsoft.SPOT.Net.NetworkInformation.NetworkInterface NI = Microsoft.SPOT.Net.NetworkInformation.NetworkInterface.GetAllNetworkInterfaces()[0];
            
            // DHCP
            NI.EnableDhcp();
            NI.ReleaseDhcpLease();
            NI.RenewDhcpLease();
            Debug.Print(NI.IPAddress.ToString());

            // Static
            NI.EnableStaticIP("192.168.2.75", "255.255.255.0", "192.168.2.1");
            Debug.Print(NI.IPAddress.ToString());

Output:

0.0.0.0
192.168.2.75


Using firmware: Netduino Plus (v4.1.1.0 b1 with extra RAM) by Secret La
"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

#5 Stefan

Stefan

    Moderator

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

Posted 01 June 2011 - 06:27 AM

Tried it with a different router (to rule out the router and... well... my router was over 10 years old so I replaced it)
Same issue with a Sitecom Wireless Gigabit Router 300N WL-368
"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

#6 PawleysPlayr

PawleysPlayr

    Member

  • Members
  • PipPip
  • 11 posts

Posted 02 June 2011 - 12:37 PM

If you create a socket, does the IP show up in the Socket.LocalEndPoint property?

#7 Valkyrie-MT

Valkyrie-MT

    Advanced Member

  • Members
  • PipPipPip
  • 315 posts
  • LocationIndiana, USA

Posted 23 July 2011 - 02:26 AM

... but it displays 0.0.0.0 on my Netduino.


Try adding this code block afer you renew the dhcp lease, but before trying to use the IP address with Debug.Print. I found it in one of the Microsoft samples included with the .NET MicroFramework, including the relevent comment... the NetduinoPlus being an LWIP device:

            NetworkInterface ni;

            // Wait for DHCP (on LWIP devices)
            while (true)
            {
                ni = NetworkInterface.GetAllNetworkInterfaces()[0];

                if (ni.IPAddress != "0.0.0.0") break;

                Debug.Print("Waiting for an IP Address...");
  
                Thread.Sleep(1000);
            }

-Valkyrie-MT

#8 micfiz

micfiz

    Member

  • Members
  • PipPip
  • 12 posts

Posted 15 November 2011 - 08:26 PM

This is very good information. I used both parts of code provided by colinR and Stefan plus the suggestion by valkyrie-mt to merge. In fact this should be a tool or test procedure for those who just opened the box of their Netduino. It goes like this if the environment is up and running. 1. load Visual Studio and create an ND + console solution, name it IP_Discover 2. copy this code from theose named above into the program.cs using System; using System.Net; using System.Net.Sockets; using System.Threading; using Microsoft.SPOT; using Microsoft.SPOT.Hardware; using SecretLabs.NETMF.Hardware; using SecretLabs.NETMF.Hardware.NetduinoPlus; using Microsoft.SPOT.Net.NetworkInformation; namespace IP_Discovery { public class Program { public static void Main() { // Fetches the first network interface NetworkInterface NI = NetworkInterface.GetAllNetworkInterfaces()[0]; // DHCP NI.EnableDhcp(); NI.ReleaseDhcpLease(); NI.RenewDhcpLease(); NetworkInterface ni; // Wait for DHCP (on LWIP devices) while (true) { ni = NetworkInterface.GetAllNetworkInterfaces()[0]; if (ni.IPAddress != "0.0.0.0") break; Debug.Print("Waiting for an IP Address..."); Thread.Sleep(1000); } Debug.Print(NI.IPAddress.ToString()); // Static NI.EnableStaticIP("192.168.2.75", "255.255.255.0", "192.168.2.1"); Debug.Print(NI.IPAddress.ToString()); } } } 3. deploy it to ND + 4. run MFDeploy 5. connect 6 watch for the debug prints of Waiting for an IP Address... 7. plug in the network cable 8. see the ip address. 9. startup a command window and ping theat ip address and watch response and the lights on the ND +. Kind of a nice warm feeling you got somethin going on!

#9 micfiz

micfiz

    Member

  • Members
  • PipPip
  • 12 posts

Posted 15 November 2011 - 08:28 PM

ok how come my code snipet looks like crap (unformatted and not in color like other posts? I copied direct from visual studio and pasted into reply.

#10 Stefan

Stefan

    Moderator

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

Posted 15 November 2011 - 08:35 PM

ok how come my code snipet looks like crap (unformatted and not in color like other posts?

I copied direct from visual studio and pasted into reply.

Use [code=auto:0] tags around them :)
"Fact that I'm a moderator doesn't make me an expert in things." Stefan, the eternal newb!
My .NETMF projects: .NETMF Toolbox / Gadgeteer Light / Some PCB designs

#11 NeonMika / Markus VV.

NeonMika / Markus VV.

    Advanced Member

  • Members
  • PipPipPip
  • 209 posts
  • LocationUpper Austria

Posted 15 November 2011 - 08:35 PM

Use {code}MyCode.IAmJustATest(){/code}

Just replace { and } with [ and ]...
It will look like:

MyCode.IAmJustATest()

NeonMika.Webserver
> Control your N+ and write webservice methods easyily
> Receive data from you N+ (in XML or JSON)
> Browse the SD on your N+ directly in the browser and d
own - and upload files

 

If you need help with NeonMika.Webserver, please just leave a note in the thread and/or contact me via Skype :)

 

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------
--- Mistakes teach you important lessons. Every time you make one, you are one step closer to your goal. ----
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------


#12 micfiz

micfiz

    Member

  • Members
  • PipPip
  • 12 posts

Posted 15 November 2011 - 08:43 PM

the result: Pinging... TinyCLR Connecting to NetduinoPlus_FizDuino...Connected Waiting for an IP Address... Waiting for an IP Address... Waiting for an IP Address... DM9161_AutoNegotiate Valid PHY Found: 31 PHY: Vendor Number Model = 0xA PHY: Model Revision Number = 0x0 AutoNegotiate complete Waiting for an IP Address... Waiting for an IP Address... Waiting for an IP Address... 0.0.0.0 192.168.2.75 Done. Waiting for debug commands...

#13 micfiz

micfiz

    Member

  • Members
  • PipPip
  • 12 posts

Posted 17 November 2011 - 03:55 AM

Here is a test of inserting code.

The above post had a flaw. Not much good doing a DHCP when afterward it gets hard wired >>> 192.168.2.75

// Wait for DHCP (on LWIP devices)
while (true)
{
   ni = NetworkInterface.GetAllNetworkInterfaces()[0];

   if (ni.IPAddress != "0.0.0.0") break;
   Debug.Print("Waiting for an IP Address...");
   Thread.Sleep(1000);
}
Debug.Print(NI.IPAddress.ToString());
// Static 
NI.EnableStaticIP("192.168.2.75", "255.255.255.0", "192.168.2.1");
Debug.Print(NI.IPAddress.ToString());





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.