Updated main.py to be more failsafe. Now using umqtt.robust too.
This commit is contained in:
150
main.py
150
main.py
@@ -4,7 +4,7 @@ import time
|
|||||||
import json
|
import json
|
||||||
import machine
|
import machine
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
from umqtt.simple import MQTTClient
|
from umqtt.robust import MQTTClient
|
||||||
import dht
|
import dht
|
||||||
import urequests
|
import urequests
|
||||||
import wifi_config
|
import wifi_config
|
||||||
@@ -115,7 +115,10 @@ def main():
|
|||||||
wlan = network.WLAN(network.STA_IF)
|
wlan = network.WLAN(network.STA_IF)
|
||||||
|
|
||||||
# Create MQTT Client
|
# Create MQTT Client
|
||||||
mqtt_client = MQTTClient(mqtt_config.MQTT_CLIENT_ID, mqtt_config.MQTT_HOST_NAME)
|
mqtt_client = MQTTClient(
|
||||||
|
client_id = mqtt_config.MQTT_CLIENT_ID,
|
||||||
|
server = mqtt_config.MQTT_HOST_NAME,
|
||||||
|
)
|
||||||
|
|
||||||
# Create DHT22
|
# Create DHT22
|
||||||
sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN))
|
sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN))
|
||||||
@@ -132,73 +135,98 @@ def main():
|
|||||||
if DEBUG:
|
if DEBUG:
|
||||||
print(f'\nStarting loop #{count}... \nWiFI Status is {wlan.status()}.')
|
print(f'\nStarting loop #{count}... \nWiFI Status is {wlan.status()}.')
|
||||||
|
|
||||||
|
# Create Local Flags for Control
|
||||||
|
wifi_ready = False
|
||||||
|
mqtt_ready = False
|
||||||
|
dht22_ready = False
|
||||||
|
|
||||||
|
# DHT22 and CPU Reading.
|
||||||
|
try:
|
||||||
|
# CPU Reading.
|
||||||
|
cpu_temp = read_cpu_temp()
|
||||||
|
|
||||||
|
# DHT22 Reading.
|
||||||
|
dht22_reading = read_dht_22(sensor)
|
||||||
|
#debug_str = "None"
|
||||||
|
if dht22_reading is not None:
|
||||||
|
temp,hum = dht22_reading
|
||||||
|
temp = temp * 9/5. + 32.0
|
||||||
|
dht22_ready = True
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
# WiFi Connection.
|
# WiFi Connection.
|
||||||
if wlan.status() != 3:
|
try:
|
||||||
ifconfig = wlan_up(wlan)
|
if wlan.status() != 3:
|
||||||
if ifconfig is None:
|
ifconfig = wlan_up(wlan)
|
||||||
if DEBUG:
|
if ifconfig is None:
|
||||||
print("Trouble to connecting WiFi: {}".format(e))
|
if DEBUG:
|
||||||
led_error_code(led, 3)
|
print("Trouble to connecting WiFi: {}".format(e))
|
||||||
time.sleep(10)
|
led_error_code(led, 3)
|
||||||
continue
|
time.sleep(10)
|
||||||
|
continue
|
||||||
|
else:
|
||||||
|
wifi_ready = True
|
||||||
|
if DEBUG:
|
||||||
|
print("Connected to WiFi: {}".format(ifconfig))
|
||||||
else:
|
else:
|
||||||
|
wifi_ready = True
|
||||||
|
ifconfig = wlan.ifconfig()
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
print("Connected to WiFi: {}".format(ifconfig))
|
print(f"Skipping WiFi Activation since WiFi Status is {wlan.status()} \n{ifconfig}.")
|
||||||
else:
|
except:
|
||||||
ifconfig = wlan.ifconfig()
|
continue
|
||||||
if DEBUG:
|
|
||||||
print(f"Skipping WiFi Activation since WiFi Status is {wlan.status()} \n{ifconfig}.")
|
|
||||||
|
|
||||||
# MQTT Conneciton.
|
# MQTT Conneciton.
|
||||||
try:
|
try:
|
||||||
mqtt_client.connect()
|
try:
|
||||||
if DEBUG:
|
mqtt_client.connect(clean_session = False)
|
||||||
print(f'MQTT Connected.')
|
mqtt_ready = True
|
||||||
|
if DEBUG:
|
||||||
except Exception as e:
|
print(f'MQTT Connected.')
|
||||||
if DEBUG:
|
|
||||||
print("Trouble to connecting to MQTT: {}".format(e))
|
|
||||||
led_error_code(led, 2)
|
|
||||||
time.sleep(5)
|
|
||||||
continue # Start back at the top of the While Loop
|
|
||||||
|
|
||||||
# DHT22 Reading.
|
|
||||||
dht22_reading = read_dht_22(sensor)
|
|
||||||
#debug_str = "None"
|
|
||||||
if dht22_reading is not None:
|
|
||||||
temp,hum = dht22_reading
|
|
||||||
temp = temp * 9/5. + 32.0
|
|
||||||
|
|
||||||
# CPU Reading.
|
except Exception as e:
|
||||||
cpu_temp = read_cpu_temp()
|
if DEBUG:
|
||||||
|
print("Trouble to connecting to MQTT: {}".format(e))
|
||||||
# Timestamp
|
led_error_code(led, 2)
|
||||||
time_now = time.localtime()
|
time.sleep(5)
|
||||||
timestamp = "{}/{}/{} {}:{}:{}".format(time_now[1],time_now[2],time_now[0],time_now[3],time_now[4],time_now[5])
|
continue # Start back at the top of the While Loop
|
||||||
|
except:
|
||||||
# Build JSON Payloads
|
continue
|
||||||
dht_data = {
|
|
||||||
'Location':mqtt_config.MQTT_ZONE_ID,
|
|
||||||
'Temperature':temp,
|
|
||||||
'Relative Humidity':hum,
|
|
||||||
}
|
|
||||||
dht_payload = json.dumps(dht_data)
|
|
||||||
|
|
||||||
hw_data = {
|
|
||||||
'Timestamp':timestamp,
|
|
||||||
'CPU Temperature':cpu_temp,
|
|
||||||
'Device':mqtt_config.MQTT_HW_ID,
|
|
||||||
'WiFi Information':ifconfig,
|
|
||||||
}
|
|
||||||
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()
|
# Ready to Publish?
|
||||||
if DEBUG:
|
try:
|
||||||
print(f'MQTT Disconnected.')
|
if wifi_ready and mqtt_ready and dht22_ready:
|
||||||
|
# Timestamp
|
||||||
|
time_now = time.localtime()
|
||||||
|
timestamp = "{}/{}/{} {}:{}:{}".format(time_now[1],time_now[2],time_now[0],time_now[3],time_now[4],time_now[5])
|
||||||
|
|
||||||
|
# Build JSON Payloads
|
||||||
|
dht_data = {
|
||||||
|
'Location':mqtt_config.MQTT_ZONE_ID,
|
||||||
|
'Temperature':temp,
|
||||||
|
'Relative Humidity':hum,
|
||||||
|
}
|
||||||
|
dht_payload = json.dumps(dht_data)
|
||||||
|
|
||||||
|
hw_data = {
|
||||||
|
'Timestamp':timestamp,
|
||||||
|
'CPU Temperature':cpu_temp,
|
||||||
|
'Device':mqtt_config.MQTT_HW_ID,
|
||||||
|
'WiFi Information':ifconfig,
|
||||||
|
}
|
||||||
|
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.')
|
||||||
|
|
||||||
|
except:
|
||||||
|
continue
|
||||||
|
|
||||||
# Sleep for a bit.
|
# Sleep for a bit.
|
||||||
if DEBUG:
|
if DEBUG:
|
||||||
|
|||||||
Reference in New Issue
Block a user