setInterval(function() { // Report state to Losant. if(device.isConnected()) { device.sendState({ key: 'value' }); } }, 1000);
setInterval(function() { // Turn on the LED. led.write(1); // Read temp voltage and convert. var tempRaw = temp.read(); var degreesC = (((tempRaw / 1024) * 5) - 0.52) * 100; var degreesF = degreesC * 1.8 + 32; console.log(degreesC); console.log(degreesF); // Report state to Losant. device.sendState({ temp: degreesF }); // Wait a little and turn off the LED. setTimeout(function() { led.write(0); }, 500); }, 1000);
board.on('ready', function() { var led = new five.Led('P1-7') var button = new five.Button({ pin: 'P1-11', isPullup: true }); button.on('down', function() { console.log('Button pressed') // When the button is pressed, send the state to Losant. device.sendState({ button: true }); }); // Listen for commands from Losant. device.on('command', function(command) { console.log('Received a command: ' + command.name) if(command.name === 'toggle') { led.toggle(); } }); });
/* eslint no-console: "off"*/ var mraa = require('mraa'); var Device = require('losant-mqtt').Device; // Reading temperature from analog input. var temp = new mraa.Aio(0); // Blinking an LED everytime temperature is read. var led = new mraa.Gpio(7); led.dir(mraa.DIR_OUT); // Construct a device instance. var device = new Device({ id: 'my-device-id', key: 'my-access-key', secret: 'my-access-secret' }); // Connect device to Losant. device.connect(); // Attach event listener for commands. device.on('command', function(command) { console.log(command.name); console.log(command.payload); }); // Once a second, read the temp and report to Losant. setInterval(function() {
button.on('down', function() { console.log('Button pressed') // When the button is pressed, send the state to Losant. device.sendState({ button: true }); });
var five = require('johnny-five'); var raspi = require('raspi-io'); var Device = require('losant-mqtt').Device; // Construct Losant device. var device = new Device({ id: 'my-device-id', key: 'my-access-key', secret: 'my-access-secret' }); // Connect the device to Losant. device.connect(); var board = new five.Board({ io: new raspi() }); board.on('ready', function() { var led = new five.Led('P1-7') var button = new five.Button({ pin: 'P1-11', isPullup: true }); button.on('down', function() { console.log('Button pressed') // When the button is pressed, send the state to Losant. device.sendState({ button: true }); });