Uploading working code.
This commit is contained in:
51
main.py
51
main.py
@@ -3,12 +3,14 @@ import rp2
|
|||||||
import time
|
import time
|
||||||
import machine
|
import machine
|
||||||
from machine import Pin
|
from machine import Pin
|
||||||
|
from umqtt.simple import MQTTClient
|
||||||
import dht
|
import dht
|
||||||
import urequests
|
import urequests
|
||||||
import wifi_config
|
import wifi_config
|
||||||
|
import 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 = 2
|
DHT_22_GPIO_PIN = 3
|
||||||
|
|
||||||
def read_cpu_temp():
|
def read_cpu_temp():
|
||||||
"""
|
"""
|
||||||
@@ -25,8 +27,7 @@ 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
|
||||||
temperature_f = temperature_c * 9/5. + 32
|
return temperature_c
|
||||||
return temperature_f
|
|
||||||
|
|
||||||
def read_vsys():
|
def read_vsys():
|
||||||
# Set pin 29 for the voltage reading
|
# Set pin 29 for the voltage reading
|
||||||
@@ -101,7 +102,7 @@ def read_dht_22(sensor):
|
|||||||
return reading_1
|
return reading_1
|
||||||
|
|
||||||
def wlan_up(wlan):
|
def wlan_up(wlan):
|
||||||
print("Connecting to Wifi...")
|
print("Connoting to Wifi...")
|
||||||
wlan.active(True)
|
wlan.active(True)
|
||||||
print("Wifi chip is active ... wlan.connect now")
|
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)
|
||||||
@@ -158,6 +159,8 @@ def main():
|
|||||||
rp2.country('US')
|
rp2.country('US')
|
||||||
wlan = network.WLAN(network.STA_IF)
|
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))
|
sensor = dht.DHT22(Pin(DHT_22_GPIO_PIN))
|
||||||
|
|
||||||
led = machine.Pin('LED', machine.Pin.OUT)
|
led = machine.Pin('LED', machine.Pin.OUT)
|
||||||
@@ -180,27 +183,47 @@ def main():
|
|||||||
led_error_code(led, 3)
|
led_error_code(led, 3)
|
||||||
continue
|
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)
|
dht22_reading = read_dht_22(sensor)
|
||||||
|
|
||||||
debug_str = "None"
|
debug_str = "None"
|
||||||
if dht22_reading is not None:
|
if dht22_reading is not None:
|
||||||
temp,hum = dht22_reading
|
temp,hum = dht22_reading
|
||||||
temp = temp * 9/5. + 32 # Convert to degF
|
temp = temp * 9/5. + 32.0
|
||||||
print("DHT22 Temp: {} ; DHT22 Humidity: {}".format(temp, hum))
|
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 = read_cpu_temp()
|
||||||
|
cpu_temp = cpu_temp * 9/5. +32.0
|
||||||
|
|
||||||
print("{} ; CPU: {} ; Vsys: {}".format(debug_str, cpu_temp, vsys_volts))
|
print("{} ; CPU: {} ; Vsys: {}".format(debug_str, cpu_temp, vsys_volts))
|
||||||
|
|
||||||
print("Going sleep ...\n\n")
|
# HW Info
|
||||||
time.sleep(10)
|
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
|
# Prep HW for sleep
|
||||||
#wlan.disconnect()
|
wlan.disconnect()
|
||||||
#wlan.active(False)
|
wlan.active(False)
|
||||||
#wlan.deinit()
|
wlan.deinit()
|
||||||
#machine.lightsleep(10)
|
machine.lightsleep(5000)
|
||||||
#print("Woke up ...")
|
print("Woke up ...")
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user