Touchiot/main.js

changeset 0
e8ccd40d0ef6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/Touchiot/main.js	Thu Jun 04 14:50:33 2015 +0200
     1.3 @@ -0,0 +1,91 @@
     1.4 +/*jslint node:true,vars:true, unparam:true */
     1.5 +/*jshint unused:true */
     1.6 +
     1.7 +
     1.8 +/*
     1.9 +The Touch Notifier Node.js sample application distributed within Intel® XDK IoT Edition under the IoT with Node.js Projects project creation option showcases how to read digital data from a Grover Starter Kit Plus – IoT Intel® Edition Touch Sensor, start a web server and communicate wirelessly using WebSockets.
    1.10 +
    1.11 +MRAA - Low Level Skeleton Library for Communication on GNU/Linux platforms
    1.12 +Library in C/C++ to interface with Galileo & other Intel platforms, in a structured and sane API with port nanmes/numbering that match boards & with bindings to javascript & python.
    1.13 +
    1.14 +Steps for installing MRAA & UPM Library on Intel IoT Platform with IoTDevKit Linux* image
    1.15 +Using a ssh client: 
    1.16 +1. echo "src maa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/intel-iotdk.conf
    1.17 +2. opkg update
    1.18 +3. opkg upgrade
    1.19 +
    1.20 +Article: https://software.intel.com/en-us/html5/articles/iot-touch-notifier-nodejs-and-html5-samples
    1.21 +*/
    1.22 +
    1.23 +//MRAA Library was installed on the board directly through ssh session
    1.24 +var mraa = require("mraa");
    1.25 +var mqtt = require("mqtt");
    1.26 +var http = require("http");
    1.27 +
    1.28 +function setOpenhabState(txtNewState) {
    1.29 +    http.get("http://192.168.42.245:8080/CMD?door_bell="+txtNewState);
    1.30 +}
    1.31 +
    1.32 +//GROVE Kit Shield D6 --> GPIO6
    1.33 +//GROVE Kit Shield D2 --> GPIO2
    1.34 +function startSensorWatch(socket) {
    1.35 +    'use strict';
    1.36 +    var touch_sensor_value = 0, last_t_sensor_value;
    1.37 +    var mqtcli = mqtt.connect('mqtt://raspeinz.local/');
    1.38 +
    1.39 +    //Touch Sensor connected to D2 connector
    1.40 +    var digital_pin_D2 = new mraa.Gpio(2);
    1.41 +    digital_pin_D2.dir(mraa.DIR_IN);
    1.42 +
    1.43 +    //Buzzer connected to D6 connector
    1.44 +    var digital_pin_D6 = new mraa.Gpio(6);
    1.45 +    digital_pin_D6.dir(mraa.DIR_OUT);
    1.46 +
    1.47 +    digital_pin_D6.write(0);
    1.48 +
    1.49 +    setInterval(function () {
    1.50 +        touch_sensor_value = digital_pin_D2.read();
    1.51 +        if (touch_sensor_value === 1 && last_t_sensor_value === 0) {
    1.52 +            console.log("Buzz ON!!!");
    1.53 +            socket.emit('message', "present");
    1.54 +            mqtcli.publish('door', 'ding');
    1.55 +            //mqtcli.end(); // end connection
    1.56 +// FUR ULRICH!            //http.get("http://ulno.local:8080/CMD?door_bell=ON");
    1.57 +            digital_pin_D6.write(touch_sensor_value);
    1.58 +        } else if (touch_sensor_value === 0 && last_t_sensor_value === 1) {
    1.59 +            console.log("Buzz OFF!!!");
    1.60 +            //socket.emit('message', "absent");
    1.61 +            mqtcli.publish('door', 'dong');
    1.62 +// FUR ULRICH!            //http.get("http://ulno.local:8080/CMD?door_bell=OFF");
    1.63 +            digital_pin_D6.write(touch_sensor_value);
    1.64 +        }
    1.65 +        last_t_sensor_value = touch_sensor_value;
    1.66 +    }, 500);
    1.67 +}
    1.68 +
    1.69 +//Create Socket.io server
    1.70 +//var http = require('http'); // See top requires
    1.71 +var app = http.createServer(function (req, res) {
    1.72 +    'use strict';
    1.73 +    res.writeHead(200, {'Content-Type': 'text/plain'});
    1.74 +    res.end('<h1>Hello world from Intel IoT platform!</h1>');
    1.75 +}).listen(1338);
    1.76 +var io = require('socket.io')(app);
    1.77 +
    1.78 +console.log("Sample Reading Touch Sensor");
    1.79 +
    1.80 +//Attach a 'connection' event handler to the server
    1.81 +io.on('connection', function (socket) {
    1.82 +    'use strict';
    1.83 +    console.log('a user connected');
    1.84 +    //Emits an event along with a message
    1.85 +    socket.emit('connected', 'Welcome');
    1.86 +
    1.87 +    //Start watching Sensors connected to Galileo board
    1.88 +    startSensorWatch(socket);
    1.89 +
    1.90 +    //Attach a 'disconnect' event handler to the socket
    1.91 +    socket.on('disconnect', function () {
    1.92 +        console.log('user disconnected');
    1.93 +    });
    1.94 +});

mercurial