Getting started with ARM mbed OS and wot.io
This past week, I managed to get some free time to dig into ARM mbed OS, and get a simple application talking to wot.io. The most obvious way to do this was to use ARM's own mbed-client interface to communicate with an instance of the mbed Device Server. As mbed Device Server (MDS) is already integrated into wot.io's Data Service Exchange, as soon as I had the mbed-client code integrated into an application, I should be able to get my data into an existing workflow.
Looking at the supported board options, and what I had laying on my desk already, I decided to use the Freescale FRDM-k64f, since it's Ethernet interface has a supported lwip driver. The board comes prepopulated with a FXOS8700CQ accelerometer and magnetometer, which has a very similar interface to the Freescale MMA8452Q that I've used in a few of my projects. While there is no obvious direct mbed OS support for the accelerometer in the FRDM-k64f hardware abstraction layer, we should be able to use the HAL support I2C interface to talk to it. ARM already provided a FXOS8700Q sensor driver under the Apache 2 license that can be easily ported to mbed OS.
Reading over the mbedos documentation, and yotta documentations, I managed to set up both a local development environment on my Mac, and I also setup a home lab development environment using a Docker container. The Docker container makes it easy to move my development environment from machine to machine, such as from home lab to office lab and back again.
Creating a new project using yotta is straight forward using the text based wizard:
Here I've created a project called accel and creates a skeleton of the module.json file which describes to yotta how to manage the dependencies for our applications. To install our own dependencies, we can use the yotta install to inject our dependency.
At the time of this writing this will modify the module.json file as follows:
If you were to do this at a later date, the version string saying ^1.1.15 will probably be different. ARM mbed OS is undergoing rapid development, and the file I generated just last week was ^1.1.11, almost a patch a day! This rapid development can be seen in all aspects of the system. On any given day, the yotta build command which actually compiles the binary application, will return different deprecation warnings, and occasionally entire libraries are in an unusable state. Generally the optimized builds will compile cleanly, but I have had problems with yotta build --debug-build failing to compile due to register coloring issues. That said, as ARM mbed OS leaves beta, I expect these issues will be resolved.
To setup the development environment for the Freescale FRDM-k64f, it is also necessary to select the appropriate target:
This configures our build environment to build for the FRDM-k64f using arm-none-eabi-gcc. A complete list of available target architectures can be obtained using the yotta search target command:
Switching which C++ compiler you use requires switching the full target. Switching targets will fetch the associated target dependencies as well, and as such it is important to build after you've selected your target. With the target set to frdm-k64f-gcc, we can build the source code for this project.
The behavior is as follows:
- initialize i2c
- initialize FXOS8700QAccelerometer
- initialize Ethernet adapter
- acquire a DHCP lease
- initialize an ipv4 tcp/ip stack
- pick a random port and
- create a M2MInterface object to connect to MDS
- create a M2MSecurity context for our MDS connection
- create a M2MDevice object to create the device's OMNA LWM2M resources
- create a M2MObject that will actually represent our device sensor tree
- create two M2MResources for our x and y axis (not following the OMNA LWM2M conventions)
- add our M2MObject and M2MDevice to a list to be used to update registrations
- setup a timer to update the registration every 20 seconds
- enable the accelerometer
- setup a timer to sample the accelerometer every 5 seconds
- setup a callback to setup the device and resource registrations
- start the main scheduler loop
To build the application, we can simply use the yotta build command to generate a accel.bin file in build/frdm-k64f-gcc/source/. This is the build artifact that is our application binary. The boot loader on the FRDM-k64f board knows how to load this file on reset. We can install this application by copying the accel.bin file to the USB storage device (on my Mac /Volumes/MBED).
Once the the application binary is installed, the device registers itself with mbed Device Server (MDS) running on our demo cluster. The data is then available to all of the services which request device notifications. The routing layer inside of the wot.io data service exchange ensures that only those users who have the rights to see the data from a given device can see it.
As the wot.io data service exchange supports multiple protocols, we can use an off the shelf command line client to read the data from the data service exchange. Just to quickly test getting this into a script, we can use wscat, a node module which speaks the RFC6455 WebSocket protocol: