How can i use machine.UART?

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
Strik3r
Posts: 14
Joined: Thu Nov 28, 2019 7:36 pm
Location: Saratov, Russia

How can i use machine.UART?

Post by Strik3r » Thu Nov 28, 2019 7:49 pm

Hello!
I want to use my NodeMCU esp8266 board to drive some relays in my PC, and i want it to recieve signals frought uart interface.
I started to learn how i can do this, and find machine.uart method. But i have faced a problem:
esp8266 is starting to recieve UART messages already after 'uart = machine.UART(0, 9600)' command, and it paste it to input line of REPL. How can i force it to recieve the UART signals to variable?
I tried all other commands, uart.init, uart.read, uart.readline - nothing changes.
Image
Image
And i'm sorry, i'm newbie, and maybe i'm understand somthing wrong?

User avatar
pythoncoder
Posts: 4232
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: How can i use machine.UART?

Post by pythoncoder » Fri Nov 29, 2019 9:07 am

Using the ESP8266 UART is difficult because the chip has only one fully functional UART which is used for the USB interface.

My preferred solutions are either to use another means of communication or to use a chip with multiple UARTs (ESP32 or Pyboard D). Others have had success with UART 0; perhaps they will advise.
Peter Hinch

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

Re: How can i use machine.UART?

Post by Roberthh » Fri Nov 29, 2019 9:09 am

If you have the UART assigned to REPL/console, you can use sys.stdout.write() to send and sys.stdin.read(), sys.stdin.readline(), ... to read data from the port, without using machine.UART. If you want to use machine.UART, you have to detach the UARt from the console first, with;

Code: Select all

import uos
uos.dupterm(None, 1) # disable REPL on UART(0)
But then you have to use webrepl for the console. Instructions to do so are in the documentation: http://docs.micropython.org/en/latest/e ... ht=webrepl

Strik3r
Posts: 14
Joined: Thu Nov 28, 2019 7:36 pm
Location: Saratov, Russia

Re: How can i use machine.UART?

Post by Strik3r » Fri Nov 29, 2019 2:48 pm

pythoncoder wrote:
Fri Nov 29, 2019 9:07 am
Using the ESP8266 UART is difficult because the chip has only one fully functional UART which is used for the USB interface.
I'm dont understand that problem. If uart can recieve and sent messages, why cant i use it?

Roberthh wrote:
Fri Nov 29, 2019 9:09 am

Code: Select all

import uos
uos.dupterm(None, 1) # disable REPL on UART(0)
thanks! Now it works as it supossed to!
Roberthh wrote:
Fri Nov 29, 2019 9:09 am
But then you have to use webrepl for the console.
I'm already use webrepl, as you can see on the screenshot! Thanks again!

Logician
Posts: 3
Joined: Mon May 11, 2020 10:48 am

Re: How can i use machine.UART?

Post by Logician » Mon May 11, 2020 10:59 am

Roberthh wrote:
Fri Nov 29, 2019 9:09 am
If you have the UART assigned to REPL/console, you can use sys.stdout.write() to send and sys.stdin.read(), sys.stdin.readline(), ... to read data from the port, without using machine.UART. If you want to use machine.UART, you have to detach the UARt from the console first, with;

Code: Select all

import uos
uos.dupterm(None, 1) # disable REPL on UART(0)
But then you have to use webrepl for the console. Instructions to do so are in the documentation: http://docs.micropython.org/en/latest/e ... ht=webrepl
Thanks for this! I suspected this might be the case, but I couldn't find any confirmation in the MicroPython documentation (including how to disable UART<->USB).

I would suggest adding this information to the relevant sections in Quick Reference and UART sections (happy to do this myself if they're editable).

I might also suggest altering boot.py to disable the USB<>UART interface if there's nothing in the receive buffer after n seconds.

Edit: I see the line is already present and commented out in boot.py, which is handy! I hadn't looked at it when I posted because I didn't have any boot settings that I wanted to persist yet.

Post Reply