RPC over Websockets
RPC over WebSockets
- RPC on mbed
- The mbed RPC server
- WebSockets on mbed
Import programRPC_Wifly_HelloWorld
RPC hello world using the Wifly Interface
Import programRPC_Ethernet_HelloWorld
RPC hello world over Ethernet
Remote Procedure Call over Websockets¶
Introduction¶
Imagine that you have several Mbeds and you want to trigger the execution of a specific procedure or method on a specific Mbed from another one. How can you do that ?
The Remote Procedure Call (RPC) mechanism is a solution.
The RPC WebSocket server
In this tutorial, we will be using a public server provided by mbed for testing purposes.
So you just have to program your mbed!
If you want more information regarding the server side code, see the mbed RPC server
This tutorial is divided into two parts:
- Part 1 - Control your mbed over WiFi
- Part 2 - Control your mbed over Ethernet
At the end of this tutorial, you will be able to control the LEDs of you mbed from a webpage.
1 - Hello World over Wifi¶
I will use in this example the WiflyInterface library. You may wish to take a look at the Wifly webpage.
1.1 - Code¶
#include "mbed.h" #include "WiflyInterface.h" #include "Websocket.h" #include "MbedJSONRpc.h" BusOut l(LED1, LED2, LED3, LED4); /* wifly interface: * - p9 and p10 are for the serial communication * - p19 is for the reset pin * - p26 is for the connection status * - "mbed" is the ssid of the network * - "password" is the password * - WPA is the security */ WiflyInterface wifly(p9, p10, p19, p26, "mbed", "password", WPA); //websocket: configuration with sub-network = demo and mbed_id = mbed_led Websocket ws("ws://sockets.mbed.org/rpc/demo/mbed_led"); //RPC object attached to the websocket server MbedJSONRpc rpc(&ws); //Test class class Test { public: Test() {}; void led(MbedJSONValue& in, MbedJSONValue& out) { int id = in[0].get<int>(); l = (1 << (id - 1)); wait(0.2); l = 0; } }; Test test; int main() { wifly.init(); //Use DHCP wifly.connect(); printf("IP Address is %s\n\r", wifly.getIPAddress()); ws.connect(); RPC_TYPE t; //------------register led---------------// if((t = rpc.registerMethod("led", &test, &Test::led)) == REGISTER_OK) printf("led is registered\r\n"); else printType(t); //wait for incoming CALL requests rpc.work(); }
Import programRPC_Wifly_HelloWorld
RPC hello world using the Wifly Interface
subnetwork and mbed_id
Don't forget to adapt your subnetwork and your mbed_id in the program:
Websocket ws("ws://sockets.mbed.org/rpc/<your_subnetwork>/<your_mbed_id>");
1.2 - You should be ready!¶
- You can now download this program into your mbed
Information
You should be able to control the LEDs of your mbed just by visiting:
http://tools.mbed.org/iot/rpc¶
If everything has been setted up correctly, you should see in a terminal:
1.3 Demo¶
In the rpc webpage, adapt the subnetwork and mbed_id fields to yours and click connect.
For instance:
You should now see 3 LEDs buttons to control your leds!
2 - Hello World over Ethernet¶
All steps are the same as for the WiflyInterface. Just grab this program instead:
Import programRPC_Ethernet_HelloWorld
RPC hello world over Ethernet
Conclusion¶
You are now able to call every registered functions on your sub-network from an Mbed connected to the RPC Websocket server on the same sub-network.
You can control everything from everywhere !