Updated to allow for redundant MQTT broker.
This commit is contained in:
83
main.py
83
main.py
@@ -9,6 +9,7 @@ import dht
|
|||||||
import urequests
|
import urequests
|
||||||
import wifi_config
|
import wifi_config
|
||||||
import mqtt_config
|
import mqtt_config
|
||||||
|
import ha_mqtt_config
|
||||||
|
|
||||||
# Change this GPIO PIN where your DHT22 sensor is connected
|
# Change this GPIO PIN where your DHT22 sensor is connected
|
||||||
DHT_22_GPIO_PIN = 3
|
DHT_22_GPIO_PIN = 3
|
||||||
@@ -120,6 +121,15 @@ def main():
|
|||||||
server = mqtt_config.MQTT_HOST_NAME,
|
server = mqtt_config.MQTT_HOST_NAME,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
# Create Home Assistant MQTT Client
|
||||||
|
ha_mqtt_client = MQTTClient(
|
||||||
|
client_id = ha_mqtt_config.MQTT_CLIENT_ID,
|
||||||
|
server = ha_mqtt_config.MQTT_HOST_NAME,
|
||||||
|
keepalive = ha_mqtt_config.MQTT_KEEP,
|
||||||
|
user = ha_mqtt_config.MQTT_USERNAME,
|
||||||
|
password = ha_mqtt_config.MQTT_PASSWORD,
|
||||||
|
)
|
||||||
|
|
||||||
# Create DHT22
|
# Create DHT22
|
||||||
sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN))
|
sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN))
|
||||||
|
|
||||||
@@ -177,22 +187,39 @@ def main():
|
|||||||
except:
|
except:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# MQTT Conneciton.
|
# MQTT Connection to Primary Broker.
|
||||||
try:
|
try:
|
||||||
|
mqtt_client.connect(clean_session = False)
|
||||||
|
mqtt_ready = True
|
||||||
|
mqtt = 'mosquitto'
|
||||||
|
if DEBUG:
|
||||||
|
print(f'Connected to Mosquitto MQTT broker.')
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
if DEBUG:
|
||||||
|
print("Trouble to connecting to Mosquitto MQTT: {}".format(e))
|
||||||
|
|
||||||
|
|
||||||
|
# MQTT Connection to Back Up Broker.
|
||||||
|
if not mqtt_ready:
|
||||||
try:
|
try:
|
||||||
mqtt_client.connect(clean_session = False)
|
try:
|
||||||
mqtt_ready = True
|
ha_mqtt_client.connect(clean_session = False)
|
||||||
if DEBUG:
|
mqtt_ready = True
|
||||||
print(f'MQTT Connected.')
|
mqtt = 'ha'
|
||||||
|
if DEBUG:
|
||||||
except Exception as e:
|
print(f'Connected to the back up, Home Assistant, MQTT broker.')
|
||||||
if DEBUG:
|
|
||||||
print("Trouble to connecting to MQTT: {}".format(e))
|
except Exception as f:
|
||||||
led_error_code(led, 2)
|
if DEBUG:
|
||||||
time.sleep(5)
|
print("Trouble to connecting to Home Assistant MQTT: {}".format(f))
|
||||||
continue # Start back at the top of the While Loop
|
led_error_code(led, 2)
|
||||||
except:
|
time.sleep(5)
|
||||||
continue
|
continue # Start back at the top of the While Loop
|
||||||
|
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
|
|
||||||
# Ready to Publish?
|
# Ready to Publish?
|
||||||
try:
|
try:
|
||||||
@@ -216,15 +243,27 @@ def main():
|
|||||||
'WiFi Information':ifconfig,
|
'WiFi Information':ifconfig,
|
||||||
}
|
}
|
||||||
hw_payload = json.dumps(hw_data)
|
hw_payload = json.dumps(hw_data)
|
||||||
|
|
||||||
# Publish
|
|
||||||
mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),dht_payload, retain=True)
|
|
||||||
mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True)
|
|
||||||
|
|
||||||
mqtt_client.disconnect()
|
|
||||||
if DEBUG:
|
|
||||||
print(f'MQTT Disconnected.')
|
|
||||||
|
|
||||||
|
if DEBUG:
|
||||||
|
print(f'Trying to publish...')
|
||||||
|
|
||||||
|
|
||||||
|
if mqtt == 'mosquitto':
|
||||||
|
# Publish
|
||||||
|
mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),dht_payload, retain=True)
|
||||||
|
mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True)
|
||||||
|
mqtt_client.disconnect()
|
||||||
|
if DEBUG:
|
||||||
|
print(f'MQTT Disconnected.')
|
||||||
|
|
||||||
|
if mqtt == 'ha':
|
||||||
|
# Publish
|
||||||
|
ha_mqtt_client.publish("home/{}".format(mqtt_config.MQTT_ZONE_ID),dht_payload, retain=True)
|
||||||
|
ha_mqtt_client.publish("hw/{}".format(mqtt_config.MQTT_HW_ID),hw_payload, retain=True)
|
||||||
|
ha_mqtt_client.disconnect()
|
||||||
|
if DEBUG:
|
||||||
|
print(f'MQTT Disconnected.')
|
||||||
|
|
||||||
except:
|
except:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user