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

Cant get an IP address

ipaddress network

  • Please log in to reply
3 replies to this topic

#1 Terrence

Terrence

    Member

  • Members
  • PipPip
  • 24 posts

Posted 17 December 2014 - 04:03 AM

public static void WaitForIPAddress()
{
	byte[] MAC = { 0x5C, 0x86, 0x4A, 0x00, 0xF2, 0x15 };

	NetworkInterface ni;
	NetworkInterface.GetAllNetworkInterfaces()[0].EnableDhcp();
	NetworkInterface.GetAllNetworkInterfaces()[0].PhysicalAddress = MAC;

	while (true)
	{
		ni = NetworkInterface.GetAllNetworkInterfaces()[0];
		if (ni.IPAddress != "0.0.0.0") break;
		Debug.Print("Waiting for an IP Address...");
		Thread.Sleep(1000);
	}
}
}

I have a Netduino plus 2, running netmf 4.3.1

I can't get an ip address.  When I look at the network interface it tells me my dns ip address, but that number is actually my gateway address?  My IP address is always 0.0.0.0.

 

I have read other posts, and am using the suggested code.  Any help would be great.

Thanks.

Terrence

 

 

 



#2 KiwiDev

KiwiDev

    Advanced Member

  • Members
  • PipPipPip
  • 100 posts
  • LocationNew Zealand

Posted 20 December 2014 - 07:05 AM

Hi Terrence,

 

Trying setting the device MAC address using the MFDeploy tool so you don't have to hard code it in your app.

 

Here is some code I use for showing network config, connectivity status changes etc.

 

Bryn

blog.devmobile.co.nz

using System;
using System.Net;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Net.NetworkInformation;


public class Program
{
   public static void Main()
   {
      NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];

      // Comment in/out as required or us MF Deploy
      networkInterface.EnableDhcp();
      networkInterface.EnableDynamicDns();

      NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged;
      NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged;

      // Display network config for debugging
      DisplayNetworkInformation();

      while (NetworkInterface.GetAllNetworkInterfaces()[0].IPAddress == IPAddress.Any.ToString())
      {
         Debug.Print("Waiting for an IP Address...");
         Thread.Sleep(1000);
      }

      Thread.Sleep(Timeout.Infinite);
   }


   static void NetworkChange_NetworkAvailabilityChanged(object sender, NetworkAvailabilityEventArgs e)
   {
      Debug.Print("NetworkChange_NetworkAvailabilityChanged " + e.IsAvailable.ToString());
   }


   static void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
   {
      Debug.Print("NetworkChange_NetworkAddressChanged");
      DisplayNetworkInformation();
   }



   public static void DisplayNetworkInformation()
   {
      NetworkInterface networkInterface = NetworkInterface.GetAllNetworkInterfaces()[0];

      // Display network config for debugging
      Debug.Print("Network configuration");
      Debug.Print(" Network interface type: " + networkInterface.NetworkInterfaceType.ToString());
      Debug.Print(" MAC Address: " + BytesToHexString(networkInterface.PhysicalAddress));
      Debug.Print(" DHCP enabled: " + networkInterface.IsDhcpEnabled.ToString());
      Debug.Print(" Dynamic DNS enabled: " + networkInterface.IsDynamicDnsEnabled.ToString());
      Debug.Print(" IP Address: " + networkInterface.IPAddress);
      Debug.Print(" Subnet Mask: " + networkInterface.SubnetMask);
      Debug.Print(" Gateway: " + networkInterface.GatewayAddress);
   }


   public static string BytesToHexString(byte[] bytes)
   {
      string hexString = string.Empty;

      // Create a character array for hexidecimal conversion.
      const string hexChars = "0123456789ABCDEF";

      // Loop through the bytes.
      for (byte b = 0; b < bytes.Length; b++)
      {
         if (b > 0)
            hexString += "-";

         // Grab the top 4 bits and append the hex equivalent to the return string.        
         hexString += hexChars[bytes[b] >> 4];

         // Mask off the upper 4 bits to get the rest of it.
         hexString += hexChars[bytes[b] & 0x0F];
      }

      return hexString;
   }
}


#3 Spiked

Spiked

    Advanced Member

  • Members
  • PipPipPip
  • 129 posts

Posted 20 December 2014 - 04:32 PM

I'd agree on using the hardware mac address first.

 

But if that is something you can not do, try setting it before you ask for a dhcp address.  Otherwisee mac '1234' ask for an IP address, then changed itself to '5678'  - it will never receive a reply.



#4 Terrence

Terrence

    Member

  • Members
  • PipPip
  • 24 posts

Posted 21 December 2014 - 11:28 AM

Thanks guys for the reply.  Turns out setting the MAC address before did the trick.

 

Terrence







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.