Description
Hi,
I have a small suggestion for improving the ESP8266 documentation.
Specifically, https://github.com/micropython/micropython/blob/master/docs/esp8266/quickref.rst
Today I attempted to read serial output from a Plantower PMS-3003 air quality sensor. This device outputs a 24 bytes at a fairly consistent interval with the same 2 start characters. It also has a checksum included in the message. (I will create a github project for this in the near future documenting my work). I used the asyncio library to do this.
async def uart_receive():
sreader = uasyncio.StreamReader(uart)
while True:
res = await sreader.read(24)
Initially, I wrote my code on an RP2, but eventually I want to connect this over Wi-Fi and log the data to a remote server, so I began bringing the code up one of my ESP8266 NodeMCU modules. However, an ESP8266 only has one UART, so I had to use the WebREPL.
Some findings which were not clear from the current MicroPython documentation:
- The Thonny IDE has the ability to connect to the WebREPL; this solves the HTTPS issue noted on https://micropython.org/webrepl/
- Setting up the WebREPL to use an existing Wi-Fi network was not clear. It would be helpful to create a separate section for users looking to do this and to instruct them to modify their boot.py in this manner:
def do_connect():
sta_if = network.WLAN(network.STA_IF)
if not sta_if.isconnected():
print('connecting to network...')
sta_if.active(True)
sta_if.connect('<network>', '<password>')
ap_if.active(False)
while not sta_if.isconnected():
pass
print('network config:', sta_if.ifconfig())
do_connect()
- Next, I disconnected the REPL from the ESP8266's UART using
uos.dupterm(None, 1)
in the boot.py. I then set up the UART to receive data usinguart = UART(the_uart, baudrate=9600, parity=None, stop=1, rx=Pin(3), tx=Pin(1))
.
After a lot of debugging, had to make two changes to my code for the ESP8266:
- I had to use
res = await sreader.readexactly(24)
instead of read(24) since I was only getting 8 or 15 bytes. I discovered this here StreamReader sreader.read() not works on asyncio v3. peterhinch/micropython-async#44, which made me read the tutorial where I read that this is normal behavior for read(). - I was getting garbage data, the byte string seemed to shift bits out over itself over multiple reads, but it wasn't until I added
uart.init(rxbuf=200)
to my code that the output was read as expected esp8266: Characters missing when reading from UART
With these changes I was able to simultaneously view the output of the air sensor from both the RP2 and ESP8266.
Therefore, I would suggest adding to the ESP8266's uart-serial-bus section that "Including a larger rxbuf
value size may help with serial communications when using the WebREPL".
Also, reading the readexactly()
and read()
issue, it may help to include a link from the official Micropython documentation on uasyncio — asynchronous I/O scheduler Peter Hinch's tutorial: Async I/O ](https://github.com/peterhinch/micropython-async/blob/master/v3/docs/TUTORIAL.md#64-writing-streaming-device-drivers).