Finish MVP
This commit is contained in:
@@ -8,13 +8,15 @@
|
||||
</head>
|
||||
<body>
|
||||
<h1>Sensor {name}</h1>
|
||||
<h2>Sensor Values:</h2>
|
||||
<h2>Sensor Values</h2>
|
||||
<ul>
|
||||
<li>Temperature: {temp}°C</li>
|
||||
<li>Humidity: {humidity}%</li>
|
||||
</ul>
|
||||
<form action="/"><button type="submit">Update Values</button></form>
|
||||
<h2>Relay:</h2>
|
||||
<form action="/update"><button type="submit">Update Values</button></form>
|
||||
<form action="/get_logs"><button type="submit">Get Logs</button></form>
|
||||
|
||||
<h2>Relay</h2>
|
||||
<p>
|
||||
Status: {relay_status}
|
||||
</p>
|
||||
@@ -24,7 +26,15 @@
|
||||
<form action="/off"><button type="submit">Relay Off</button></form>
|
||||
<form action="/toggle"><button type="submit">Toggle Relay</button></form>
|
||||
</span>
|
||||
</p>
|
||||
|
||||
<h2>Config</h2>
|
||||
<p>
|
||||
<form action="/set_limit" method="POST">
|
||||
<label for="limit">Humidity Limit (%)</label>
|
||||
<input type="number"/ id="limit" name="limit" value="{humidity_limit}">
|
||||
<button type="submit">Set</button>
|
||||
</form>
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
||||
<meta http-equiv="X-UA-Compatible" content="ie=edge">
|
||||
<title>Sensor Test Sensor</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1>Sensor Test Sensor</h1>
|
||||
<ul>
|
||||
<li>Temperature: 20.7°C</li>
|
||||
<li>Humidity: 62.6%</li>
|
||||
</ul>
|
||||
<p>
|
||||
<form action="/submit" method="GET"><button type="submit" formaction="/toggle">Toggle Relay</button></form>
|
||||
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
|
||||
179
code/src/main.py
179
code/src/main.py
@@ -1,25 +1,94 @@
|
||||
#!/bin/python
|
||||
|
||||
import dht
|
||||
from machine import Pin, Timer
|
||||
from machine import Pin, Timer, RTC
|
||||
from micropyserver import MicroPyServer
|
||||
from micropyserver_utils import send_response
|
||||
from micropyserver_utils import send_response, get_request_post_params
|
||||
import ntptime
|
||||
import os
|
||||
|
||||
DHT_PIN = 26
|
||||
RELAY_PIN = 4
|
||||
UPDATE_INTERVAL_S = 2
|
||||
UPDATE_INTERVAL_S = 60
|
||||
#MAX_LOG_SIZE = 1.5 * 1024 * 1024 # MegaByte
|
||||
MAX_LOG_SIZE = 200
|
||||
|
||||
name = "Test Sensor"
|
||||
humidity_limit_path = 'humidity_limit'
|
||||
sensor_log_path = 'sensors.log'
|
||||
sensor_log_path = 'sensors.log'
|
||||
log_file = None
|
||||
log_cursor_file = None
|
||||
|
||||
server = MicroPyServer()
|
||||
dht = dht.DHT22(Pin(DHT_PIN))
|
||||
relay = Pin(RELAY_PIN, Pin.OUT)
|
||||
update_timer = Timer(0)
|
||||
index_template = ""
|
||||
|
||||
humidity_limit = 80
|
||||
|
||||
temp = 0
|
||||
humidity = 0
|
||||
|
||||
def read(_):
|
||||
def redirect_root():
|
||||
send_response(server, ' ', http_code=303, content_type="text/plain", extend_headers=["Location: /"])
|
||||
|
||||
def handle_update(_):
|
||||
update("")
|
||||
|
||||
redirect_root()
|
||||
|
||||
def write_log():
|
||||
global log_file
|
||||
pos = log_file.tell()
|
||||
if pos > MAX_LOG_SIZE:
|
||||
log_file.seek(0, 0)
|
||||
|
||||
log_values(log_file)
|
||||
|
||||
log_cursor_file.seek(0,0)
|
||||
log_cursor_file.write(str(log_file.tell()))
|
||||
|
||||
log_cursor_file.seek(0,0)
|
||||
content = log_cursor_file.readline()
|
||||
print(content)
|
||||
|
||||
def log_values(file):
|
||||
global temp, humidity
|
||||
time = ntptime.time()
|
||||
log_string = f'{time:014d},{str(temp)},{str(humidity)};'
|
||||
file.write(log_string)
|
||||
file.flush()
|
||||
|
||||
def get_logs(_):
|
||||
log = ''
|
||||
try:
|
||||
with open(sensor_log_path) as log_file:
|
||||
log = log_file.readline()
|
||||
except OSError:
|
||||
None
|
||||
|
||||
send_response(server, log, http_code=200, content_type="text/plain", extend_headers=['Content-Disposition: attachment; filename="logs.csv"',"Location: /"])
|
||||
|
||||
def update(_):
|
||||
read_sensors()
|
||||
|
||||
update_relay()
|
||||
|
||||
write_log()
|
||||
|
||||
def update_relay():
|
||||
if humidity > humidity_limit:
|
||||
relay_on()
|
||||
return
|
||||
|
||||
relay_off()
|
||||
|
||||
def high_humidity() -> bool:
|
||||
return humidity >= humidity_limit
|
||||
|
||||
def read_sensors():
|
||||
global temp, humidity
|
||||
try:
|
||||
dht.measure()
|
||||
@@ -30,10 +99,15 @@ def read(_):
|
||||
humidity = dht.humidity()
|
||||
|
||||
def serve_root(_):
|
||||
global server, temp, humidity, index_template, relay
|
||||
global server, temp, humidity, index_template, relay, humidity_limit
|
||||
|
||||
relay_status = "On" if relay.value() else "Off"
|
||||
index = index_template.replace('{name}', name).replace('{temp}', str(temp)).replace('{humidity}', str(humidity)).replace('{relay_status}', relay_status)
|
||||
index = index_template \
|
||||
.replace('{name}', name) \
|
||||
.replace('{temp}', str(temp)) \
|
||||
.replace('{humidity}', str(humidity)) \
|
||||
.replace('{relay_status}', relay_status) \
|
||||
.replace('{humidity_limit}', str(humidity_limit))
|
||||
|
||||
send_response(server, index)
|
||||
|
||||
@@ -42,33 +116,108 @@ def toggle_relay(_):
|
||||
val = relay.value()
|
||||
relay.value(not val)
|
||||
|
||||
send_response(server, ' ', http_code=303, content_type="text/plain", extend_headers=["Location: /"])
|
||||
redirect_root()
|
||||
|
||||
def relay_off(_):
|
||||
def handle_relay_off(_):
|
||||
relay_off()
|
||||
redirect_root()
|
||||
|
||||
def relay_off():
|
||||
global relay
|
||||
relay.value(0)
|
||||
send_response(server, ' ', http_code=303, content_type="text/plain", extend_headers=["Location: /"])
|
||||
|
||||
|
||||
def relay_on(_):
|
||||
def handle_relay_on(_):
|
||||
relay_on()
|
||||
redirect_root()
|
||||
|
||||
def relay_on():
|
||||
global relay
|
||||
relay.value(1)
|
||||
send_response(server, ' ', http_code=303, content_type="text/plain", extend_headers=["Location: /"])
|
||||
|
||||
def set_humidity_limit(request):
|
||||
global humidity_limit
|
||||
|
||||
params = get_request_post_params(request)
|
||||
print(params)
|
||||
|
||||
if not params or not params["limit"]:
|
||||
redirect_root()
|
||||
return
|
||||
|
||||
new_value = int(params["limit"])
|
||||
if new_value < 0 or new_value > 100:
|
||||
redirect_root()
|
||||
return
|
||||
|
||||
humidity_limit = new_value
|
||||
write_limit(new_value)
|
||||
|
||||
redirect_root()
|
||||
|
||||
def write_limit(limit: int):
|
||||
with open(humidity_limit_path, 'w') as file:
|
||||
file.write(str(humidity_limit))
|
||||
|
||||
def read_limit() -> int:
|
||||
with open(humidity_limit_path) as file:
|
||||
return int(file.readline())
|
||||
|
||||
def init():
|
||||
init_clock()
|
||||
init_log()
|
||||
init_settings()
|
||||
init_timer()
|
||||
init_webserver()
|
||||
|
||||
def init_log():
|
||||
global log_file, log_cursor_file
|
||||
try:
|
||||
log_file = open(sensor_log_path, 'r+')
|
||||
except OSError:
|
||||
log_file = open(sensor_log_path, 'w+')
|
||||
try:
|
||||
log_cursor_file = open(f'{sensor_log_path}.cursor', 'r+')
|
||||
except OSError:
|
||||
log_cursor_file = open(f'{sensor_log_path}.cursor', 'w+')
|
||||
|
||||
cursor = 0
|
||||
cursor_content = log_cursor_file.readline()
|
||||
print(cursor_content)
|
||||
try:
|
||||
cursor = int(cursor_content)
|
||||
except:
|
||||
cursor = 0
|
||||
|
||||
print(cursor)
|
||||
log_file.seek(cursor, 0)
|
||||
|
||||
def init_clock():
|
||||
ntptime.settime()
|
||||
|
||||
def init_settings():
|
||||
global humidity_limit
|
||||
|
||||
try:
|
||||
os.stat(humidity_limit_path)
|
||||
except OSError:
|
||||
write_limit(humidity_limit)
|
||||
|
||||
humidity_limit = read_limit()
|
||||
|
||||
def init_timer():
|
||||
global update_timer
|
||||
update_timer = update_timer.init(period=UPDATE_INTERVAL_S * 1000, callback=read)
|
||||
update_timer = update_timer.init(period=UPDATE_INTERVAL_S * 1000, callback=update)
|
||||
|
||||
def init_webserver():
|
||||
load_index_template()
|
||||
server.add_route('/', serve_root)
|
||||
server.add_route('/toggle', toggle_relay)
|
||||
server.add_route('/off', relay_off)
|
||||
server.add_route('/on', relay_on)
|
||||
server.add_route('/off', handle_relay_off)
|
||||
server.add_route('/on', handle_relay_on)
|
||||
server.add_route('/set_limit', set_humidity_limit, method="POST")
|
||||
server.add_route('/update', handle_update)
|
||||
server.add_route('/get_logs', get_logs)
|
||||
|
||||
def load_index_template():
|
||||
global index_template
|
||||
@@ -84,5 +233,3 @@ def main():
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user