Simple code to demonstrate how to manually use RPC functionality to easily access the microcontroller hardware via WebInterface. Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed HTTPServer
Revision 0:c308e6d6f56b, committed 2011-10-27
- Comitter:
- botdream
- Date:
- Thu Oct 27 23:01:40 2011 +0000
- Commit message:
Changed in this revision
diff -r 000000000000 -r c308e6d6f56b Libs/EthernetNetIf.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/EthernetNetIf.lib Thu Oct 27 23:01:40 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r c308e6d6f56b Libs/HTTPServer.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/HTTPServer.lib Thu Oct 27 23:01:40 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
diff -r 000000000000 -r c308e6d6f56b Libs/RPCInterface.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/RPCInterface.lib Thu Oct 27 23:01:40 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/MichaelW/code/RPCInterface/#a9e2c45097c8
diff -r 000000000000 -r c308e6d6f56b Libs/mbed.bld --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/mbed.bld Thu Oct 27 23:01:40 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
diff -r 000000000000 -r c308e6d6f56b main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 27 23:01:40 2011 +0000 @@ -0,0 +1,150 @@ +/* //--------------------------------------------------------------------------------------------- +http://192.168.1.100/rpc/DigitalOut/new%20LED1%20myled1 + +http://192.168.1.100/rpc/DigitalOut/new LED1 myled1 +http://192.168.1.100/rpc/DigitalOut/new LED2 myled2 +http://192.168.1.100/rpc/DigitalOut/new LED3 myled3 +http://192.168.1.100/rpc/DigitalOut/new LED4 myled4 + +http://192.168.1.100/rpc/ + +http://192.168.1.100/rpc/myled1/read +http://192.168.1.100/rpc/myled1/write 1 + +http://192.168.1.100/rpc/myled2/write 2 +http://192.168.1.100/rpc/myled3/write 3 +http://192.168.1.100/rpc/myled4/write 4 + +//--------------------------------------------------------------------------------------------- +// resources +//--------------------------------------------------------------------------------------------- +http://mbed.org/handbook/C-Data-Types +http://mbed.org/cookbook/RPC-Interface-Library +http://mbed.org/cookbook/HTTP-Server +http://mbed.org/cookbook/Ethernet +http://mbed.org/handbook/Ticker +//--------------------------------------------------------------------------------------------- */ +#include "mbed.h" +#include "EthernetNetIf.h" +#include "HTTPServer.h" +#include "RPCVariable.h" +#include "RPCFunction.h" +//--------------------------------------------------------------------------------------------- +DigitalOut myled1(LED1); +DigitalOut myled2(LED2); +DigitalOut myled3(LED3); +DigitalOut myled4(LED4); +Serial pc(USBTX, USBRX); // tx, rx +//--------------------------------------------------------------------------------------------- +#define internaldebug // send debug messages to USB Serial port (9600,1,N) +//#define dhcpenable // auto-setup IP Address from DHCP router +//--------------------------------------------------------------------------------------------- +// Timer Interrupt - NetPool +//--------------------------------------------------------------------------------------------- +Ticker netpool; +//--------------------------------------------------------------------------------------------- +// Ethernet Object Setup +//--------------------------------------------------------------------------------------------- +#ifdef dhcpenable + EthernetNetIf eth; +#else + EthernetNetIf eth( + IpAddr(192,168,1,100), //IP Address + IpAddr(255,255,255,0), //Network Mask + IpAddr(192,168,1,254), //Gateway + IpAddr(192,168,1,254) //DNS + ); +#endif +//--------------------------------------------------------------------------------------------- +// HTTP Server +//--------------------------------------------------------------------------------------------- +HTTPServer httpserver; +LocalFileSystem fs("webfs"); +//--------------------------------------------------------------------------------------------- + +//--------------------------------------------------------------------------------------------- +// ISR -> Pool Ethernet - will be triggered by netpool ticker +//--------------------------------------------------------------------------------------------- +void netpoolupdate() +{ + Net::poll(); +} +//--------------------------------------------------------------------------------------------- + +//--------------------------------------------------------------------------------------------- +// MAIN +//--------------------------------------------------------------------------------------------- +int main() +{ + // Set Serial Port Transfer Rate + pc.baud(115200); + + //-------------------------------------------------------- + // Setting RPC + //-------------------------------------------------------- + Base::add_rpc_class<AnalogIn>(); + Base::add_rpc_class<AnalogOut>(); + Base::add_rpc_class<DigitalIn>(); + Base::add_rpc_class<DigitalOut>(); + Base::add_rpc_class<DigitalInOut>(); + 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>(); + Base::add_rpc_class<BusInOut>(); + Base::add_rpc_class<Serial>(); + //-------------------------------------------------------- + + //-------------------------------------------------------- + // Setting Ethernet + //-------------------------------------------------------- + #ifdef internaldebug + printf("\r\nSetting up Ethernet interface!\r\n"); + #endif + // Create return object for error check + EthernetErr ethErr = eth.setup(); + if(ethErr) + { + #ifdef internaldebug + printf("\r\nError %d in Ethernet setup.\r\n", ethErr); + #endif + return -1; + } + #ifdef internaldebug + printf("\r\nEthernet setup completed with success!\r\n"); + #endif + //-------------------------------------------------------- + + //-------------------------------------------------------- + // adding Handlers + //-------------------------------------------------------- + FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path + + httpserver.addHandler<RPCHandler>("/rpc"); + httpserver.addHandler<FSHandler>("/"); //Default handler + //-------------------------------------------------------- + + //-------------------------------------------------------- + // bind http server to port 80 (Listen) + //-------------------------------------------------------- + httpserver.bind(80); + #ifdef internaldebug + printf("Listening on port 80!\r\n"); + #endif + //-------------------------------------------------------- + + //-------------------------------------------------------- + // ISR -> attach timer interrupt to update Net::Pool(); + //-------------------------------------------------------- + netpool.attach(&netpoolupdate, 0.1); + //-------------------------------------------------------- + + //-------------------------------------------------------- + // main loop + //-------------------------------------------------------- + // main loop + while(1) + wait(0.5); +} +//--------------------------------------------------------------------------------------------- \ No newline at end of file