-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebtest.js
202 lines (168 loc) · 4.62 KB
/
webtest.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// initialize everything, web server, socket.io, filesystem, johnny-five
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
, url = require('url')
, five = require("johnny-five")
, mysql = require('mysql')
, connectionsArray = []
, connection = mysql.createConnection({
host: 'localhost',
user: 'dbuser',
password: 'dbpass',
database: 'nodejs',
port: 3306
})
, POLLING_INTERVAL = 500
, pollingTimer
, board1, board2,
servo,
led, led1, led2, led3, led4, led5,
sensor1, sensor2, sensor3, sensor4, sensor5;
// If there is an error connecting to the database
connection.connect(function(err) {
// connected! (unless `err` is set)
if (err) {
console.log(err);
}
});
var uno = new five.Board({ port: "COM7" });
//uno2 = new five.Board();
// on board ready
uno.on("ready", function() {
// init a led on pin 13, strobe every 1000ms
led = new five.Led(13).strobe(1000);
led1 = new five.Led(2);
led2 = new five.Led(3);
led3 = new five.Led(4);
led4 = new five.Led(5);
led5 = new five.Led(6);
// setup a stanard servo, center at start
servo = new five.Servo({
pin:10,
range: [0,180],
type: "standard",
center:true
});
// poll this sensor every second
sensor1 = new five.Sensor({
pin: "A0",
freq: 1000
});
sensor2 = new five.Sensor({
pin: "A1",
freq: 1000
});
});
// handle web server
function handler (req, res) {
var path = url.parse(req.url).pathname;
var fsCallback = function(error, data) {
if(error) throw error;
res.writeHead(200);
res.write(data);
res.end();
}
switch(path) {
case '/client':
doc = fs.readFile(__dirname + '/client.html', fsCallback);
break;
default:
doc = fs.readFile(__dirname + '/index.html', fsCallback);
break;
}
}
// make web server listen on port 80
app.listen(8080);
var pollingLoop = function() {
// Doing the database query
var query = connection.query('SELECT * FROM users'),
users = []; // this array will contain the result of our db query
// setting the query listeners
query
.on('error', function(err) {
// Handle error, and 'end' event will be emitted after this as well
console.log(err);
updateSockets(err);
})
.on('result', function(user) {
// it fills our array looping on each user row inside the db
users.push(user);
})
.on('end', function() {
// loop on itself only if there are sockets still connected
if (connectionsArray.length) {
pollingTimer = setTimeout(pollingLoop, POLLING_INTERVAL);
updateSockets({
users: users
});
} else {
console.log('The server timer was stopped because there are no more socket connections on the app')
}
});
};
var sensor1tmp;
// on a socket connection
io.sockets.on('connection', function (socket) {
socket.emit('news', { hello: 'world' });
// if board is ready
if(uno.isReady){
// read in sensor data, pass to browser
sensor1.on("data",function(){
if(this.value>336)
led1.on();
else
led1.off();
//check if sensor1's value changed
//if changed, perform
if(sensor1tmp != this.value) {
socket.emit('sensor1', { raw: this.value }); }
sensor1tmp = this.value;
});
}
// if servo message received
socket.on('servo', function (data) {
console.log(data);
if(uno.isReady){ servo.to(data.pos); }
});
// if led message received
socket.on('led1on', function () {
if(uno.isReady){ led1.on(); }
});
socket.on('led1off', function () {
if(uno.isReady){ led1.off(); }
});
// if led message received
socket.on('led2on', function () {
if(uno.isReady){ led2.on(); }
});
socket.on('led2off', function () {
if(uno.isReady){ led2.off(); }
});
// if led message received
socket.on('led3on', function () {
if(uno.isReady){ led3.on(); }
});
socket.on('led3off', function () {
if(uno.isReady){ led3.off(); }
});
// if led message received
socket.on('led4on', function () {
if(uno.isReady){ led4.on(); }
});
socket.on('led4off', function () {
if(uno.isReady){ led4.off(); }
});
// if led message received
socket.on('led5on', function () {
if(uno.isReady){ led5.on(); }
});
socket.on('led5off', function () {
if(uno.isReady){ led5.off(); }
});
// if led message received
socket.on('led', function (data) {
console.log(data);
if(uno.isReady){ led.strobe(data.delay); }
});
});