Created
August 14, 2015 14:15
-
-
Save mokjpn/988f697cfb95c85b4a36 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
serial = require('serialport') | |
mqtt = require 'mqtt' | |
SerialPort = serial.SerialPort | |
sp = new SerialPort("/dev/tty.usbserial-XXXXXXXX", { # replace this to your serial port name. | |
baudrate: 115200, | |
parser: serial.parsers.readline("\n") | |
}) | |
atdesk = 0 | |
total = 0 | |
totaldesk = 0 | |
sp.on('open', -> | |
sp.on('data', (data) -> | |
#console.log data | |
deviceID = data.slice(1, 3) # 1 | |
#console.log deviceID | |
if deviceID == '00' | |
DI1 = data.slice(33, 35) # c | |
DIc = data.slice(35,37) # d | |
#console.log DI1 | |
dic = parseInt(DIc,16) & 0x01 | |
atdesk = ! parseInt(DI1, 16) & 0x01 | |
if dic | |
total = total + 1 | |
totaldesk = totaldesk + atdesk | |
) | |
) | |
host = 'mqtt://mqtthost:mqttport/' # replace this to your MQTT server's hostname and port. | |
client = mqtt.connect(host, { username: 'user', password : 'password' }) # replace username and password to your MQTT server's account. | |
topic= 'mokada/intheroom' | |
setInterval -> | |
if totaldesk / total > 0.1 | |
message = "In" | |
else | |
message = "Out" | |
console.log totaldesk,total,message | |
totaldesk = 0 | |
total = 0 | |
client.publish topic, JSON.stringify { date: Date.now().toString(), status: message } | |
, 30000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment