Page 1 of 1

esp8266 UART(0, 9600) not work

Posted: Wed Dec 01, 2021 2:05 pm
by y1ngkhun
Hi

my esp8266 NodeMCU
UART 0 baudrate 9600 not working

Code: Select all

from machine import UART
uart = UART(0, 9600)
I get freezes after runung please Help Thanks

Re: esp8266 UART(0, 9600) not work

Posted: Thu Dec 02, 2021 7:00 am
by Roberthh
UART0 is used for REPL at 115200 baud. If you redefine it to 9600 baud, REPL looks lost. ESP8266 has only one full functional UART. There is a second TX.only UART. Beside that, there is. PR for a software UART outstanding.

Re: esp8266 UART(0, 9600) not work

Posted: Thu Dec 02, 2021 10:07 am
by Shards
There is a way to detach the ESP8266 UART from REPL and use it as a normal UART at a preferred baudrate. You can also restore REPL.

Code: Select all

from machine import UART
import uos

def connect_to_UART(baudrate=9600):
	uos.dupterm(None,1)
	return UART(0,baudrate,timeout=5000)

def REPL_restore():
	uart = connect_to_UART(baudrate=115200)
	uos.dupterm(uart,1)  
 
uart = connect_to_UART()
c=''
while True:
		while not uart.any():
		   pass
		c = uart.read(1)
		#provide escape route to restore REPL
		if 'x' in c:
			REPL_restore()
			break        
		uart.write(c)
Code tested using an ESP01 board with 1Mb Flash running Micropython 1.17. Terminal program picocom. You will need to change baudrate on the fly to test.

I use these functions with the tiny ESP01 boards which have only one easily accessible GPIO pin so a working UART is useful. If the redirection code is in main.py a means to restore REPL is essential. Should work on any ESP8266 board and any port with 'dupterm' available.