Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
MQTT for Raspberry Pi
var mqtt = require('mqtt')
//var Gpio = require('onoff').Gpio; //include onoff to interact with the GPIO
var LEDA = new Gpio(3, 'out'); //use GPIO pin 4, and specify that it is output
var LEDB = new Gpio(4, 'out'); //use GPIO pin 4, and specify that it is output
const say = require('say')
function esperando() {
// stuff you want to happen right away
console.log('Prendiendo foco y esperando 2 segundos');
}
var options = {
port: 1883,
//host: mqtt_config.noclass_url,
host: "192.168.5.10",
// The CA list will be used to determine if server is authorized
protocol: 'mqtt'
}
var client = mqtt.connect(options)
// SUSCRIBIR A ACTIVIDADES PARA REGISTRARLAS
client.on('connect', function () {
console.log('Conectado a MQTT con exito.')
client.subscribe('#')
})
client.on('message', function (topic, message) {
msg = message.toString()
if(topic === "/cocoa/caseta/rfid/a") {
if(msg === "abrir") {
console.log("Aqui suena ruido y se prende foco de la A")
say.speak("Welcome A", 'Samantha', 1)
LEDA.writeSync(1); //set pin state to 1 (turn LED on)
setTimeout(esperando, 2000);
LEDA.writeSync(0); //set pin state to 0 (turn LED off)
// prender foco
}
} else if(topic === "/cocoa/caseta/rfid/b") {
if(msg === "abrir") {
console.log("Aqui suena ruido y se prende foco de la B")
say.speak("Welcome B", 'Samantha', 1)
LEDB.writeSync(1); //set pin state to 1 (turn LED on)
setTimeout(esperando, 2000);
LEDB.writeSync(0); //set pin state to 0 (turn LED off)
}
}
})
client.on('error', function(err) {
console.log(err);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.