Factored out webserver sample into dedicated module
This commit is contained in:
@@ -21,6 +21,7 @@ from mp3player import MP3Player
|
|||||||
from nfc import Nfc
|
from nfc import Nfc
|
||||||
from rp2_neopixel import NeoPixel
|
from rp2_neopixel import NeoPixel
|
||||||
from utils import BTreeFileManager, Buttons, SDContext, TimerManager, LedManager
|
from utils import BTreeFileManager, Buttons, SDContext, TimerManager, LedManager
|
||||||
|
from webserver import start_webserver
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import hwconfig
|
import hwconfig
|
||||||
@@ -56,29 +57,6 @@ def setup_wifi():
|
|||||||
|
|
||||||
DB_PATH = '/sd/tonberry.db'
|
DB_PATH = '/sd/tonberry.db'
|
||||||
|
|
||||||
webserver = Microdot()
|
|
||||||
|
|
||||||
@webserver.route('/')
|
|
||||||
async def index(request):
|
|
||||||
print("wohoo, a guest :)")
|
|
||||||
print(f" app: {request.app}")
|
|
||||||
print(f" client: {request.client_addr}")
|
|
||||||
print(f" method: {request.method}")
|
|
||||||
print(f" url: {request.url}")
|
|
||||||
print(f" headers: {request.headers}")
|
|
||||||
print(f" cookies: {request.cookies}")
|
|
||||||
return "TonberryPico says 'Hello World!'"
|
|
||||||
|
|
||||||
|
|
||||||
@webserver.route('/v1/api/playback/control', methods=['POST'])
|
|
||||||
async def playback_control(request):
|
|
||||||
if not request.json:
|
|
||||||
return {'success': False}
|
|
||||||
|
|
||||||
# Example:
|
|
||||||
# curl -H "Content-Type: application/json" --data '{"action": "play", "target_type": "audio_file", "target_id": "1234"}' http://192.168.4.1/v1/api/playback/control
|
|
||||||
print(f'Calling {request.json["action"]} on {request.json["target_type"]} with id \
|
|
||||||
{request.json["target_id"]}')
|
|
||||||
|
|
||||||
def run():
|
def run():
|
||||||
asyncio.new_event_loop()
|
asyncio.new_event_loop()
|
||||||
@@ -87,9 +65,8 @@ def run():
|
|||||||
|
|
||||||
# Wifi with default config
|
# Wifi with default config
|
||||||
setup_wifi()
|
setup_wifi()
|
||||||
#webserver.run(port=80)
|
start_webserver()
|
||||||
server = asyncio.create_task(webserver.start_server(port=80))
|
|
||||||
|
|
||||||
# Setup MP3 player
|
# Setup MP3 player
|
||||||
with SDContext(mosi=hwconfig.SD_DI, miso=hwconfig.SD_DO, sck=hwconfig.SD_SCK, ss=hwconfig.SD_CS,
|
with SDContext(mosi=hwconfig.SD_DI, miso=hwconfig.SD_DO, sck=hwconfig.SD_SCK, ss=hwconfig.SD_CS,
|
||||||
baudrate=hwconfig.SD_CLOCKRATE):
|
baudrate=hwconfig.SD_CLOCKRATE):
|
||||||
|
|||||||
@@ -1,30 +1,19 @@
|
|||||||
import rp2
|
'''
|
||||||
import network
|
SPDX-License-Identifier: MIT
|
||||||
import ubinascii
|
Copyright (c) 2024-2025 Stefan Kratochwil <Kratochwil-LA@gmx.de>
|
||||||
|
'''
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
|
||||||
from microdot import Microdot
|
from microdot import Microdot
|
||||||
|
|
||||||
rp2.country('DE')
|
webapp = Microdot()
|
||||||
|
server = None
|
||||||
|
|
||||||
wlan = network.WLAN(network.AP_IF)
|
def start_webserver():
|
||||||
wlan.config(ssid='TonberryPico', security=network.WLAN.SEC_OPEN)
|
server = asyncio.create_task(webapp.start_server(port=80))
|
||||||
# Important: we cannot change the ip in station mode, otherwise dhcp won't work!
|
|
||||||
# wlan.ipconfig(addr4='10.0.0.1')
|
|
||||||
wlan.active(True) # loads the firmware
|
|
||||||
while wlan.active() is False:
|
|
||||||
pass
|
|
||||||
wlan.config(pm=network.WLAN.PM_NONE)
|
|
||||||
|
|
||||||
mac = ubinascii.hexlify(network.WLAN().config('mac'), ':').decode()
|
@webapp.route('/')
|
||||||
print(f" mac: {mac}")
|
|
||||||
print(f" channel: {wlan.config('channel')}")
|
|
||||||
print(f" essid: {wlan.config('essid')}")
|
|
||||||
print(f" txpower: {wlan.config('txpower')}")
|
|
||||||
print(f"ifconfig: {wlan.ifconfig()}")
|
|
||||||
|
|
||||||
app = Microdot()
|
|
||||||
|
|
||||||
|
|
||||||
@app.route('/')
|
|
||||||
async def index(request):
|
async def index(request):
|
||||||
print("wohoo, a guest :)")
|
print("wohoo, a guest :)")
|
||||||
print(f" app: {request.app}")
|
print(f" app: {request.app}")
|
||||||
@@ -36,7 +25,7 @@ async def index(request):
|
|||||||
return "TonberryPico says 'Hello World!'"
|
return "TonberryPico says 'Hello World!'"
|
||||||
|
|
||||||
|
|
||||||
@app.route('/v1/api/playback/control', methods=['POST'])
|
@webapp.route('/v1/api/playback/control', methods=['POST'])
|
||||||
async def playback_control(request):
|
async def playback_control(request):
|
||||||
if not request.json:
|
if not request.json:
|
||||||
return {'success': False}
|
return {'success': False}
|
||||||
@@ -46,5 +35,3 @@ async def playback_control(request):
|
|||||||
print(f'Calling {request.json["action"]} on {request.json["target_type"]} with id \
|
print(f'Calling {request.json["action"]} on {request.json["target_type"]} with id \
|
||||||
{request.json["target_id"]}')
|
{request.json["target_id"]}')
|
||||||
|
|
||||||
|
|
||||||
app.run(port=80)
|
|
||||||
Reference in New Issue
Block a user