esp8266 UART(0, 9600) not work

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
y1ngkhun
Posts: 7
Joined: Thu Jul 08, 2021 7:49 am

esp8266 UART(0, 9600) not work

Post by y1ngkhun » Wed Dec 01, 2021 2:05 pm

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

Online
User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: esp8266 UART(0, 9600) not work

Post by Roberthh » Thu Dec 02, 2021 7:00 am

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.

Shards
Posts: 39
Joined: Fri Jun 25, 2021 5:14 pm
Location: Milton Keynes, UK

Re: esp8266 UART(0, 9600) not work

Post by Shards » Thu Dec 02, 2021 10:07 am

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.

Post Reply