Skip to content

Instantly share code, notes, and snippets.

@matbor
Last active June 8, 2017 12:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matbor/c61560746c9c38d751070311ed338c18 to your computer and use it in GitHub Desktop.
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
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