5/29/2019

Home Assistant :: Server Switch ใช้กรณี ปิดเปิด Air Manual

Home Assistant :: Server Switch ใช้กรณี ปิดเปิด Air Manual เมื่อ MN-Server ไม่ทำงานหรือสั่งการไม่ได้ (สถานะต้องปิดไว้ถ้าเปิดจะเปิด 2 เครื่องพร้อมกัน)

http://192.168.2.200:8123/




มีไว้สำหรับปิด หรือ เปิด Air Manual เมื่อ MN-Server ไม่ทำงาน หรือสั่งงานไม่ได้
1. ROOM_A_ON_OFF (ไว้ปิดเปิด Air ตึกหน้า เปิดจะเปิดทั้งสองตัว เพราะมี PI เครื่องเดียว สั่ง GPI อันเดียว)
  1. nano /home/homeassistant/.homeassistant/configuration.yaml

  1.  - platform: mqtt
  2.     name: "ROOM_A_ON_OFF"
  3.     state_topic: "hass/state/ROOM_A_ON_OFF"
  4.     command_topic: "hass/cmd/ROOM_A_ON_OFF"
  5.     payload_on: "ON"
  6.     payload_of: "OFF"
  7.     retain: true

ใช้ Service ที่เครื่อง Raspberry PI ในการรับส่ง mqtt ชื่อ ROOM_A_ON_OFF
Code ที่เครื่อง PI
  1. nano /home/pi/mqttAir.py

  1.  
  2. #!/usr/bin/python
  3. # -*- coding: utf-8 -*-
  4.  
  5. # Copyright (c) 2010-2013 Roger Light <roger@atchoo.org>
  6. #
  7. # All rights reserved. This program and the accompanying materials
  8. # are made available under the terms of the Eclipse Distribution License v1.0
  9. # which accompanies this distribution.
  10. #
  11. # The Eclipse Distribution License is available at
  12. #   http://www.eclipse.org/org/documents/edl-v10.php.
  13. #
  14. # Contributors:
  15. #    Roger Light - initial implementation
  16. # Copyright (c) 2010,2011 Roger Light <roger@atchoo.org>
  17. # All rights reserved.
  18.  
  19. # This shows a simple example of an MQTT subscriber.
  20.  
  21. #import context  # Ensures paho is in PYTHONPATH
  22. import paho.mqtt.client as mqtt
  23.  
  24. ### PI ###
  25. import RPi.GPIO as GPIO                 #Add the GPIO library to a Python sketch
  26. import time                             #Add the time library to a Python sketch
  27. GPIO.setwarnings(False)
  28. GPIO.setmode(GPIO.BOARD)                #Setup GPIO using Board numbering
  29. GPIO.setup(16, GPIO.OUT)
  30.  
  31. ### Piface ###
  32. import pifacedigitalio
  33. pf = pifacedigitalio.PiFaceDigital()
  34.  
  35. def on_connect(mqttc, obj, flags, rc):
  36.     print("rc: " + str(rc))
  37.  
  38. def on_message(mqttc, obj, msg):
  39.     print(msg.topic + " " + str(msg.qos) + " " + str(msg.payload))
  40.     mqttc.publish("hass/state/ROOM_A_ON_OFF", str(msg.payload), 0, True)
  41. ## ON OFF ##
  42.    if str(msg.payload) == "ON":
  43.         #GPIO.output(16,GPIO.HIGH)
  44.         pf.relays[1].turn_on()
  45.         pf.relays[0].turn_on()
  46.     else:
  47.         pf.relays[1].turn_off()
  48.         pf.relays[0].turn_off()
  49.         #GPIO.output(16,GPIO.LOW)
  50.  
  51. def on_publish(mqttc, obj, mid):
  52.     print("mid: " + str(mid))
  53.  
  54. def on_subscribe(mqttc, obj, mid, granted_qos):
  55.     print("Subscribed: " + str(mid) + " " + str(granted_qos))
  56.  
  57. def on_log(mqttc, obj, level, string):
  58.     print(string)
  59.  
  60. # If you want to use a specific client id, use
  61. # mqttc = mqtt.Client("client-id")
  62. # but note that the client id must be unique on the broker. Leaving the client
  63. # id parameter empty will generate a random id for you.
  64. mqttc = mqtt.Client()
  65. mqttc.on_message = on_message
  66. mqttc.on_connect = on_connect
  67. mqttc.on_publish = on_publish
  68. mqttc.on_subscribe = on_subscribe
  69. # Uncomment to enable debug messages
  70. # mqttc.on_log = on_log
  71. mqttc.connect("192.168.2.200", 1883, 60)
  72. mqttc.subscribe("hass/cmd/ROOM_A_ON_OFF", 0)
  73.  
  74. mqttc.loop_forever()
  75.  
  76.  


Run เป็น Service
https://intranet.sci.com/blog.php?u=281&b=1583
Service
  1. sudo python /home/pi/mqttAir.py

ไว้รับคำสั่ง mqtt และสั่งเปิด Air ตาม Code

****** หมายเหตุ บางครั้งสั่งแล้วไม่ปิดไม่เปิด Service อาจจะค้าง ต้อง Restart Service ใหม่ หรือ Reboot เครื่อง PI ใหม่ *******

2. ROOM_B_ON_OFF (รอติดตั้ง ห้อง Server ตึกหลัง)

No comments:

Post a Comment