Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
10 years, 2 months ago.
planted LPC1768 on my prototype board for my IoT project on mbed OS.How do I get the IP address?
I have planted LPC1768 on my prototype board for my IoT project on mbed OS.How do I get the IP address of this device to interact? Any suggested Applications to compile/load from mbed online compiler?
1 Answer
10 years, 2 months ago.
Hi John,
You can initialize Ethernet with a fixed address, so your question is more likely related to being a DHCP client. I've derived a custom version of EthernetInterface - I think I've marked the special APIs in the example below.
To get the IP Address, use the getIPAddress() API. Some of my devices have a local display that I post the "buf" onto, and for others I may send it to the serial/debug port as you'll see below.
sprintf(buf, "%15s", eth.getIPAddress());
For my needs, the following seems to give me a pretty robust interface to Ethernet that will reconnect if you unplug/replug. I don't show below, but I also have a WatchDog running. I have some nodes on my network where I've never had to manually intervene to get them going after power outage, or network rewiring...
// fragments extracted from an application, do not expect this to compile...
EthernetInterface eth;
...
int main()
{
...
pc.printf("Initializing network interface...\r\n");
if (0 == eth.init()) { // Using DHCP (this extracted fragment does not show fixed IP)
pc.printf("Name: %s\r\n", nn);
eth.setName(nn); // Not part of standard EthernetInterface to register name with server
do {
pc.printf("Connecting to network...\r\n");
if (0 == eth.connect()) { // success!
bool initFlag = true;
linkup = true; // I have an LED on the Ethernet connected mapped to this signal
ShowIPAddress(true);
int speed = eth.get_connection_speed(); // not part of standard EthernetInterface
pc.printf("Connected at %d Mb/s\r\n", speed);
while (eth.is_connected()) {
// Here's the forever loop within which you might do things
//pc.printf("Connection is good.\r\n");
if (initFlag) {
SyncToNTPServer();
}
initFlag = false; // end of the first pass
wd.Service();
} // this is the end of the main "forever" loop, exiting only when Ethernet connection is lost
linkup = false;
pc.printf("lost connection.\r\n");
ShowIPAddress(false); // show --- instead of an IP address
eth.disconnect();
}
else {
pc.printf(" ... failed to connect.\r\n");
}
} while (1); // this is the end of the outer "forever" loop, which permits retries for the network
}
...
void ShowIPAddress(bool show)
{
char buf[16];
if (show)
sprintf(buf, "%15s", eth.getIPAddress());
else
sprintf(buf, "%15s", "---.---.---.---");
lcd.puts(480 - 15 * 8, 256, buf);
pc.printf("Ethernet connected as %s\r\n", buf);
}
You said you were looking for an example. You may find much better examples. Here's a non-trivial Firmware update framework I put together so a node can self-update via the network. The network appliances that use this are based on the LPC1768 module and use Ethernet. They poll a server once a day (or on reboot) to see if there is newer firmware - they then download it, install it, and reboot. That works well for the devices that are truly "embedded" and not adjacent to a PC.