-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhousekeeping.py
66 lines (47 loc) · 1.24 KB
/
housekeeping.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import gc
import utime as time
import logging
from machine import Pin
import uasyncio as asyncio
from wifi_manager import WifiManager as wm
# from micropython import mem_info
import ntptime
# correct for Amsterdam Summertime
ntptime.NTP_DELTA -= 7200
log = logging.getLogger('housekeeping')
LED_PIN = 5
led = None
next_time_set = 0
def adjust_time():
if wm.wlan().active():
try:
ntptime.settime()
log.info('Time set to %s', str(time.localtime()))
return True
except Exception:
pass
log.error('Cannot set time...')
return False
def check_time():
global next_time_set
if time.time() > next_time_set:
if adjust_time():
next_time_set = time.time() + 24 * 3600
else:
next_time_set = time.time() + 120
def setup():
global led, next_time_set
led = Pin(LED_PIN, Pin.OUT)
led.value(1)
check_time()
async def janitor():
n = 0
while True:
led.value(0 if led.value() else 1) # blink led
n += 1
if n > 30:
gc.collect()
n = 0
# mem_info()
check_time()
await asyncio.sleep(1)