Add sensors and fix logging

This commit is contained in:
Mike Cifelli 2023-02-07 11:22:31 -05:00
parent e3fc7a7666
commit 69810b8c7c
5 changed files with 34 additions and 31 deletions

41
main.py
View File

@ -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():

View File

@ -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:])

View File

@ -0,0 +1,2 @@
from .mcp9808 import MCP9808
from .watersensor import WaterSensor

View File

@ -13,6 +13,3 @@ class WaterSensor:
def isWaterPresent(self):
return self.reading > self.threshold
def waterStatus(self):
return 'Present' if self.isWaterPresent() else 'Absent'

View File

@ -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;
}
</style>
</head>
<body>
<h2>Pico W</h2>
<p>{{datetime}}</p>
<p>Led is <span class="{{ledClass}}">{{ledStatus}}</span></p>
<p>{{temperature}} &#176;C</p>
<p>Water is <span class="{{waterClass}}">{{waterStatus}}</span></p>
</body>
</html>