1
0

fout met vorige commit

This commit is contained in:
2025-06-29 02:43:42 +02:00
parent 270866863c
commit 78d2b108b2

View File

@@ -4,6 +4,7 @@ import board
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import http.server import http.server
import threading import threading
import json
sensor = adafruit_dht.DHT11(board.D6) sensor = adafruit_dht.DHT11(board.D6)
LED_PIN = 21 LED_PIN = 21
@@ -13,20 +14,36 @@ 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)
sensor_data = {
'tempc': 0.0,
'tempf': 0.0,
'humidity': 0.0,
'led': False,
'fan': False
}
def check_data(): def check_data():
while True: while True:
try: try:
temperatuur_C = sensor.temperature temperatuur_C = sensor.temperature
temperatuur_F = temperatuur_C * (9/5) + 32 temperatuur_F = temperatuur_C * (9/5) + 32
humidity = sensor.humidity humidity = sensor.humidity
sensor_data['tempc'] = temperatuur_C
sensor_data['tempf'] = temperatuur_F
sensor_data['humidity'] = humidity
except RuntimeError as err: except RuntimeError as err:
print(err.args[0]) print(err.args[0])
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['fan'] = True
else: else:
GPIO.output(LED_PIN, GPIO.LOW) GPIO.output(LED_PIN, GPIO.LOW)
GPIO.output(FAN_PIN, GPIO.LOW) GPIO.output(FAN_PIN, GPIO.LOW)
sensor_data['led'] = False
sensor_data['fan'] = False
class handler_class(http.server.BaseHTTPRequestHandler): class handler_class(http.server.BaseHTTPRequestHandler):
def do_GET (self): def do_GET (self):
@@ -35,7 +52,7 @@ class handler_class(http.server.BaseHTTPRequestHandler):
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()
website = """ website = """
<DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>IOT DHT11 sensor status<title> <title>IOT DHT11 sensor status<title>
@@ -46,7 +63,31 @@ class handler_class(http.server.BaseHTTPRequestHandler):
<p>Temp_C = <span id="tempc">--</span>C</p> <p>Temp_C = <span id="tempc">--</span>C</p>
<p>Temp_F = <span id="tempf">--</span>F</p> <p>Temp_F = <span id="tempf">--</span>F</p>
<p>humidity = <span id="humid">--</span>%</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('humidity').textContent = data.humid.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(): def web_server():
web_binding = ('', 8000) web_binding = ('', 8000)
server = http.server.HTTPServer(web_binding, handler_class) server = http.server.HTTPServer(web_binding, handler_class)