RPC variable Javascript

15 Dec 2010

Hello

I'm trying to change the value of a variable (f in my code) on the mbed via HTTP.

I want to use javascript as i can see on this page http://mbed.org/cookbook/Interfacing-with-JavaScript and the RPCVariable library

my mbed code

/*
Copyright (c) 2010 ARM Ltd
 
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
 
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "mbed.h"
#include "EthernetNetIf.h"
#include "HTTPServer.h"
#include "RPCVariable.h"

DigitalOut led1(LED1, "led1");
DigitalOut led2(LED2, "led2");
DigitalOut led3(LED3, "led3");
DigitalOut led4(LED4, "led4");

LocalFileSystem fs("webfs");

EthernetNetIf eth;  
HTTPServer svr;

int main() {
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();
    Base::add_rpc_class();

    float f;
    int i;
    char c;
    RPCVariable rpc_f(&f, "f");
    RPCVariable rpc_i(&i, "i");
    RPCVariable rpc_c(&c, "c");

  printf("Setting up...\n");
  EthernetErr ethErr = eth.setup();
  if(ethErr)
  {
    printf("Error %d in setup.\n", ethErr);
    return -1;
  }
  printf("Setup OK\n");
  
  FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
  FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
  
  svr.addHandler("/hello");
  svr.addHandler("/rpc");
  svr.addHandler("/files");
  svr.addHandler("/"); //Default handler
  //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
  
  svr.bind(80);
  
  printf("Listening...\n");
    
  Timer tm;
  tm.start();
  
  //Listen indefinitely
  while(true)
  {
    Net::poll();
    if(tm.read()>.5)
    {
      led1=!led1; //Show that we are alive
      tm.start();
    }
  }
  
  return 0;

}
The problem is that Idon't know what to put in my html code to use the RPCVariable and to change the value of f.

 

20 Dec 2010

No one for that?

20 Dec 2010

Hi

The use of the RPCVariable is very similar to the RPCFunction. Within the javascript you need to frist create your RPCVariable, passing the mbed object and the name you gave the RPCVariable on mbed.

MyVar = new RPCVariable(mbed, "MyVar_name_as_on_mbed");

To write to it:

MyVar.write(<the value to write>)

To read from it depends on the type of result you expect to get.

i = MyVar.read_int();
f = MyVar.read_float();
StringResult = MyVar.read();

The Interfacing Using JavaScript should show you how to set up the connection to mbed and give an example of how to fit the JavaScript into your HTML page. Hope this helps.

Michael