Debugged code to be more reliable.
This commit is contained in:
130
main.py
130
main.py
@@ -27,25 +27,10 @@ def read_cpu_temp():
|
|||||||
cpu_temp_sensor = machine.ADC(4)
|
cpu_temp_sensor = machine.ADC(4)
|
||||||
reading = cpu_temp_sensor.read_u16() * cpu_temp_conversion_factor
|
reading = cpu_temp_sensor.read_u16() * cpu_temp_conversion_factor
|
||||||
temperature_c = 27 - (reading - 0.706) / 0.001721
|
temperature_c = 27 - (reading - 0.706) / 0.001721
|
||||||
return temperature_c
|
temperature_f = temperature_c * 9/5. + 32.0
|
||||||
|
return temperature_f
|
||||||
|
|
||||||
def read_vsys():
|
def read_dht_22(sensor):
|
||||||
# Set pin 29 for the voltage reading
|
|
||||||
# Note, this will break Wi-Fi until the pin is set back to original state
|
|
||||||
# See lines below where the state is reset
|
|
||||||
Pin(29, Pin.IN)
|
|
||||||
Vsys = machine.ADC(3)
|
|
||||||
conversion_factor = 3.3 * 3 / 65535
|
|
||||||
reading = Vsys.read_u16() * conversion_factor
|
|
||||||
|
|
||||||
# Reset pin back to the original state
|
|
||||||
# This original state is found by printing the value of the pin at the start of this function
|
|
||||||
# alt=7 seems to be Wi-Fi related, so
|
|
||||||
Pin(29, Pin.ALT, Pin.PULL_DOWN, alt=7)
|
|
||||||
|
|
||||||
return reading
|
|
||||||
|
|
||||||
def read_dht_22_raw(sensor):
|
|
||||||
"""
|
"""
|
||||||
reads the temperature and humidity from dht.DHT22 sensor.
|
reads the temperature and humidity from dht.DHT22 sensor.
|
||||||
returns tuple(temperature, humidity) if no errors
|
returns tuple(temperature, humidity) if no errors
|
||||||
@@ -56,73 +41,28 @@ def read_dht_22_raw(sensor):
|
|||||||
temperature = sensor.temperature()
|
temperature = sensor.temperature()
|
||||||
humidity = sensor.humidity()
|
humidity = sensor.humidity()
|
||||||
return temperature, humidity
|
return temperature, humidity
|
||||||
except OSError:
|
except:
|
||||||
return None
|
|
||||||
|
|
||||||
def read_dht_22_with_retry(sensor):
|
|
||||||
"""Same as [read_dht_22_raw] but tries a few times before giving up. Same returns as [read_dht_22_raw]"""
|
|
||||||
count = 0
|
|
||||||
while count < 2:
|
|
||||||
reading = read_dht_22_raw(sensor)
|
|
||||||
count += 1
|
|
||||||
if reading is not None:
|
|
||||||
return reading
|
|
||||||
time.sleep(2)
|
time.sleep(2)
|
||||||
return None
|
|
||||||
|
|
||||||
def read_dht_22(sensor):
|
|
||||||
"""
|
|
||||||
When DHT22 runs on 3.3v sometimes the output results are incomplete, at least what I've seen before
|
|
||||||
i.e it can return 2deg, 0deg for a measurement, and the normal readings
|
|
||||||
This is a hack to see if this solves this problem, we take 2 measurements and if they are not same (or close), discard
|
|
||||||
"""
|
|
||||||
reading_1 = read_dht_22_with_retry(sensor)
|
|
||||||
|
|
||||||
if reading_1 is None:
|
|
||||||
print("read_dht_22, reading 1 is None. Abort")
|
|
||||||
return None
|
return None
|
||||||
# print("Reading 1: {}".format(reading_1))
|
|
||||||
|
|
||||||
time.sleep(2)
|
|
||||||
|
|
||||||
reading_2 = read_dht_22_with_retry(sensor)
|
|
||||||
|
|
||||||
if reading_2 is None:
|
|
||||||
print("read_dht_22, reading 2 is None. Abort")
|
|
||||||
return None
|
|
||||||
|
|
||||||
# print("Reading 2: {}".format(reading_2))
|
|
||||||
|
|
||||||
diff = abs(reading_1[0] - reading_2[0])
|
|
||||||
#print("Reading between 1 and 2 is {}".format(diff))
|
|
||||||
if diff > 2:
|
|
||||||
print("Reading between 1 and 2 is more than 2 deg apart, {}".format(diff))
|
|
||||||
return None
|
|
||||||
|
|
||||||
return reading_1
|
|
||||||
|
|
||||||
def wlan_up(wlan):
|
def wlan_up(wlan):
|
||||||
print("Connoting to Wifi...")
|
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
print("Wifi chip is active ... wlan.connect now")
|
|
||||||
wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD)
|
wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD)
|
||||||
print("wlan.connect is done")
|
|
||||||
|
|
||||||
# Wait for connect or fail
|
# Wait for connect or fail
|
||||||
max_wait = 10
|
max_wait = 10
|
||||||
while max_wait > 0:
|
while max_wait > 0:
|
||||||
if wlan.status() < 0 or wlan.status() >= 3:
|
if wlan.status() < 0 or wlan.status() >= 3:
|
||||||
break
|
break
|
||||||
max_wait -= 1
|
max_wait -= 1
|
||||||
print('waiting for connection...')
|
print('Waiting for WiFi connection...')
|
||||||
time.sleep(1)
|
time.sleep(1)
|
||||||
|
|
||||||
if wlan.status() != 3:
|
if max_wait == 0:
|
||||||
raise RuntimeError('network connection failed, {}'.format(wlan.status()))
|
return None
|
||||||
|
|
||||||
ifconfig = wlan.ifconfig()
|
ifconfig = wlan.ifconfig()
|
||||||
print(ifconfig)
|
print(ifconfig)
|
||||||
print("Connected to Wifi")
|
|
||||||
return ifconfig
|
return ifconfig
|
||||||
|
|
||||||
def led_error_code(led, error_code: int):
|
def led_error_code(led, error_code: int):
|
||||||
@@ -154,6 +94,7 @@ def led_error_code(led, error_code: int):
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
print("Start up")
|
print("Start up")
|
||||||
|
count = 0
|
||||||
|
|
||||||
# Set Wi-Fi Country
|
# Set Wi-Fi Country
|
||||||
rp2.country('US')
|
rp2.country('US')
|
||||||
@@ -171,25 +112,30 @@ def main():
|
|||||||
print("Enter main loop")
|
print("Enter main loop")
|
||||||
|
|
||||||
while True:
|
while True:
|
||||||
|
|
||||||
led.value(False)
|
led.value(False)
|
||||||
# Read vsys before doing any WiFi connections makes the board more stable over long runs
|
count += 1
|
||||||
vsys_volts = read_vsys()
|
|
||||||
try:
|
print(f'\nStarting loop #{count}... \nWiFI Status is {wlan.status()}.')
|
||||||
|
|
||||||
|
if wlan.status() != 3:
|
||||||
ifconfig = wlan_up(wlan)
|
ifconfig = wlan_up(wlan)
|
||||||
except Exception as e:
|
if ifconfig is None:
|
||||||
print("Trouble to connecting WiFi: {}".format(e))
|
print("Trouble to connecting WiFi: {}".format(e))
|
||||||
# Should we raise a problem vs just try it again ?
|
led_error_code(led, 3)
|
||||||
# raise RuntimeError('Trouble to connecting WiFi, {}'.format(e))
|
continue
|
||||||
led_error_code(led, 3)
|
else:
|
||||||
continue
|
print("Connected to WiFi: {}".format(ifconfig))
|
||||||
|
else:
|
||||||
|
ifconfig = wlan.ifconfig()
|
||||||
|
print(f"Skipping WiFi Activation since WiFi Status is {wlan.status()} \n{ifconfig}.")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
mqtt_client.connect()
|
mqtt_client.connect()
|
||||||
print("Connected to MQTT")
|
print(f'MQTT Connected.')
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print("Trouble to connecting to MQTT: {}".format(e))
|
print("Trouble to connecting to MQTT: {}".format(e))
|
||||||
# Should we raise a problem vs just try it again ?
|
|
||||||
# raise RuntimeError('Trouble to connecting to MQTT, {}'.format(e))
|
|
||||||
led_error_code(led, 2)
|
led_error_code(led, 2)
|
||||||
continue
|
continue
|
||||||
|
|
||||||
@@ -201,31 +147,21 @@ def main():
|
|||||||
temp = temp * 9/5. + 32.0
|
temp = temp * 9/5. + 32.0
|
||||||
mqtt_client.publish("m/v1/home/{}/0/temperature".format(mqtt_config.MQTT_ZONE_ID), "DHT22 Temp = {} degF".format(str(temp)), retain=True)
|
mqtt_client.publish("m/v1/home/{}/0/temperature".format(mqtt_config.MQTT_ZONE_ID), "DHT22 Temp = {} degF".format(str(temp)), retain=True)
|
||||||
mqtt_client.publish("m/v1/home/{}/0/humidity".format(mqtt_config.MQTT_ZONE_ID), "DHT22 %Hum = {} %".format(str(hum)), retain=True)
|
mqtt_client.publish("m/v1/home/{}/0/humidity".format(mqtt_config.MQTT_ZONE_ID), "DHT22 %Hum = {} %".format(str(hum)), retain=True)
|
||||||
debug_str = "{} ; {}".format(temp, hum,)
|
debug_str = "{} ; {}".format(temp, hum)
|
||||||
|
|
||||||
cpu_temp = read_cpu_temp()
|
cpu_temp = read_cpu_temp()
|
||||||
cpu_temp = cpu_temp * 9/5. +32.0
|
|
||||||
|
|
||||||
print("{} ; CPU: {} ; Vsys: {}".format(debug_str, cpu_temp, vsys_volts))
|
print("{} ; CPU: {}".format(debug_str, cpu_temp))
|
||||||
|
|
||||||
# HW Info
|
# HW Info
|
||||||
mqtt_client.publish("m/v1/hw/{}/cpu/temperature".format(mqtt_config.MQTT_HW_ID), "CPU Temp = {} degF".format(str(cpu_temp)), retain=True)
|
mqtt_client.publish("m/v1/hw/{}/cpu/temperature".format(mqtt_config.MQTT_HW_ID), "CPU Temp = {} degF".format(str(cpu_temp)), retain=True)
|
||||||
mqtt_client.publish("m/v1/hw/{}/vsys/voltage".format(mqtt_config.MQTT_HW_ID), "CPU Voltage = {} V".format(str(vsys_volts)), retain=True)
|
|
||||||
mqtt_client.publish("m/v1/hw/{}/wlan/info".format(mqtt_config.MQTT_HW_ID), str(ifconfig), retain=True)
|
mqtt_client.publish("m/v1/hw/{}/wlan/info".format(mqtt_config.MQTT_HW_ID), str(ifconfig), retain=True)
|
||||||
|
|
||||||
print("Killing the MQTT Connection")
|
|
||||||
mqtt_client.disconnect()
|
mqtt_client.disconnect()
|
||||||
print("Going sleep ...")
|
print(f'MQTT Disconnected.')
|
||||||
time.sleep(1)
|
|
||||||
|
|
||||||
# Prep HW for sleep
|
# Sleep for a bit.
|
||||||
wlan.disconnect()
|
print(f'Finished loop #{count}.')
|
||||||
wlan.active(False)
|
time.sleep(5)
|
||||||
wlan.deinit()
|
|
||||||
machine.lightsleep(5000)
|
|
||||||
print("Woke up ...")
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
print('Start/Woke, reset clause {}'.format(machine.reset_cause()))
|
|
||||||
main()
|
main()
|
||||||
|
|||||||
Reference in New Issue
Block a user