Ethernet Interface

This content relates to a deprecated version of Mbed

Mbed 2 is now deprecated. For the latest version please see the Mbed OS documentation.

For the latest Ethernet API, please see Ethernet.

Hardware

First of all you have to connect your mbed to a RJ45 jack: Ethernet RJ45.

Software

Import libraryEthernetInterface

mbed IP library over Ethernet

The EthernetInterface library provides you with a simple API to connect to the internet.

Import library

Static Public Member Functions

static int init ()
Initialize the interface with DHCP.
static int init (const char *ip, const char *mask, const char *gateway)
Initialize the interface with a static IP address.
static int connect (unsigned int timeout_ms=15000)
Connect Bring the interface up, start DHCP if needed.
static int disconnect ()
Disconnect Bring the interface down.
static char * getMACAddress ()
Get the MAC address of your Ethernet interface.
static char * getIPAddress ()
Get the IP address of your Ethernet interface.
static char * getGateway ()
Get the Gateway address of your Ethernet interface.
static char * getNetworkMask ()
Get the Network mask of your Ethernet interface.

#include "EthernetInterface.h"

First, you need to setup the connection by choosing whether you want to use DHCP or a static IP addressing with the init() function.

Then, simply connect() to the network. When you're done, disconnect() from the network.

When you are connected, you can use either the Socket API or any high-level component (HTTP Client).

Examples

These examples demonstrate how to get started with the Socket API & Ethernet.

Import program

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 
00004 int main() {
00005     EthernetInterface eth;
00006     eth.init(); //Use DHCP
00007     eth.connect();
00008     printf("IP Address is %s\n", eth.getIPAddress());
00009     
00010     TCPSocketConnection sock;
00011     sock.connect("mbed.org", 80);
00012     
00013     char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
00014     sock.send_all(http_cmd, sizeof(http_cmd)-1);
00015     
00016     char buffer[300];
00017     int ret;
00018     while (true) {
00019         ret = sock.receive(buffer, sizeof(buffer)-1);
00020         if (ret <= 0)
00021             break;
00022         buffer[ret] = '\0';
00023         printf("Received %d chars from server:\n%s\n", ret, buffer);
00024     }
00025       
00026     sock.close();
00027     
00028     eth.disconnect();
00029     
00030     while(1) {}
00031 }

Running this example the mbed will output something similar from the serial port:

IP Address is 10.2.131.195
Received 293 chars from server:
HTTP/1.1 200 OK
Server: nginx/0.7.62
Date: Fri, 27 Jul 2012 14:36:00 GMT
Content-Type: text/plain
Connection: close
Last-Modified: Fri, 27 Jul 2012 13:30:34 GMT
Vary: Accept-Encoding
Content-Length: 14
X-ServedBy: web_4
X-Varnish: 230435690
Age: 0
Via: 1.1 varnish

Hello world!

Import program

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003  
00004 int main() {
00005     EthernetInterface eth;
00006     eth.init(); //Use DHCP
00007     eth.connect();
00008     
00009     UDPSocket sock;
00010     sock.init();
00011     
00012     Endpoint nist;
00013     nist.set_address("utcnist.colorado.edu", 37);
00014     
00015     char out_buffer[] = "plop"; // Does not matter
00016     sock.sendTo(nist, out_buffer, sizeof(out_buffer));
00017     
00018     char in_buffer[4];
00019     int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer));
00020     
00021     unsigned int timeRes = ntohl( *((unsigned int*)in_buffer));
00022     printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes);
00023     
00024     sock.close();
00025     
00026     eth.disconnect();
00027     while(1) {}
00028 }

Feedback

We would be glad to get your feedback on this forum thread:


All wikipages