'user strict';
var config      = require('./config.js');
var firebase    = require('firebase');
// var rpio        = require('rpio');
var SerialPort = require('serialport');

firebase.initializeApp(config.firebase.default);

// GPIO 14 TX   // PIN 08 Transmit Serial Message
// GPIO 15 RX   // PIN 10 Read Serial Message
// GPIO 18 Read // PIN 12 Read Status by polling - There are not Interrupts in Raspberry PI
// rpio.open(8, rpio.OUTPUT, rpio.LOW);

var port = new SerialPort('/dev/ttyS0', {
    baudRate: 9600
});

// Set event handler for errors
port.on('error', function(err) {
  console.log('LOG Error: ', err.message);
})

// SmartDesk Protocols
// Reference: https://nodejs.org/api/buffer.html
const buttonDownBuffer          = Buffer.from([216, 216, 102, 1, 1]);
const buttonUpBuffer            = Buffer.from([216, 216, 102, 2, 2]);
const button1Buffer             = Buffer.from([216, 216, 102, 4, 4]);
const button2Buffer             = Buffer.from([216, 216, 102, 8, 8]);
const button3Buffer             = Buffer.from([216, 216, 102, 16, 16]);
const button4Buffer             = Buffer.from([216, 216, 102, 32, 32]);
const buttonConfigureBuffer     = Buffer.from([216, 216, 102, 64, 64]);

var smartdeskStateRef = firebase.database().ref('smartdeskState');
smartdeskStateRef.on('value', function(snapshot) {
    console.log(snapshot.val());

    for (var i = 0; i < 5; i++) {
        // rpio.writebuf(8, buttonUpBuffer);
        // rpio.msleep(250);
        port.write(buttonUpBuffer, function(err) {
            if (err) {
                return console.log('Error on write: ', err.message);
            }
            console.log('Message written');
        });
    }
});
@@ -89,19 +89,16 @@
 base');%0A
-// 
 var rpio
@@ -443,16 +443,231 @@
 o.LOW);%0A
+// var txBuff = Buffer.alloc(10);%0Aconst pirPin = 12;%0Avar pirBuffer = Buffer.alloc(10);%0Arpio.open(pirPin, rpio.INPUT);   // Read-Only Input%0Arpio.poll(pirPin, function() %7B%0A    console.log('TEST:::');%0A%7D);%0A%0A// Open port
 %0Avar por
@@ -724,17 +724,16 @@
 600%0A%7D);%0A
-%0A
 // Set e
@@ -834,16 +834,105 @@
 age);%0A%7D)
+;%0A// Read data%0Aport.on('readable', function () %7B%0A  console.log('Data:', port.read());%0A%7D);
 %0A%0A// Sma
@@ -1493,16 +1493,20 @@
 , 64%5D);%0A
+    
 %0Avar sma
@@ -1565,16 +1565,17 @@
 tate');%0A
+%0A
 smartdes

Read smartdesk state data