Created
November 20, 2016 15:49
-
-
Save jamesbulpin/f5b3e6f71ffd43f32e2a5d2f8c4d16f7 to your computer and use it in GitHub Desktop.
A simple Meshblu connector to play base64 encoded mp3 and drive a Big Mouth Billy Bass motors.
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
// Put a meshblu.json file in the same directory as this file. This must | |
// contain the Meshlu device UUID and token, e.g.: | |
// {"server":"meshblu.octoblu.com","port":80,"uuid":"01234567-89ab-cdef-0123-456789abcdef","token":"123456789012345678901234567890"} | |
var meshblu = require('meshblu'); | |
var meshbluJSON = require("./meshblu.json"); | |
var exec = require('child_process').exec; | |
var tempfile = require('tempfile'); | |
var fs = require('fs'); | |
var SerialPort = require("serialport"); | |
// Specifies how you want your message payload to be passed | |
// from Octoblu to your device | |
var MESSAGE_SCHEMA = { | |
type: 'object', | |
properties: { | |
mp3: { | |
type: 'string', | |
required: true | |
} | |
} | |
}; | |
var portdev = "/dev/ttyACM0"; | |
var port = new SerialPort(portdev, { | |
baudRate: 9600, | |
parser: SerialPort.parsers.readline('\n') | |
}); | |
port.on('error', function(err) { | |
console.log('Error: ', err.message); | |
}) | |
port.on('data', function (data) { | |
console.log('Data: ' + data); | |
conn.message({ | |
"devices": "*", | |
"command": data | |
}); | |
}); | |
// We'll process messages sequentially - build a queue | |
function MyController() { | |
this.timeout = 100; | |
this.queue = []; | |
this.ready = true; | |
}; | |
MyController.prototype.exec = function() { | |
this.queue.push(arguments); | |
this.process(); | |
}; | |
MyController.prototype.action = function(message) { | |
var self = this; | |
console.log("Processing message..."); | |
if ('mp3' in message) { | |
var mp3file = tempfile(".mp3"); | |
port.write("HEAD MOVE\n"); | |
fs.writeFile(mp3file, new Buffer(message.mp3, "base64"), function(err) { | |
if (err) { | |
console.log("Error writing temporary MP3 file: " + err); | |
fs.unlinkSync(mp3file); | |
port.write("HEAD RELEASE\n"); | |
self.ready = true; | |
self.process(); | |
} | |
else { | |
exec("omxplayer " + mp3file, function(err, stdout, stderr) { | |
console.log('stdout: ' + stdout); | |
console.log('stderr: ' + stderr); | |
if (err !== null) { | |
console.log('exec error: ' + err); | |
} | |
port.write("TAIL MOVE\n"); | |
setTimeout(function() { | |
port.write("TAIL RELEASE\n"); | |
}, 500); | |
setTimeout(function() { | |
self.ready = true; | |
self.process(); | |
}, 1500); | |
}); | |
} | |
}); | |
} | |
}; | |
MyController.prototype.process = function() { | |
if (this.queue.length === 0) return; | |
if (!this.ready) return; | |
var self = this; | |
this.ready = false; | |
this.action.apply(this, this.queue.shift()); | |
}; | |
var messageController = new MyController(); | |
// Not specifying a UUID/Token auto-registers a new device | |
var conn = meshblu.createConnection({ | |
"uuid": meshbluJSON.uuid, | |
"token": meshbluJSON.token, | |
"server": "meshblu.octoblu.com", | |
"port": 80 | |
}); | |
conn.on('notReady', function(data){ | |
console.log('Octoblu UUID FAILED AUTHENTICATION!'); | |
console.log(data); | |
}); | |
conn.on('ready', function(rdata){ | |
console.log('UUID AUTHENTICATED!'); | |
console.log(rdata); | |
conn.update({ | |
"uuid": meshbluJSON.uuid, | |
"token": meshbluJSON.token, | |
"messageSchema": MESSAGE_SCHEMA, | |
}); | |
conn.on('message', function(data){ | |
console.log('Octoblu message received'); | |
//console.log(data); | |
messageController.exec(data); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment