Compare commits
9 Commits
78d2b108b2
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4d6f69450 | ||
|
|
5de9255526 | ||
|
|
27aaf239c7 | ||
|
|
af035f0358 | ||
| b9c2c6054a | |||
| ce27e252a5 | |||
|
|
cc1170a4d1 | ||
|
|
806aaab8bd | ||
|
|
501186efa3 |
@@ -1,3 +1,3 @@
|
|||||||
# school-DHT11-sensor
|
# school-DHT11-sensor
|
||||||
|
|
||||||
a simple python file that reads data from a DHT11 sensor and uses it in multiple ways
|
a simple python file that reads data from a DHT11 sensor and uses it to decide if a fan and LED should turn on
|
||||||
44
dht11.py
44
dht11.py
@@ -6,14 +6,16 @@ import http.server
|
|||||||
import threading
|
import threading
|
||||||
import json
|
import json
|
||||||
|
|
||||||
sensor = adafruit_dht.DHT11(board.D6)
|
sensor = adafruit_dht.DHT11(board.D31)
|
||||||
LED_PIN = 21
|
LED_PIN = 40
|
||||||
FAN_PIN = 22
|
FAN_PIN = 15
|
||||||
|
|
||||||
|
#stel GPIO in (ik hoop dat het werkt. ik kan het niet testen)
|
||||||
GPIO.setmode(GPIO.BCM)
|
GPIO.setmode(GPIO.BCM)
|
||||||
GPIO.setup(LED_PIN, GPIO.OUT)
|
GPIO.setup(LED_PIN, GPIO.OUT)
|
||||||
GPIO.setup(FAN_PIN, GPIO.OUT)
|
GPIO.setup(FAN_PIN, GPIO.OUT)
|
||||||
|
|
||||||
|
#array voor sensor data
|
||||||
sensor_data = {
|
sensor_data = {
|
||||||
'tempc': 0.0,
|
'tempc': 0.0,
|
||||||
'tempf': 0.0,
|
'tempf': 0.0,
|
||||||
@@ -22,7 +24,11 @@ sensor_data = {
|
|||||||
'fan': False
|
'fan': False
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fan_timer_start = 0
|
||||||
|
fan_timer_actief = False
|
||||||
|
|
||||||
def check_data():
|
def check_data():
|
||||||
|
global fan_timer_actief, fan_timer_start
|
||||||
while True:
|
while True:
|
||||||
try:
|
try:
|
||||||
temperatuur_C = sensor.temperature
|
temperatuur_C = sensor.temperature
|
||||||
@@ -33,29 +39,50 @@ def check_data():
|
|||||||
sensor_data['humidity'] = humidity
|
sensor_data['humidity'] = humidity
|
||||||
except RuntimeError as err:
|
except RuntimeError as err:
|
||||||
print(err.args[0])
|
print(err.args[0])
|
||||||
|
# zet fan en led aan als temperatuur hoger is dan 20 C
|
||||||
if temperatuur_C > 20:
|
if temperatuur_C > 20:
|
||||||
GPIO.output(LED_PIN, GPIO.HIGH)
|
GPIO.output(LED_PIN, GPIO.HIGH)
|
||||||
GPIO.output(FAN_PIN, GPIO.HIGH)
|
GPIO.output(FAN_PIN, GPIO.HIGH)
|
||||||
sensor_data['led'] = True
|
sensor_data['led'] = True
|
||||||
sensor_data['fan'] = True
|
sensor_data['fan'] = True
|
||||||
|
fan_timer_actief = False
|
||||||
|
fan_timer_start = 0
|
||||||
else:
|
else:
|
||||||
GPIO.output(LED_PIN, GPIO.LOW)
|
if sensor_data['fan'] == True and fan_timer_actief == False:
|
||||||
GPIO.output(FAN_PIN, GPIO.LOW)
|
fan_timer_actief = True
|
||||||
|
fan_timer_start = time.time()
|
||||||
|
sensor_data['fan'] = True
|
||||||
sensor_data['led'] = False
|
sensor_data['led'] = False
|
||||||
|
GPIO.output(FAN_PIN, GPIO.HIGH)
|
||||||
|
GPIO.output(LED_PIN, GPIO.LOW)
|
||||||
|
elif sensor_data['fan'] == True and fan_timer_actief == True and time.time() - fan_timer_start > 300: # 5 minuten
|
||||||
|
fan_timer_actief = False
|
||||||
|
fan_timer_start = 0
|
||||||
sensor_data['fan'] = False
|
sensor_data['fan'] = False
|
||||||
|
sensor_data['led'] = False
|
||||||
|
GPIO.output(FAN_PIN, GPIO.LOW)
|
||||||
|
GPIO.output(LED_PIN, GPIO.LOW)
|
||||||
|
else:
|
||||||
|
fan_timer_actief = False
|
||||||
|
fan_timer_start = 0
|
||||||
|
sensor_data['fan'] = False
|
||||||
|
sensor_data['led'] = False
|
||||||
|
GPIO.output(FAN_PIN, GPIO.LOW)
|
||||||
|
GPIO.output(LED_PIN, GPIO.LOW)
|
||||||
|
|
||||||
class handler_class(http.server.BaseHTTPRequestHandler):
|
class handler_class(http.server.BaseHTTPRequestHandler):
|
||||||
def do_GET (self):
|
def do_GET (self):
|
||||||
if self.path == '/':
|
if self.path == '/':
|
||||||
|
#headers
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
self.send_header('Content-type', 'text/html; charset=utf-8')
|
self.send_header('Content-type', 'text/html; charset=utf-8')
|
||||||
self.end_headers()
|
self.end_headers()
|
||||||
|
#html website
|
||||||
website = """
|
website = """
|
||||||
<!DOCTYPE html>
|
<!DOCTYPE html>
|
||||||
<html>
|
<html>
|
||||||
<head>
|
<head>
|
||||||
<title>IOT DHT11 sensor status<title>
|
<title>IOT DHT11 sensor status</title>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
@@ -72,7 +99,7 @@ class handler_class(http.server.BaseHTTPRequestHandler):
|
|||||||
.then(data => {
|
.then(data => {
|
||||||
document.getElementById('tempc').textContent = data.tempc.toFixed(1);
|
document.getElementById('tempc').textContent = data.tempc.toFixed(1);
|
||||||
document.getElementById('tempf').textContent = data.tempf.toFixed(1);
|
document.getElementById('tempf').textContent = data.tempf.toFixed(1);
|
||||||
document.getElementById('humidity').textContent = data.humid.toFixed(1);
|
document.getElementById('humid').textContent = data.humidity.toFixed(1);
|
||||||
document.getElementById('led').textContent = data.led ? 'AAN' : 'UIT';
|
document.getElementById('led').textContent = data.led ? 'AAN' : 'UIT';
|
||||||
document.getElementById('fan').textContent = data.fan ? 'AAN' : 'UIT';
|
document.getElementById('fan').textContent = data.fan ? 'AAN' : 'UIT';
|
||||||
});
|
});
|
||||||
@@ -89,6 +116,7 @@ class handler_class(http.server.BaseHTTPRequestHandler):
|
|||||||
self.wfile.write(json.dumps(sensor_data).encode())
|
self.wfile.write(json.dumps(sensor_data).encode())
|
||||||
|
|
||||||
def web_server():
|
def web_server():
|
||||||
|
#web server port en IP binding
|
||||||
web_binding = ('', 8000)
|
web_binding = ('', 8000)
|
||||||
server = http.server.HTTPServer(web_binding, handler_class)
|
server = http.server.HTTPServer(web_binding, handler_class)
|
||||||
print ("server draait op port 8000")
|
print ("server draait op port 8000")
|
||||||
|
|||||||
Reference in New Issue
Block a user