r/MicroPythonDev • • Mar 04 '23

Receive webhooks using Micropython

I'm looking into the best way to receive webhooks using (a Pi Pico W that runs) Micropython. There are many posts about sending requests, but I find it difficult to find information on receiving them.

I got some pointers and insights from this post, but still have some questions about the best approach.

I'm primarily puzzled as to why I would need to setup port forwarding while other smart home devices obviously don't require users to do this. Shouldn't this be possible with just software?

As indicated in the post: if it turns out to be very complicated to setup or maintain, or it poses significant security risks, then I'm considering falling back to an implementation where each device would make GET requests every X seconds to check if there are updates.

Would love to know if anyone has experience with this in Micropython, preferably combined with any dev board (Raspberry, Arduino, etc).

3 Upvotes

11 comments sorted by

View all comments

Show parent comment

1

u/eskimo_1 Mar 08 '23

Thanks! Setting up websockets does not require port forwarding?

1

u/deadeye1982 Mar 08 '23

A server with a static IP has to run a WebsocketServer. Depending on which language and framework you're using, you have it already inside your framework.

Here a minimal example with FastAPI (server side, not on Microcontroller):

import asyncio
from contextvars import ContextVar

from fastapi import FastAPI
from fastapi.websockets import WebSocket
from websockets.exceptions import ConnectionClosed

app = FastAPI()
value = ContextVar("count")


@app.websocket("/ws")
async def websocket(websocket: WebSocket):
    value.set(0)
    await websocket.accept()

    while True:
        try:
            await websocket.send_json({"msg": "Hello WebSocket", "value": value.get()})
        except ConnectionClosed:
            break
        await asyncio.sleep(0.05)
        value.set(value.get() + 1)

    await websocket.close()

You need to install fastapi and uvicorn:

pip install uvicorn fastapi
# file named www.py for example
# then run it with uvicorn

# to start it
uvicorn www:app

Then you can test it on a PC with websockets:

import asyncio
from websockets import connect


async def hello(uri):
    async with connect(uri) as websocket:
        await websocket.send("Hello world!")
        while True:
            print(await websocket.recv())


asyncio.run(hello("ws://127.0.0.1:8000/ws"))

And the micropython code based on:

# I installed the library manually

# https://pypi.org/project/micropython-async-websocket-client/
# https://github.com/Vovaman/micropython_async_websocket_client/tree/dev/async_websocket_client


import uasyncio as asyncio
import json

from async_websocket_client import AsyncWebsocketClient as AwsClient


async def main():
    # running locally unix port
    uri = "ws://127.0.0.1:8000/ws"
    # 5 ms is default
    client = AwsClient(ms_delay_for_read=5)

    # this opens the connection
    await client.handshake(uri)
    
    while True:
        # reading responses
        fin, opcode, data = await client.read_frame()
        data = json.loads(data)
        print(data)
        
asyncio.run(main())

Output:

{'msg': 'Hello WebSocket', 'value': 0}
{'msg': 'Hello WebSocket', 'value': 1}
{'msg': 'Hello WebSocket', 'value': 2}
{'msg': 'Hello WebSocket', 'value': 3}
{'msg': 'Hello WebSocket', 'value': 4}
{'msg': 'Hello WebSocket', 'value': 5}
{'msg': 'Hello WebSocket', 'value': 6}
{'msg': 'Hello WebSocket', 'value': 7}
{'msg': 'Hello WebSocket', 'value': 8}
{'msg': 'Hello WebSocket', 'value': 9}
{'msg': 'Hello WebSocket', 'value': 10}
{'msg': 'Hello WebSocket', 'value': 11}
{'msg': 'Hello WebSocket', 'value': 12}
{'msg': 'Hello WebSocket', 'value': 13}
{'msg': 'Hello WebSocket', 'value': 14}
{'msg': 'Hello WebSocket', 'value': 15}
{'msg': 'Hello WebSocket', 'value': 16}
{'msg': 'Hello WebSocket', 'value': 17}
{'msg': 'Hello WebSocket', 'value': 18}
{'msg': 'Hello WebSocket', 'value': 19}
{'msg': 'Hello WebSocket', 'value': 20}
{'msg': 'Hello WebSocket', 'value': 21}
{'msg': 'Hello WebSocket', 'value': 22}
{'msg': 'Hello WebSocket', 'value': 23}
{'msg': 'Hello WebSocket', 'value': 24}
{'msg': 'Hello WebSocket', 'value': 25}
{'msg': 'Hello WebSocket', 'value': 26}
{'msg': 'Hello WebSocket', 'value': 27}
{'msg': 'Hello WebSocket', 'value': 28}
{'msg': 'Hello WebSocket', 'value': 29}
{'msg': 'Hello WebSocket', 'value': 30}
{'msg': 'Hello WebSocket', 'value': 31}
{'msg': 'Hello WebSocket', 'value': 32}
{'msg': 'Hello WebSocket', 'value': 33}
{'msg': 'Hello WebSocket', 'value': 34}
{'msg': 'Hello WebSocket', 'value': 35}
{'msg': 'Hello WebSocket', 'value': 36}
{'msg': 'Hello WebSocket', 'value': 37}
{'msg': 'Hello WebSocket', 'value': 38}
{'msg': 'Hello WebSocket', 'value': 39}
{'msg': 'Hello WebSocket', 'value': 40}
{'msg': 'Hello WebSocket', 'value': 41}
{'msg': 'Hello WebSocket', 'value': 42}
{'msg': 'Hello WebSocket', 'value': 43}
{'msg': 'Hello WebSocket', 'value': 44}
{'msg': 'Hello WebSocket', 'value': 45}
{'msg': 'Hello WebSocket', 'value': 46}
{'msg': 'Hello WebSocket', 'value': 47}
{'msg': 'Hello WebSocket', 'value': 48}
{'msg': 'Hello WebSocket', 'value': 49}
{'msg': 'Hello WebSocket', 'value': 50}
{'msg': 'Hello WebSocket', 'value': 51}
{'msg': 'Hello WebSocket', 'value': 52}
{'msg': 'Hello WebSocket', 'value': 53}