THIS EXAMPLE IS DEPRECATED AND IS SUPERCEDED BY ANOTHER EXAMPLE AVAILABLE HERE mbed-os-example-client
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.
The application:
- 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.
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.h
in 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.bin
to 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.
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.
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.h
file, find the parameterSOCKET_MODE
. - The default is
M2MInterface::UDP
. - To switch to TCP, change it to
M2MInterface::TCP
.
Then re-build and flash the application.
Tip: The instructions in this document remain the same, irrespective of the socket mode you select.
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
- 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
SW2
button on the device a number of times (make a note of how many times you did that). - Go to Device Connector > API Console.
- Enter
https://api.connector.mbed.com/endpoints/DEVICE_NAME/3200/0/5501
in the URI field and click TEST API. ReplaceDEVICE_NAME
with your actual endpoint name. The device name can be found in thesource/security.h
file, see variableMBED_ENDPOINT_NAME
or it can be found from the traces Monitoring the application. - The number of times you pressed
SW2
is shown. - Press the
SW3
button to unregister from mbed Device Connector. You should seeUnregistered Object Successfully
printed to the serial port and LED starts blinking. This will also stop your application. Press theRESET
button 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, blinksLED1
when executed (POST).3201/0/5853
. Blink pattern, used by the blink function to determine how to blink. In the format of1000:500:1000:500:1000:500
(PUT).
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.