[WIP] dht22 sensor working so far

This commit is contained in:
Maik Müller 2024-04-25 18:35:50 +02:00
parent e0a2ab9fd7
commit c8f4baf984
3 changed files with 46 additions and 1 deletions

32
dht22.py Normal file
View File

@ -0,0 +1,32 @@
from dht import DHT22
from machine import ADC, Pin
class TemperatureHumiditySensor:
dht22_sensor_pin_int = -1
dht22_sensor = None
most_recent_values = {}
def __init__(self, settings):
print("Hello from dht22 sensor class")
print(settings)
self.dht22_sensor_pin_int = settings['pin_int']
self.dht22_sensor = DHT22(Pin(self.dht22_sensor_pin_int, Pin.IN, Pin.PULL_UP))
def read(self):
try:
self.dht22_sensor.measure()
self.most_recent_values = {
'temperature': {
'value': self.dht22_sensor.temperature(),
'unit': '°C'},
'humidity': {
'value': self.dht22_sensor.humidity(),
'unit': '%'
}
}
except OSError:
print('DHT22 Error reading temperature/humidity. Check wires')
print()

View File

@ -1,10 +1,12 @@
import time
from moisture_sensor import MoistureSensor
from dht22 import TemperatureHumiditySensor
class GrowSystem:
moisture_sensor = None
temperature_humidity_sensor = None
most_recent_values = {}
@ -14,11 +16,20 @@ class GrowSystem:
if not self.moisture_sensor:
self.moisture_sensor = MoistureSensor(settings['moisture_sensor'])
if not self.temperature_humidity_sensor:
self.temperature_humidity_sensor = TemperatureHumiditySensor(settings['temperature_humidity_sensor'])
def start(self):
print("Start reading sensors ...")
while True:
# Moisture Sensor
self.moisture_sensor.read()
self.most_recent_values['moisture_sensor'] = self.moisture_sensor.most_recent_value
# Temperature and Humidity Sensor
self.temperature_humidity_sensor.read()
self.most_recent_values['temperature_humidity_sensor'] = self.temperature_humidity_sensor.most_recent_values
print(self.most_recent_values)
time.sleep(1)

View File

@ -11,10 +11,12 @@ from grow_system import GrowSystem
settings = {
'wlan_ssid': 'Oppa-95.lan',
'wlan_pw': '95%04-MM',
'led_pin_int': 15,
'moisture_sensor': {
'pin_int': 26
},
'temperature_humidity_sensor': {
'pin_int': 15
},
'pump_pin_int': 24
}