Remote control via WebREPL

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Remote control via WebREPL

Post by HermannSW » Wed Oct 17, 2018 12:39 am

My remote control scenario (trigger servo payload drop mechanism attached to flying drone) could be done simpler.
But I really like that I can use WebREPL for that.
I just did toggle builtin LED of remote ESP01s by toggling a button on local ESP01s:
https://www.youtube.com/watch?v=tQHDQqGaous

The interesting feature of current solution is, that no modules need to be installed on remote ESP01s, only WebREPL needs to be enabled on it. The local ESP01s executes the needed MicroPython commands on remote ESP01s, so code for local and remote ESP01s is in one script running on local ESP01s, here "as is":

Code: Select all

import network
import uwebsockets.client
import time
from machine import Pin

but = Pin(0, Pin.IN, Pin.PULL_UP)
led = Pin(2, Pin.OUT)
led.value(0)

sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect("MicroPython-b7e5c8", "12345678")

while not(sta_if.isconnected()):
    led.value(1-led.value())
    time.sleep(0.2)

led.value(1)
websocket = uwebsockets.client.connect('ws://'+sta_if.ifconfig()[2]+':8266/')

def wait_for_prompt():
    resp = ""
    while not(">>> " in resp):
        resp = websocket.recv()
        print("< {}".format(resp))

def do(cmd):
    websocket.send(cmd+"\r\n")
    wait_for_prompt()

resp = websocket.recv()
assert resp == "Password: ", resp

do("abcd")

do("import machine")

do("led = machine.Pin(1, machine.Pin.OUT)")

do("led.value(1)")

while True:
    if but.value() == 0:
        led.value(0)
        do("led.value(0)")

        time.sleep(0.1)
        while but.value() == 0:
            time.sleep(0.1)

        led.value(1)
        do("led.value(1)")

    time.sleep(0.1)
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

Post Reply