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