IoT Cookbook > Arduino Yun > Accelerometer

Yun Accelerometer component

The Arduino Yun Accelerometer component publishes the raw accelerometer data from a Tinkerkit accelerometer module via WAMP.

Arduino Yun with accelerometer

Trying it out

The code for this can be found in the crossbarexamples GitHub repository under iotcookbook. You need to clone this (or download it as a ZIP file).

Open a shell for the component directory.

Start up Crossbar.io:

crossbar start

This also serves a frontend where you can view the accelerometer data logged at http://localhost:8080.

In accelerometer_yun.js, add the URL of the machine on which Crossbar.io runs:

var connection = new autobahn.Connection({
    // replace with the url of your crossbar instance
    url: "ws://<URL OF YOUR CROSSBAR INSTANCE>/ws",
    realm: "iot_cookbook"
});

You need to set up the Yun for using AutobahnJS, and have a Tinkerkit accelerometer connected. (This is expected on pins 2 and 3 / I2/3 on the Tinkerkit shield.)

Transfer accelerometer_yun.js onto the Yun, e.g. by doing

scp accelerometer_yun.js root@<IP of your Yun>:~/

Then run accelerometer_yun.js

node accelerometer_yun.js

This should log

Arduino Yun Accelerometer starting ...
Arduino connected (over /dev/ttyATH0, board version 2.3)
Connecting to router ...
Router connected. Session ID: 1595783623

Once this is running, open the browser console for the frontend page, and you'll see the raw accelerometer data logged as it is received.

received accelerometer data Object {y: 566, x: 551}
received accelerometer data Object {y: 566, x: 552}
received accelerometer data Object {y: 565, x: 551}
...

and when you move the accelerometer beyond the acceleration threshold

received accelerometer data Object {y: 978, x: 504}
alarm activated!
received accelerometer data Object {y: 772, x: 543}
alarm activated!
received accelerometer data Object {y: 753, x: 564}
alarm activated!
...

The API

The accelerometer component publishes the raw accelerometer data as JSON to the topic io.crossbar.examples.yun.accelerometer.on_accelerometer_data.

Using it

In your own project: