Example using MbedJSONRpc: call distant method
Dependencies: MbedJSONRpc EthernetNetIf mbed DNSResolver MbedJSONValue
main.cpp@0:b7259f428f5f, 2011-09-22 (annotated)
- Committer:
- samux
- Date:
- Thu Sep 22 12:31:37 2011 +0000
- Revision:
- 0:b7259f428f5f
- Child:
- 2:d439eefcc605
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
samux | 0:b7259f428f5f | 1 | #include "mbed.h" |
samux | 0:b7259f428f5f | 2 | #include "MbedJSONRpc.h" |
samux | 0:b7259f428f5f | 3 | #include "MbedJSONValue.h" |
samux | 0:b7259f428f5f | 4 | #include "Websocket.h" |
samux | 0:b7259f428f5f | 5 | |
samux | 0:b7259f428f5f | 6 | Serial pc(USBTX, USBRX); |
samux | 0:b7259f428f5f | 7 | |
samux | 0:b7259f428f5f | 8 | //websocket: configuration with sub-network = samux and mbed_id = mbed_acc |
samux | 0:b7259f428f5f | 9 | Websocket webs("ws://sockets.mbed.org:888/ws/samux/client"); |
samux | 0:b7259f428f5f | 10 | |
samux | 0:b7259f428f5f | 11 | //RPC object attached to the websocket server |
samux | 0:b7259f428f5f | 12 | MbedJSONRpc rpc(&webs); |
samux | 0:b7259f428f5f | 13 | |
samux | 0:b7259f428f5f | 14 | int main() { |
samux | 0:b7259f428f5f | 15 | |
samux | 0:b7259f428f5f | 16 | RPC_TYPE t; |
samux | 0:b7259f428f5f | 17 | |
samux | 0:b7259f428f5f | 18 | //in: argument for the distant method (here empty) |
samux | 0:b7259f428f5f | 19 | //out results of the distant method (accelerometers values) |
samux | 0:b7259f428f5f | 20 | MbedJSONValue in, out; |
samux | 0:b7259f428f5f | 21 | |
samux | 0:b7259f428f5f | 22 | while (!webs.connect()) |
samux | 0:b7259f428f5f | 23 | pc.printf("cannot connect websocket, retrying\r\n"); |
samux | 0:b7259f428f5f | 24 | |
samux | 0:b7259f428f5f | 25 | //CALL getAcc on mbed_acc |
samux | 0:b7259f428f5f | 26 | if ((t = rpc.call("getAcc", "mbed_acc", in, out)) == CALL_OK) { |
samux | 0:b7259f428f5f | 27 | printf("call success\r\n"); |
samux | 0:b7259f428f5f | 28 | printf("acc_x: %d\r\n", out[0].get<int>()); |
samux | 0:b7259f428f5f | 29 | printf("acc_y: %d\r\n", out[1].get<int>()); |
samux | 0:b7259f428f5f | 30 | printf("acc_z: %d\r\n", out[2].get<int>()); |
samux | 0:b7259f428f5f | 31 | } else |
samux | 0:b7259f428f5f | 32 | printType(t); |
samux | 0:b7259f428f5f | 33 | } |