From 69810b8c7cfcb71fc5794c398de168639d9a2810 Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Tue, 7 Feb 2023 11:22:31 -0500 Subject: [PATCH] Add sensors and fix logging --- main.py | 41 ++++++++++++++++++++++++----------------- net/logging.py | 4 ++-- sensors/__init__.py | 2 ++ sensors/watersensor.py | 3 --- www/index.html | 15 ++++++--------- 5 files changed, 34 insertions(+), 31 deletions(-) diff --git a/main.py b/main.py index a9afdd2..50d2cd2 100644 --- a/main.py +++ b/main.py @@ -1,20 +1,23 @@ import json import time -from machine import Pin +from machine import I2C from net import AdafruitIO from net import logging from net import ntp from net import Server from net import templates from net import util +from sensors import MCP9808 +from sensors import WaterSensor class LedServer(Server): def __init__(self): super().__init__() - self.led = Pin('LED', Pin.OUT) + self.waterSensor = WaterSensor(0) + self.mcp = MCP9808(I2C(0)) self.aio = AdafruitIO() self.ntp_interval_in_seconds = 60 * 60 self.aio_interval_in_seconds = 60 * 5 @@ -42,12 +45,6 @@ class LedServer(Server): def handlePath(self, path): if path == 'index.json': return self.getJsonData() - elif path == 'light/on': - self.led.on() - return self.getJsonData() - elif path == 'light/off': - self.led.off() - return self.getJsonData() return self.getPathData(path) @@ -61,22 +58,32 @@ class LedServer(Server): return templates.render( f'www/{path or self.default_path}', - ledStatus=self.ledTextValue(), - ledClass=self.ledCssClass(), - datetime=util.datetime() + datetime=util.datetime(), + temperature=self.getTemperatureTextValue(), + waterStatus=self.waterTextValue(), + waterClass=self.waterCssClass() ) def getReading(self): - return {'timestamp': util.datetimeISO8601(), 'readings': self.getState()} + return { + 'timestamp': util.datetimeISO8601(), + 'readings': self.getState() + } def getState(self): - return {'is-led-on': bool(self.led.value())} + return { + 'is-water-present': self.waterSensor.isWaterPresent(), + 'temperature': self.mcp.get_temp() + } - def ledTextValue(self): - return 'ON' if self.led.value() else 'OFF' + def getTemperatureTextValue(self): + return round(self.mcp.get_temp(), 2) - def ledCssClass(self): - return 'led-on' if self.led.value() else 'led-off' + def waterTextValue(self): + return 'Present' if self.waterSensor.isWaterPresent() else 'Absent' + + def waterCssClass(self): + return 'water-present' if self.waterSensor.isWaterPresent() else 'water-absent' def main(): diff --git a/net/logging.py b/net/logging.py index 1415ff0..675683d 100644 --- a/net/logging.py +++ b/net/logging.py @@ -67,8 +67,8 @@ def truncate(file, target_size): # try to find a line break nearby to split first chunk on break_position = max( - chunk.find(b'\n', -discard), # search forward - chunk.rfind(b'\n', -discard) # search backwards + chunk.find(b'\n', -discard), # search forward + chunk.rfind(b'\n', 0, -discard) # search backwards ) if break_position != -1: # if we found a line break.. outfile.write(chunk[break_position + 1:]) diff --git a/sensors/__init__.py b/sensors/__init__.py index e69de29..af4d296 100644 --- a/sensors/__init__.py +++ b/sensors/__init__.py @@ -0,0 +1,2 @@ +from .mcp9808 import MCP9808 +from .watersensor import WaterSensor diff --git a/sensors/watersensor.py b/sensors/watersensor.py index 2485bfa..32c25e5 100644 --- a/sensors/watersensor.py +++ b/sensors/watersensor.py @@ -13,6 +13,3 @@ class WaterSensor: def isWaterPresent(self): return self.reading > self.threshold - - def waterStatus(self): - return 'Present' if self.isWaterPresent() else 'Absent' diff --git a/www/index.html b/www/index.html index a2a1c46..778179b 100644 --- a/www/index.html +++ b/www/index.html @@ -12,14 +12,6 @@ color: #ddd; } - .led-on { - color: greenyellow; - } - - .led-off { - color: lightblue; - } - .water-present { color: pink; } @@ -28,13 +20,18 @@ color: lightblue; } + .temperature { + font-family: monospace; + } +

Pico W

{{datetime}}

-

Led is {{ledStatus}}

+

{{temperature}} °C

+

Water is {{waterStatus}}