summaryrefslogtreecommitdiff
path: root/read_dht11.py
diff options
context:
space:
mode:
authorRobert Scheibe <rob.scheibe@gmail.com>2025-01-04 16:06:05 +0100
committerRobert Scheibe <rob.scheibe@gmail.com>2025-01-04 16:06:05 +0100
commita07c89c0fb9659c9961f8d9cef7ec49123b2171f (patch)
tree98818e61cf9a9bf184a5db12ff2e776c10781d09 /read_dht11.py
parent047178cc439a68c1366a215f4827b52f6437309b (diff)
added moisture sensor
Diffstat (limited to 'read_dht11.py')
-rwxr-xr-xread_dht11.py39
1 files changed, 27 insertions, 12 deletions
diff --git a/read_dht11.py b/read_dht11.py
index 524fdba..e560a8a 100755
--- a/read_dht11.py
+++ b/read_dht11.py
@@ -3,10 +3,22 @@ import sqlite3
from datetime import datetime
import adafruit_dht
import board
+import busio
+import adafruit_ads1x15.ads1115 as ADS
+from adafruit_ads1x15.analog_in import AnalogIn
# GPIO-Pin für den DHT11-Sensor
DHT_SENSOR = adafruit_dht.DHT11(board.D26)
+# Initialize the I2C interface
+i2c = busio.I2C(board.SCL, board.SDA)
+
+# Create an ADS1115 object
+ads = ADS.ADS1115(i2c)
+
+# Define the analog input channel
+channel = AnalogIn(ads, ADS.P0)
+
# Funktion zur Initialisierung der SQLite-Datenbank
def init_db(db_name):
conn = sqlite3.connect(db_name)
@@ -17,30 +29,32 @@ def init_db(db_name):
id INTEGER PRIMARY KEY AUTOINCREMENT,
timestamp TEXT NOT NULL,
temperature REAL NOT NULL,
- humidity REAL NOT NULL
+ humidity REAL NOT NULL,
+ moisture REAL NOT NULL
)
""")
conn.commit()
return conn
# Funktion zum Einfügen von Daten in die Datenbank
-def insert_data(conn, timestamp, temperature, humidity):
+def insert_data(conn, timestamp, temperature, humidity, moisture):
cursor = conn.cursor()
cursor.execute("""
- INSERT INTO sensor_data (timestamp, temperature, humidity)
- VALUES (?, ?, ?)
- """, (timestamp, temperature, humidity))
+ INSERT INTO sensor_data (timestamp, temperature, humidity, moisture)
+ VALUES (?, ?, ?, ?)
+ """, (timestamp, temperature, humidity, moisture))
conn.commit()
# Funktion zum Lesen von Temperatur und Luftfeuchtigkeit vom DHT11-Sensor
def read_dht11():
humidity = DHT_SENSOR.humidity
temperature = DHT_SENSOR.temperature
+ moisture = channel.value
if humidity is not None and temperature is not None:
- return round(temperature, 1), round(humidity, 1)
+ return round(temperature, 1), round(humidity, 1), moisture
else:
- print("Fehler beim Lesen des Sensors. Versuche erneut...")
- return None, None
+ print("Fehler beim Lesen der Sensoren. Versuche erneut...")
+ return None, None, None
# Hauptfunktion
def main():
@@ -48,14 +62,15 @@ def main():
conn = init_db(db_name)
# Ruft Temperatur und Luftfeuchtigkeit ab
- temperature, humidity = read_dht11()
+ temperature, humidity, moisture = read_dht11()
+
- if temperature is not None and humidity is not None:
+ if temperature is not None and humidity is not None and moisture is not None:
# Holt das aktuelle Datum und die Uhrzeit
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Speichert die Daten in der Datenbank
- insert_data(conn, timestamp, temperature, humidity)
- print(f"Gespeichert: {timestamp} - Temperatur: {temperature}°C - Luftfeuchtigkeit: {humidity}%")
+ insert_data(conn, timestamp, temperature, humidity, moisture)
+ print(f"Gespeichert: {timestamp} - Temperatur: {temperature}°C - Luftfeuchtigkeit: {humidity}% - Bodenfeutigkeit {moisture}")
conn.close()