Aquifer [main]

Add sensors and fix logging

=> 69810b8c7cfcb71fc5794c398de168639d9a2810

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 getTemperatureTextValue(self):
+        return round(self.mcp.get_temp(), 2)
 
-    def ledTextValue(self):
-        return 'ON' if self.led.value() else 'OFF'
+    def waterTextValue(self):
+        return 'Present' if self.waterSensor.isWaterPresent() else 'Absent'
 
-    def ledCssClass(self):
-        return 'led-on' if self.led.value() else 'led-off'
+    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}}

   
Proxy Information
Original URL
gemini://git.cifelli.xyz/aquifer/main/cdiff/69810b8c7cfcb71fc5794c398de168639d9a2810
Status Code
Success (20)
Meta
text/gemini; charset=utf-8
Capsule Response Time
494.376465 milliseconds
Gemini-to-HTML Time
0.322084 milliseconds

This content has been proxied by September (3851b).