User Tools

Site Tools


Sidebar

Launch of Linino.org

wiki:reedswitch

Control a Reed Switch with Linino

In this example we are going to use a small device called reed switch. When the device detects a magnetic field around it, the two ferrous materials inside the switch are attracted to each other and so the switch is closed. When the action of the magnetic field is ceased, the ferrous material separate and the switch goes back to its open state.

The reed switch 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 means of 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
  • reed switch
  • a magnet

The Node Application

So, to create an application allowing us to control the force sensor leveraging Linino OS, you need to install the Node.Js platform and the Ideino IDE which makes available a set of embedded libraries allowing us to control the Linino ONE board pins by means of Node.Js itself. Please refer to this guide to perform a hassle-free installation of Node.Js and Ideino : http://wiki.linino.org/doku.php?id=wiki:nodejscript.

The code script we are going to write will be able to read the state of the digital pin D8 on the Linino ONE and, according to this state, modify the image embedded in the linked web page.

In this particular example, the web page plays an alarm sound that is enableded when the digital pin state is a high one (i.e. digital logic state 1), while if the state is a low one the alarm is disabled.

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_gif_id = 'alarm',
    alarm_on= '/html/img/alarm_on.gif',
    alarm_off= '/html/img/alarm_off.gif';
 

pin8 represents the board digital pin D8 through which we usually read the digital values fed by the reed switch 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(".gif") != -1 ){
            page = path.join(__dirname,'html','img',path.basename(req.url));
            file = fs.readFileSync(page);
            res.writeHead(200, {'Content-Type': 'image/gif});
        }
        else{
            page = path.join(__dirname,'html','alarm.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 sound alarm embedded in the web page will be played or not through the html bindings :

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

The Html Page

Once you’re logged into ideino, open the examples project and then create a subfolder named “hmtl”. The next step is to create a html file which in this case will be called as alarm.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 board itself, otherwise your project won’t work properly.

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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>
  </head>
  <body >
 
    <script type="text/javascript">
      window.onload = connect('10.0.0.12');
    </script>
    <div align="center">
      <img id="alarm" src="/html/img/alarm_off.gif" weidth="100%" height="100%" >
    </div>
  </body>
</html>

/var/www/wiki.linino.org/data/pages/wiki/reedswitch.txt · Last modified: 2014/05/21 06:16 by arturo

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