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
main.cpp@0:c9e14abbbc89, 2011-10-27 (annotated)
- Committer:
- botdream
- Date:
- Thu Oct 27 23:08:16 2011 +0000
- Revision:
- 0:c9e14abbbc89
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
botdream | 0:c9e14abbbc89 | 1 | /* //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 2 | // Request to set internal Global var "flag_blink" |
botdream | 0:c9e14abbbc89 | 3 | http://192.168.1.100/rpc/blink/write%200 |
botdream | 0:c9e14abbbc89 | 4 | http://192.168.1.100/rpc/blink/write%201 |
botdream | 0:c9e14abbbc89 | 5 | |
botdream | 0:c9e14abbbc89 | 6 | http://192.168.1.100/rpc/blink/write 0 |
botdream | 0:c9e14abbbc89 | 7 | http://192.168.1.100/rpc/blink/write 1 |
botdream | 0:c9e14abbbc89 | 8 | |
botdream | 0:c9e14abbbc89 | 9 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 10 | // resources |
botdream | 0:c9e14abbbc89 | 11 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 12 | http://mbed.org/handbook/C-Data-Types |
botdream | 0:c9e14abbbc89 | 13 | http://mbed.org/cookbook/RPC-Interface-Library |
botdream | 0:c9e14abbbc89 | 14 | http://mbed.org/cookbook/HTTP-Server |
botdream | 0:c9e14abbbc89 | 15 | http://mbed.org/cookbook/Ethernet |
botdream | 0:c9e14abbbc89 | 16 | http://mbed.org/handbook/Ticker |
botdream | 0:c9e14abbbc89 | 17 | //--------------------------------------------------------------------------------------------- */ |
botdream | 0:c9e14abbbc89 | 18 | #include "mbed.h" |
botdream | 0:c9e14abbbc89 | 19 | #include "EthernetNetIf.h" |
botdream | 0:c9e14abbbc89 | 20 | #include "HTTPServer.h" |
botdream | 0:c9e14abbbc89 | 21 | #include "RPCVariable.h" |
botdream | 0:c9e14abbbc89 | 22 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 23 | DigitalOut myled(LED1); |
botdream | 0:c9e14abbbc89 | 24 | Serial pc(USBTX, USBRX); // tx, rx |
botdream | 0:c9e14abbbc89 | 25 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 26 | #define internaldebug // send debug messages to USB Serial port (9600,1,N) |
botdream | 0:c9e14abbbc89 | 27 | //#define dhcpenable // auto-setup IP Address from DHCP router |
botdream | 0:c9e14abbbc89 | 28 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 29 | // Timer Interrupt - NetPool |
botdream | 0:c9e14abbbc89 | 30 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 31 | Ticker netpool; |
botdream | 0:c9e14abbbc89 | 32 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 33 | // Ethernet Object Setup |
botdream | 0:c9e14abbbc89 | 34 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 35 | #ifdef dhcpenable |
botdream | 0:c9e14abbbc89 | 36 | EthernetNetIf eth; |
botdream | 0:c9e14abbbc89 | 37 | #else |
botdream | 0:c9e14abbbc89 | 38 | EthernetNetIf eth( |
botdream | 0:c9e14abbbc89 | 39 | IpAddr(192,168,1,100), //IP Address |
botdream | 0:c9e14abbbc89 | 40 | IpAddr(255,255,255,0), //Network Mask |
botdream | 0:c9e14abbbc89 | 41 | IpAddr(192,168,1,254), //Gateway |
botdream | 0:c9e14abbbc89 | 42 | IpAddr(192,168,1,254) //DNS |
botdream | 0:c9e14abbbc89 | 43 | ); |
botdream | 0:c9e14abbbc89 | 44 | #endif |
botdream | 0:c9e14abbbc89 | 45 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 46 | // HTTP Server |
botdream | 0:c9e14abbbc89 | 47 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 48 | HTTPServer httpserver; |
botdream | 0:c9e14abbbc89 | 49 | LocalFileSystem fs("webfs"); |
botdream | 0:c9e14abbbc89 | 50 | |
botdream | 0:c9e14abbbc89 | 51 | uint8_t flag_blink; |
botdream | 0:c9e14abbbc89 | 52 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 53 | |
botdream | 0:c9e14abbbc89 | 54 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 55 | // ISR -> Pool Ethernet - will be triggered by netpool ticker |
botdream | 0:c9e14abbbc89 | 56 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 57 | void netpoolupdate() |
botdream | 0:c9e14abbbc89 | 58 | { |
botdream | 0:c9e14abbbc89 | 59 | Net::poll(); |
botdream | 0:c9e14abbbc89 | 60 | } |
botdream | 0:c9e14abbbc89 | 61 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 62 | |
botdream | 0:c9e14abbbc89 | 63 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 64 | // MAIN |
botdream | 0:c9e14abbbc89 | 65 | //--------------------------------------------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 66 | int main() |
botdream | 0:c9e14abbbc89 | 67 | { |
botdream | 0:c9e14abbbc89 | 68 | // Set Serial Port Transfer Rate |
botdream | 0:c9e14abbbc89 | 69 | pc.baud(115200); |
botdream | 0:c9e14abbbc89 | 70 | |
botdream | 0:c9e14abbbc89 | 71 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 72 | // Setting Ethernet |
botdream | 0:c9e14abbbc89 | 73 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 74 | #ifdef internaldebug |
botdream | 0:c9e14abbbc89 | 75 | printf("\r\nSetting up Ethernet interface!\r\n"); |
botdream | 0:c9e14abbbc89 | 76 | #endif |
botdream | 0:c9e14abbbc89 | 77 | // Create return object for error check |
botdream | 0:c9e14abbbc89 | 78 | EthernetErr ethErr = eth.setup(); |
botdream | 0:c9e14abbbc89 | 79 | if(ethErr) |
botdream | 0:c9e14abbbc89 | 80 | { |
botdream | 0:c9e14abbbc89 | 81 | #ifdef internaldebug |
botdream | 0:c9e14abbbc89 | 82 | printf("\r\nError %d in Ethernet setup.\r\n", ethErr); |
botdream | 0:c9e14abbbc89 | 83 | #endif |
botdream | 0:c9e14abbbc89 | 84 | return -1; |
botdream | 0:c9e14abbbc89 | 85 | } |
botdream | 0:c9e14abbbc89 | 86 | #ifdef internaldebug |
botdream | 0:c9e14abbbc89 | 87 | printf("\r\nEthernet setup completed with success!\r\n"); |
botdream | 0:c9e14abbbc89 | 88 | #endif |
botdream | 0:c9e14abbbc89 | 89 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 90 | |
botdream | 0:c9e14abbbc89 | 91 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 92 | // adding RPC variables |
botdream | 0:c9e14abbbc89 | 93 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 94 | flag_blink = false; |
botdream | 0:c9e14abbbc89 | 95 | RPCVariable<uint8_t> RPCblink(&flag_blink, "blink"); |
botdream | 0:c9e14abbbc89 | 96 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 97 | |
botdream | 0:c9e14abbbc89 | 98 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 99 | // adding Handlers |
botdream | 0:c9e14abbbc89 | 100 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 101 | FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path |
botdream | 0:c9e14abbbc89 | 102 | |
botdream | 0:c9e14abbbc89 | 103 | httpserver.addHandler<RPCHandler>("/rpc"); |
botdream | 0:c9e14abbbc89 | 104 | httpserver.addHandler<FSHandler>("/"); //Default handler |
botdream | 0:c9e14abbbc89 | 105 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 106 | |
botdream | 0:c9e14abbbc89 | 107 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 108 | // bind http server to port 80 (Listen) |
botdream | 0:c9e14abbbc89 | 109 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 110 | httpserver.bind(80); |
botdream | 0:c9e14abbbc89 | 111 | #ifdef internaldebug |
botdream | 0:c9e14abbbc89 | 112 | printf("Listening on port 80!\r\n"); |
botdream | 0:c9e14abbbc89 | 113 | #endif |
botdream | 0:c9e14abbbc89 | 114 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 115 | |
botdream | 0:c9e14abbbc89 | 116 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 117 | // ISR -> attach timer interrupt to update Net::Pool(); |
botdream | 0:c9e14abbbc89 | 118 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 119 | netpool.attach(&netpoolupdate, 0.1); |
botdream | 0:c9e14abbbc89 | 120 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 121 | |
botdream | 0:c9e14abbbc89 | 122 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 123 | // main loop |
botdream | 0:c9e14abbbc89 | 124 | //-------------------------------------------------------- |
botdream | 0:c9e14abbbc89 | 125 | // main loop |
botdream | 0:c9e14abbbc89 | 126 | while(1) |
botdream | 0:c9e14abbbc89 | 127 | { |
botdream | 0:c9e14abbbc89 | 128 | if(flag_blink) |
botdream | 0:c9e14abbbc89 | 129 | { |
botdream | 0:c9e14abbbc89 | 130 | myled = 1; |
botdream | 0:c9e14abbbc89 | 131 | wait(0.5); |
botdream | 0:c9e14abbbc89 | 132 | myled = 0; |
botdream | 0:c9e14abbbc89 | 133 | } |
botdream | 0:c9e14abbbc89 | 134 | wait(0.5); |
botdream | 0:c9e14abbbc89 | 135 | } |
botdream | 0:c9e14abbbc89 | 136 | } |
botdream | 0:c9e14abbbc89 | 137 | //--------------------------------------------------------------------------------------------- |