Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. var five = require('johnny-five');
  2. var raspi = require('raspi-io');
  3. var Device = require('losant-mqtt').Device;
  4.  
  5. // Construct Losant device.
  6. var device = new Device({
  7. id: 'my-device-id',
  8. key: 'my-access-key',
  9. secret: 'my-access-secret'
  10. });
  11.  
  12. // Connect the device to Losant.
  13. device.connect();
  14.  
  15. var board = new five.Board({
  16. io: new raspi()
  17. });
  18.  
  19. board.on('ready', function() {
  20.  
  21. var led = new five.Led('P1-7')
  22. var button = new five.Button({
  23. pin: 'P1-11',
  24. isPullup: true
  25. });
  26.  
  27. button.on('down', function() {
  28. console.log('Button pressed')
  29. // When the button is pressed, send the state to Losant.
  30. device.sendState({ button: true });
  31. });
  32.  
  33. // Listen for commands from Losant.
  34. device.on('command', function(command) {
  35. console.log('Received a command: ' + command.name)
  36. if(command.name === 'toggle') {
  37. led.toggle();
  38. }
  39. });
  40. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement