From 2494b3b9284b59a5af49e8a3ed2e48aad7e5253b Mon Sep 17 00:00:00 2001 From: Mike Cifelli <1836280-mike-cifelli@users.noreply.gitlab.com> Date: Thu, 9 Feb 2023 12:05:49 -0500 Subject: [PATCH] Send water alerts via ntfy --- main.py | 18 +++++++++++++++--- net/__init__.py | 1 + net/adafruit_io.py | 4 ++-- net/config.sample.py | 2 +- net/ntfy.py | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 54 insertions(+), 6 deletions(-) create mode 100644 net/ntfy.py diff --git a/main.py b/main.py index f1fcc9a..f53fd2b 100644 --- a/main.py +++ b/main.py @@ -4,6 +4,7 @@ import time from machine import I2C from net import AdafruitIO from net import logging +from net import Ntfy from net import ntp from net import Server from net import templates @@ -19,10 +20,12 @@ class SensorServer(Server): self.waterSensor = WaterSensor(0) self.mcp = MCP9808(I2C(0)) self.aio = AdafruitIO() + self.ntfy = Ntfy() self.ntp_interval_in_seconds = 60 * 60 self.aio_interval_in_seconds = 60 * 5 self.ntp_ticks = time.ticks_ms() self.aio_ticks = time.ticks_ms() + self.isWaterPresent = False ntp.sync() @@ -38,6 +41,15 @@ class SensorServer(Server): self.aio_ticks = ticks self.aio.upload(self.getReading()) + self.updateWater() + + def updateWater(self): + isWaterPresent = self.waterSensor.isWaterPresent() + + if isWaterPresent != self.isWaterPresent: + self.isWaterPresent = isWaterPresent + self.ntfy.message(f'Water is {self.waterTextValue()}') + def handlePath(self, path): if path == 'index.json': return self.getJsonData() @@ -68,7 +80,7 @@ class SensorServer(Server): def getState(self): return { - 'is-water-present': self.waterSensor.isWaterPresent(), + 'is-water-present': self.isWaterPresent, 'temperature': self.mcp.get_temp() } @@ -76,10 +88,10 @@ class SensorServer(Server): return round(self.mcp.get_temp(), 2) def waterTextValue(self): - return 'Present' if self.waterSensor.isWaterPresent() else 'Absent' + return 'Present' if self.isWaterPresent else 'Absent' def waterCssClass(self): - return 'water-present' if self.waterSensor.isWaterPresent() else 'water-absent' + return 'water-present' if self.isWaterPresent else 'water-absent' def main(): diff --git a/net/__init__.py b/net/__init__.py index 126c83a..a87a860 100644 --- a/net/__init__.py +++ b/net/__init__.py @@ -1,2 +1,3 @@ from .adafruit_io import AdafruitIO +from .ntfy import Ntfy from .server import Server diff --git a/net/adafruit_io.py b/net/adafruit_io.py index 3c08845..23ddff9 100644 --- a/net/adafruit_io.py +++ b/net/adafruit_io.py @@ -7,7 +7,7 @@ from .config import secrets class AdafruitIO: def __init__(self): - self.nickname = config['adafruit_io_nickname'] + self.hostname = config['hostname'] self.headers = { 'x-aio-key': secrets['adafruit_io_key'], @@ -45,7 +45,7 @@ class AdafruitIO: for key, value in reading['readings'].items(): payload['feeds'].append({ - 'key': f'{self.nickname}-{key}', + 'key': f'{self.hostname}-{key}', 'value': str(value) }) diff --git a/net/config.sample.py b/net/config.sample.py index 430bae6..965929b 100644 --- a/net/config.sample.py +++ b/net/config.sample.py @@ -1,5 +1,5 @@ config = { - 'adafruit_io_nickname': '', + 'hostname': '', 'adafruit_io_group': '' } diff --git a/net/ntfy.py b/net/ntfy.py new file mode 100644 index 0000000..6c1c628 --- /dev/null +++ b/net/ntfy.py @@ -0,0 +1,35 @@ +import urequests + +from . import logging +from .config import config +from .config import secrets + + +class Ntfy: + def __init__(self): + self.hostname = config['hostname'] + + host = secrets['ntfy_host'] + topic = secrets['ntfy_topic'] + self.url = f'https://{host}/{topic}' + + def message(self, message): + result = self.sendMessage(message) + + try: + if result.status_code != 200: + self.logError(result) + return False + + return True + finally: + result.close() + + def sendMessage(self, message): + return urequests.post(self.url, data=f"{self.hostname} - {message}") + + def logError(self, result): + status = result.status_code + reason = result.reason.decode("utf-8") + + logging.debug(f'ntfy upload issue: {status} {reason}')