IoT Cookbook > Arduino Yun > Tilt Sensor

Yun Tilt Sensor component

The Arduino Yun Tilt Sensor component allows querying the current state of a Tinkerkit tilt sensor and publishes events when this state changes.

Arduino Yun with tilt sensor

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).

You need to have one Tinkerkit tilt sensor connected to I0 on the Tinkerkit shield (pin 0).

Open a shell for the component directory.

Start up Crossbar.io:

crossbar start

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

In tilt_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, including setting up Firmata on the MCU part of the Yun.

Transfer tilt_yun.js on the Yun, e.g. by doing

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

The script is run using Node.js, so you need to have this installed ('opkg install node').

Then run tilt_yun.js

node tilt_yun.js

This should log

Arduino Yun Tilt Sensor 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 reload the page. You shoud see

connected
tilt state currently is:  false
...

and when you tilt the sensor

Tilt state changed to:  true
Tilt state changed to:  false
Tilt state changed to:  true
Tilt state changed to:  false
...

The API

The component provides one procedure io.crossbar.examples.yun.tilt.get_is_tilted which does not require any arguments and returns a Boolean for the tilt state.

It also publishes changes in the tilt state to `io.crossbar.examples.yun.tilt.on_is_tilted``where the changed tilt state, as a Boolean, is sent as an argument.

Using it

In your own project: