Skip to content

Commit 992eb7f

Browse files
committed
led 2 example working
1 parent b4057b2 commit 992eb7f

File tree

3 files changed

+44
-23
lines changed

3 files changed

+44
-23
lines changed

gurgleapps_webserver.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ def match_route(self, path_components):
160160
match = True
161161
params = []
162162
for idx, pattern_component in enumerate(route_pattern):
163-
print("pattern_component: "+pattern_component +
164-
" path_component: "+path_components[idx])
165163
if pattern_component.startswith('<') and pattern_component.endswith('>'):
166164
param_value = path_components[idx]
167165
params.append(param_value)

main.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
from machine import Pin
66
import ujson as json
77

8-
delay = 0.5
8+
blink_off_time = 0.5
9+
blink_on_time = 0.5
10+
911
status = True
1012
led = Pin("LED", Pin.OUT)
1113

@@ -21,14 +23,22 @@ async def say_hello(request, response, name):
2123

2224
async def send_status(request, response):
2325
# send boolean status and number frequency
24-
response_string = json.dumps({"status": status, "delay": delay})
26+
response_string = json.dumps({"status": status, "delay": (blink_off_time + blink_on_time) *0.5, "blink_on_time": blink_on_time, "blink_off_time": blink_off_time})
2527
await response.send_json(response_string, 200)
2628

29+
async def set_blink_pattern(request, response, on, off):
30+
print("on: " + on)
31+
print("off: " + off)
32+
global blink_off_time, blink_on_time
33+
blink_off_time = float(off)
34+
blink_on_time = float(on)
35+
await send_status(request, response)
2736

2837
async def set_delay(request, response, new_delay):
2938
print("new delay: " + new_delay)
30-
global delay
31-
delay = float(new_delay)
39+
global blink_off_time, blink_on_time
40+
blink_off_time = float(new_delay)
41+
blink_on_time = float(new_delay)
3242
await send_status(request, response)
3343

3444
async def stop_flashing(request, response):
@@ -45,20 +55,23 @@ async def main():
4555
await server.start_server()
4656

4757
async def background_task():
48-
global delay, status
4958
while True:
50-
await asyncio.sleep(delay)
5159
if status:
52-
led.toggle()
60+
led.on()
61+
await asyncio.sleep(blink_on_time)
62+
led.off()
63+
await asyncio.sleep(blink_off_time)
5364
else:
5465
led.off()
66+
await asyncio.sleep(0.2)
5567

5668

5769
async def run():
5870
await asyncio.gather(main(), background_task())
5971

6072
server = GurgleAppsWebserver(config.WIFI_SSID, config.WIFI_PASSWORD, port=80, timeout=20, doc_root="/www", log_level=2)
6173
server.add_function_route("/set-delay/<delay>", set_delay)
74+
server.add_function_route("/set-blink-pattern/<on_time>/<off_time>", set_blink_pattern)
6275
server.add_function_route("/stop", stop_flashing)
6376
server.add_function_route("/start", start_flashing)
6477
server.add_function_route("/status", send_status)

www/led2.html

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@
6060
</div>
6161
</div>
6262
<div class="mb-4">
63-
<span id="status" class="text-gray-700 font-semibold">Status: Not Flashing</span>
63+
<div id="status" class="text-gray-700 font-semibold">Status:</div>
64+
<div id="status2" class="text-gray-700 font-semibold">Status2:</div>
6465
</div>
6566
<div class="flex justify-between">
6667
<button id="startBtn" class="bg-blue-600 text-white font-semibold py-2 px-4 rounded focus:outline-none hover:bg-blue-700">Start</button>
@@ -70,27 +71,38 @@
7071
</div>
7172
<script>
7273
const statusElement = document.getElementById('status');
74+
const statusElement2 = document.getElementById('status2');
7375
const startBtn = document.getElementById('startBtn');
7476
const stopBtn = document.getElementById('stopBtn');
75-
const delayInput = document.getElementById('delay');
77+
const onTimeInput = document.getElementById('onTime');
78+
const offTimeInput = document.getElementById('offTime');
7679

7780
(async () => {
78-
const response = await fetch('/status');
79-
updateStatus(response);
81+
const response = await fetch('/status')
82+
updateStatus(response)
8083
})();
8184

8285
async function updateStatus(response) {
8386
if (response.ok) {
84-
const json = await response.json();
87+
const json = await response.json()
8588
console.log(json)
86-
delayInput.value = json.delay;
87-
const statusText = json.status ? 'Flashing' : 'Not Flashing';
88-
statusElement.textContent = `Status: ${statusText} | Delay: ${json.delay} sec`;
89+
onTimeInput.value = json.blink_on_time
90+
offTimeInput.value = json.blink_off_time
91+
const statusText = json.status ? 'Flashing' : 'Not Flashing'
92+
statusElement.textContent = `Status: ${statusText}`
93+
statusElement2.textContent = `On Time: ${json.blink_on_time} sec | Off Time: ${json.blink_off_time} sec`
8994
} else {
90-
statusElement.textContent = 'Error: ' + response.status;
95+
statusElement.textContent = 'Error: ' + response.status
9196
}
9297
}
9398

99+
async function sendNewBlinkTimes() {
100+
const onTime = onTimeInput.value;
101+
const offTime = offTimeInput.value;
102+
const response = await fetch(`/set-blink-pattern/${onTime}/${offTime}`);
103+
updateStatus(response);
104+
}
105+
94106
startBtn.addEventListener('click', async () => {
95107
const response = await fetch('/start');
96108
updateStatus(response);
@@ -101,11 +113,9 @@
101113
updateStatus(response);
102114
});
103115

104-
delayInput.addEventListener('change', async () => {
105-
const delay = delayInput.value;
106-
const response = await fetch(`/set-delay/${delay}`);
107-
updateStatus(response);
108-
});
116+
onTimeInput.addEventListener('change', sendNewBlinkTimes)
117+
offTimeInput.addEventListener('change', sendNewBlinkTimes)
118+
109119
</script>
110120
</body>
111121
</html>

0 commit comments

Comments
 (0)