Something went wrong. Try again.
Something went wrong. Try again.
micropython-experiments led-ws.py
1.4 kB · 55 lines
Python
at main
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556import network
SSID = "..."PASSWORD = "..."
# Connect to Wi-Fiwlan = network.WLAN(network.STA_IF)wlan.active(True)wlan.connect(SSID, PASSWORD)print("Connecting...")while not wlan.isconnected(): passwlan.ifconfig(( '192.168.68.151', # same IP '255.255.255.0', # same mask '192.168.68.1', # same gateway '1.1.1.1' # Google DNS instead of 192.168.1.1))print("Connected: ", wlan.ifconfig())
# Diagnostics (sometimes DNS fails)import socketprint("Resolving detectportal.firefox.com")print(socket.getaddrinfo("detectportal.firefox.com", 80))
print("Resolving github.com...")print(socket.getaddrinfo("github.com", 443))
# Install aiohttp (only needed once)import mipmip.install("aiohttp")print("Installed")
import aiohttpimport asynciofrom machine import PWM, Pin
led = PWM(Pin(1, Pin.OUT))
async def main(): async with aiohttp.ClientSession() as session: async with session.ws_connect("ws://192.168.68.NNN:8765") as ws: async for msg in ws: print("Received:", msg.data) try: value = int(msg.data) if not 0 <= value < 1024: print("INVALID: ", value) else: led.duty(value) except Exception as exc: print("Error: ", exc)
asyncio.run(main())