Skip to content

Commit 5be5b19

Browse files
authored
Merge pull request #6 from gurgleapps/dev
Dev
2 parents 98d46a9 + faf4521 commit 5be5b19

File tree

8 files changed

+107
-20
lines changed

8 files changed

+107
-20
lines changed

.github/workflows/mpy_compile.yml

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
name: Compile to MPY
2+
3+
on:
4+
push:
5+
branches:
6+
- dev
7+
# paths:
8+
# - '**.py'
9+
10+
jobs:
11+
compile:
12+
runs-on: ubuntu-latest
13+
14+
steps:
15+
- name: Checkout repository
16+
uses: actions/checkout@v2
17+
with:
18+
token: ${{ secrets.GITHUB_TOKEN }}
19+
20+
- name: Set up Python
21+
uses: actions/setup-python@v2
22+
with:
23+
python-version: 3.x
24+
25+
- name: Install Micropython
26+
run: |
27+
sudo apt-get update
28+
sudo apt-get install -y build-essential libreadline-dev libffi-dev git pkg-config
29+
git clone https://github.com/micropython/micropython.git
30+
cd micropython/mpy-cross
31+
make
32+
33+
- name: list to see if it's there
34+
run: |
35+
ls -al ./micropython/mpy-cross/build
36+
37+
- name: Compile .py files to .mpy
38+
run: |
39+
rm -rf mpy
40+
mkdir mpy
41+
find . -maxdepth 1 -name "*.py" ! -name "main.py" ! -name "config.py" -exec sh -c './micropython/mpy-cross/build/mpy-cross {} -o mpy/$(basename -s .py {}).mpy' \;
42+
43+
44+
45+
- name: Remove micropython folder
46+
run: |
47+
rm -rf micropython
48+
49+
- name: Commit .mpy files
50+
run: |
51+
git config --global user.name "GitHub Actions Bot"
52+
git config --global user.email "nereply@gurgleapps.com"
53+
git add -A
54+
git commit -m "Auto-compiled .mpy files" || echo "No changes to commit"
55+
git remote set-url origin https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
56+
git push || echo "No changes to push"

README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ The latest features include improved memory usage, support for additional microc
2929
5. Run `main.py` and look for the IP address of your web server
3030
6. Point your browsers to http://<YOUR_IP>
3131

32+
## Using .py or .mpy files
33+
34+
This repository provides both `.py` and `.mpy` files for most modules. While the `.py` files are standard Python files, the `.mpy` files are precompiled MicroPython bytecode files. Using `.mpy` files can result in reduced memory usage and faster execution times on your microcontroller.
35+
36+
### Recommendations
37+
- For most microcontrollers, you can choose between `.py` and `.mpy` files based on your preference.
38+
- For ESP8266, due to its limited memory, it is recommended to use `.mpy` files for modules other than `main.py` and `config.py`.
39+
40+
To choose between `.py` and `.mpy` files, follow these steps:
41+
42+
1. Copy `main.py` and `config.py` from the root directory to your microcontroller.
43+
2. Choose between the `.py` and `.mpy` files for the remaining modules:
44+
- If you prefer to use the standard Python files, copy the corresponding files from the root directory to your microcontroller.
45+
- If you prefer to use the precompiled MicroPython bytecode files, copy the corresponding files from the `mpy` folder to your microcontroller.
46+
47+
Remember to customize `config.py` with your Wi-Fi details and other settings before running the code.
48+
3249

3350
## Documentation
3451

gurgleapps_webserver.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
5858
print('connected')
5959
status = self.wlan.ifconfig()
6060
print('ip = ' + status[0])
61-
self.serving = True
61+
self.server_running = False
6262
self.ip_address = status[0]
6363
print('point your browser to http://', status[0])
6464
# asyncio.new_event_loop()
@@ -73,9 +73,22 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
7373

7474
async def start_server(self):
7575
print("start_server")
76-
server_task = asyncio.create_task(asyncio.start_server(
77-
self.serve_request, "0.0.0.0", self.port))
78-
await server_task
76+
self.server = await asyncio.start_server(
77+
self.serve_request, "0.0.0.0", self.port
78+
)
79+
80+
async def stop_server(self):
81+
if self.server is not None:
82+
self.server.close()
83+
await self.server.wait_closed()
84+
self.server = None
85+
print("stop_server")
86+
87+
async def start_server_with_background_task(self, background_task):
88+
async def main():
89+
await asyncio.gather(self.start_server(), background_task())
90+
await main()
91+
7992

8093
# async def start_server(self):
8194
# print("start_server")

main.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
Date: 2021-04-01
66
Description: Demonstrates how to use the GurgleApps Webserver
77
"""
8-
import config
98
from gurgleapps_webserver import GurgleAppsWebserver
9+
import config
1010
import utime as time
1111
import uasyncio as asyncio
1212
from machine import Pin
@@ -30,6 +30,7 @@
3030
blink_on_time = 0.5
3131

3232
status = True
33+
shutdown = False
3334

3435
async def example_func(request, response, param1, param2):
3536
print("example_func")
@@ -71,11 +72,18 @@ async def start_flashing(request, response):
7172
status = True
7273
await send_status(request, response)
7374

74-
async def main():
75-
await server.start_server()
75+
async def stop_server(request, response):
76+
global shutdown
77+
await response.send_html("Server stopping")
78+
await server.stop_server()
79+
shutdown = True
80+
7681

77-
async def background_task():
78-
while True:
82+
async def main():
83+
global shutdown
84+
if config.BLINK_IP:
85+
await(server.blink_ip(led_pin = led, last_only = config.BLINK_LAST_ONLY))
86+
while not shutdown:
7987
if status:
8088
led.on()
8189
await asyncio.sleep(blink_on_time)
@@ -84,15 +92,7 @@ async def background_task():
8492
else:
8593
led.off()
8694
await asyncio.sleep(0.2)
87-
88-
89-
async def run():
90-
#await(server.blink_ip(led_pin=led))
91-
if config.BLINK_IP:
92-
await(server.blink_ip(led_pin = led, last_only = config.BLINK_LAST_ONLY))
93-
94-
await asyncio.gather(main(), background_task())
95-
95+
9696
server = GurgleAppsWebserver(config.WIFI_SSID, config.WIFI_PASSWORD, port=80, timeout=20, doc_root="/www", log_level=2)
9797
server.add_function_route("/set-delay/<delay>", set_delay)
9898
server.add_function_route(
@@ -104,6 +104,7 @@ async def run():
104104
server.add_function_route("/status", send_status)
105105
server.add_function_route("/example/func/<param1>/<param2>", example_func)
106106
server.add_function_route("/hello/<name>", say_hello)
107+
server.add_function_route("/stop-server", stop_server)
107108

108-
109-
asyncio.run(run())
109+
asyncio.run(server.start_server_with_background_task(main))
110+
print('DONE')

mpy/board.mpy

446 Bytes
Binary file not shown.

mpy/gurgleapps_webserver.mpy

7.73 KB
Binary file not shown.

mpy/request.mpy

104 Bytes
Binary file not shown.

mpy/response.mpy

900 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)