Compare commits
17 Commits
8366e290e3
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a4d6f69450 | ||
|
|
5de9255526 | ||
|
|
27aaf239c7 | ||
|
|
af035f0358 | ||
| b9c2c6054a | |||
| ce27e252a5 | |||
|
|
cc1170a4d1 | ||
|
|
806aaab8bd | ||
|
|
501186efa3 | ||
|
|
78d2b108b2 | ||
|
|
270866863c | ||
|
|
31da346652 | ||
|
|
928b3f8a71 | ||
|
|
d7bb5af086 | ||
|
|
810f3fe69d | ||
|
|
308029ca82 | ||
|
|
38a96bb61d |
@@ -1,3 +1,3 @@
|
||||
# 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
|
||||
102
dht11.py
102
dht11.py
@@ -4,35 +4,127 @@ import board
|
||||
import RPi.GPIO as GPIO
|
||||
import http.server
|
||||
import threading
|
||||
import json
|
||||
|
||||
sensor = adafruit_dht.DHT11(board.D6)
|
||||
LED_PIN = 21
|
||||
FAN_PIN = 22
|
||||
sensor = adafruit_dht.DHT11(board.D31)
|
||||
LED_PIN = 40
|
||||
FAN_PIN = 15
|
||||
|
||||
#stel GPIO in (ik hoop dat het werkt. ik kan het niet testen)
|
||||
GPIO.setmode(GPIO.BCM)
|
||||
GPIO.setup(LED_PIN, GPIO.OUT)
|
||||
GPIO.setup(FAN_PIN, GPIO.OUT)
|
||||
|
||||
#array voor sensor data
|
||||
sensor_data = {
|
||||
'tempc': 0.0,
|
||||
'tempf': 0.0,
|
||||
'humidity': 0.0,
|
||||
'led': False,
|
||||
'fan': False
|
||||
}
|
||||
|
||||
fan_timer_start = 0
|
||||
fan_timer_actief = False
|
||||
|
||||
def check_data():
|
||||
global fan_timer_actief, fan_timer_start
|
||||
while True:
|
||||
try:
|
||||
temperatuur_C = sensor.temperature
|
||||
temperatuur_F = temperatuur_C * (9/5) + 32
|
||||
humidity = sensor.humidity
|
||||
sensor_data['tempc'] = temperatuur_C
|
||||
sensor_data['tempf'] = temperatuur_F
|
||||
sensor_data['humidity'] = humidity
|
||||
except RuntimeError as err:
|
||||
print(err.args[0])
|
||||
# zet fan en led aan als temperatuur hoger is dan 20 C
|
||||
if temperatuur_C > 20:
|
||||
GPIO.output(LED_PIN, GPIO.HIGH)
|
||||
GPIO.output(FAN_PIN, GPIO.HIGH)
|
||||
sensor_data['led'] = True
|
||||
sensor_data['fan'] = True
|
||||
fan_timer_actief = False
|
||||
fan_timer_start = 0
|
||||
else:
|
||||
if sensor_data['fan'] == True and fan_timer_actief == False:
|
||||
fan_timer_actief = True
|
||||
fan_timer_start = time.time()
|
||||
sensor_data['fan'] = True
|
||||
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['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):
|
||||
def do_GET (self):
|
||||
if self.path == '/':
|
||||
#headers
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'text/html; charset=utf-8')
|
||||
self.end_headers()
|
||||
#html website
|
||||
website = """
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<title>IOT DHT11 sensor status</title>
|
||||
<meta charset="UTF-8">
|
||||
</head>
|
||||
<body>
|
||||
<h1>IOT DHT11 Sensor Status</h1>
|
||||
<p>Temp_C = <span id="tempc">--</span>C</p>
|
||||
<p>Temp_F = <span id="tempf">--</span>F</p>
|
||||
<p>humidity = <span id="humid">--</span>%</p>
|
||||
<p>LED = <span id="led">UIT</span></p>
|
||||
<p>FAN = <span id="fan">UIT</span></p>
|
||||
<script>
|
||||
setInterval(() => {
|
||||
fetch('/data')
|
||||
.then(res => res.json())
|
||||
.then(data => {
|
||||
document.getElementById('tempc').textContent = data.tempc.toFixed(1);
|
||||
document.getElementById('tempf').textContent = data.tempf.toFixed(1);
|
||||
document.getElementById('humid').textContent = data.humidity.toFixed(1);
|
||||
document.getElementById('led').textContent = data.led ? 'AAN' : 'UIT';
|
||||
document.getElementById('fan').textContent = data.fan ? 'AAN' : 'UIT';
|
||||
});
|
||||
}, 1000);
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
self.wfile.write(website.encode())
|
||||
elif self.path == '/data':
|
||||
self.send_response(200)
|
||||
self.send_header('Content-type', 'application/json')
|
||||
self.end_headers()
|
||||
self.wfile.write(json.dumps(sensor_data).encode())
|
||||
|
||||
def web_server():
|
||||
#web server port en IP binding
|
||||
web_binding = ('', 8000)
|
||||
server = http.server.HTTPServer(web_binding)
|
||||
print ("server draait")
|
||||
server = http.server.HTTPServer(web_binding, handler_class)
|
||||
print ("server draait op port 8000")
|
||||
server.serve_forever()
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
threading.Thread(target=web_server, daemon=True).start()
|
||||
threading.Thread(target=check_data, daemon=True).start()
|
||||
while True:
|
||||
time.sleep(1)
|
||||
Reference in New Issue
Block a user