updates from pi
This commit is contained in:
parent
36c6bc910a
commit
36b3d4626e
76
relay.py
76
relay.py
@ -3,13 +3,16 @@ import time,sys
|
|||||||
from paho.mqtt import client as mqtt_client
|
from paho.mqtt import client as mqtt_client
|
||||||
|
|
||||||
#mqtt settings
|
#mqtt settings
|
||||||
broker = '192.168.2.2'
|
broker = '192.168.3.2'
|
||||||
port = 1883
|
port = 1883
|
||||||
uname = 'ecyhass'
|
#uname = 'ecyhass'
|
||||||
pw = 'ecyhass1216'
|
#pw = 'ecyhass1216'
|
||||||
topic_sensor = 'garage/door_sensor'
|
topic_sensor = 'garage/door_sensor'
|
||||||
topic_control = 'garage/door_control'
|
topic_control = 'garage/door_control'
|
||||||
client_id = str(210)
|
client_id = str(210)
|
||||||
|
BUTTON_TIME=1 #seconds
|
||||||
|
|
||||||
|
RUN=1
|
||||||
|
|
||||||
#GPIO settings
|
#GPIO settings
|
||||||
relay_pin = 15
|
relay_pin = 15
|
||||||
@ -19,25 +22,46 @@ gpio.setmode(gpio.BOARD)
|
|||||||
gpio.setup(relay_pin,gpio.OUT)
|
gpio.setup(relay_pin,gpio.OUT)
|
||||||
gpio.setup(sensor_pin,gpio.IN,pull_up_down=gpio.PUD_DOWN)
|
gpio.setup(sensor_pin,gpio.IN,pull_up_down=gpio.PUD_DOWN)
|
||||||
|
|
||||||
def mqtt_connect():
|
def mqtt_on_message(client, userdata, msg):
|
||||||
def on_connect(client, userdata, flags, rc):
|
data=str(msg.payload.decode("utf-8"))
|
||||||
|
print("message received " ,data)
|
||||||
|
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_connect(client, userdata, flags, rc):
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
print("Connected to MQTT Broker")
|
print("Connected to MQTT Broker")
|
||||||
else:
|
else:
|
||||||
print("Failed to Connect MQTT")
|
print("Failed to Connect MQTT")
|
||||||
|
|
||||||
|
|
||||||
|
def mqtt_setup():
|
||||||
|
#Connect
|
||||||
client = mqtt_client.Client(client_id)
|
client = mqtt_client.Client(client_id)
|
||||||
client.username_pw_set(uname,pw)
|
#client.username_pw_set(uname,pw)
|
||||||
client.on_connect = on_connect
|
client.on_connect = mqtt_on_connect
|
||||||
client.connect(broker,port)
|
while(1): # keep trying until the network is up
|
||||||
|
try:
|
||||||
|
client.connect(broker,port,keepalive=60)
|
||||||
|
break
|
||||||
|
except:
|
||||||
|
print("RELAY:Cant connect. waiting 1s")
|
||||||
|
time.sleep(1)
|
||||||
|
print("RELAY: Subscribed")
|
||||||
|
#Subscribe
|
||||||
|
client.subscribe(topic_control)
|
||||||
|
client.on_message = mqtt_on_message
|
||||||
return client
|
return client
|
||||||
|
|
||||||
def mqtt_subscribe(client):
|
def publish_status(client):
|
||||||
def on_message(client, userdata, msg):
|
client.publish(topic_sensor,str(get_sensor()),retain=True)
|
||||||
print(f"Received `{msg.payload.decode()}` from `{msg.topic}` topic")
|
|
||||||
|
|
||||||
client.subscribe(topic_control)
|
|
||||||
client.on_message = on_message
|
|
||||||
|
|
||||||
def set_relay(value):
|
def set_relay(value):
|
||||||
if value==1:
|
if value==1:
|
||||||
@ -48,19 +72,25 @@ def set_relay(value):
|
|||||||
def get_sensor():
|
def get_sensor():
|
||||||
return gpio.input(sensor_pin)
|
return gpio.input(sensor_pin)
|
||||||
|
|
||||||
def set_from_sensor():
|
def press_button(client):
|
||||||
set_relay(get_sensor())
|
set_relay(1)
|
||||||
|
time.sleep(BUTTON_TIME)
|
||||||
|
set_relay(0)
|
||||||
|
|
||||||
def end():
|
def end():
|
||||||
gpio.cleanup()
|
print("stop")
|
||||||
exit()
|
RUN=0
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
mqtt_subscribe(mqtt_connect())
|
print("Start Relay Contorl")
|
||||||
while(1):
|
client=mqtt_setup()
|
||||||
continue
|
client.loop_start()
|
||||||
# set_from_sensor()
|
publish_status(client)
|
||||||
# time.sleep(.25)
|
while(RUN):
|
||||||
|
time.sleep(5)
|
||||||
|
publish_status(client)
|
||||||
|
gpio.cleanup()
|
||||||
|
exit()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user