『瀚思彼岸』» 智能家居技术论坛

标题: HomeAssistant-Bridge-Mqtt安装教程(以测试无坑)(ubuntu树莓派d... [打印本页]

作者: 人生观    时间: 2017-8-15 13:28
标题: HomeAssistant-Bridge-Mqtt安装教程(以测试无坑)(ubuntu树莓派d...
本帖最后由 人生观 于 2017-8-21 13:10 编辑

本帖资料来源于本站和互联网搜索引擎!!! 本人只是简单规整HomeAssistant安装教程(Jones)本站大神代码
在安装PIP的时候如果出现失败本帖提供本地安装文件[attach]1771[/attach]
  1. #首先改一下sudoer设置,省得sudo时总要求输密码sudo nano /etc/sudoers

  2. #在最下面(看好,是此文件的最下方,否则会被下面的设置所覆盖而无效!)添加以下内容(jones改为你的用户名):
  3. jones ALL=NOPASSWD: ALL

  4. #可选项,如果在安装ubuntu时没有更改时区的,使用下面的代码更改时区
  5. sudo dpkg-reconfigure tzdata

  6. #必选项!更换国内源!如果不更换,sudo apt-get update会非常慢
  7. #首先是备份原源地址,然后将虚线间的代码加入sources.list,contrl + x, y 退出
  8. sudo mv /etc/apt/sources.list /etc/apt/sources.list.bak
  9. sudo nano /etc/apt/sources.list
  10. ---------------------------------------------------------------------------------------------------------
  11. deb http://mirrors.aliyun.com/ubuntu/ yakkety main restricted
  12. deb http://mirrors.aliyun.com/ubuntu/ yakkety-updates main restricted
  13. deb http://mirrors.aliyun.com/ubuntu/ yakkety universe
  14. deb http://mirrors.aliyun.com/ubuntu/ yakkety-updates universe
  15. deb http://mirrors.aliyun.com/ubuntu/ yakkety multiverse
  16. deb http://mirrors.aliyun.com/ubuntu/ yakkety-updates multiverse
  17. deb http://mirrors.aliyun.com/ubuntu/ yakkety-backports main restricted universe multiverse
  18. deb http://mirrors.aliyun.com/ubuntu/ yakkety-security main restricted
  19. deb http://mirrors.aliyun.com/ubuntu/ yakkety-security universe
  20. deb http://mirrors.aliyun.com/ubuntu/ yakkety-security multiverse
  21. ---------------------------------------------------------------------------------------------------------

  22. #更新源信息,安装更新
  23. sudo apt-get update && sudo apt-get upgrade -y

  24. #做一些清理工作,安装python3,默认应该是已经安装的
  25. sudo apt-get autoclean
  26. sudo apt-get clean
  27. sudo apt-get purge -y python3-pip
  28. sudo apt-get install python3

  29. #安装PIP
  30. wget https://bootstrap.pypa.io/get-pip.py
  31. sudo python3 ./get-pip.py
  32. sudo apt-get install python3-pip

  33. #安装Python3虚拟环境
  34. sudo apt-get install python3-venv

  35. #添加一个名为homeassistant的用户
  36. sudo useradd -rm homeassistant

  37. #转到/srv目录,建立homeassistant文件夹
  38. cd /srv
  39. sudo mkdir homeassistant

  40. #更改此文件夹的所有者和所属组
  41. sudo chown homeassistant:homeassistant homeassistant

  42. #更换用户
  43. sudo su -s /bin/bash homeassistant

  44. #切换目录,创建并进入虚拟环境
  45. cd /srv/homeassistant
  46. python3 -m venv homeassistant_venv
  47. source /srv/homeassistant/homeassistant_venv/bin/activate

  48. #虚拟环境下安装pip
  49. pip install --upgrade pip

  50. #安装依赖netdisco,理论上直接默认安装即可,但有的Hass版本需要指定1.0.0rc3,则按下面的命令输入
  51. pip3 install netdisco
  52. #pip3 install netdisco==1.0.0rc3

  53. #正式安装HomeAssistant,速度会非常快
  54. pip3 install homeassistant

  55. #安装完毕,退出虚拟环境
  56. exit

  57. #设置开机启动,建立service文件,将#中间的部分拷入,按ctrl + x, y 退出。
  58. sudo nano /etc/systemd/system/[email protected]

  59. #########################################################################
  60. [Unit]
  61. Description=Home Assistant
  62. After=network.target

  63. [Service]
  64. Type=simple
  65. User=homeassistant
  66. Environment=PATH="$VIRTUAL_ENV/bin:$PATH"
  67. ExecStart=/srv/homeassistant/homeassistant_venv/bin/hass -c "/home/homeassistant/.homeassistant"

  68. [Install]
  69. WantedBy=multi-user.target
  70. #########################################################################

  71. #更新系统设置
  72. sudo systemctl daemon-reload

  73. #设置HomeAssistant开机启动
  74. sudo systemctl enable [email protected]

  75. #启动HomeAssistant
  76. sudo systemctl start [email protected]

  77. #重新启动HomeAssistant
  78. sudo systemctl restart [email protected]

  79. #查看HomeAssistant状态
  80. sudo systemctl status [email protected]
复制代码

HomeBridge-博联 安装代码(搜索引擎搞来的)已测试!!!
  1. 搭建homebridge的部分

  2. #先更新和升级当前环境

  3. sudo apt-get update

  4. sudo apt-get -y upgrade

  5. -------------------------

  6. #安装三个系统组件 screen ,samba, git

  7. sudo apt-get install screen

  8. sudo apt-get install samba

  9. sudo apt-get install git

  10. --------------------------

  11. sudo apt-get install git make

  12. sudo apt-get install g++

  13. curl -sL https://deb.nodesource.com/setup_7.x |sudo -E bash -

  14. sudo apt-get install nodejs=v7.x


  15. #安装必要的依赖包

  16. sudo apt-get -y install libavahi-compat-libdnssd-dev

  17. #安装homebridge,hap-nodejs , node-gyp

  18. sudo npm install -g --unsafe-perm homebridge hap-nodejs node-gyp

  19. cd /usr/lib/node_modules/homebridge/

  20. sudo npm install --unsafe-perm bignum

  21. cd /usr/lib/node_modules/hap-nodejs/node_modules/mdns

  22. sudo node-gyp BUILDTYPE=Release rebuild

  23. ###假如你不是用我上述的方法安装的NodeJS的话,那么很有可能/usr/lib/这个目录会变成/usr/local/lib/。


  24. cd /

  25. #安装与homebridge关联的 homeassistant

  26. sudo npm install -g --unsafe-perm homebridge

  27. sudo npm install -g homebridge-homeassistant  

  28. ##(摄像头插件)

  29. sudo npm install -g homebridge-ip-camera

  30. ###(博联插件)  

  31. sudo npm install -g homebridge-broadlink-rm  

  32. #####(升级)
  33. #
  34. sudo npm upgrade -g homebridge-homeassistant

  35. sudo npm install -g [email protected]

  36. #运行homebridge试试看是否正常运行,如果可以正常运行,就ctrl+c退出来

  37. homebridge



  38. cd /home/lyp/.homebridge

  39. #建立config.json的配置文件

  40. sudo nano config.json


  41. #鼠标右键粘贴如下内容

  42. ------------------------------

  43. {
  44.     "bridge": {
  45.         "name": "Homebridge",
  46.         "username": "00:0X:2X:CX:9X:X1",
  47.         "port": 51826,
  48.         "pin": "123-55-888"
  49.     },

  50.     "platforms": [
  51.   {
  52.     "platform": "HomeAssistant",
  53.     "name": "HomeAssistant",
  54.     "host": "http://192.168.1.122:8123",
  55.     "password": "rocketer",
  56.     "supported_types": ["binary_sensor", "climate", "cover", "fan", "garage_door", "device_tracker", "input_boolean", "light", "lock", "media_player", "rollershutter","sensor", "scene", "switch"]
  57.   }
  58.     ]
  59. }

  60. --------------------------------

  61. 上面的内容,注意格式,注意大小写、粘贴完毕后,按键盘上的Ctrl+X键,输入 Y,保存退出。


  62. 另外:请注意这几段段代码
  63.    "username": "aX:XC:a9:4X:8X:36",   ###这个MAC地址大写就可以}
  64.    "port": 51826,                     ###通讯端口,可以修改,但我觉得不用修改###
  65.    "pin": "123-22-999"                ###PIN码,任意修改,格式是XXX-XX-XXX,只能是数字####

  66. "host": "http://192.168.121.195:8123",      
  67. ###其中的192.168.121.195是你的树莓派的局域网IP地址####

  68. "password": "mimamima",
  69. ###这个是密码,因为我们没有修改Ubuntu的登录密码,所以这里是mimamima####


  70. #再次输入homebridge  回车,检查运行有没有错误
  71. #
  72. #搭建homebridge的部分

  73. #先更新和升级当前环境

  74. sudo apt-get update

  75. sudo apt-get upgrade

  76. ####将homebridge设置成随系统启动######
  77. cd /

  78. sudo adduser --system homebridge

  79. sudo mkdir /var/homebridge

  80. sudo cp /home/lyp/.homebridge/config.json /var/homebridge/  (注意使用自己的用户名和路径)

  81. sudo cp -r /home/lyp/.homebridge/persist /var/homebridge

  82. sudo chmod -R 0777 /var/homebridge

  83. cd /etc/default

  84. sudo nano homebridge

  85. #########将下面的内容复制粘贴进去,然后Ctrl+X,然后Y,回车,保存退出####

  86. # Defaults / Configuration options for homebridge
  87. # The following settings tells homebridge where to find the config.json file and where to persist the data (i.e. pairing and others)
  88. HOMEBRIDGE_OPTS=-U /var/homebridge

  89. # If you uncomment the following line, homebridge will log more
  90. # You can display this via systemd's journalctl: journalctl -f -u homebridge
  91. # DEBUG=*

  92. ####分界线#####################################

  93. cd /etc/systemd/system

  94. sudo nano homebridge.service

  95. #########将下面的内容复制粘贴进去,然后Ctrl+X,然后Y,回车,保存退出####

  96. [Unit]
  97. Description=Node.js HomeKit Server
  98. After=syslog.target network-online.target

  99. [Service]
  100. Type=simple
  101. User=homebridge
  102. EnvironmentFile=/etc/default/homebridge
  103. ExecStart=/usr/lib/node_modules/homebridge/bin/homebridge $HOMEBRIDGE_OPTS
  104. Restart=on-failure
  105. RestartSec=10
  106. KillMode=process

  107. [Install]
  108. WantedBy=multi-user.target

  109. ####分界线#####################################

  110. cd /

  111. sudo systemctl daemon-reload

  112. sudo systemctl enable homebridge

  113. sudo systemctl start homebridge

  114. sudo systemctl status homebridge

  115. sudo reboot      ####重启####

  116. ###最后,你想返回去修改PIN码,MAC地址等等参数,可以按以下命令行来做####

  117. sudo systemctl stop homebridge         ###停止homebridge运行###

  118. cd /var/homebridge                     ###进入目录###   

  119. sudo nano config.json                  ###编辑配置文件###                 

  120. sudo reboot                            ###重启####
复制代码

MQTT安装代码(Jones)本站大神代码
  1. #更新下系统,更新前记得改国内源,不知怎么更换的看这贴:[url=http://bbs.hassbian.com/thread-27-1-1.html]http://bbs.hassbian.com/thread-27-1-1.html[/url]
  2. sudo apt-get update
  3. sudo apt-get upgrade -y

  4. #安装mosquitto和mosquitto-clients(后者主要用于测试,如果不想测试,可以只安装前者)
  5. sudo apt-get install mosquitto mosquitto-clients

  6. #更改mqtt设置
  7. sudo nano /etc/mosquitto/conf.d/mqtt.conf

  8. #在上面文件里加入:
  9. =====================================
  10. allow_anonymous false
  11. password_file /etc/mosquitto/pwfile
  12. =====================================

  13. #设置用户和密码,这里用户为mqtt,可改为你想设的用户名,密码自设,这里都设为mqtt
  14. sudo mosquitto_passwd -c /etc/mosquitto/pwfile mqtt

  15. #设置开机启动,以及启动,停止,重启代码(enable为开机启动,其他你们懂的):
  16. sudo /etc/init.d/mosquitto enable|disable|start|stop|restart|reload|

  17. #重启系统
  18. sudo reboot

  19. #测试MQTT,在一个终端下发布,另个终端下接收,测试是否成功。看不懂此条者请忽略。
  20. mosquitto_sub -d -u mqtt -P mqtt -t dev/test
  21. mosquitto_pub -d -u mqtt -P mqtt -t dev/test -m "hello world"
复制代码


Samba 配置 功能方便局域网内修改HomeAssistant配置文件在使用的时候请去掉我在代码中括号内的备注!!!切记
  1. ##再来建立Samba服务#####

  2. sudo smbpasswd -a pi    (账户名要写已有账户树莓派默认PI 其他系统写你自己的账户名)

  3. sudo mv /etc/samba/smb.conf /etc/samba/smb.conf.original

  4. sudo nano /etc/samba/smb.conf

  5. ########将下面的内容,复制粘贴进去#################

  6. [global]
  7. netbios name = raspberrypi       (这个是局域网显示的设备名称你可以修改成你喜欢的也可以默认)
  8. server string = The Pi File Center
  9. workgroup = WORKGROUP
  10. hosts allow =
  11. remote announce =
  12. remote browse sync =

  13. [HOME ASSISTANT]
  14. path = /home/homeassistant/.homeassistant
  15. comment = No comment
  16. browsable = yes
  17. read only = no
  18. valid users =
  19. writable = yes
  20. guest ok = yes
  21. public = yes
  22. create mask = 0777
  23. directory mask = 0777
  24. force user = root
  25. force create mode = 0777
  26. force directory mode = 0777
  27. hosts allow =

  28. ######分界线####################################

  29. sudo service smbd restart

  30. #########手动重启Home-assistant#################

  31. sudo systemctl restart [url=mailto:[email protected]][email protected][/url]

  32. #################################################
复制代码


到此结束 ~~!!!











作者: Solita    时间: 2017-8-15 13:56
hass不是自带MQTT吗,为什么要安装第三方的呢,自带的不好用?
作者: 人生观    时间: 2017-8-15 14:00
Solita 发表于 2017-8-15 13:56
hass不是自带MQTT吗,为什么要安装第三方的呢,自带的不好用?

我一直都是用自己安装的 HASS 自带不知道哇
作者: jimi    时间: 2017-8-17 16:11
hass自带mqtt么?我接触hass之前就有mqtt了
作者: wgqtx    时间: 2017-8-17 19:10
好详细的样子
作者: godzqh    时间: 2017-8-24 08:39
学习一下!!!
作者: zhizhi1991    时间: 2017-8-24 09:01
学习一下~谢谢分享~
作者: 新生活    时间: 2018-11-25 21:52
谢谢分享,




欢迎光临 『瀚思彼岸』» 智能家居技术论坛 (https://bbs.hassbian.com/) Powered by Discuz! X3.5