accessing uart0 on nodemcu board

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
User avatar
cemox
Posts: 34
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

accessing uart0 on nodemcu board

Post by cemox » Mon Oct 08, 2018 5:50 pm

hi,
knowing that uart0 is dedicated to repl and uart1 is only capable of sending data (no rx), I searched the forum to find a solution. it is said that repl is not active when the code is run from main.py, I tried that, did not work. I also tried to disable repl by uos.dupterm(None), no luck.

In the below code, I setup uart1 as sender, it sends a short message at intervals, I checked it via usb-serial ftdi converter, it works as expected. I connected the uart1 TX pin (d4) to RX pin of the board, created sort of loopback. When I monitor the uart0, it prints out None as the received data. Is there a way of reading a sensor data from RX pin of the board, I do not need repl or webrepl, I am going to run the code as main.py. Thanks !

[code]
import time
import uos
from machine import UART


uart0 = UART(0)
uart1 = UART(1)
print(uart0)
print(uart1)


print("starting")
uos.dupterm(None)

uart1.write("send me something")

uart0.write("waiting for data")

while (True):

uart1.write("test data")
time.sleep(0.5)

a = uart0.read(32)
print(a)

if a is not None:
print(a)
uart0.write('received data')
[/code]

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

Re: accessing uart0 on nodemcu board

Post by Roberthh » Mon Oct 08, 2018 7:48 pm

Even if REPL is not used when running code, uart0 ist not available as such, unless you call uos.dupterm(None) before instantiating it, to make it available as uart again.
Without that, you can read the data on uart0 rx with sys.stdin.read(), and write to sys.stdout. But that read is blocking.
Edit: once uos.dupterm(None) is effective, print should not create any output on uart0 tx.

User avatar
cemox
Posts: 34
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

Re: accessing uart0 on nodemcu board

Post by cemox » Tue Oct 09, 2018 7:11 am

[quote=Roberthh post_id=30848 time=1539028096 user_id=601]
Even if REPL is not used when running code, uart0 ist not available as such, unless you call uos.dupterm(None) before instantiating it, to make it available as uart again.
Without that, you can read the data on uart0 rx with sys.stdin.read(), and write to sys.stdout. But that read is blocking.
Edit: once uos.dupterm(None) is effective, print should not create any output on uart0 tx.
[/quote]

Thanks for the answer. You are right after uos.dupeterm(None), print does not work in a normal way, but I can still see the output in a terminal program like CuteCom. Besides I am not using repl, I run the code as main.py from the board. I even tried powering the board externally without plugging the board USB port.

All I observe is that it receives None from uart. The condition in the piece of code below, is never met as well!

[quote]
[code]
a = uart0.read(32)

if a is not None:
uart0.write('received data')
[/code]
[/quote]

Micropython is great, I have been playing with it for a long time, but reading serial data from outside is a simple and important feature it has to have.

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

Re: accessing uart0 on nodemcu board

Post by Roberthh » Tue Oct 09, 2018 7:14 am

Which version of the firmware are you using?

Edit: You should also consider that uart.read() does not block. So if no data is available at the time of reading, it returns None.

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

Re: accessing uart0 on nodemcu board

Post by Roberthh » Tue Oct 09, 2018 6:11 pm

I made a short test code for uart0 read, and it works. The major difference is the second parameter on dupterm.

Code: Select all

from machine import UART
import uos

uos.dupterm(None, 1)
uart = UART(0, 115200)
uart.write(b"The quick brown fox jumps over the lazy dog\r\n")
ch = b""
while ch != b"q":
    if uart.any():
        ch = uart.read()
        uart.write(ch)

uos.dupterm(UART(0, 115200), 1)

User avatar
cemox
Posts: 34
Joined: Mon Oct 08, 2018 5:31 pm
Location: Turkey

Re: accessing uart0 on nodemcu board -SOLVED

Post by cemox » Wed Oct 10, 2018 4:27 pm

Thank you Robert, people like you make these forums great, I mean it . I do appreciate your efforts.

It should have been little easier to make use of uart with esp8266 based boards, I believe the fantastic developers of micropython will achieve that.

Cheers

dariox
Posts: 8
Joined: Sat Dec 22, 2018 12:29 pm

Re: accessing uart0 on nodemcu board

Post by dariox » Sun Dec 23, 2018 3:52 pm

I'm working on something similar. I have a modem hooked up to my ESP8266 12E via serial in order to send a receive AT commands to the modem. The cabeling is a follow:
ESP8266 3.3V --> Modem 3.3V
ESP8266 GND --> Modem GND
ESP8266 RX --> Modem TX
ESP8266 D4 --> Modem RX


However, if I run the uos.dupterm(None, 1) command I get an error:

raceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid dupterm index
>>>


From the conversation above, I understood, that uart0 is somehow dedicated to the REPL, but it can be used to read. In order to send commands, I need to use uart1 which is D4?

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

Re: accessing uart0 on nodemcu board

Post by Roberthh » Sun Dec 23, 2018 4:19 pm

That works for me, on a fresh built firmware. The question is, what is in you boot.py. Mine looks like:

Code: Select all

# This file is executed on every boot (including wake-boot from deepsleep)
import esp
esp.osdebug(None)
import uos, machine
uos.dupterm(machine.UART(0, 115200), 1)
import gc
#import webrepl
#webrepl.start()
gc.collect()

dariox
Posts: 8
Joined: Sat Dec 22, 2018 12:29 pm

Re: accessing uart0 on nodemcu board

Post by dariox » Sun Dec 23, 2018 9:37 pm

I've flashed the same boot.py, but still the same result. I'm currently running no main.py. FW is
esp8266-20180511-v1.9.4.bin

Christian Walther
Posts: 169
Joined: Fri Aug 19, 2016 11:55 am

Re: accessing uart0 on nodemcu board

Post by Christian Walther » Mon Dec 24, 2018 8:41 am

This feature is not part of that release yet, you need to use a newer daily build or build your own. I believe the relevant commit is afd0701, which should make any of the daily builds available from the download page suitable.

Post Reply