Send water alerts via ntfy

This commit is contained in:
Mike Cifelli 2023-02-09 12:05:49 -05:00
parent 97e63124d4
commit 2494b3b928
5 changed files with 54 additions and 6 deletions

18
main.py
View File

@ -4,6 +4,7 @@ import time
from machine import I2C from machine import I2C
from net import AdafruitIO from net import AdafruitIO
from net import logging from net import logging
from net import Ntfy
from net import ntp from net import ntp
from net import Server from net import Server
from net import templates from net import templates
@ -19,10 +20,12 @@ class SensorServer(Server):
self.waterSensor = WaterSensor(0) self.waterSensor = WaterSensor(0)
self.mcp = MCP9808(I2C(0)) self.mcp = MCP9808(I2C(0))
self.aio = AdafruitIO() self.aio = AdafruitIO()
self.ntfy = Ntfy()
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
self.ntp_ticks = time.ticks_ms() self.ntp_ticks = time.ticks_ms()
self.aio_ticks = time.ticks_ms() self.aio_ticks = time.ticks_ms()
self.isWaterPresent = False
ntp.sync() ntp.sync()
@ -38,6 +41,15 @@ class SensorServer(Server):
self.aio_ticks = ticks self.aio_ticks = ticks
self.aio.upload(self.getReading()) 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): def handlePath(self, path):
if path == 'index.json': if path == 'index.json':
return self.getJsonData() return self.getJsonData()
@ -68,7 +80,7 @@ class SensorServer(Server):
def getState(self): def getState(self):
return { return {
'is-water-present': self.waterSensor.isWaterPresent(), 'is-water-present': self.isWaterPresent,
'temperature': self.mcp.get_temp() 'temperature': self.mcp.get_temp()
} }
@ -76,10 +88,10 @@ class SensorServer(Server):
return round(self.mcp.get_temp(), 2) return round(self.mcp.get_temp(), 2)
def waterTextValue(self): def waterTextValue(self):
return 'Present' if self.waterSensor.isWaterPresent() else 'Absent' return 'Present' if self.isWaterPresent else 'Absent'
def waterCssClass(self): def waterCssClass(self):
return 'water-present' if self.waterSensor.isWaterPresent() else 'water-absent' return 'water-present' if self.isWaterPresent else 'water-absent'
def main(): def main():

View File

@ -1,2 +1,3 @@
from .adafruit_io import AdafruitIO from .adafruit_io import AdafruitIO
from .ntfy import Ntfy
from .server import Server from .server import Server

View File

@ -7,7 +7,7 @@ from .config import secrets
class AdafruitIO: class AdafruitIO:
def __init__(self): def __init__(self):
self.nickname = config['adafruit_io_nickname'] self.hostname = config['hostname']
self.headers = { self.headers = {
'x-aio-key': secrets['adafruit_io_key'], 'x-aio-key': secrets['adafruit_io_key'],
@ -45,7 +45,7 @@ class AdafruitIO:
for key, value in reading['readings'].items(): for key, value in reading['readings'].items():
payload['feeds'].append({ payload['feeds'].append({
'key': f'{self.nickname}-{key}', 'key': f'{self.hostname}-{key}',
'value': str(value) 'value': str(value)
}) })

View File

@ -1,5 +1,5 @@
config = { config = {
'adafruit_io_nickname': '', 'hostname': '',
'adafruit_io_group': '' 'adafruit_io_group': ''
} }

35
net/ntfy.py Normal file
View File

@ -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}')