Skip to content

Commit 53ac822

Browse files
authored
Merge pull request #2 from jedie/develop
merge Develop
2 parents cad469e + a82a76e commit 53ac822

File tree

9 files changed

+185
-221
lines changed

9 files changed

+185
-221
lines changed

src/constants.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
WATCHDOG_CHECK_PERIOD = const(30 * 1000) # 30 sec
2-
WATCHDOG_TIMEOUT = const(25 * 1000) # 25 sec
1+
WATCHDOG_CHECK_PERIOD = const(50 * 1000) # 50 sec
2+
WATCHDOG_TIMEOUT = const(40 * 1000) # 40 sec
33

4-
WIFI_TIMER = const(20 * 1000) # 20 sec
5-
NTP_TIMER = const(5 * 60 * 1000) # 5 min
4+
WIFI_TIMER = const(30 * 1000) # 30 sec
5+
NTP_TIMER = const(15 * 60 * 1000) # 15 min
66

77
assert WATCHDOG_TIMEOUT > WIFI_TIMER
88
assert WATCHDOG_CHECK_PERIOD > WATCHDOG_TIMEOUT

src/main.py

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,62 @@
11
import gc
22

3+
import machine
4+
import micropython
35
import utime as time
46

7+
micropython.alloc_emergency_exception_buf(128)
8+
59
for no in range(3, 0, -1):
610
print('%i main.py wait...' % no)
711
time.sleep(1)
812

9-
import micropython
10-
11-
micropython.alloc_emergency_exception_buf(128)
13+
from watchdog import watchdog # noqa isort:skip
14+
from leds import power_led # noqa isort:skip
15+
from ntp import ntp_sync # noqa isort:skip
16+
from wifi import wifi # noqa isort:skip
1217

13-
from leds import power_led
14-
from ntp import ntp_sync
15-
from watchdog import watchdog
16-
from webswitch import main
17-
from wifi import wifi
1818

19+
print('watchdog:', watchdog)
1920
print('wifi:', wifi)
2021
print('ntp_sync:', ntp_sync)
2122
print('power_led:', power_led)
22-
print('watchdog:', watchdog)
23+
24+
25+
def get_debounced_value(pin):
26+
"""get debounced value from pin by waiting for 20 msec for stable value"""
27+
cur_value = pin.value()
28+
stable = 0
29+
while stable < 20:
30+
if pin.value() == cur_value:
31+
stable = stable + 1
32+
else:
33+
stable = 0
34+
cur_value = pin.value()
35+
time.sleep_ms(1)
36+
return cur_value
37+
38+
39+
def button_pressed(pin):
40+
print('button pressed...')
41+
cur_button_value = get_debounced_value(pin)
42+
if cur_button_value == 1:
43+
if relay_pin.value() == 1:
44+
print('turn off by button.')
45+
relay_pin.value(0)
46+
else:
47+
print('turn on by button.')
48+
relay_pin.value(1)
49+
50+
garbage_collection()
51+
52+
53+
button_pin = machine.Pin(0, machine.Pin.IN)
54+
button_pin.irq(button_pressed)
2355

2456

2557
print('gc.collect()')
2658
gc.collect()
2759

2860

61+
from webswitch import main # noqa isort:skip
2962
main()

src/ntp.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import utime as time
88
from watchdog import watchdog
99

10-
1110
rtc = machine.RTC()
1211

1312

@@ -57,10 +56,14 @@ def _sync(self):
5756
watchdog.feed()
5857
return
5958

