RPC hello world using the Wifly Interface

Dependencies:   MbedJSONRpc MbedJSONValue WebSocketClient WiflyInterface mbed

Committer:
samux
Date:
Fri Aug 24 14:12:36 2012 +0000
Revision:
3:d2f4dc85eb08
Parent:
2:ed304e8b2f0c
Child:
4:fa9af2789476
fixed typo

Who changed what in which revision?

UserRevisionLine numberNew contents of line
samux 0:935c9e275b45 1 #include "mbed.h"
samux 0:935c9e275b45 2 #include "WiflyInterface.h"
samux 0:935c9e275b45 3 #include "Websocket.h"
samux 0:935c9e275b45 4 #include "MbedJSONRpc.h"
samux 0:935c9e275b45 5
samux 0:935c9e275b45 6 BusOut l(LED1, LED2, LED3, LED4);
samux 0:935c9e275b45 7
samux 0:935c9e275b45 8 /* wifly interface:
samux 0:935c9e275b45 9 * - p9 and p10 are for the serial communication
samux 0:935c9e275b45 10 * - p19 is for the reset pin
samux 0:935c9e275b45 11 * - p26 is for the connection status
samux 0:935c9e275b45 12 * - "mbed" is the ssid of the network
samux 0:935c9e275b45 13 * - "password" is the password
samux 0:935c9e275b45 14 * - WPA is the security
samux 0:935c9e275b45 15 */
samux 0:935c9e275b45 16 WiflyInterface wifly(p9, p10, p19, p26, "mbed", "password", WPA);
samux 0:935c9e275b45 17
samux 3:d2f4dc85eb08 18 //websocket: configuration with sub-network = demo and mbed_id = mbed_led
samux 0:935c9e275b45 19 Websocket ws("ws://sockets.mbed.org/rpc/demo/mbed_led");
samux 0:935c9e275b45 20
samux 0:935c9e275b45 21 //RPC object attached to the websocket server
samux 0:935c9e275b45 22 MbedJSONRpc rpc(&ws);
samux 0:935c9e275b45 23
samux 0:935c9e275b45 24 //Test class
samux 0:935c9e275b45 25 class Test {
samux 0:935c9e275b45 26 public:
samux 0:935c9e275b45 27 Test() {};
samux 0:935c9e275b45 28 void led(MbedJSONValue& in, MbedJSONValue& out) {
samux 0:935c9e275b45 29 int id = in[0].get<int>();
samux 0:935c9e275b45 30 l = (1 << (id - 1));
samux 0:935c9e275b45 31 wait(0.2);
samux 0:935c9e275b45 32 l = 0;
samux 0:935c9e275b45 33 }
samux 0:935c9e275b45 34 };
samux 0:935c9e275b45 35
samux 0:935c9e275b45 36 Test test;
samux 0:935c9e275b45 37
samux 0:935c9e275b45 38 int main() {
samux 0:935c9e275b45 39
samux 0:935c9e275b45 40 wifly.init(); //Use DHCP
samux 0:935c9e275b45 41 wifly.connect();
samux 0:935c9e275b45 42 printf("IP Address is %s\n\r", wifly.getIPAddress());
samux 0:935c9e275b45 43
samux 0:935c9e275b45 44 ws.connect();
samux 0:935c9e275b45 45
samux 0:935c9e275b45 46 RPC_TYPE t;
samux 0:935c9e275b45 47
samux 0:935c9e275b45 48 //------------register getAcc---------------//
samux 0:935c9e275b45 49 if((t = rpc.registerMethod("led", &test, &Test::led)) == REGISTER_OK)
samux 0:935c9e275b45 50 printf("led is registered\r\n");
samux 0:935c9e275b45 51 else
samux 0:935c9e275b45 52 printType(t);
samux 0:935c9e275b45 53
samux 0:935c9e275b45 54 //wait for incoming CALL requests
samux 0:935c9e275b45 55 rpc.work();
samux 0:935c9e275b45 56 }