From 84b9df2728218be37510e2907f43b32229e7693f Mon Sep 17 00:00:00 2001 From: Shaun Setlock Date: Sun, 13 Nov 2022 21:22:15 -0500 Subject: [PATCH] Uploading working code. --- main.py | 51 +++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 14 deletions(-) diff --git a/main.py b/main.py index b438590..2ec1af2 100644 --- a/main.py +++ b/main.py @@ -3,12 +3,14 @@ import rp2 import time import machine from machine import Pin +from umqtt.simple import MQTTClient import dht import urequests import wifi_config +import mqtt_config # Change this GPIO PIN where your DHT22 sensor is connected -DHT_22_GPIO_PIN = 2 +DHT_22_GPIO_PIN = 3 def read_cpu_temp(): """ @@ -25,8 +27,7 @@ def read_cpu_temp(): cpu_temp_sensor = machine.ADC(4) reading = cpu_temp_sensor.read_u16() * cpu_temp_conversion_factor temperature_c = 27 - (reading - 0.706) / 0.001721 - temperature_f = temperature_c * 9/5. + 32 - return temperature_f + return temperature_c def read_vsys(): # Set pin 29 for the voltage reading @@ -101,7 +102,7 @@ def read_dht_22(sensor): return reading_1 def wlan_up(wlan): - print("Connecting to Wifi...") + print("Connoting to Wifi...") wlan.active(True) print("Wifi chip is active ... wlan.connect now") wlan.connect(wifi_config.HOME_WIFI_SSID, wifi_config.HOME_WIFI_PWD) @@ -158,6 +159,8 @@ def main(): rp2.country('US') wlan = network.WLAN(network.STA_IF) + mqtt_client = MQTTClient(mqtt_config.MQTT_CLIENT_ID, mqtt_config.MQTT_HOST_NAME) + sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN)) led = machine.Pin('LED', machine.Pin.OUT) @@ -180,27 +183,47 @@ def main(): led_error_code(led, 3) continue + try: + mqtt_client.connect() + print("Connected to MQTT") + except Exception as 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) + continue + dht22_reading = read_dht_22(sensor) debug_str = "None" if dht22_reading is not None: temp,hum = dht22_reading - temp = temp * 9/5. + 32 # Convert to degF - print("DHT22 Temp: {} ; DHT22 Humidity: {}".format(temp, hum)) - + 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/humidity".format(mqtt_config.MQTT_ZONE_ID), "DHT22 %Hum = {} %".format(str(hum)), retain=True) + debug_str = "{} ; {}".format(temp, hum,) + cpu_temp = read_cpu_temp() + cpu_temp = cpu_temp * 9/5. +32.0 print("{} ; CPU: {} ; Vsys: {}".format(debug_str, cpu_temp, vsys_volts)) - print("Going sleep ...\n\n") - time.sleep(10) + # 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/{}/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) + + print("Killing the MQTT Connection") + mqtt_client.disconnect() + print("Going sleep ...") + time.sleep(1) # Prep HW for sleep - #wlan.disconnect() - #wlan.active(False) - #wlan.deinit() - #machine.lightsleep(10) - #print("Woke up ...") + wlan.disconnect() + wlan.active(False) + wlan.deinit() + machine.lightsleep(5000) + print("Woke up ...")