60-
def __repr__(self):
61-
return '<NtpSync last refresh: %s - success count: %i - error count: %i' % (
59+
def __str__(self):
60+
return 'NtpSync last refresh: %s - success count: %i - error count: %i' % (
6261
self.last_refresh, self.success_count, self.error_count
6362
)
6463

6564

6665
ntp_sync = NtpSync()
66+
67+
68+
if __name__ == '__main__':
69+
print(ntp_sync)

src/watchdog.py

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ def feed(self):
5757
def deinit(self):
5858
self.timer.deinit()
5959

60-
def __repr__(self):
60+
def __str__(self):
6161
return (
62-
'<Watchdog timeout: %s'
62+
'Watchdog timeout: %s'
6363
' - last diff: %s'
6464
' - last refresh: %s'
6565
' - timer count: %s'
@@ -72,29 +72,5 @@ def __repr__(self):
7272
watchdog = Watchdog()
7373

7474

75-
def test(watchdog):
76-
print('test start...')
77-
watchdog.deinit() # deinit global watchdog
78-
79-
# Create new one with shorter timeout:
80-
watchdog = Watchdog(
81-
check_period=3 * 1000, # 2 sec
82-
timeout=2 * 1000, # 1 sec
83-
)
84-
print(watchdog)
85-
for _ in range(10):
86-
print('.', end='')
87-
time.sleep(0.5)
88-
watchdog.feed()
89-
print('\nfeed end\n')
90-
91-
print(watchdog)
92-
93-
while True:
94-
print(watchdog)
95-
time.sleep(0.5)
96-
97-
9875
if __name__ == '__main__':
99-
test(watchdog) # will reset the device !
100-
print('test end.') # should never happen
76+
print(watchdog)

src/watchdog_test.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
import utime as time
3+
from watchdog import Watchdog, watchdog
4+
5+
6+
def test(watchdog):
7+
print('test start...')
8+
watchdog.deinit() # deinit global watchdog
9+
10+
# Create new one with shorter timeout:
11+
watchdog = Watchdog(
12+
check_period=3 * 1000, # 2 sec
13+
timeout=2 * 1000, # 1 sec
14+
)
15+
print(watchdog)
16+
for _ in range(10):
17+
print('.', end='')
18+
time.sleep(0.5)
19+
watchdog.feed()
20+
print('\nfeed end\n')
21+
22+
print(watchdog)
23+
24+
while True:
25+
print(watchdog)
26+
time.sleep(0.5)
27+
28+
29+
if __name__ == '__main__':
30+
test(watchdog) # will reset the device !
31+
print('test end.') # should never happen

src/webswitch.css

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
html{
2+
font-family: Georgia, serif;
3+
text-align: center;
4+
font-size: 1.5em;
5+
}
6+
.button{
7+
background-color: #e7bd3b;
8+
border: none;
9+
border-radius: 4px; color: #fff;
10+
padding: 16px 40px;
11+
cursor: pointer;
12+
}
13+
.button2{
14+
background-color: #4286f4;
15+
}
16+
small {
17+
font-family: "Courier New", Courier, monospace;
18+
font-size: 0.5em;
19+
}

src/webswitch.html

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<html>
2+
<head>
3+
<title>Sonoff S20 - ESP Web Server</title>
4+
<meta name="viewport" content="width=device-width, initial-scale=1">
5+
<link rel="stylesheet" href="webswitch.css" type="text/css">
6+
</head>
7+
<body>
8+
<h1>Sonoff S20 - ESP Web Server</h1>
9+
<p>state: <strong>{state}</strong></p>
10+
<p>{message}</p>
11+
<p>
12+
<a href="/?power=on"><button class="button">ON</button></a>
13+
<a href="/?power=off"><button class="button button2">OFF</button></a>
14+
</p>
15+
<p>
16+
<a href="/?soft_reset"><button class="button">Soft reset Device</button></a>
17+
<a href="/?hard_reset"><button class="button">Hard reset Device</button></a>
18+
</p>
19+
<p><small>
20+
{wifi}<br>
21+
{ntp_sync}<br>
22+
{watchdog}
23+
</small></p>
24+
<p><small>
25+
{alloc}bytes of heap RAM that are allocated<br>
26+
{free}bytes of available heap RAM<br>
27+
Server time in UTC: {utc}
28+
</small></p>
29+
</body>
30+
</html>

0 commit comments

Comments
 (0)