Page 1 of 1

How can i use machine.UART?

Posted: Thu Nov 28, 2019 7:49 pm
by Strik3r
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?

Re: How can i use machine.UART?

Posted: Fri Nov 29, 2019 9:07 am
by pythoncoder
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.

Re: How can i use machine.UART?

Posted: Fri Nov 29, 2019 9:09 am
by Roberthh
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

Re: How can i use machine.UART?

Posted: Fri Nov 29, 2019 2:48 pm
by Strik3r
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!

Re: How can i use machine.UART?

Posted: Mon May 11, 2020 10:59 am
by Logician
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.