RPC hello world over Ethernet
Dependencies: EthernetInterface MbedJSONRpc MbedJSONValue WebSocketClient mbed-rtos mbed
main.cpp@0:6538aa30a040, 2012-08-24 (annotated)
- Committer:
- samux
- Date:
- Fri Aug 24 14:57:31 2012 +0000
- Revision:
- 0:6538aa30a040
rpc hello world with the mbed official EthernetInterface
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:6538aa30a040 | 1 | #include "mbed.h" |
samux | 0:6538aa30a040 | 2 | #include "MbedJSONRpc.h" |
samux | 0:6538aa30a040 | 3 | #include "MbedJSONValue.h" |
samux | 0:6538aa30a040 | 4 | #include "EthernetInterface.h" |
samux | 0:6538aa30a040 | 5 | #include "Websocket.h" |
samux | 0:6538aa30a040 | 6 | |
samux | 0:6538aa30a040 | 7 | Serial pc(USBTX, USBRX); |
samux | 0:6538aa30a040 | 8 | BusOut l(LED1, LED2, LED3, LED4); |
samux | 0:6538aa30a040 | 9 | |
samux | 0:6538aa30a040 | 10 | //websocket: configuration with sub-network = demo and mbed_id = mbed_led |
samux | 0:6538aa30a040 | 11 | Websocket ws("ws://sockets.mbed.org/rpc/demo/mbed_led"); |
samux | 0:6538aa30a040 | 12 | |
samux | 0:6538aa30a040 | 13 | //RPC object attached to the websocket server |
samux | 0:6538aa30a040 | 14 | MbedJSONRpc rpc(&ws); |
samux | 0:6538aa30a040 | 15 | |
samux | 0:6538aa30a040 | 16 | //Test class |
samux | 0:6538aa30a040 | 17 | class Test { |
samux | 0:6538aa30a040 | 18 | public: |
samux | 0:6538aa30a040 | 19 | Test() {}; |
samux | 0:6538aa30a040 | 20 | void led(MbedJSONValue& in, MbedJSONValue& out) { |
samux | 0:6538aa30a040 | 21 | int id = in[0].get<int>(); |
samux | 0:6538aa30a040 | 22 | l = (1 << (id - 1)); |
samux | 0:6538aa30a040 | 23 | wait(0.2); |
samux | 0:6538aa30a040 | 24 | l = 0; |
samux | 0:6538aa30a040 | 25 | } |
samux | 0:6538aa30a040 | 26 | }; |
samux | 0:6538aa30a040 | 27 | |
samux | 0:6538aa30a040 | 28 | Test test; |
samux | 0:6538aa30a040 | 29 | |
samux | 0:6538aa30a040 | 30 | int main() { |
samux | 0:6538aa30a040 | 31 | |
samux | 0:6538aa30a040 | 32 | EthernetInterface eth; |
samux | 0:6538aa30a040 | 33 | eth.init(); //Use DHCP |
samux | 0:6538aa30a040 | 34 | eth.connect(); |
samux | 0:6538aa30a040 | 35 | printf("IP Address is %s\n\r", eth.getIPAddress()); |
samux | 0:6538aa30a040 | 36 | |
samux | 0:6538aa30a040 | 37 | ws.connect(); |
samux | 0:6538aa30a040 | 38 | |
samux | 0:6538aa30a040 | 39 | RPC_TYPE t; |
samux | 0:6538aa30a040 | 40 | |
samux | 0:6538aa30a040 | 41 | //------------register getAcc---------------// |
samux | 0:6538aa30a040 | 42 | if((t = rpc.registerMethod("led", &test, &Test::led)) == REGISTER_OK) |
samux | 0:6538aa30a040 | 43 | printf("led is registered\r\n"); |
samux | 0:6538aa30a040 | 44 | else |
samux | 0:6538aa30a040 | 45 | printType(t); |
samux | 0:6538aa30a040 | 46 | |
samux | 0:6538aa30a040 | 47 | //wait for incoming CALL requests |
samux | 0:6538aa30a040 | 48 | rpc.work(); |
samux | 0:6538aa30a040 | 49 | } |