Mbed Client/OS Read/Write

Hi. I have been looking at the examples for mbed os/client, where it shows how to define resources both static and dynamic. This is cool stuff but it is a one way comunication. It is probably a stupid question and maybe I’ll figure it out once I actually have my mbed device, but is there an example somewhere where the device also listens to the calls and responds thereafter.

For example could I switch on an LED from a REST call? I guess what I am looking for is a callback when a resouce is being accessed. I can see there is an example setting up a tcp server but to me that seems like it is missing the trick really.

The biggest reason I think is should be possible is this line from the documentation, “/endpoints/node-001/dev/temp?noResp=true”. To me nResp suggests there is some sort of two way communication but I might be wrong.

Hope the question makes sense.

Erik

In answer to my own question set_execute_function in M2MResourceInstance looks interesting. I am still not sure how to set a value for a resource from the REST api though.

Thanks

Erik

I am hoping one can change a value simply by using a PUT or a POST on that resource. Can’t find any documentation on the format but with a device it should be possible to work out by trial and error.

Cheers

Erik

Hi Erik, you can use value_updated function. This is already present in the example main.cpp. Then you can post data to the device via connector, e.g.:

curl -X PUT -d "THIS IS SPARTA" -v -H 'Authorization: Bearer YOUR_AUTH_TOKEN' https://api.connector.mbed.com/endpoints/YOUR_DEVICE/Test/0/D

Then read the data on the device again via:

void value_updated(M2MBase *base, M2MBase::BaseType type) {
    output.printf("\nValue updated of Object name %s and Type %d\n",
           base->name().c_str(), type);

    M2MResource* res = (M2MResource*)base;
    uint8_t* buffer = NULL;
    uint32_t length = 0;
    res->get_value(buffer, length);
    output.printf("Got data len=%d data=%s\n", length, (char*)buffer);
}

I’ll be publishing an example with end-to-end code later this week on the blog.

Hi janjongboom, cheers for the reply. Yeah, once I got my board delivered and could run the code, I discovered the value_update callback. It’s all very neat this mbed stuff. :slightly_smiling:

Thanks

Erik

@esigth

The better way to do this is to trigger events with a POST call. When you define your resource make sure to enable POST
res->set_operation(M2MBase::GET_PUT_POST_ALLOWED);

, then you add this res->set_execute_function(execute_callback(this,&MbedClient::resourceCallbackFn));
to the resource declaration statement. This will cause the following function to be called whenever a post is made to the resource

/* * Callback function to handle POST data, registed below * The argument passed in is a null terminated string representing the POST request payload. */ void resourceCallbackFn(void *postData) { printf("\r\nPOST callback!, payload received : '%s'\r\n",postData); // Toggle LED static DigitalOut led(LED1); led = !led; }

The GET/PUT methods are meant to be used to keep data in sync with mbed connector. To trigger events you should use POST methods. The method that @janjongboom reccomended does not work unless you hardcode this, where as adding POST callback functions is far more flexible and easier to scale with multiple resources.

For a more robust example see here : https://github.com/BlackstoneEngineering/mbed-client-examples/tree/c99e993aad8c47ea93268880e90c77e45720b4ff

Cheers. :slightly_smiling: That looks like a nice way of doing thing…

Erik