Hope someone can help, I am working on an MQTT Arduino, Raspberry pi, Hap-Node homekit solution for my automated driveway gate. Have been able to configure code to enable the gate to open and close using Homekit (garage) with the help of everyone around, however am having difficulty getting the Homekit to recognise when the gate triggers an actual open or close status. I have this status being read by the MQTT broker as a “1” and “0” but cant figure out the accessory.js code to be able to communicate with homekit to switch the open or close status on the IPhone (ie if I manually use a remote to open the gate etc)… any help would be greatly appreciated as I am not very good at coding.
Code below
//START SETUP
var garageName = ‘Gate’;
var uuidTag = ‘garage’;
//END SETUP
var Accessory = require(’…/’).Accessory;
var Service = require(’…/’).Service;
var Characteristic = require(’…/’).Characteristic;
var uuid = require(’…/’).uuid;
var MQTT_IP = ‘192.168.0.21’
var mqtt_topic = ‘/Home/Gate’
//MQTT Setup
var mqtt = require(‘mqtt’);
var options = {
port: 1883,
host: MQTT_IP,
clientId: ‘cdfadgbhK343’
};
var client = mqtt.connect(options);
client.on(‘message’, function(topic, message) {
});
var garageUUID = uuid.generate(‘hap-nodejs:accessories:’+uuidTag);
var garage = exports.accessory = new Accessory(garageName, garageUUID);
var GARAGE_DOOR = {
opened: false,
open: function() {
console.log(“Opening the Garage!”);
client.publish(mqtt_topic, ‘1’);
GARAGE_DOOR.opened = true;
},
close: function() {
console.log(“Closing the Garage!”);
client.publish(mqtt_topic, ‘0’);
GARAGE_DOOR.opened = false;
},
identify: function() {
console.log(“Identify the Garage”);
}
};
// Add properties for publishing (in case we’re using Core.js and not BridgedCore.js)
garage.username = “C1:5D:3F:F2:5E:FA”; //edit this if you use Core.js
garage.pincode = “031-45-154”;
garage
.getService(Service.AccessoryInformation)
.setCharacteristic(Characteristic.Manufacturer, “Liftmaster”)
.setCharacteristic(Characteristic.Model, “Rev-1”)
.setCharacteristic(Characteristic.SerialNumber, “TW000165”);
garage.on(‘identify’, function(paired, callback) {
GARAGE_DOOR.identify();
callback();
});
garage
.addService(Service.GarageDoorOpener, “Garage Door”)
.setCharacteristic(Characteristic.TargetDoorState, Characteristic.TargetDoorState.CLOSED) // force initial state to CLOSED
.getCharacteristic(Characteristic.TargetDoorState)
.on(‘set’, function(value, callback) {
if (value == Characteristic.TargetDoorState.CLOSED) {
GARAGE_DOOR.close();
callback();
garage
.getService(Service.GarageDoorOpener)
.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.CLOSED);
}
else if (value == Characteristic.TargetDoorState.OPEN) {
GARAGE_DOOR.open();
Service.GarageDoorOpener.LockCurrentState=false;
Service.GarageDoorOpener.LockTargetState=false
garage
.getService(Service.GarageDoorOpener)
.setCharacteristic(Characteristic.CurrentDoorState, Characteristic.CurrentDoorState.OPEN);
callback();
}
});
garage
.getService(Service.GarageDoorOpener)
.getCharacteristic(Characteristic.CurrentDoorState)
.on(‘get’, function(callback) {
var err = null;
if (GARAGE_DOOR.opened) {
console.log("Query: Is Garage Open? Yes.");
callback(err, Characteristic.CurrentDoorState.OPEN);
}
else {
console.log("Query: Is Garage Open? No.");
callback(err, Characteristic.CurrentDoorState.CLOSED);
}
});