THIS EXAMPLE IS DEPRECATED AND IS SUPERCEDED BY ANOTHER EXAMPLE AVAILABLE HERE mbed-os-example-client
Getting started with mbed Client on mbed OS
This is the mbed Client example for mbed OS (we also have one for Linux). It demonstrates how to register a device with mbed Device Connector, how to read and write values, and how to deregister. If you are unfamiliar with mbed Device Connector, we recommend that you read the introduction to the data model first.
- Registers with mbed Device Connector.
- Gives mbed Device Connector access to its resources (read and write).
- Records the number of clicks on the device’s button and sends the number to mbed Device Connector.
- Lets you control the blink pattern of the LED on the device (through mbed Device Connector).
- An FRDM-K64F board.
- An Ethernet connection to the internet.
- An Ethernet cable.
- A micro-USB cable.
- An ARM mbed account.
- yotta - to build the example programs. To learn how to build mbed OS applications with yotta, see the user guide.
- A serial port monitor.
To set up the example, please:
- Build the example.
- Set up an IP address. This step is optional.
- Set a socket type. This step is optional.
Building the example
To build the example application:
- Clone this repository.
- Go to mbed Device Connector and log in with your mbed account.
- On mbed Device Connector, go to My Devices > Security credentials, and get new credentials for your device by clicking the Get my device security credentials button.
- Store the credentials as
source/security.hin this project's directory.
- Open a command line tool and navigate to the project’s directory.
- Set yotta's build target. For example, if you are targeting the FRDM-K64F board:
yotta target frdm-k64f-gcc.
- Build the application by using the command
yotta build. yotta builds a binary file in the project’s directory.
- Plug the Ethernet cable into the board.
- Plug the micro-USB cable into the OpenSDA port. The board is listed as a mass-storage device.
- Drag the binary
build/frdm-k64f-gcc/source/mbed-client-examples.binto the board to flash the application.
- The board is automatically programmed with the new binary. A flashing LED on it indicates that it is still working. When the LED stops blinking, the board is ready to work.
- Press the RESET button to run the program.
- For verification, continue to the Monitoring the application chapter.
IP address setup (optional)
This example uses IPv4 to communicate with the mbed Device Connector Server. The example program should automatically get an IPv4 address from the router when connected over Ethernet.
If your network does not have DHCP enabled, you have to manually assign a static IP address to the board. We recommend having DHCP enabled to make everything run smoothly.
Changing socket type (binding mode - optional)
Your device can connect to mbed Device Connector via one of two binding modes: UDP or TCP. The default is UDP.
To change the binding mode:
- In the
simpleclient.hfile, find the parameter
- The default is
- To switch to TCP, change it to
Then re-build and flash the application.
Tip: The instructions in this document remain the same, irrespective of the socket mode you select.
Monitoring the application
The application prints debug messages over the serial port, so you can monitor its activity with a serial port monitor. The application uses baud rate 115200.
Note: Instructions to set this up are located here.
After connecting you should see messages about connecting to mbed Device Connector:
In app_start() IP address 10.2.15.222 Device name 6868df22-d353-4150-b90a-a878130859d9
Note: Device name is the endpoint name you will need later on Testing the application chapter.
After you click the
SW2 button on your board you should see messages about the value changes:
handle_button_click, new value of counter is 1
Testing the application
- Flash the application.
- Verify that the registration succeeded. You should see
Registered object successfully!printed to the serial port.
- On mbed Device Connector, go to My devices > Connected devices. Your device should be listed here.
- Press the
SW2button on the device a number of times (make a note of how many times you did that).
- Go to Device Connector > API Console.
https://api.connector.mbed.com/endpoints/DEVICE_NAME/3200/0/5501in the URI field and click TEST API. Replace
DEVICE_NAMEwith your actual endpoint name. The device name can be found in the
source/security.hfile, see variable
MBED_ENDPOINT_NAMEor it can be found from the traces Monitoring the application.
- The number of times you pressed
- Press the
SW3button to unregister from mbed Device Connector. You should see
Unregistered Object Successfullyprinted to the serial port and LED starts blinking. This will also stop your application. Press the
RESETbutton to run the program again.
NOTE: If you get an error, for example
Server Response: 410 (Gone), clear your browser's cache, log out, and log back in.
NOTE: Only GET methods can be executed through Device Connector > API Console. For the other methods check the mbed Device Connector Quick Start.
The application exposes three resources:
3200/0/5501. Number of presses of SW2 (GET).
3201/0/5850. Blink function, blinks
LED1when executed (POST).
3201/0/5853. Blink pattern, used by the blink function to determine how to blink. In the format of
For information on how to get notifications when resource 1 changes, or how to use resources 2 and 3, take a look at the mbed Device Connector Quick Start.