Samuel Mokrani / Mbed 2 deprecated RPC_Ethernet_HelloWorld

Dependencies:   EthernetInterface MbedJSONRpc MbedJSONValue WebSocketClient mbed-rtos mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "MbedJSONRpc.h"
00003 #include "MbedJSONValue.h"
00004 #include "EthernetInterface.h"
00005 #include "Websocket.h"
00006 
00007 Serial pc(USBTX, USBRX);
00008 BusOut l(LED1, LED2, LED3, LED4);
00009 
00010 //websocket: configuration with sub-network = demo and mbed_id = mbed_led 
00011 Websocket ws("ws://sockets.mbed.org/rpc/demo/mbed_led");
00012 
00013 //RPC object attached to the websocket server
00014 MbedJSONRpc rpc(&ws);
00015 
00016 //Test class
00017 class Test {
00018 public:
00019     Test() {};
00020     void led(MbedJSONValue& in, MbedJSONValue& out) {
00021         int id = in[0].get<int>();
00022         l = (1 << (id - 1));
00023         wait(0.2);
00024         l = 0;
00025     }
00026 };
00027 
00028 Test test;
00029 
00030 int main() {
00031 
00032     EthernetInterface eth;
00033     eth.init(); //Use DHCP
00034     eth.connect();
00035     printf("IP Address is %s\n\r", eth.getIPAddress());
00036     
00037     ws.connect();
00038     
00039     RPC_TYPE t;
00040 
00041     //------------register getAcc---------------//
00042     if((t = rpc.registerMethod("led", &test, &Test::led)) == REGISTER_OK)
00043         printf("led is registered\r\n");
00044     else
00045         printType(t);
00046     
00047     //wait for incoming CALL requests
00048     rpc.work();
00049 }