Skip to content
Rémi Bèges edited this page Apr 12, 2016 · 9 revisions

This tutorial will guide you on how to establish bi-directionnal communication between an Mbed device and your computer, in just a few lines of code.

If you don't know yet how to setup telemetry on mbed, see the previous tutorial.

For this tutorial, you will also need to import the mbed library.

Publishing

Let's start off by sending data from the mbed board to the computer. With telemetry, sending data is called publishing. Any data is published to a topic, which is basically a label.

The following code publishes random data every 300 ms to the computer.

#include "Telemetry.h"
#include "mbed.h"

int main()
{
    Telemetry TM;
    
    Timer refresh;
    refresh.start();

    for( ; ; )
    {
        if(refresh.read_ms() > 300)
        {
            refresh.reset();
            TM.pub_u32("rand",rand());    
        }
    }

First, the Telemetry class is instantiated. By default it will use the serial port (UART) with a baudrate of 9600 bauds.

Then, a timer is created to limit the rate at which we are going to publish.

Finally, every 300 ms random data is published on a topic called "rand".

Full code on mbed and importable program

If you connect to the device with pytelemetrycli, you can run a few commands to observe the result desktop-side: todo : animated ttty gif

 >: ls
foo
 >: print foo
123

Subscribing

Minimal code

At this point, you know how to send data from the device to the computer.

The other way around is just as simple.

telemetry lets you attach any variable to a given topic.

Every time a new value under the given topic is received, the attached variable will be updated (if received type and variable type match). This is called subscribing a variable to a topic.

void main()
{
    uint8_t myInc = 0;
    Telemetry TM;

    // Subscribe myInc variable to topic "foo"
    TM.attach_u8_to("foo",&myInc); 

    for( ; ; )
    {
        TM.update();
    }
}

In this code, the variable myInc is updated with any new value received under topic "foo" with a payload of type uint8. The update is performed during TM.update().

You can attach all standard integer types and float numbers to any topic using this method.

Conclusion and going further

At this point, you are now able to exchange data with a computer in both ways, with minimal efforts.

You may want to check at this point more advanced features or the list of examples.

Setup

Get started for embedded platforms

Get started for remote debug and remote control

  • Fast data visualization with the command line interface (todo)
  • Fast prototyping remote program control with python (todo)

General knowledge

Troubleshooting

  • Frequently Asked Questions todo

Examples and projects

Clone this wiki locally