Git Product home page Git Product logo

pico-eth-ch9121's Introduction

pico-eth-ch9121

Micropython flask-like for raspberry pi pico and pico-eth-ch9121 Ethernet to UART converter.

ressources

Pico-eth-ch9121:

Raspberry pi pico micropython:

Pin

pin

Lexic

  • Pico: Raspberry pi pico Board.
  • ch9121: Pico-eth-ch9121 Board.
  • Lan: Local Area Network.

Setup the Pico-eth-ch9121 Board

The configuration for TCP server is stored in config.py.

project structure

  1. main.py is the entry point of the code for the pico.
  2. lib is the default directory for the modules. No need of __init__.py file. Our libraries will be stored there.
  3. lib\server.py is the class for the ch9121 server with the UART0 configuration.
  4. lib\web.py is the tiny framework (flask-like) to manage the request and response.
  5. lib\action.py is only for the exemple.

exemple

main.py

import gc
import uasyncio as asyncio
from ujson import dumps, loads
from config import WEB_CONFIG
from server import CH9121
from web import App, reponse
from action import do_led

# Setup the server and the App
server = CH9121()

loop = asyncio.get_event_loop()
print("start config")
loop.run_until_complete(server.set_tcp_server(**SERVER_CONFIG))
print("config complet")
loop.close()

app = App(
    server=server,
    token=WEB_CONFIG["TOKEN"]
)

# Memory clean
gc.collect()

# Route section
@app.route('/', methods=["POST"], security=True)
def main(request):
    actions = loads(request.headers["actions"])
    do_led(actions["LED"])
    return reponse(
        http_code=200,
        body=dumps(actions),
        content_type="json"
    )

# Loop section
loop = asyncio.get_event_loop()
loop.create_task(app.serve())
loop.run_forever()

The very important part is @app.route('/', methods=["POST"], security=True)

  1. the route (here is the root route), Each route must be different and they is no match pattern.
  2. the methods allowed (POST, GET...).
  3. the security (it is optional). The define token in the config.py must be past to the request in the header with the key token.

IDE

vs-code with the pico-go extension is very usefull for developping the project.

  • tips: add in .vscode\settings.json "python.analysis.extraPaths": [".vscode\Pico-Stub\stubs", "lib"]` to be in the same condition as the pico.

Thonny is more stable to upload and run the code on the pico than pico-go extension.

pico-eth-ch9121's People

Contributors

lgueho avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.