Skip to content

Commit

Permalink
Merge pull request #6 from gurgleapps/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
mirkin authored Apr 15, 2023
2 parents 98d46a9 + faf4521 commit 5be5b19
Show file tree
Hide file tree
Showing 8 changed files with 107 additions and 20 deletions.
56 changes: 56 additions & 0 deletions .github/workflows/mpy_compile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: Compile to MPY

on:
push:
branches:
- dev
# paths:
# - '**.py'

jobs:
compile:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
token: ${{ secrets.GITHUB_TOKEN }}

- name: Set up Python
uses: actions/setup-python@v2
with:
python-version: 3.x

- name: Install Micropython
run: |
sudo apt-get update
sudo apt-get install -y build-essential libreadline-dev libffi-dev git pkg-config
git clone https://github.com/micropython/micropython.git
cd micropython/mpy-cross
make
- name: list to see if it's there
run: |
ls -al ./micropython/mpy-cross/build
- name: Compile .py files to .mpy
run: |
rm -rf mpy
mkdir mpy
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' \;
- name: Remove micropython folder
run: |
rm -rf micropython
- name: Commit .mpy files
run: |
git config --global user.name "GitHub Actions Bot"
git config --global user.email "[email protected]"
git add -A
git commit -m "Auto-compiled .mpy files" || echo "No changes to commit"
git remote set-url origin https://${{ secrets.GITHUB_TOKEN }}@github.com/${{ github.repository }}
git push || echo "No changes to push"
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,23 @@ The latest features include improved memory usage, support for additional microc
5. Run `main.py` and look for the IP address of your web server
6. Point your browsers to http://<YOUR_IP>

## Using .py or .mpy files

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.

### Recommendations
- For most microcontrollers, you can choose between `.py` and `.mpy` files based on your preference.
- For ESP8266, due to its limited memory, it is recommended to use `.mpy` files for modules other than `main.py` and `config.py`.

To choose between `.py` and `.mpy` files, follow these steps:

1. Copy `main.py` and `config.py` from the root directory to your microcontroller.
2. Choose between the `.py` and `.mpy` files for the remaining modules:
- If you prefer to use the standard Python files, copy the corresponding files from the root directory to your microcontroller.
- If you prefer to use the precompiled MicroPython bytecode files, copy the corresponding files from the `mpy` folder to your microcontroller.

Remember to customize `config.py` with your Wi-Fi details and other settings before running the code.


## Documentation

Expand Down
21 changes: 17 additions & 4 deletions gurgleapps_webserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www
print('connected')
status = self.wlan.ifconfig()
print('ip = ' + status[0])
self.serving = True
self.server_running = False
self.ip_address = status[0]
print('point your browser to http://', status[0])
# asyncio.new_event_loop()
Expand All @@ -73,9 +73,22 @@ def __init__(self, wifi_ssid, wifi_password, port=80, timeout=20, doc_root="/www

async def start_server(self):
print("start_server")
server_task = asyncio.create_task(asyncio.start_server(
self.serve_request, "0.0.0.0", self.port))
await server_task
self.server = await asyncio.start_server(
self.serve_request, "0.0.0.0", self.port
)

async def stop_server(self):
if self.server is not None:
self.server.close()
await self.server.wait_closed()
self.server = None
print("stop_server")

async def start_server_with_background_task(self, background_task):
async def main():
await asyncio.gather(self.start_server(), background_task())
await main()


# async def start_server(self):
# print("start_server")
Expand Down
33 changes: 17 additions & 16 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
Date: 2021-04-01
Description: Demonstrates how to use the GurgleApps Webserver
"""
import config
from gurgleapps_webserver import GurgleAppsWebserver
import config
import utime as time
import uasyncio as asyncio
from machine import Pin
Expand All @@ -30,6 +30,7 @@
blink_on_time = 0.5

status = True
shutdown = False

async def example_func(request, response, param1, param2):
print("example_func")
Expand Down Expand Up @@ -71,11 +72,18 @@ async def start_flashing(request, response):
status = True
await send_status(request, response)

async def main():
await server.start_server()
async def stop_server(request, response):
global shutdown
await response.send_html("Server stopping")
await server.stop_server()
shutdown = True


async def background_task():
while True:
async def main():
global shutdown
if config.BLINK_IP:
await(server.blink_ip(led_pin = led, last_only = config.BLINK_LAST_ONLY))
while not shutdown:
if status:
led.on()
await asyncio.sleep(blink_on_time)
Expand All @@ -84,15 +92,7 @@ async def background_task():
else:
led.off()
await asyncio.sleep(0.2)


async def run():
#await(server.blink_ip(led_pin=led))
if config.BLINK_IP:
await(server.blink_ip(led_pin = led, last_only = config.BLINK_LAST_ONLY))

await asyncio.gather(main(), background_task())


server = GurgleAppsWebserver(config.WIFI_SSID, config.WIFI_PASSWORD, port=80, timeout=20, doc_root="/www", log_level=2)
server.add_function_route("/set-delay/<delay>", set_delay)
server.add_function_route(
Expand All @@ -104,6 +104,7 @@ async def run():
server.add_function_route("/status", send_status)
server.add_function_route("/example/func/<param1>/<param2>", example_func)
server.add_function_route("/hello/<name>", say_hello)
server.add_function_route("/stop-server", stop_server)


asyncio.run(run())
asyncio.run(server.start_server_with_background_task(main))
print('DONE')
Binary file added mpy/board.mpy
Binary file not shown.
Binary file added mpy/gurgleapps_webserver.mpy
Binary file not shown.
Binary file added mpy/request.mpy
Binary file not shown.
Binary file added mpy/response.mpy
Binary file not shown.

0 comments on commit 5be5b19

Please sign in to comment.