daily pastebin goal
53%
SHARE
TWEET

Untitled

a guest Sep 4th, 2018 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var Accessory = require('../').Accessory;
  2. var Service = require('../').Service;
  3. var Characteristic = require('../').Characteristic;
  4. var uuid = require('../').uuid;
  5.  
  6. var SENSOR = {
  7.   currentTemperature: 25,
  8.   currentHumidity: 50,
  9.   getTemperature: function() {
  10.     console.log("Getting the current temperature!");
  11.     return SENSOR.currentTemperature;
  12.   },
  13.   getHumidity: function() {
  14.     console.log("Getting the current humidity!");
  15.     return SENSOR.currentHumidity;
  16.   }
  17. }
  18.  
  19. // MQTT Setup
  20. var mqtt = require('mqtt');
  21. console.log("Connecting to MQTT broker...");
  22. var mqtt = require('mqtt');
  23. var options = {
  24.   port: 1883,
  25.   host: 'localhost',
  26.   //username: "",
  27.   //password:"",
  28.   clientId: 'Dorm Sensor'
  29. };
  30. var client = mqtt.connect(options);
  31. console.log("Dorm Sensor Connected to MQTT broker");
  32. client.subscribe('sensor/#');
  33. client.on('message', function(topic, message) {
  34.   console.log(parseFloat(message));
  35.   // message is Buffer
  36.   data = JSON.parse(message);
  37.   if (data === null) {return null}
  38.   if(topic == 'sensor/temperature') {
  39.     SENSOR.currentTemperature = parseFloat(data);
  40.   } else if (topic == 'sensor/humidity') {
  41.     SENSOR.currentHumidity = parseFloat(data);
  42.   }
  43. });
  44.  
  45. // Generate a consistent UUID for our Temperature Sensor Accessory that will remain the same
  46. // even when restarting our server. We use the `uuid.generate` helper function to create
  47. // a deterministic UUID based on an arbitrary "namespace" and the string "sensor".
  48. var sensorUUID = uuid.generate('hap-nodejs:accessories:sensor');
  49.  
  50. // This is the Accessory that we'll return to HAP-NodeJS that represents our fake lock.
  51. var sensor = exports.accessory = new Accessory('Sensor', sensorUUID);
  52.  
  53. // Add properties for publishing (in case we're using Core.js and not BridgedCore.js)
  54. sensor.username = "C4:5D:1A:1E:52:FA";
  55. sensor.pincode = "031-45-154";
  56.  
  57. // Add the actual TemperatureSensor Service.
  58. // We can see the complete list of Services and Characteristics in `lib/gen/HomeKitTypes.js`
  59. sensor
  60.   .addService(Service.TemperatureSensor)
  61.   .getCharacteristic(Characteristic.CurrentTemperature)
  62.   .on('get', function(callback) {
  63.    
  64.     // return our current value
  65.     callback(null, SENSOR.getTemperature());
  66.   });
  67.  
  68. // Add the Humidity Service
  69. sensor
  70.   .addService(Service.HumiditySensor)
  71.   .getCharacteristic(Characteristic.CurrentRelativeHumidity)
  72.   .on('get', function(callback) {
  73.  
  74.     // return our current value
  75.     callback(null, SENSOR.getHumidity());
  76.   });
  77.  
  78. // update readings every 10 seconds
  79. setInterval(function() {
  80.  
  81.   // update the characteristic value so interested iOS devices can get notified
  82.   sensor
  83.     .getService(Service.TemperatureSensor)
  84.     .setCharacteristic(Characteristic.CurrentTemperature, SENSOR.currentTemperature);
  85.  
  86.   sensor
  87.     .getService(Service.HumiditySensor)
  88.     .setCharacteristic(Characteristic.CurrentRelativeHumidity, SENSOR.currentHumidity);
  89.  
  90. }, 3000);
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top