How do I print my IP address?

30 Jun 2012

Dumb newbie question. How do I print my mbed's IP address?

If I do something along these lines:

struct IpAddr myip;

....

myip = eth.getIp(); pc.printf("Interface is up, local IP is %s\n", inet_ntoa(myip));

I get compiler errors about the unknown function inet_ntoa.

What's the easiest way of printing out the mbed's IP address?

Thanks.

30 Jun 2012

Take a look at the HTTP server example on the Cookbook. It shows in there how to print IP

30 Jun 2012

Couldn't find anything in the cookbook I must admit. Although there was a mention that the IP addr is printed out the console.

Anyway, I discovered that putting this:

  1. include "inet.h"

allowed the inet_ntoa() function to work, which was perfect.

30 Jun 2012

yeah the IP is printed to console there is a getip() function

IpAddr ethIp = eth.getIp();
printf("Connected ok, IP : %d.%d.%d.%d\n", ethIp[0], ethIp[1], ethIp[2], ethIp[3]);
const char* hwAddr = eth.getHwAddr();
printf("HW address : %02x:%02x:%02x:%02x:%02x:%02x\n",
   hwAddr[0], hwAddr[1], hwAddr[2],
   hwAddr[3], hwAddr[4], hwAddr[5]); 
02 Jul 2012

frank van hooft wrote:

How do I print my mbed's IP address?

You can also use the getIPAddress method:

    EthernetInterface eth;
    eth.init(); //Use DHCP
    eth.connect();
    printf("IP Address is %s\n", eth.getIPAddress());

See the Ethernet Interface documentation.

Cheers, Emilio