Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python2.7
- import paho.mqtt.client as mqttc
- import urllib
- import RPi.GPIO as GPIO
- import time
- import Adafruit_DHT as dht
- # Conf MQTT broker
- broker_ip = "192.168.1.17"
- broker_port = 1883
- broker_timeout = 60
- topic_sub = "Homino/#"
- # GPIO setup
- GPIO.setmode(GPIO.BCM)
- GPIO.setwarnings(False)
- # Garage Topics
- garageDoorTopic = 'Homino/garage/door'
- garageLightTopic = 'Homino/garge/light'
- # PINS for Garage
- gOpenCmdPIN = 27
- gCloseCmdPIN = 28
- gLightCmdPIN = 26
- GPIO.setup(gOpenCmdPIN, GPIO.OUT)
- GPIO.setup(gCloseCmdPIN, GPIO.OUT)
- # GPIO.setup(doorSensorPIN, GPIO.IN, pull_up_down=GPIO.PUD_UP)
- # Home Topics
- homeLightTopic = 'Homino/home/light'
- homeBuzzerTopic = 'Homino/buzzer'
- temperatureTopic = 'Homino/home/temperature'
- humidityTopic = 'Homino/home/humidity'
- rainTopic = 'Homino/home/rain'
- fireTopic = 'Homino/home/fire'
- # Home PINS
- hLightCmdPIN = 25
- hBuzzCmdPIN = 24
- hTempHumSensorPIN = 23
- hRainSensorPIN = 22
- hFireSensorPIN = 6
- GPIO.setup(hLightCmdPIN, GPIO.OUT)
- GPIO.setup(hBuzzCmdPIN, GPIO.OUT)
- GPIO.setup(hTempHumSensorPIN, GPIO.IN)
- GPIO.setup(hRainSensorPIN, GPIO.IN)
- GPIO.setup(hFireSensorPIN, GPIO.IN)
- #sensor = Adafruit_DHT.DHT11
- # Kitchen Topics
- kitchenLightTopic = 'Homino/kitchen/light'
- kitchenWindowTopic = 'Homino/kitchen/window'
- # Kitchen PINS
- kLightCmdPIN = 5
- kWindowOpenPIN = 12
- kWindowClosePIN = 2
- GPIO.setup(kLightCmdPIN, GPIO.OUT)
- GPIO.setup(kWindowOpenPIN, GPIO.OUT)
- GPIO.setup(kWindowClosePIN, GPIO.OUT)
- # bedroom Topics
- bedFanTopic = 'Homino/bed/fan'
- bedRgbTopic = 'Homino/bed/rgb'
- # bedroom PINS
- bFanCmdPIN = 15
- bRCmdPIN = 8
- bGCmdPIN = 7
- bBCmdPIN = 1
- GPIO.setup(bFanCmdPIN, GPIO.OUT)
- GPIO.setup(bRCmdPIN, GPIO.OUT)
- GPIO.setup(bGCmdPIN, GPIO.OUT)
- GPIO.setup(bBCmdPIN, GPIO.OUT)
- roomFanTopic = 'Homino/room/fan'
- roomLightTopic = 'Homino/room/light'
- rFanCmdPIN = 6
- rLightCmdPIN = 13
- GPIO.setup(rFanCmdPIN, GPIO.OUT)
- GPIO.setup(rLightCmdPIN, GPIO.OUT)
- securityStatusTopic = 'Homino/security'
- securitySensorPIN = 18
- GPIO.setup(securitySensorPIN, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
- # end GPIO setup
- def getSecurityStatus():
- if GPIO.input(securitySensorPIN):
- return 'ok'
- else:
- return 'none'
- def on_publish(mosq, obj, mid):
- print("mid: " + str(mid))
- print("")
- print("")
- def on_subscribe(mosq, obj, mid, granted_qos):
- print("Subscribed: " + str(mid) + " " + str(granted_qos))
- print("")
- def on_log(mosq, obj, level, string):
- print(string)
- def turnLightOn(PIN):
- GPIO.output(PIN, True)
- time.sleep(0.5)
- def turnLightOff(PIN):
- GPIO.output(PIN, False)
- time.sleep(0.5)
- def openDoor(PIN):
- GPIO.output(PIN, True)
- time.sleep(0.5)
- GPIO.output(PIN, False)
- def closeDoor(PIN):
- GPIO.output(PIN, True)
- time.sleep(0.5)
- GPIO.output(PIN, False)
- def turnFanOff(PIN):
- GPIO.output(PIN, False)
- time.sleep(0.5)
- def turnFanOn(PIN):
- GPIO.output(PIN, True)
- time.sleep(0.5)
- def main():
- def on_connect(client, userdata, flags, rc):
- client.subscribe(topic_sub)
- def on_message(client, userdata, msg):
- print("TOPIC: " + msg.topic + " - PAYLOAD: " + str(msg.payload))
- humidity, temperature = dht.read_retry(dht.DHT22, hTempHumSensorPIN)
- humidity = round(humidity, 2)
- temperature = round(temperature, 2)
- client.publish(temperatureTopic, temperature)
- client.publish(humidityTopic, humidity)
- #garage light
- if msg.topic == garageLightTopic:
- if msg.payload == 'on':
- print("garage light on ...")
- turnLightOn(gLightCmdPIN)
- elif msg.payload == 'off':
- print("garage light off ...")
- turnLightOff(gLightCmdPIN)
- #garage door
- if msg.topic == garageDoorTopic:
- if msg.payload == 'open':
- print("openig garage ...")
- openDoor(gOpenCmdPIN)
- elif msg.payload == 'close':
- print("closing garage ...")
- closeDoor(gCloseCmdPIN)
- #home light
- if msg.topic == homeLightTopic:
- if msg.payload == 'on':
- print("home light on ...")
- turnLightOn(hLightCmdPIN)
- elif msg.payload == 'off':
- print("home light off")
- turnLightOff(hLightCmdPIN)
- #kitchen light
- if msg.topic == kitchenLightTopic:
- if msg.payload == 'on':
- print("kitchen light on")
- turnLightOn(kLightCmdPIN)
- elif msg.payload == 'off':
- print("kitchen light off")
- turnLightOff(kLightCmdPIN)
- #kitchen window
- if msg.topic == kitchenWindowTopic:
- if msg.payload == 'open':
- print("open window ...")
- openDoor(kWindowOpenPIN)
- elif msg.payload == 'close':
- print("close window ...")
- closeDoor(kWindowClosePIN)
- #room light
- if msg.topic == roomLightTopic:
- if msg.payload == 'on':
- print("room light on ...")
- turnLightOn(rLightCmdPIN)
- elif msg.payload == 'off':
- print("room light off ...")
- turnLightOff(rLightCmdPIN)
- #room fan
- if msg.topic == roomFanTopic:
- if msg.payload == 'on':
- print("room fan on ...")
- turnFanOn(rFanCmdPIN)
- elif msg.payload == 'off':
- print("room fan off")
- turnFanOff(rFanCmdPIN)
- # bed RGB
- if msg.topic == bedRgbTopic:
- if msg.payload == 'red':
- print("bed light RED ...")
- turnLightOn(bRCmdPIN)
- elif msg.payload == 'blue':
- print("blue light RED ...")
- turnLightOn(bBCmdPIN)
- elif msg.payload == 'green':
- print("green light RED ...")
- turnLightOn(bGCmdPIN)
- else:
- print("bed light off")
- turnLightOff(bRCmdPIN)
- turnLightOff(bGCmdPIN)
- turnLightOff(bBCmdPIN)
- #bedroom fan
- if msg.topic == bedFanTopic:
- if msg.payload == 'on':
- turnFanOn(bFanCmdPIN)
- elif msg.payload == 'off':
- turnFanOff(bFanCmdPIN)
- client = mqttc.Client()
- client.on_connect = on_connect
- client.on_message = on_message
- client.connect(broker_ip, broker_port, broker_timeout)
- client.loop_forever()
- if __name__ == "__main__":
- try:
- main()
- except KeyboardInterrupt:
- GPIO.cleanup()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement