From e69a2d9912f2c2c41aee8f472c6de08c92eadf95 Mon Sep 17 00:00:00 2001 From: Tilman Date: Mon, 17 May 2021 08:49:56 +0200 Subject: [PATCH] =?UTF-8?q?Rasenm=C3=A4her-Flucht=20und=20Pushover-Nachric?= =?UTF-8?q?hten?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- garage/garage.py | 49 +++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-) diff --git a/garage/garage.py b/garage/garage.py index 3833015..641f9e0 100644 --- a/garage/garage.py +++ b/garage/garage.py @@ -13,7 +13,7 @@ cherrypy.config.update({ logger = logging.getLogger('Rotating Log') logger.setLevel(logging.INFO) -logHandler = handlers.RotatingFileHandler('garage.log', maxBytes=1000000, backupCount=2) +logHandler = handlers.RotatingFileHandler('/home/pi/garage.log', maxBytes=1000000, backupCount=2) logHandler.setLevel(logging.INFO) logHandler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s - %(message)s', '%Y-%m-%d %H:%M:%S')) logger.addHandler(logHandler) @@ -39,38 +39,45 @@ GPIO.setup(PIN_DOOR_OPEN, GPIO.IN, GPIO.PUD_UP) GPIO.setup(PIN_DOOR_CLOSED, GPIO.IN, GPIO.PUD_UP) GPIO.setup(PIN_SENSOR_GATE, GPIO.IN, GPIO.PUD_UP) +def pushover(message): + logger.info(message) + response = requests.post("https://api.pushover.net/1/messages.json", data = { + "token": "a9k1uyzypuaz1yszqqdzmp17s6r5q2", + "user": "ukja84pd2n2cmxvdrnfwdrf8eo1wc5", + "message": message + }) + logger.info(response) # move robomower into safe zone def mower_safety(): try: - #response = requests.get("http://192.168.178.54/json?user=lewe&pass=nFdcX3sMD115WAap&cmd=status") - jsonstr = '{"name": "Rasi", "id": "C7697E", "status": {"status": 17, "distance": 0, "stopped": false, "duration": 4290, "mode": 2, "battery": 100, "hours": 90}, "timer": {"status": 0}, "wlan": {"signal": -86}, "health": {"temperature": 23, "humidity": 25}, "clock": {"date": "2021-05-13", "time": "14:23:27", "unix": 1620915807}, "successful": true}' + response = requests.get("http://192.168.178.54/json?user=lewe&pass=nFdcX3sMD115WAap&cmd=status") + #response = '{"name": "Rasi", "id": "C7697E", "status": {"status": 17, "distance": 0, "stopped": false, "duration": 4290, "mode": 2, "battery": 100, "hours": 90}, "timer": {"status": 0}, "wlan": {"signal": -86}, "health": {"temperature": 23, "humidity": 25}, "clock": {"date": "2021-05-13", "time": "14:23:27", "unix": 1620915807}, "successful": true}' except requests.exceptions.RequestException as e: - #raise SystemExit(e) - logger.warning("couldn't reach mower") + #logger.warning("couldn't reach mower") + pushover("couldn't reach mower (retrieving status)") return None - #status = json.loads(response.text) - status = json.loads(jsonstr) - logger.info(status["status"]["status"]) + status = json.loads(response.text) + #status = json.loads(jsonstr) if (status["status"]["status"] in [2, 3, 5]): - logger.info("mower, run for your life") + pushover("mower, run for your life") else: - logger.info("lock mower in garage") + pushover("lock mower in garage") # robomower back to auto mode after gate has been closed def mower_auto(): - logger.info("set mower back to auto mode") + pushover("set mower back to auto mode") try: response = requests.get("http://192.168.178.54/json?cmd=mode&mode=auto&user=lewe&pass=nFdcX3sMD115WAap&cmd=status") except requests.exceptions.RequestException as e: - logger.warning("couldn't reach mower") + #logger.warning("couldn't reach mower") + pushover("couldn't reach mower (setting auto mode)") return None - def garage_button(channel): time.sleep(0.01) # el cheapo relay module creates lots of interference, so we check again after a waiting period (see https://electronics.stackexchange.com/a/361793/253533) - if channel == 0 or GPIO.input(PIN_GARAGE_BUTTON) == False: + if channel == 0 or GPIO.input(PIN_GARAGE_BUTTON) == GPIO.LOW: logger.info("trigger motor") GPIO.output(PIN_GARAGE, GPIO.LOW) GPIO.output(PIN_LED, GPIO.HIGH) @@ -82,19 +89,19 @@ def garage_button(channel): def gate_button(channel): time.sleep(0.01) - if channel == 0 or GPIO.input(PIN_GATE_BUTTON) == False: - logger.info("trigger remote") + if channel == 0 or GPIO.input(PIN_GATE_BUTTON) == GPIO.LOW: + if GPIO.input(PIN_SENSOR_GATE) == 0: + mower_safety() + else: + mower_auto() + logger.info("trigger gate") GPIO.output(PIN_GATE, GPIO.HIGH) GPIO.output(PIN_LED, GPIO.HIGH) time.sleep(0.4) GPIO.output(PIN_GATE, GPIO.LOW) GPIO.output(PIN_LED, GPIO.LOW) - if GPIO.input(PIN_SENSOR_GATE) == 0: - mower_auto() - else: - mower_safety() else: - logger.info("flare (remote button)") + logger.info("flare (gate button)") GPIO.add_event_detect(PIN_GARAGE_BUTTON, GPIO.FALLING, callback=garage_button, bouncetime=250) GPIO.add_event_detect(PIN_GATE_BUTTON, GPIO.FALLING, callback=gate_button, bouncetime=250)