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
- For details on how to use this in the compiler, see Importing Projects and Libraries in to the Compiler
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 | |
| HTTPServer | Creates an HTTPServer object. |
| ~HTTPServer | Destroys the HTTPServer and all open connections. |
| addHandler | Add a new content handler to handle requests. |
| poll | You have to call this method at least every 250ms to let the http server run. |
| timeout | Sets the timout for a HTTP request. |
Creates an HTTPServer object.
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 )
Destroys the HTTPServer and all open connections.
virtual ~HTTPServer()
Add a new content handler to handle requests.
virtual void addHandler( HTTPHandler * handler )
You have to call this method at least every 250ms to let the http server run.
inline static void poll()
Sets the timout for a HTTP request.
void timeout( int value )
- Full api at http://mbed.org/projects/cookbook/api/EMAC/lwip/trunk/HTTPServer
- Full source at http://mbed.org/projects/cookbook/svn/EMAC/lwip/trunk/
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
- Pure lwip HTTP Server - Using lwIP direct to building a http server
- Pure lwip HTTP Client - Using lwIP direct to build a http client
- 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
