Simple code to demonstrate how to map an RPC global variable that controls LED blink functionality via REST HTTP Get request. Note: Demo program to be used on the GeekSessionLab Talk (November 2011). http://devrendezvous.com/?lang=en
Dependencies: EthernetNetIf mbed HTTPServer
Revision 0:c9e14abbbc89, committed 2011-10-27
- Comitter:
- botdream
- Date:
- Thu Oct 27 23:08:16 2011 +0000
- Commit message:
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/EthernetNetIf.lib Thu Oct 27 23:08:16 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/HTTPServer.lib Thu Oct 27 23:08:16 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/RPCInterface.lib Thu Oct 27 23:08:16 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/MichaelW/code/RPCInterface/#a9e2c45097c8
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/Libs/mbed.bld Thu Oct 27 23:08:16 2011 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/63bcd7ba4912
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Thu Oct 27 23:08:16 2011 +0000 @@ -0,0 +1,137 @@ +/* //--------------------------------------------------------------------------------------------- +// Request to set internal Global var "flag_blink" +http://192.168.1.100/rpc/blink/write%200 +http://192.168.1.100/rpc/blink/write%201 + +http://192.168.1.100/rpc/blink/write 0 +http://192.168.1.100/rpc/blink/write 1 + +//--------------------------------------------------------------------------------------------- +// 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" +//--------------------------------------------------------------------------------------------- +DigitalOut myled(LED1); +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"); + +uint8_t flag_blink; +//--------------------------------------------------------------------------------------------- + +//--------------------------------------------------------------------------------------------- +// 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 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 RPC variables + //-------------------------------------------------------- + flag_blink = false; + RPCVariable<uint8_t> RPCblink(&flag_blink, "blink"); + //-------------------------------------------------------- + + //-------------------------------------------------------- + // 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) + { + if(flag_blink) + { + myled = 1; + wait(0.5); + myled = 0; + } + wait(0.5); + } +} +//--------------------------------------------------------------------------------------------- \ No newline at end of file