From 3fb2cf38e513dda96488dd86c1d040a72c9ae10e Mon Sep 17 00:00:00 2001
From: jaseg <git@jaseg.net>
Date: Thu, 23 Jan 2020 11:49:44 +0100
Subject: Add some more monitoring

---
 main.py.example | 61 ++++++++++++++++++++++++++++++++++++++++++---------------
 1 file changed, 45 insertions(+), 16 deletions(-)

(limited to 'main.py.example')

diff --git a/main.py.example b/main.py.example
index 8bffbbe..b468d5d 100644
--- a/main.py.example
+++ b/main.py.example
@@ -1,5 +1,5 @@
 
-from machine import Pin, ADC, Timer
+from machine import Pin, ADC, Timer, WDT, reset
 import network
 import time
 import ujson
@@ -102,30 +102,59 @@ def usign(secret, scope, payload=None, seq=None):
     return ujson.dumps({'payload': payload, 'auth': auth})
 
 def notify(scope, **kwargs):
+    wifi_connect()
     data = usign(NOTIFICATION_SECRET, scope, kwargs)
     print(unix_time(), 'Notifying', NOTIFICATION_URL)
     urequests.post(NOTIFICATION_URL, data=data, headers={'Content-Type': 'application/json'})
 
+
+def format_exc(limit=None, chain=True):
+    return "".join(repr(i) for i in sys.exc_info())
+
 def loop():
     global rms, capture
     last_notification, last_heartbeat = 0, 0
+    n_exc, last_exc_clear = 0, unix_time()
+    last_ntp_sync = unix_time()
+
+    wdt = WDT(timeout=60000)
+
     while True:
-        now = unix_time()
-        if (now - last_notification) > NOTIFICATION_COOLDOWN and rms > RMS_THRESHOLD:
+        try:
+            now = unix_time()
+            if (now - last_notification) > NOTIFICATION_COOLDOWN and rms > RMS_THRESHOLD:
+                    old_capture = capture
+                    rms = 0
+                    while rms == 0:
+                        time.sleep(0.1)
+                    rms = 0
+                    notify('default', rms=rms, capture=[old_capture, capture])
+                    last_notification = now
+
+            if (now - last_heartbeat) > HEARTBEAT_INTERVAL:
+                notify('heartbeat')
+                last_heartbeat = now
+
+            if (now - last_ntp_sync) > 3600 * 24:
                 wifi_connect()
-                old_capture = capture
-                rms = 0
-                while rms == 0:
-                    time.sleep(0.1)
-                rms = 0
-                notify('default', rms=rms, capture=[old_capture, capture])
-                last_notification = now
-
-        if (now - last_heartbeat) > HEARTBEAT_INTERVAL:
-            notify('heartbeat')
-            last_heartbeat = now
-
-        time.sleep(0.1)
+                ntptime.settime()
+                last_ntp_sync = now
+
+            if (now - last_exc_clear) > 300:
+                if n_exc > 0:
+                    n_exc -= 1
+                last_exc_clear = now
+
+            wdt.feed()
+
+        except:
+            notify('error', e=format_exc())
+            n_exc += 1
+            if n_exc >= 5:
+                reset()
+
+        finally:
+            time.sleep(0.1)
 
 wifi_connect()
 ntptime.settime()
-- 
cgit