Skip to content

Commit 71bea5b

Browse files
authored
Merge pull request #5 from jedie/develop
Develop
2 parents ff0ad07 + 44ba0b2 commit 71bea5b

File tree

2 files changed

+18
-33
lines changed

2 files changed

+18
-33
lines changed

src/webswitch.html

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,16 @@ <h1>Sonoff S20 - ESP Web Server</h1>
1313
<a href="/?power=off"><button class="button button2">OFF</button></a>
1414
</p>
1515
<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>
16+
<a href="/?reset"><button class="button">Reset Device</button></a>
1817
</p>
1918
<p><small>
2019
{wifi}<br>
2120
{ntp_sync}<br>
22-
{watchdog}
21+
{watchdog}<br>
22+
{rtc_memory}
2323
</small></p>
2424
<p><small>
25-
{alloc}bytes of heap RAM that are allocated<br>
26-
{free}bytes of available heap RAM<br>
25+
total: {total:.2f} KB, used: {alloc:.2f} KB, free: {free:.2f} KB<br>
2726
Server time in UTC: {utc}
2827
</small></p>
2928
</body>

src/webswitch.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66
import uasyncio as asyncio
77
import uos as os
88
import utime as time
9-
from leds import power_led
9+
from leds import power_led, relay
1010
from ntp import ntp_sync
1111
from watchdog import watchdog
1212
from wifi import wifi
13-
from leds import power_led, relay
1413

1514
rtc = machine.RTC()
1615
button_pin = machine.Pin(0, machine.Pin.IN)
@@ -21,6 +20,9 @@ def send_web_page(writer, message=''):
2120
yield from writer.awrite('Content-type: text/html; charset=utf-8\r\n')
2221
yield from writer.awrite('Connection: close\r\n\r\n')
2322

23+
alloc = gc.mem_alloc() / 1024
24+
free = gc.mem_free() / 1024
25+
2426
with open('webswitch.html', 'r') as f:
2527
yield from writer.awrite(f.read().format(
2628
state=relay.state,
@@ -29,10 +31,12 @@ def send_web_page(writer, message=''):
2931
wifi=wifi,
3032
ntp_sync=ntp_sync,
3133
watchdog=watchdog,
34+
rtc_memory=machine.RTC().memory(),
3235

3336
utc=rtc.datetime(),
34-
alloc=gc.mem_alloc(),
35-
free=gc.mem_free(),
37+
total=alloc + free,
38+
alloc=alloc,
39+
free=free,
3640
))
3741
gc.collect()
3842

@@ -62,7 +66,7 @@ async def request_handler(reader, writer):
6266

6367
not_found = True
6468
soft_reset = False
65-
hard_reset = False
69+
reset = False
6670

6771
if method == 'GET':
6872
if url == '/':
@@ -89,27 +93,15 @@ async def request_handler(reader, writer):
8993
yield from send_web_page(writer, message='power off')
9094
not_found = False
9195

92-
elif url == '/?soft_reset':
96+
elif url == '/?reset':
9397
relay.off()
9498
yield from send_web_page(
9599
writer,
96100
message=(
97-
'Soft reset device...'
101+
'Reset device...'
98102
' Restart WebServer by pressing the Button on your device!'
99103
))
100-
print('Soft reset device...')
101-
soft_reset = True
102-
not_found = False
103-
104-
elif url == '/?hard_reset':
105-
relay.off()
106-
yield from send_web_page(
107-
writer,
108-
message=(
109-
'Hard reset device...'
110-
' Restart WebServer by pressing the Button on your device!'
111-
))
112-
hard_reset = True
104+
reset = True
113105
not_found = False
114106

115107
if not_found:
@@ -119,19 +111,13 @@ async def request_handler(reader, writer):
119111
yield from writer.aclose()
120112
gc.collect()
121113

122-
if hard_reset:
114+
if reset:
123115
print('Hard reset device wait with flash LED...')
124116
power_led.flash(sleep=0.1, count=20)
125117
print('Hard reset device...')
126118
machine.reset()
127119
sys.exit()
128120

129-
if soft_reset:
130-
print('Soft reset device wait with flash LED...')
131-
power_led.flash(sleep=0.1, count=20)
132-
print('Soft reset device...')
133-
sys.exit()
134-
135121
watchdog.feed()
136122
power_led.on()
137123

@@ -144,7 +130,7 @@ def main():
144130
print('Wait for WiFi connection %s sec.' % s)
145131
time.sleep(s)
146132
s += 5
147-
133+
148134
print('Start webserver on %s...' % wifi.station.ifconfig()[0])
149135
loop = asyncio.get_event_loop()
150136

0 commit comments

Comments
 (0)