/******************************************************************************* * Copyright (c) 2013, 2014 Benjamin Cabe and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * Benjamin Cabe - initial API and implementation *******************************************************************************/ /** Serial port configuration **/ var serialport = require("serialport"); var SerialPort = serialport.SerialPort; // localize object constructor var sp = new SerialPort("/dev/ttyAMA0", { baudrate: 57600, parser: serialport.parsers.readline("\n") }); /** MQTT client configuration **/ var mqtt = require('mqtt'); var mqttClient = mqtt.createClient(1883, 'iot.eclipse.org'); mqttClient.subscribe('benjamin/rapiro/command'); sp.on("open", function() { console.log('serial port opened'); }); function publishPicture(error, stdout, stderr) { console.log("... sending the picture") mqttClient.publish('benjamin/rapiro/pic', stdout); console.log("... picture sent!") } var sys = require('sys') var exec = require('child_process').exec; var cmd = function(topic, message) { if(message === "TAKE_PICTURE") { console.log("Taking a picture!") exec("raspistill -w 320 -h 240 -hf -vf -t 1 -o - | base64", publishPicture); } else { console.log("Asking Rapiro to execute command: " + message) sp.write(message + '\n'); } }; mqttClient.on('message', cmd);