Touchiot/main.js

Thu, 04 Jun 2015 14:50:33 +0200

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 04 Jun 2015 14:50:33 +0200
changeset 0
e8ccd40d0ef6
permissions
-rw-r--r--

Genesis of lecture sources for Droidcon Berlin 2015 in Postbahnhof.

michael@0 1 /*jslint node:true,vars:true, unparam:true */
michael@0 2 /*jshint unused:true */
michael@0 3
michael@0 4
michael@0 5 /*
michael@0 6 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.
michael@0 7
michael@0 8 MRAA - Low Level Skeleton Library for Communication on GNU/Linux platforms
michael@0 9 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.
michael@0 10
michael@0 11 Steps for installing MRAA & UPM Library on Intel IoT Platform with IoTDevKit Linux* image
michael@0 12 Using a ssh client:
michael@0 13 1. echo "src maa-upm http://iotdk.intel.com/repos/1.1/intelgalactic" > /etc/opkg/intel-iotdk.conf
michael@0 14 2. opkg update
michael@0 15 3. opkg upgrade
michael@0 16
michael@0 17 Article: https://software.intel.com/en-us/html5/articles/iot-touch-notifier-nodejs-and-html5-samples
michael@0 18 */
michael@0 19
michael@0 20 //MRAA Library was installed on the board directly through ssh session
michael@0 21 var mraa = require("mraa");
michael@0 22 var mqtt = require("mqtt");
michael@0 23 var http = require("http");
michael@0 24
michael@0 25 function setOpenhabState(txtNewState) {
michael@0 26 http.get("http://192.168.42.245:8080/CMD?door_bell="+txtNewState);
michael@0 27 }
michael@0 28
michael@0 29 //GROVE Kit Shield D6 --> GPIO6
michael@0 30 //GROVE Kit Shield D2 --> GPIO2
michael@0 31 function startSensorWatch(socket) {
michael@0 32 'use strict';
michael@0 33 var touch_sensor_value = 0, last_t_sensor_value;
michael@0 34 var mqtcli = mqtt.connect('mqtt://raspeinz.local/');
michael@0 35
michael@0 36 //Touch Sensor connected to D2 connector
michael@0 37 var digital_pin_D2 = new mraa.Gpio(2);
michael@0 38 digital_pin_D2.dir(mraa.DIR_IN);
michael@0 39
michael@0 40 //Buzzer connected to D6 connector
michael@0 41 var digital_pin_D6 = new mraa.Gpio(6);
michael@0 42 digital_pin_D6.dir(mraa.DIR_OUT);
michael@0 43
michael@0 44 digital_pin_D6.write(0);
michael@0 45
michael@0 46 setInterval(function () {
michael@0 47 touch_sensor_value = digital_pin_D2.read();
michael@0 48 if (touch_sensor_value === 1 && last_t_sensor_value === 0) {
michael@0 49 console.log("Buzz ON!!!");
michael@0 50 socket.emit('message', "present");
michael@0 51 mqtcli.publish('door', 'ding');
michael@0 52 //mqtcli.end(); // end connection
michael@0 53 // FUR ULRICH! //http.get("http://ulno.local:8080/CMD?door_bell=ON");
michael@0 54 digital_pin_D6.write(touch_sensor_value);
michael@0 55 } else if (touch_sensor_value === 0 && last_t_sensor_value === 1) {
michael@0 56 console.log("Buzz OFF!!!");
michael@0 57 //socket.emit('message', "absent");
michael@0 58 mqtcli.publish('door', 'dong');
michael@0 59 // FUR ULRICH! //http.get("http://ulno.local:8080/CMD?door_bell=OFF");
michael@0 60 digital_pin_D6.write(touch_sensor_value);
michael@0 61 }
michael@0 62 last_t_sensor_value = touch_sensor_value;
michael@0 63 }, 500);
michael@0 64 }
michael@0 65
michael@0 66 //Create Socket.io server
michael@0 67 //var http = require('http'); // See top requires
michael@0 68 var app = http.createServer(function (req, res) {
michael@0 69 'use strict';
michael@0 70 res.writeHead(200, {'Content-Type': 'text/plain'});
michael@0 71 res.end('<h1>Hello world from Intel IoT platform!</h1>');
michael@0 72 }).listen(1338);
michael@0 73 var io = require('socket.io')(app);
michael@0 74
michael@0 75 console.log("Sample Reading Touch Sensor");
michael@0 76
michael@0 77 //Attach a 'connection' event handler to the server
michael@0 78 io.on('connection', function (socket) {
michael@0 79 'use strict';
michael@0 80 console.log('a user connected');
michael@0 81 //Emits an event along with a message
michael@0 82 socket.emit('connected', 'Welcome');
michael@0 83
michael@0 84 //Start watching Sensors connected to Galileo board
michael@0 85 startSensorWatch(socket);
michael@0 86
michael@0 87 //Attach a 'disconnect' event handler to the socket
michael@0 88 socket.on('disconnect', function () {
michael@0 89 console.log('user disconnected');
michael@0 90 });
michael@0 91 });

mercurial