142 lines
3.4 KiB
Python
142 lines
3.4 KiB
Python
import RPi.GPIO as gpio
|
|
import time,sys
|
|
from paho.mqtt import client as mqtt_client
|
|
|
|
#mqtt settings
|
|
broker = '192.168.3.2'
|
|
port = 1883
|
|
#uname = 'ecyhass'
|
|
#pw = 'ecyhass1216'
|
|
topic_sensor = 'garage/door_sensor'
|
|
topic_control = 'garage/door_control'
|
|
client_id = str(210)
|
|
BUTTON_TIME=0.75 #seconds
|
|
|
|
RUN=1
|
|
CONNECTED = 0
|
|
|
|
#GPIO settings
|
|
relay_pin = 15
|
|
sensor_pin = 19
|
|
#gpio setup
|
|
gpio.setmode(gpio.BOARD)
|
|
gpio.setup(relay_pin,gpio.OUT)
|
|
gpio.setup(sensor_pin,gpio.IN,pull_up_down=gpio.PUD_DOWN)
|
|
|
|
def mqtt_on_message(client, userdata, msg):
|
|
data=str(msg.payload.decode("utf-8"))
|
|
print("message received " ,data)
|
|
sys.stdout.flush()
|
|
if data=="set_low":
|
|
set_relay(0)
|
|
if data=="set_high":
|
|
set_relay(1)
|
|
if data=="open_door":
|
|
press_button(client)
|
|
#if data=="end":
|
|
# end()
|
|
# client.loop_stop()
|
|
|
|
def mqtt_on_disconnect(client, userdata, rc):
|
|
global CONNECTED
|
|
print("RELAY: LOST CONNECTION")
|
|
sys.stdout.flush()
|
|
CONNECTED=0
|
|
|
|
def mqtt_on_connect(client, userdata, flags, rc):
|
|
global CONNECTED
|
|
if rc == 0:
|
|
print("Connected to MQTT Broker")
|
|
sys.stdout.flush()
|
|
CONNECTED=1
|
|
else:
|
|
print("Failed to Connect MQTT")
|
|
sys.stdout.flush()
|
|
|
|
|
|
def mqtt_setup():
|
|
#Connect
|
|
client = mqtt_client.Client(client_id)
|
|
#client.username_pw_set(uname,pw)
|
|
client.on_connect = mqtt_on_connect
|
|
client.on_disconnect = mqtt_on_disconnect
|
|
client.on_message = mqtt_on_message
|
|
while(1): # keep trying until the network is up
|
|
try:
|
|
client.connect(broker,port,keepalive=60)
|
|
break
|
|
except:
|
|
print("RELAY:Cant connect. waiting 1s")
|
|
sys.stdout.flush()
|
|
time.sleep(1)
|
|
print("RELAY: Subscribed")
|
|
sys.stdout.flush()
|
|
#Subscribe
|
|
client.subscribe(topic_control)
|
|
return client
|
|
|
|
def publish_status(client):
|
|
#client.publish(topic_sensor,str(get_sensor()),retain=True)
|
|
client.publish(topic_sensor,str(get_sensor()))
|
|
|
|
def set_relay(value):
|
|
if value==1:
|
|
gpio.output(relay_pin,gpio.HIGH)
|
|
else:
|
|
gpio.output(relay_pin,gpio.LOW)
|
|
|
|
def get_sensor():
|
|
return gpio.input(sensor_pin)
|
|
|
|
def press_button(client):
|
|
set_relay(1)
|
|
time.sleep(BUTTON_TIME)
|
|
set_relay(0)
|
|
|
|
def end():
|
|
print("stop")
|
|
sys.stdout.flush()
|
|
RUN=0
|
|
|
|
def main():
|
|
reset_count=0
|
|
print("Start Relay Contorl")
|
|
sys.stdout.flush()
|
|
client=mqtt_setup()
|
|
client.loop_start()
|
|
publish_status(client)
|
|
while(RUN):
|
|
time.sleep(5)
|
|
#print("CONNECTED:",CONNECTED)
|
|
publish_status(client)
|
|
reset_count=reset_count+1
|
|
if CONNECTED==0:
|
|
print("RELAY: Attempting reconnect")
|
|
sys.stdout.flush()
|
|
try:
|
|
client.loop_stop()
|
|
client.reconnect()
|
|
client.subscribe(topic_control)
|
|
client.loop_start()
|
|
except:
|
|
print("Failed to reconnect")
|
|
sys.stdout.flush()
|
|
elif reset_count>=720:
|
|
try:
|
|
client.loop_stop()
|
|
client.reconnect()
|
|
client.subscribe(topic_control)
|
|
client.loop_start()
|
|
reset_count=0
|
|
except:
|
|
print("Couldn't reconnect");
|
|
sys.stdout.flush()
|
|
|
|
gpio.cleanup()
|
|
exit()
|
|
|
|
|
|
|
|
if __name__=="__main__":
|
|
main()
|