M405M

Untitled

Jun 16th, 2021
861
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python2.7
  2.  
  3. import paho.mqtt.client as mqttc
  4. import urllib
  5. import RPi.GPIO as GPIO
  6. import time
  7. import Adafruit_DHT as dht
  8.  
  9. # Conf MQTT broker
  10. broker_ip = "192.168.1.17"
  11. broker_port = 1883
  12. broker_timeout = 60
  13.  
  14. topic_sub = "Homino/#"
  15.  
  16. # GPIO setup
  17.  
  18. GPIO.setmode(GPIO.BCM)
  19. GPIO.setwarnings(False)
  20. # Garage Topics
  21. garageDoorTopic = 'Homino/garage/door'
  22. garageLightTopic = 'Homino/garge/light'
  23. # PINS for Garage
  24. gOpenCmdPIN = 27
  25. gCloseCmdPIN = 28
  26. gLightCmdPIN = 26
  27.  
  28. GPIO.setup(gOpenCmdPIN, GPIO.OUT)
  29. GPIO.setup(gCloseCmdPIN, GPIO.OUT)
  30. # GPIO.setup(doorSensorPIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
  31.  
  32. # Home Topics
  33. homeLightTopic = 'Homino/home/light'
  34. homeBuzzerTopic = 'Homino/buzzer'
  35. temperatureTopic = 'Homino/home/temperature'
  36. humidityTopic = 'Homino/home/humidity'
  37. rainTopic = 'Homino/home/rain'
  38. fireTopic = 'Homino/home/fire'
  39.  
  40.  
  41. # Home PINS
  42. hLightCmdPIN = 25
  43. hBuzzCmdPIN = 24
  44. hTempHumSensorPIN = 23
  45. hRainSensorPIN = 22
  46. hFireSensorPIN = 6
  47. GPIO.setup(hLightCmdPIN, GPIO.OUT)
  48. GPIO.setup(hBuzzCmdPIN, GPIO.OUT)
  49. GPIO.setup(hTempHumSensorPIN, GPIO.IN)
  50. GPIO.setup(hRainSensorPIN, GPIO.IN)
  51. GPIO.setup(hFireSensorPIN, GPIO.IN)
  52.  
  53. #sensor = Adafruit_DHT.DHT11
  54.  
  55. # Kitchen Topics
  56. kitchenLightTopic = 'Homino/kitchen/light'
  57. kitchenWindowTopic = 'Homino/kitchen/window'
  58. # Kitchen PINS
  59. kLightCmdPIN = 5
  60. kWindowOpenPIN = 12
  61. kWindowClosePIN = 2
  62. GPIO.setup(kLightCmdPIN, GPIO.OUT)
  63. GPIO.setup(kWindowOpenPIN, GPIO.OUT)
  64. GPIO.setup(kWindowClosePIN, GPIO.OUT)
  65.  
  66. # bedroom Topics
  67. bedFanTopic = 'Homino/bed/fan'
  68. bedRgbTopic = 'Homino/bed/rgb'
  69. # bedroom PINS
  70. bFanCmdPIN = 15
  71. bRCmdPIN = 8
  72. bGCmdPIN = 7
  73. bBCmdPIN = 1
  74. GPIO.setup(bFanCmdPIN, GPIO.OUT)
  75. GPIO.setup(bRCmdPIN, GPIO.OUT)
  76. GPIO.setup(bGCmdPIN, GPIO.OUT)
  77. GPIO.setup(bBCmdPIN, GPIO.OUT)
  78.  
  79. roomFanTopic = 'Homino/room/fan'
  80. roomLightTopic = 'Homino/room/light'
  81. rFanCmdPIN = 6
  82. rLightCmdPIN = 13
  83. GPIO.setup(rFanCmdPIN, GPIO.OUT)
  84. GPIO.setup(rLightCmdPIN, GPIO.OUT)
  85.  
  86. securityStatusTopic = 'Homino/security'
  87. securitySensorPIN = 18
  88. GPIO.setup(securitySensorPIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
  89.  
  90.  
  91. # end GPIO setup
  92.  
  93. def getSecurityStatus():
  94.     if GPIO.input(securitySensorPIN):
  95.         return 'ok'
  96.     else:
  97.         return 'none'
  98.  
  99.  
  100. def on_publish(mosq, obj, mid):
  101.     print("mid: " + str(mid))
  102.     print("")
  103.     print("")
  104.  
  105.  
  106. def on_subscribe(mosq, obj, mid, granted_qos):
  107.     print("Subscribed: " + str(mid) + " " + str(granted_qos))
  108.     print("")
  109.  
  110.  
  111. def on_log(mosq, obj, level, string):
  112.     print(string)
  113.  
  114.  
  115. def turnLightOn(PIN):
  116.     GPIO.output(PIN, True)
  117.     time.sleep(0.5)
  118.  
  119.  
  120. def turnLightOff(PIN):
  121.     GPIO.output(PIN, False)
  122.     time.sleep(0.5)
  123.  
  124.  
  125. def openDoor(PIN):
  126.     GPIO.output(PIN, True)
  127.     time.sleep(0.5)
  128.     GPIO.output(PIN, False)
  129.  
  130.  
  131. def closeDoor(PIN):
  132.     GPIO.output(PIN, True)
  133.     time.sleep(0.5)
  134.     GPIO.output(PIN, False)
  135.  
  136.  
  137. def turnFanOff(PIN):
  138.     GPIO.output(PIN, False)
  139.     time.sleep(0.5)
  140.  
  141.  
  142. def turnFanOn(PIN):
  143.     GPIO.output(PIN, True)
  144.     time.sleep(0.5)
  145.  
  146.  
  147. def main():
  148.     def on_connect(client, userdata, flags, rc):
  149.         client.subscribe(topic_sub)
  150.  
  151.     def on_message(client, userdata, msg):
  152.         print("TOPIC: " + msg.topic + " - PAYLOAD: " + str(msg.payload))
  153.  
  154.         humidity, temperature = dht.read_retry(dht.DHT22, hTempHumSensorPIN)
  155.         humidity = round(humidity, 2)
  156.         temperature = round(temperature, 2)
  157.         client.publish(temperatureTopic, temperature)
  158.         client.publish(humidityTopic, humidity)
  159.  
  160.  
  161.         #garage light
  162.         if msg.topic == garageLightTopic:
  163.             if msg.payload == 'on':
  164.                 print("garage light on ...")
  165.                 turnLightOn(gLightCmdPIN)
  166.             elif msg.payload == 'off':
  167.                 print("garage light off ...")
  168.                 turnLightOff(gLightCmdPIN)
  169.  
  170.         #garage door
  171.         if msg.topic == garageDoorTopic:
  172.             if msg.payload == 'open':
  173.                 print("openig garage ...")
  174.                 openDoor(gOpenCmdPIN)
  175.             elif msg.payload == 'close':
  176.                 print("closing garage ...")
  177.                 closeDoor(gCloseCmdPIN)
  178.  
  179.         #home light
  180.         if msg.topic == homeLightTopic:
  181.             if msg.payload == 'on':
  182.                 print("home light on ...")
  183.                 turnLightOn(hLightCmdPIN)
  184.             elif msg.payload == 'off':
  185.                 print("home light off")
  186.                 turnLightOff(hLightCmdPIN)
  187.  
  188.  
  189.         #kitchen light
  190.         if msg.topic == kitchenLightTopic:
  191.             if msg.payload == 'on':
  192.                 print("kitchen light on")
  193.                 turnLightOn(kLightCmdPIN)
  194.             elif msg.payload == 'off':
  195.                 print("kitchen light off")
  196.                 turnLightOff(kLightCmdPIN)
  197.  
  198.         #kitchen window
  199.         if msg.topic == kitchenWindowTopic:
  200.             if msg.payload == 'open':
  201.                 print("open window ...")
  202.                 openDoor(kWindowOpenPIN)
  203.             elif msg.payload == 'close':
  204.                 print("close window ...")
  205.                 closeDoor(kWindowClosePIN)
  206.  
  207.         #room light
  208.         if msg.topic == roomLightTopic:
  209.             if msg.payload == 'on':
  210.                 print("room light on ...")
  211.                 turnLightOn(rLightCmdPIN)
  212.             elif msg.payload == 'off':
  213.                 print("room light off ...")
  214.                 turnLightOff(rLightCmdPIN)
  215.  
  216.         #room fan
  217.         if msg.topic == roomFanTopic:
  218.             if msg.payload == 'on':
  219.                 print("room fan on ...")
  220.                 turnFanOn(rFanCmdPIN)
  221.             elif msg.payload == 'off':
  222.                 print("room fan off")
  223.                 turnFanOff(rFanCmdPIN)
  224.  
  225.         # bed RGB
  226.         if msg.topic == bedRgbTopic:
  227.             if msg.payload == 'red':
  228.                 print("bed light RED ...")
  229.                 turnLightOn(bRCmdPIN)
  230.             elif msg.payload == 'blue':
  231.                 print("blue light RED ...")
  232.                 turnLightOn(bBCmdPIN)
  233.             elif msg.payload == 'green':
  234.                 print("green light RED ...")
  235.                 turnLightOn(bGCmdPIN)
  236.             else:
  237.                 print("bed light off")
  238.                 turnLightOff(bRCmdPIN)
  239.                 turnLightOff(bGCmdPIN)
  240.                 turnLightOff(bBCmdPIN)
  241.         #bedroom fan
  242.         if msg.topic == bedFanTopic:
  243.             if msg.payload == 'on':
  244.                 turnFanOn(bFanCmdPIN)
  245.             elif msg.payload == 'off':
  246.                 turnFanOff(bFanCmdPIN)
  247.  
  248.     client = mqttc.Client()
  249.     client.on_connect = on_connect
  250.     client.on_message = on_message
  251.  
  252.     client.connect(broker_ip, broker_port, broker_timeout)
  253.  
  254.     client.loop_forever()
  255.  
  256.  
  257. if __name__ == "__main__":
  258.     try:
  259.         main()
  260.     except KeyboardInterrupt:
  261.         GPIO.cleanup()
  262.  
RAW Paste Data