Last active
September 8, 2017 15:50
-
-
Save andriiburka/f2352f659d6db7a622e61f08e8592166 to your computer and use it in GitHub Desktop.
HAP-NodeJS temperature sensor DHT11
This file contains 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 Accessory = require('../').Accessory; | |
var Service = require('../').Service; | |
var Characteristic = require('../').Characteristic; | |
var uuid = require('../').uuid; | |
var mqtt = require('mqtt'); | |
var mqttMSG = 0; | |
var name = "Sonoff Temperature Sensor"; //accessory name | |
var sonoffUUID = "hap-nodejs:accessories:sonoff:temperature:" + name; //change this to your preferences | |
var sonoffUsername = "FC:52:15:EB:54:27"; | |
var MQTT_NAME = 'LivingRoom/SonoffLight' //MQTT topic that was set on the Sonoff firmware | |
var MQTT_IP = 'localhost' //change this if your MQTT broker is different | |
var options = { | |
port: 1883, | |
host: MQTT_IP, | |
// username: 'pi', enable only if you have authentication on your MQTT broker | |
// password: 'raspberry', enable only if you have authentication on your MQTT broker | |
clientId: MQTT_NAME+'HAP' | |
}; | |
var sonoffTopic = 'cmnd/'+MQTT_NAME+'/status'; | |
var client = mqtt.connect(options); | |
client.on('connect', function () { | |
client.subscribe('stat/'+MQTT_NAME+'/STATUS10') | |
}); | |
var sonoffObject = { | |
currentTemperature: 38, | |
getReadings: function() { | |
client.publish(sonoffTopic, '10'); | |
}, | |
identify: function() { | |
console.log(name + " Identified!"); | |
} | |
} | |
var sonoffTemp = exports.accessory = new Accessory(name, uuid.generate(sonoffUUID)); | |
sonoffTemp.username = sonoffUsername; | |
sonoffTemp.pincode = "031-45-154"; | |
// listen for the "identify" event for this Accessory | |
sonoffTemp.on('identify', function(paired, callback) { | |
sonoffObject.identify(); | |
callback(); | |
}); | |
sonoffTemp | |
.addService(Service.TemperatureSensor) | |
.getCharacteristic(Characteristic.CurrentTemperature) | |
.on('get', function(callback) { | |
// return our current value | |
callback(null, sonoffObject.currentTemperature); | |
}); | |
client.on('message', function(topic, message) { | |
// console.log(message.toString()); | |
message = message.toString(); | |
mqttMSG = JSON.parse(message); | |
// console.log(mqttMSG.StatusSNS.DHT11.Temperature); | |
sonoffObject.currentTemperature = mqttMSG.StatusSNS.DHT11.Temperature; | |
}); | |
setInterval(function() { | |
sonoffObject.getReadings(); | |
sonoffTemp | |
.getService(Service.TemperatureSensor) | |
.setCharacteristic(Characteristic.CurrentTemperature, sonoffObject.currentTemperature); | |
}, 60000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment