User Tools

Site Tools


Sidebar

Launch of Linino.org

wiki:tiltsensor

Controlling a Tilt Sensor with Linino

In this example we are going to use a device called tilt sensor. Inside the sensor are present a couple of marbles acting as electrical contact when the sensor is set in vertical positioning (closed switch). By tilting the sensor itself, the marbles don’t establish a real electrical contact so the sensor can be thought as an open switch.

The tilt sensor is a two pin device. One of them is directly fed by the circuit power supply while the other one is connected to the system ground by a resistor (we have used in our experiment a 10 KΩ resistor) and to an digital input of the Linino ONE board (yellow wire) as well.

Components :

  • one 10 KΩ resistor
  • tilt sensor

The Node Application

To develop an application, with the aim of creating a dynamic web page that shows the orientation of the Linino ONE card using the values returned by the tilt sensor, is necessary install Node.js platform and Ideino IDE which provide a set of libraries that allow to controll Linino ONE Board pin by Node.Js itself. By following the Linino.org wiki page: http://wiki.linino.org/doku.php?id=wiki:nodejscript is possible install Node.js platform and Ideino IDE by a simple sh script. After installing everything normally at url http://linino.local:2424 will be the Ideino IDE:

After logging-in to Ideino IDE with your root password, create a new project by clicking on the Combo-Box button located in the top-right side of the screen :

Once the new project is opened, we have to create a new javascript file to write a web server listening on a given port. The following code describes all the necessary steps.

Create a board object and a html object with the ideino-linino-lib :

var board = require('ideino-linino-lib').Board,
       html = require('ideino-linino-lib').Htmlboard,
       fs = require('fs'),
       http = require('http'),
       url = require('url'),
       path = require('path');

Javascript variables definition :

var pin8  = { pin: board.pin.digital.D8 , value : 0 },
      html_img_id = 'magnet',
      board_up = '"/html/img/board_up.jpg"',
      board_down = '"/html/img/board_down.jpg"';
 

pin8 represents the board digital pin D8 through which we usually read the digital values fed by the tilt sensor to the board.

html_img_id is the “tag img” of the html page we are going to interact with.

board_up/board_down contains the remote path to the image we are going to embed in our web page.

Creating a Web Server

This code describes the making of a http server listening on the localhost port number 1337 :

board.connect(function(){
    var file, page;
    http.createServer(function(req, res) {
        if(req.url.indexOf(".jpg") != -1 ){
            page = path.join(__dirname,'html','img',path.basename(req.url));
            file = fs.readFileSync(page);
            res.writeHead(200, {'Content-Type': 'image/jpeg'});
        }
        else{
            page = path.join(__dirname,'html','page-server-side-write-tilt.html');
            file = fs.readFileSync(page); 
            res.writeHead(200, {'Content-Type': 'text/HTML'});
        }
        res.end(file);        
    }).listen(1337)

Please take care to insert the correct path the html web page into the file object of the javascript code. This is a crucial point for the proper setup of your work environment.

To read the D8 pin digital values, we firstly have to invoke the pinMode function to set the input to a given pin and after that we set a conditional statement with the digitalRead function. According to the data collected by the pin of the board, the image embedded in the web page will be switched :

board.pinMode(pin8.pin, board.MODES.INPUT);   
board.digitalRead(pin8.pin, function( data ){
        if(data.value === 0)
            html.write(html_img_id,'src',board_up);
        else
            html.write(html_img_id,'src',board_down);
    });

The Html Page

Once you’re logged into ideino, open the examples project and then create a subfolder named “html”. The next step is to create a html file which in this case will be called as page-server-side-write-tilt.html. In addition, create another subfolder named “img” that will be containing our images to embed into the web page.

Please take care to insert the IP address of you board into the “src” field of the html page instead of the dynamic dns resolution of the boards itself, otherwise your project won’t properly work :

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=windows-1250">
    <script src="http://10.0.0.12:2424/js/ideino-linino-lib/socket.io.js"></script>
    <script src="http://10.0.0.12:2424/js/ideino-linino-lib/ideino-linino-lib-client.js"></script>
 
      <style>
        body {
            background-color: rgb(230, 230, 230);
        }
        div {
            text-align: center;
            vertical-align:middle;
        }
        img {
            position: absolute;
            width: 524px; /*the image width*/
            height: 164px; /*the image height*/
            top: 50%;
            left: 50%;
            margin-left: -262px;
            margin-top: -82px;
        }
      </style>
  </head>
  <body>
    <script type="text/javascript">
      window.onload = connect('10.0.0.12');
    </script>
 
        <img id="magnet" src="/html/img/board_up.jpg">
 
  </body>
</html>

/var/www/wiki.linino.org/data/pages/wiki/tiltsensor.txt · Last modified: 2014/05/25 02:31 by administrator

2014 © dog hunter llc and the Linino Community. Linino.org is a dog hunter sponsored community project. Credits