这个项目使用Apples homekit和raspberry pi(https://github.com/KhaosT/HAP-NodeJS)上的node.js服务器来打开/关闭设备。所以Light_accessory.js,当vaule为真(1)时,使用子进程和接线pi打开灯(继电器)。当值为假(0)时,它还需要关闭灯(gpio写入7 1)。我尝试添加"可能等于"这样它也可以关闭灯泡(继电器)。试图添加两个值是由于我和一夜的谷歌搜索和语法错误.. 我花了更多的时间在这个项目上比我想承认的更多..相当简单的目标非常类似于我用PHP做了一段时间的项目。
?php
if(isset($_GET['trigger']) && $_GET['trigger'] == 1) {
error_reporting(E_ALL);
exec('gpio write 7 0');
}
if(isset($_GET['trigger']) && $_GET['trigger'] == 2) {
error_reporting(E_ALL);
exec('gpio write 7 1');
}
?>
............................................... .................................................. .................................................. ...............
当值为true(1)时,这是将gpio设置为低(gpio写入7 0)的位置。
{
cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) {
exec('gpio write 7 0' + value,function(error, stdout, stderr) {}
);
},
perms: ["pw", "pr", "ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn On the Light",
designedMaxLength: 1
}, {
如何添加
exec('gpio write 7 1'(error, stdout, stderr)
当javascript中的值为0时?这样灯(继电器)也会关闭。
............................................... .................................................. .................................................. ...............
Light_accessory.js的粗略概述;最重要的部分主要是产品描述而不是实际功能。 " cType:types.POWER_STATE_CTYPE,"在" OnUpdate:"是神奇发生的地方。
............................................... .................................................. .................................................. ...............
完整的Light_accessory脚本
// HomeKit types required
var types = require("./types.js")
var exports = module.exports = {};
var exec = require('child_process').exec;
var execute = function(accessory, characteristic, value) {
console.log("executed accessory: " + accessory + ", and characteristic: " + characteristic + ", with value: " + value + ".");
}
exports.accessory = {
displayName: "Light 1",
username: "1A:2B:3C:4D:5E:FF",
pincode: "031-45-154",
services: [{
sType: types.ACCESSORY_INFORMATION_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Light 1",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.MANUFACTURER_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Oltica",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.MODEL_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Rev-1",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.SERIAL_NUMBER_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "A1S2NASF88EW",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.IDENTIFY_CTYPE,
onUpdate: null,
perms: ["pw"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Identify Accessory",
designedMaxLength: 1
}]
}, {
sType: types.LIGHTBULB_STYPE,
characteristics: [{
cType: types.NAME_CTYPE,
onUpdate: null,
perms: ["pr"],
format: "string",
initialValue: "Light 1",
supportEvents: false,
supportBonjour: false,
manfDescription: "Bla",
designedMaxLength: 255
}, {
cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) {
exec('gpio write 7 0' + value,function(error, stdout, stderr) {}
);
},
perms: ["pw", "pr", "ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn On the Light",
designedMaxLength: 1
}, {
cType: types.HUE_CTYPE,
onUpdate: function(value) {
console.log("Change:", value);
execute("Test Accessory 1", "Light - Hue", value);
},
perms: ["pw", "pr", "ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "Adjust Hue of Light",
designedMinValue: 0,
designedMaxValue: 360,
designedMinStep: 1,
unit: "arcdegrees"
}, {
cType: types.BRIGHTNESS_CTYPE,
onUpdate: function(value) {
console.log("Change:", value);
execute("Test Accessory 1", "Light - Brightness", value);
},
perms: ["pw", "pr", "ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "Adjust Brightness of Light",
designedMinValue: 0,
designedMaxValue: 100,
designedMinStep: 1,
unit: "%"
}, {
cType: types.SATURATION_CTYPE,
onUpdate: function(value) {
console.log("Change:", value);
execute("Test Accessory 1", "Light - Saturation", value);
},
perms: ["pw", "pr", "ev"],
format: "int",
initialValue: 0,
supportEvents: false,
supportBonjour: false,
manfDescription: "Adjust Saturation of Light",
designedMinValue: 0,
designedMaxValue: 100,
designedMinStep: 1,
unit: "%"
}]
}]
}
答案 0 :(得分:1)
弗洛姆自己搞乱这个包裹,我注意到它的价值是真的'或者' false'不是1或0
尝试检查是否然后写出1
value == true ? 1 : 0
根据要求,这里使用您的示例。
{
cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) {
exec('gpio write 7 0 ' + (value == true ? 1 : 0) ,function(error, stdout, stderr) {}
);
},
perms: ["pw", "pr", "ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn On the Light",
designedMaxLength: 1
}
答案 1 :(得分:0)
{
cType: types.POWER_STATE_CTYPE,
onUpdate: function(value) {
if(value == 1){
exec('gpio write 7 0')
setTimeout(function() {exec('gpio write 7 1')},250)
}
if(value == 0){
exec('gpio write 6 0')
setTimeout(function() {exec('gpio write 6 1')},250)
}
},
perms: ["pw", "pr", "ev"],
format: "bool",
initialValue: false,
supportEvents: false,
supportBonjour: false,
manfDescription: "Turn On the Light",
designedMaxLength: 1
},