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 json
import time import time
from machine import Pin from machine import I2C
from net import AdafruitIO from net import AdafruitIO
from net import logging from net import logging
from net import ntp from net import ntp
from net import Server from net import Server
from net import templates from net import templates
from net import util from net import util
from sensors import MCP9808
from sensors import WaterSensor
class LedServer(Server): class LedServer(Server):
def __init__(self): def __init__(self):
super().__init__() super().__init__()
self.led = Pin('LED', Pin.OUT) self.waterSensor = WaterSensor(0)
self.mcp = MCP9808(I2C(0))
self.aio = AdafruitIO() self.aio = AdafruitIO()
self.ntp_interval_in_seconds = 60 * 60 self.ntp_interval_in_seconds = 60 * 60
self.aio_interval_in_seconds = 60 * 5 self.aio_interval_in_seconds = 60 * 5
@ -42,12 +45,6 @@ class LedServer(Server):
def handlePath(self, path): def handlePath(self, path):
if path == 'index.json': if path == 'index.json':
return self.getJsonData() 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) return self.getPathData(path)
@ -61,22 +58,32 @@ class LedServer(Server):
return templates.render( return templates.render(
f'www/{path or self.default_path}', f'www/{path or self.default_path}',
ledStatus=self.ledTextValue(), datetime=util.datetime(),
ledClass=self.ledCssClass(), temperature=self.getTemperatureTextValue(),
datetime=util.datetime() waterStatus=self.waterTextValue(),
waterClass=self.waterCssClass()
) )
def getReading(self): def getReading(self):
return {'timestamp': util.datetimeISO8601(), 'readings': self.getState()} return {
'timestamp': util.datetimeISO8601(),
'readings': self.getState()
}
def getState(self): 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): def getTemperatureTextValue(self):
return 'ON' if self.led.value() else 'OFF' return round(self.mcp.get_temp(), 2)
def ledCssClass(self): def waterTextValue(self):
return 'led-on' if self.led.value() else 'led-off' return 'Present' if self.waterSensor.isWaterPresent() else 'Absent'
def waterCssClass(self):
return 'water-present' if self.waterSensor.isWaterPresent() else 'water-absent'
def main(): def main():

View File

@ -68,7 +68,7 @@ def truncate(file, target_size):
# try to find a line break nearby to split first chunk on # try to find a line break nearby to split first chunk on
break_position = max( break_position = max(
chunk.find(b'\n', -discard), # search forward chunk.find(b'\n', -discard), # search forward
chunk.rfind(b'\n', -discard) # search backwards chunk.rfind(b'\n', 0, -discard) # search backwards
) )
if break_position != -1: # if we found a line break.. if break_position != -1: # if we found a line break..
outfile.write(chunk[break_position + 1:]) 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): def isWaterPresent(self):
return self.reading > self.threshold return self.reading > self.threshold
def waterStatus(self):
return 'Present' if self.isWaterPresent() else 'Absent'

View File

@ -12,14 +12,6 @@
color: #ddd; color: #ddd;
} }
.led-on {
color: greenyellow;
}
.led-off {
color: lightblue;
}
.water-present { .water-present {
color: pink; color: pink;
} }
@ -28,13 +20,18 @@
color: lightblue; color: lightblue;
} }
.temperature {
font-family: monospace;
}
</style> </style>
</head> </head>
<body> <body>
<h2>Pico W</h2> <h2>Pico W</h2>
<p>{{datetime}}</p> <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> </body>
</html> </html>