Archived
1
0

Uploading working code.

This commit is contained in:
Shaun Setlock
2022-11-13 21:22:15 -05:00
parent 4cbc0f7e05
commit 84b9df2728

49
main.py
View File

@@ -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 ...")