Skip to main content

An async WebSockets library for MicroPython on ESP32

Project description

micropython-websockets

适用于ESP32的异步MicroPython WebSockets模块。

安装方法

在Thonny中安装到ESP32:

  1. 用USB将ESP32连接到电脑
  2. 打开Thonny,右下角切换为 MicroPython(ESP32)
  3. 菜单 Tools → Manage packages
  4. 搜索 micropython-websockets
  5. 点击安装

使用示例

import asyncio
import network
import micropython_websockets as websockets

# ── Wi-Fi 连接 ─────────────────────────────────────────────────────────────
async def connect_wifi(ssid, password):
    wlan = network.WLAN(network.STA_IF)
    wlan.active(True)
    if not wlan.isconnected():
        print("正在连接 Wi-Fi...")
        wlan.connect(ssid, password)
        while not wlan.isconnected():
            await asyncio.sleep(0.5)
    print("Wi-Fi 已连接,IP:", wlan.ifconfig()[0])

# ── WebSocket 客户端 ────────────────────────────────────────────────────────
async def main():
    await connect_wifi("你的WiFi名称", "你的WiFi密码")

    # 替换为 PC 的局域网 IP
    uri = "ws://192.168.1.107:8765"

    async with websockets.connect(uri) as ws:
        print("已连接到服务器:", uri)

        # 发送消息
        await ws.send("Hello from ESP32!")
        print("已发送: Hello from ESP32!")

        # 接收回复
        reply = await ws.recv()
        print("收到回复:", reply)

    print("连接已关闭")

asyncio.run(main())

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

micropython_websockets-0.1.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

micropython_websockets-0.1.0-py3-none-any.whl (15.7 kB view details)

Uploaded Python 3

File details

Details for the file micropython_websockets-0.1.0.tar.gz.

File metadata

  • Download URL: micropython_websockets-0.1.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.4

File hashes

Hashes for micropython_websockets-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f822e91d042233812aa18a6038ede40cdc89d3ede716330365a3f0ff7a8fd0b1
MD5 2fff3ee55f215bec90f2cc80783d76df
BLAKE2b-256 51b374e820f5cb3e995d27cc9f215f4fb85be1666cf0fd63fb269a38f201e89e

See more details on using hashes here.

File details

Details for the file micropython_websockets-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for micropython_websockets-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9029dadbd50482e8139b99ee4ec9ba37e64a02874bfcc32a7d96ec07bf095803
MD5 90181159150095a20190912aabd47a2b
BLAKE2b-256 4b403fd5675fd0018db54bf79c3551e2baa775984a1c4983fd5516c8c6d19c8c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page