【教程-智能家居】通过Siri用树莓派和homekit进行交互

目前已知实验成功的树莓派版本为:树莓派2和3

由于我是用Word排版的,直接复制粘贴,因此部分命令可能缺少空格,请注意!


This tutorial only works with Jessie

(此教程只适用于Jessie版本!)

STEP 1
如何安装HAP_Nodejs
(不要安装在/root文件夹下面)
sudo apt-get update
sudo apt-get upgrade
sudo apt-get remove nodejs-legacy
sudo apt-get install python-dev python-pip
sudo apt-get install libavahi-compat-libdnssd-dev
sudo wget http://node-arm.herokuapp.com/node_latest_armhf.deb
dpkg -i node_latest_armhf.deb
node -v
npm -v
git clone https://github.com/KhaosT/HAP-NodeJS.git
cd HAP-NodeJS
sudo npm install -g node-gyp
sudo npm install node-persist
sudo npm install srp
sudo npm install mdns
sudo npm install ed25519
sudo npm install curve25519-n
sudo npm install debug
sudo npm install python-shell


cd accessories
sudo nano deskLamp_accessory.js
copy this into:

(把下面的代码复制进去)

var PythonShell = require('python-shell');
// HomeKit types required
var types = require("./types.js")
var exports = module.exports ={};
 
var execute = function(accessory,characteristic,value){ console.log("executed accessory:" + accessory + ", and characteristic: " + characteristic +", with value: " +  value +"."); }
 
exports.accessory = {
  displayName: "desk Lamp",
  username: "1A:2B:3C:4D:1E:FF", #mac addressneeds to be unique
  pincode: "031-45-154",
  services: [{
    sType: types.ACCESSORY_INFORMATION_STYPE,
    characteristics: [{
    cType:types.NAME_CTYPE,
    onUpdate:null,
    perms:["pr"],
        format: "string",
        initialValue: "desk Lamp", #initialValue needs to besame as displayName
        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 Light Service",
        supportEvents: false,
        supportBonjour: false,
        manfDescription: "Bla",
        designedMaxLength: 255  
    },{
    cType:types.POWER_STATE_CTYPE,
    onUpdate:function(value)
    {
        console.log("Change:",value);
        if(value) {
            PythonShell.run('/python/light1.py', function (err) {
                console.log('OnSuccess');
            });# light1.py is the python script you want to run when turningon the light
 
        }else {
            PythonShell.run('/python/light0.py',function (err) {
                console.log("OffSuccess");
            });# light0.py isthe python script you want to run when turning off the light
 
        }
    },
    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("TestAccessory 1", "Light - Hue", value); },
    perms:["pw","pr","ev"],
        format: "int",
        initialValue: 0,
        supportEvents: false,
        supportBonjour: false,
        manfDescription: "Doesn’t actually adjust Hue of Light",
        designedMinValue: 0,
        designedMaxValue: 360,
        designedMinStep: 1,
        unit: "arcdegrees"
    },{
    cType:types.BRIGHTNESS_CTYPE,
    onUpdate:function(value) { console.log("Change:",value); execute("TestAccessory 1", "Light - Brightness", value); },
    perms:["pw","pr","ev"],
        format: "int",
        initialValue: 0,
        supportEvents: false,
        supportBonjour: false,
        manfDescription: "Doesn’t actually adjust Brightness ofLight",
        designedMinValue: 0,
        designedMaxValue: 100,
        designedMinStep: 1,
        unit: "%"
    },{
    cType:types.SATURATION_CTYPE,
    onUpdate:function(value) { console.log("Change:",value); execute("TestAccessory 1", "Light - Saturation", value); },
    perms:["pw","pr","ev"],
        format: "int",
        initialValue: 0,
        supportEvents: false,
        supportBonjour: false,
        manfDescription: "Doesn’t actually adjust Saturation ofLight",
        designedMinValue: 0,
        designedMaxValue: 100,
        designedMinStep: 1,
        unit: "%"
    }]
  }]
}


cd ..
mkdir python
cd python
nano light1.py

Copy this into:

(复制下面的代码进去)

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, 1)

Save, and close the file

(保存,关闭文件)

nano light0.py

Copy this into:

(复制下面的代码进去)

import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT)
GPIO.output(16, 0)

Save, and close the file

(保存,关闭文件)

Connect a led GND into one of the RPi GND pin. and connect the led "+" pin tho the RPi GPIO.16 pin. That’s the 8 pin in the outer line.

(将led正极接GPIO16引脚,负极接地)

Test the python codes:

(测试下刚才的python代码)

python light1.py 

(led should turn on)(led应该点亮)

python light0.py 

(led should turn off)(led应该熄灭)

If everything is ok, go back to HAP-NodeJS folder.

(如果一切顺利,回到HAP-NodeJS文件夹

cd ..
npm rebuild
node Core.js

Now you can open you Homekit app, parsing it with your accessories!

(现在你可以打开你的homekit软件,开始配对,输入pincode即为deskLamp_accessory.js中的031-45-154

STEP 2

(Let’ssay we want code.js to run automatically, then we need install forever.)

how to install forever on your Raspberry Pi.

(如何安装forever)

Only install this if you are installing the HAP-NodeJS server


The first command you want to run is: 

cd

The second command you want to run is: 

sudo npm install forever -g

That should complete the installation! Now, you can use the forever command wherever youneed it. Ensure you do use sudo or elseit won't work.

cd HAP-NodeJS
sudo forever start Core.js

Then it’s done


STEP 3

Start HAP on Reboot

(开机自启HAP-NodeJS)

Ensure your HAP-NodeJS directory is /home/pi/HAP-NodeJS

(确保你的HAP-NodeJS安装在/home/pi/HAP-NodeJS文件夹下)

cd
sudo nano /etc/rc.local

Now, you want to add the code before the "exit0" line. Add this line before "exit 0":

(在"exit 0"前面加上下面这段代码)

cd /home/pi/HAP-NodeJS
sudo forever start Core.js
Then you're ready to go!

(大功告成!)

参考链接:1.  https://github.com/KhaosT/HAP-NodeJS点击打开链接

        2.  http://www.instructables.com/id/Raspberry-Pi-2-Homekit-from-zero-to-Hey-Siri/?ALLSTEPS点击打开链接

你可能感兴趣的:(raspberry,pi3)