NodeMCU creating a UART to TCP 'tunnel'

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
guye1296
Posts: 2
Joined: Sat Apr 07, 2018 5:50 pm

NodeMCU creating a UART to TCP 'tunnel'

Post by guye1296 » Sun Apr 08, 2018 8:49 pm

Hello,
I've been having a hard time with controlling the UART. I know that UART0 is also mapped to the REPL's stdin and stdout - which makes debugging a lot harder when debugging applications that use UART. I also know that when there is no blocking call the REPL is scheduled.
The propose of the ESP8266 is to communicate with both a board via UART and a remote server via TCP. In order to do that I've used select.ipoll which is a blocking call - so it the REPL shouldn't be scheduled at any stage.

The thing is, I can't read use the board's UART.
Since I can't debug, I've turned on a led when receiving UART messages and wrote the message contents into a file so I can inspect it later.

When sending a byte from the other board - the led turns on, but the message received is null (\x00).

I'm adding the code - since it's quite simple:

def run(self):
# UART <--> socket passtrough
p = select.poll()
p.register(self._sock, select.POLLIN)
p.register(self._uart, select.POLLIN)

led = machine.Pin(2, machine.Pin.OUT, machine.Pin.PULL_UP)
led.on()

while True:
for source, _ in p.ipoll():
if source.__class__ == socket.socket:
msg = source.recv(16)
debug.concat_log_file(msg, "tcp.log")
self._uart.write(msg)

if source.__class__ == machine.UART:
led.off()
msg = source.read()
debug.concat_log_file("read msg", "uart.log")
debug.concat_log_file(msg, "uart.log")
self._sock.send(msg)
led.on()

If anyone is wondering - I've defined the UART the following way:
self._uart = machine.UART(0, 9600, timeout=2)

The board is sending at the same baudrate.
I'll appreciate any help...

Guy.

Post Reply