EMAC

Provide the code to use the Ethernet MAC Layer. In the current state it uses the lwip stack to build a full functional TCP/IP stack.

Library

Import Library:

http://mbed.org/projects/cookbook/svn/EMAC/lwip/trunk

or import precompiled Library:

http://mbed.org/projects/cookbook/svn/EMAC/lwip/precomp

Import Example program:

http://mbed.org/projects/cookbook/svn/EMAC/lwip/examples/HTTPServer

Hardware

To use this Library you have to connect the Ethernet pins of your mbed board with a Network. I have used a MagJack SI-60002-F to provide a RJ45 Jack.

            |   |   |   |         
          --||--||--||--||-----   Socket mbed
        /  8 | 6 | 4 | 2 |    /|       1 RD+
       /    7   5   3   1    / |       2 
      /                     /  |       3 RD-
     /    Bel Stewart      /   |       4 TD-
    /      MagJack        /    |       5
   /     SI-60002-F      /     |       6 TD+
  /___________________  /      |                                                               
 |      --------       |       |                                                               
 |    --        --     |       |                                                               
 | --- |          ---- |      /                                                                
 ||    /--/-/-/-/-/-/-||     /                                 ___                   ________   
 ||   /  8 7 6 5 4 3 2||    /              ________________  -/   \-        ____    |   ____ |  
 ||  /  / / / / / / / ||   /      ,= ,++, /,  ,  ,  ,  ,  ,\|,  ,  ,| ,  , /,  ,| , |,  ,  ,_|  
 || /  / / / / / / / /||  /      ============================================================  
 ||/  ' ' ' ' ' ' ' ' || /        |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |    
 | ------------------- |/         |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |  |    
  ---------------------                                               ^  ^  ^  ^                
  Ethernet Socket Front           mbed Board                         TD+TD-RD+RD-               

HTTP Server

A very easy to use HTTP Server which will server files from the flashdrive. The default file is called "index.htm".

Furthermore it can serve rpc requests when you request a page starting with /rpc. For example: "http://mbed-ip-or-hostname/rpc/led1/write+1" For the rpc path you can use rather , or + or = or in a modern browser space which will be converted to %20.

By default the server uses DHCP to connect to the network and will print out it's IP address to stdout (by default USB serial).

#include "mbed.h"
#include "HTTPServer.h"
#include "HTTPRPC.h"
#include "HTTPFS.h"
HTTPServer http; DigitalOut led1(LED1, "led1"); DigitalOut led2(LED2, "led2"); DigitalOut led3(LED3, "led3"); DigitalOut led4(LED4, "led4"); LocalFileSystem local("local");
int main(void) { Base::add_rpc_class<AnalogIn>(); Base::add_rpc_class<AnalogOut>(); Base::add_rpc_class<DigitalIn>(); Base::add_rpc_class<DigitalOut>(); Base::add_rpc_class<PwmOut>(); Base::add_rpc_class<Timer>(); Base::add_rpc_class<SPI>(); Base::add_rpc_class<BusOut>(); Base::add_rpc_class<BusIn>();
http.addHandler(new HTTPRPC()); http.addHandler(new HTTPFileSystemHandler("/", "/local/")); http.bind();
while(1) { http.poll(); } }
Source Code of HTTPServer.bin (LPC1768 LPC2368).

API

HTTPServer.h
Functions
HTTPServerCreates an HTTPServer object.
~HTTPServerDestroys the HTTPServer and all open connections.
addHandlerAdd a new content handler to handle requests.
pollYou have to call this method at least every 250ms to let the http server run.
timeoutSets the timout for a HTTP request.
HTTPServer(const char *hostname,  
struct ip_addr ip =  ip_addr_any,
struct ip_addr nm =  ip_addr_any,
struct ip_addr gw =  ip_addr_any,
struct ip_addr dns =  ip_addr_any,
unsigned short port =  80)
Creates an HTTPServer object.
virtual ~HTTPServer()
Destroys the HTTPServer and all open connections.
virtual void addHandler(HTTPHandler *handler)
Add a new content handler to handle requests.
inline static void poll()
You have to call this method at least every 250ms to let the http server run.
void timeout(int value)
Sets the timout for a HTTP request.

Resources

HTTP Client

  • HTTP Client - The HTTPClient class.
  • Twitter - A simple demo how to use the HTTPClient class with twitter
  • Timetric - A simple demo how to use the HTTPClient class with timetric

lwIP internal examples

  • Simple HTTP Server Using - TCPCallback classes to build a http server to how how to use the classes
  • Simple HTTP Client - Using TCPCallback classes to build a http client to how how to use the classes

External resources


Development Log