Last active
June 8, 2017 12:48
-
-
Save matbor/c61560746c9c38d751070311ed338c18 to your computer and use it in GitHub Desktop.
Example of receiving an mqtt temperature for https://github.com/KhaosT/HAP-NodeJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var OutsideTemperature = 0.0; | |
// MQTT Setup | |
var mqtt = require('mqtt'); | |
console.log("Connecting to MQTT broker..."); | |
var mqtt = require('mqtt'); | |
var options = { | |
port: 1883, | |
host: '192.168.4.24', | |
clientId: 'AdyPi_OutsideTemperatureSensor' | |
}; | |
var client = mqtt.connect(options); | |
console.log("Outside Temperature Sensor Connected to MQTT broker"); | |
client.subscribe('/house/weather/outside1/temperature/current/1'); | |
client.on('message', function(topic, message) { | |
console.log(topic + ' -> ' + parseFloat(message)); | |
OutsideTemperature = parseFloat(message); | |
}); | |
var Accessory = require('../').Accessory; | |
var Service = require('../').Service; | |
var Characteristic = require('../').Characteristic; | |
var uuid = require('../').uuid; | |
// Generate a consistent UUID for our Temperature Sensor Accessory that will remain the same | |
// even when restarting our server. We use the `uuid.generate` helper function to create | |
// a deterministic UUID based on an arbitrary "namespace" and the string "temperature-sensor". | |
var sensorUUID = uuid.generate('hap-nodejs:accessories:temperature-sensor'); | |
// This is the Accessory that we'll return to HAP-NodeJS that represents our fake lock. | |
var sensor = exports.accessory = new Accessory('Outside Temperature Sensor', sensorUUID); | |
// Add properties for publishing (in case we're using Core.js and not BridgedCore.js) | |
sensor.username = "C3:5D:3A:AE:5E:FB"; // needs to be unique if you have multiple accessories | |
sensor.pincode = "031-45-154"; | |
// Add the actual TemperatureSensor Service. | |
// We can see the complete list of Services and Characteristics in `lib/gen/HomeKitTypes.js` | |
sensor | |
.addService(Service.TemperatureSensor) | |
.getCharacteristic(Characteristic.CurrentTemperature) | |
.on('get', function(callback) { | |
// return our current value | |
callback(null, OutsideTemperature); | |
}); | |
// randomize our temperature reading every 3 seconds | |
setInterval(function() { | |
// update the characteristic value so interested iOS devices can get notified | |
sensor | |
.getService(Service.TemperatureSensor) | |
.setCharacteristic(Characteristic.CurrentTemperature, OutsideTemperature); | |
}, 3000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment