forked from Mangu/AzureIoTHandsOnLabs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
65 lines (54 loc) · 1.73 KB
/
client.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
var net = require('net');
var SerialPort = require('serialport');
var PIPE_NAME = "mypipe";
// for Windows...
//var PIPE_PATH = "\\\\.\\pipe\\" + PIPE_NAME;
// for linux
var PIPE_PATH = "./" + PIPE_NAME;
var x = 0;
// EDIT: update port name
// Windows
//portName = 'COM4';
// linux
portName = '/dev/ttyUSB0';
// open serial port
var myPort = new SerialPort(portName, {
baudRate: 9600,
parser: SerialPort.parsers.readline("\n")
});
// setup callback for getting serial data
myPort.on('data', sendSerialData);
// open named pipe for writing
var client = net.connect(PIPE_PATH, function() {
console.log('Client: on connection');
})
// if we received a command, and it contains the string "ON", then send the "ON" command down the serial port
// otherwise send "OFF"
client.on('data', function(data) {
console.log('Received Command:', data.toString());
if(data.toString().indexOf("ON") > -1)
{
myPort.write("ON\n");
myPort.drain();
console.log('Sending Command: ON');
}
else
{
myPort.write("OFF\n");
myPort.drain();
console.log('Sending Command: OFF');
}
});
client.on('end', function() {
console.log('Client: shutting down');
})
//when we receive data from the serial port (i.e. the "dumb" arduino device), send it down the named pipe to the sensor module
function sendSerialData(data) {
// this is here because on linux, we don't necessarily start with a clean serial buffer for some reason I'm too lazy to troubleshoot.. :-)
// sometimes the first 'read' gets gibberish so we just make sure that what we received on the read is 12 bytes
// (5 bytes of humidity including decimal point, a comma, 5 bytes of temp, and a newline)
if(data.length == 12) {
client.write(data.toString());
}
console.log(data);
}