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