I want to access my ESP32 console remotely and I think that the best way to do so is to create TCP tunnel from the ESP32 to a server and use this server as a really for the connections, so that when I connect using WebREPL client to the server, the connection is forwarded trough the TCP tunnel to the ESP32 board and here the request is then sent to the actual WebREPL server.
The code I come up with is available at https://gist.github.com/alessionossa/7277df576a29edda4a29500e4e71bc20, however the code on ESP32 gets stuck, probably because the current code requires too many parallel operations. I tried using both threads and uasyncio, but I was not able to get a working solution.
- A server that listens on two ports:
- First port is for the connection from the ESP32 board (1), this connection is persistent and the TCP tunnel will be used to forward traffic to the microcontroller.
- Second port is for the external connection from WebREPL client (2).
- The script on ESP32, which connects to the server creating a TCP tunnel and forwards the data coming from the tunnel to WebREPL's port (4).
The code blocks on WebREPL connection from: ('127.0.0.1', 51639) but I can still Ctrl-C. If I do that I think it interrupts WebREPL code because I get
Traceback (most recent call last):
File "webrepl.py", line 43, in accept_conn
File "websocket_helper.py", line 20, in server_handshake
KeyboardInterrupt:
But also I get an something unexpected in the log when it first connects to the server:
[...]
Checkpoint 1: False; None; None
Connecting to ('159.223.29.36', 12014)...
Connected to ('159.223.29.36', 12014)
Checkpoint 2: True; <socket>; None
Connecting to ('localhost', 8266)...
Connected to ('localhost', 8266)
Checkpoint 3: True; <socket>; <socket>
Starting tasks...
Tasks started...
Tunnel daeomon sleeping
Checkpoint 4: True; <socket>; <socket>
Checkpoint 5: True; None; None
Checkpoint 1: False; None; None
[...]
The unexpected things are:
- At Checkpoint 5 sockets variables are empty and actually it looks like it doesn’t wait
await uasyncio.sleep(5)in this case (and only in this case); - It says
Starting tasks...but it doesn’t print :
Started forwarding loop: remote -> localhost:8266
Entered async_recvfrom(sock, count)
Started forwarding loop: localhost:8266 -> remote
Entered async_recvfrom(sock, count)
