made reconnect actually start receiving again
This commit is contained in:
parent
30c418b8d4
commit
1f61719768
15
relay.py
15
relay.py
@ -26,6 +26,7 @@ gpio.setup(sensor_pin,gpio.IN,pull_up_down=gpio.PUD_DOWN)
|
|||||||
def mqtt_on_message(client, userdata, msg):
|
def mqtt_on_message(client, userdata, msg):
|
||||||
data=str(msg.payload.decode("utf-8"))
|
data=str(msg.payload.decode("utf-8"))
|
||||||
print("message received " ,data)
|
print("message received " ,data)
|
||||||
|
sys.stdout.flush()
|
||||||
if data=="set_low":
|
if data=="set_low":
|
||||||
set_relay(0)
|
set_relay(0)
|
||||||
if data=="set_high":
|
if data=="set_high":
|
||||||
@ -39,15 +40,18 @@ def mqtt_on_message(client, userdata, msg):
|
|||||||
def mqtt_on_disconnect(client, userdata, rc):
|
def mqtt_on_disconnect(client, userdata, rc):
|
||||||
global CONNECTED
|
global CONNECTED
|
||||||
print("RELAY: LOST CONNECTION")
|
print("RELAY: LOST CONNECTION")
|
||||||
|
sys.stdout.flush()
|
||||||
CONNECTED=0
|
CONNECTED=0
|
||||||
|
|
||||||
def mqtt_on_connect(client, userdata, flags, rc):
|
def mqtt_on_connect(client, userdata, flags, rc):
|
||||||
global CONNECTED
|
global CONNECTED
|
||||||
if rc == 0:
|
if rc == 0:
|
||||||
print("Connected to MQTT Broker")
|
print("Connected to MQTT Broker")
|
||||||
|
sys.stdout.flush()
|
||||||
CONNECTED=1
|
CONNECTED=1
|
||||||
else:
|
else:
|
||||||
print("Failed to Connect MQTT")
|
print("Failed to Connect MQTT")
|
||||||
|
sys.stdout.flush()
|
||||||
|
|
||||||
|
|
||||||
def mqtt_setup():
|
def mqtt_setup():
|
||||||
@ -56,17 +60,19 @@ def mqtt_setup():
|
|||||||
#client.username_pw_set(uname,pw)
|
#client.username_pw_set(uname,pw)
|
||||||
client.on_connect = mqtt_on_connect
|
client.on_connect = mqtt_on_connect
|
||||||
client.on_disconnect = mqtt_on_disconnect
|
client.on_disconnect = mqtt_on_disconnect
|
||||||
|
client.on_message = mqtt_on_message
|
||||||
while(1): # keep trying until the network is up
|
while(1): # keep trying until the network is up
|
||||||
try:
|
try:
|
||||||
client.connect(broker,port,keepalive=60)
|
client.connect(broker,port,keepalive=60)
|
||||||
break
|
break
|
||||||
except:
|
except:
|
||||||
print("RELAY:Cant connect. waiting 1s")
|
print("RELAY:Cant connect. waiting 1s")
|
||||||
|
sys.stdout.flush()
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
print("RELAY: Subscribed")
|
print("RELAY: Subscribed")
|
||||||
|
sys.stdout.flush()
|
||||||
#Subscribe
|
#Subscribe
|
||||||
client.subscribe(topic_control)
|
client.subscribe(topic_control)
|
||||||
client.on_message = mqtt_on_message
|
|
||||||
return client
|
return client
|
||||||
|
|
||||||
def publish_status(client):
|
def publish_status(client):
|
||||||
@ -89,10 +95,12 @@ def press_button(client):
|
|||||||
|
|
||||||
def end():
|
def end():
|
||||||
print("stop")
|
print("stop")
|
||||||
|
sys.stdout.flush()
|
||||||
RUN=0
|
RUN=0
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Start Relay Contorl")
|
print("Start Relay Contorl")
|
||||||
|
sys.stdout.flush()
|
||||||
client=mqtt_setup()
|
client=mqtt_setup()
|
||||||
client.loop_start()
|
client.loop_start()
|
||||||
publish_status(client)
|
publish_status(client)
|
||||||
@ -102,10 +110,15 @@ def main():
|
|||||||
publish_status(client)
|
publish_status(client)
|
||||||
if CONNECTED==0:
|
if CONNECTED==0:
|
||||||
print("RELAY: Attempting reconnect")
|
print("RELAY: Attempting reconnect")
|
||||||
|
sys.stdout.flush()
|
||||||
try:
|
try:
|
||||||
|
client.loop_stop()
|
||||||
client.reconnect()
|
client.reconnect()
|
||||||
|
client.subscribe(topic_control)
|
||||||
|
client.loop_start()
|
||||||
except:
|
except:
|
||||||
print("Failed to reconnect")
|
print("Failed to reconnect")
|
||||||
|
sys.stdout.flush()
|
||||||
gpio.cleanup()
|
gpio.cleanup()
|
||||||
exit()
|
exit()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user