Trying to read MBUS data through 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
frepe360
Posts: 2
Joined: Sat Apr 10, 2021 6:08 pm

Trying to read MBUS data through UART

Post by frepe360 » Sat Apr 10, 2021 7:00 pm

Hi!

I am trying to read some stuff from a power meter through the UART of a Wemos D1 mini (https://www.wemos.cc/en/latest/d1/d1_mini.html)

I have wired it up as suggested here: https://github.com/psvanstrom/esphome-p1reader/#readme

(I realize that that link points to a NodeMCU device while I am using a D1 mini...)

Anyway, I am accessing the d1 mini through webrepl/wifi (not connected through USB) and I am trying to use machine.UART to read the input from my power meter, which is supposed to be the MBUS protocol.

Code: Select all

myuart = UART(0,115200)
myuart.read()
... gives nothing.

I have also tried the trick mentioned in the section "UART (serial bus)" here: https://docs.micropython.org/en/latest/ ... ckref.html

Like this:

Code: Select all

uos.dupterm(None,1);mb=uart(0,115200,timeout=10000);value=mb.read();uos.dupterm(UART(0,115200),1)
That gives me no data from the power meter either.

Am I doing something fundamentally wrong here? Where to start looking for the error?

microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

Re: Trying to read MBUS data through UART

Post by microBoa » Mon Apr 12, 2021 1:43 am

I've had some difficulty reading from the uart myself lately on an ESP8266. I think this should work for the Wemos. I'm a beginner with Micropython, so I hope I don't lead your astray.

EDIT: when you do uos.dupterm(None, 1) you won't see any output from print() or REPL until you change it back again, so you need to do uart.write().

Code: Select all

import machine
import uselect
import uos

from machine import UART

uos.dupterm(None, 1)
uart = UART(0, 115200, timeout=10, timeout_char=10)

# if stream_queue & (select.POLLHUP | select.POLLERR):
poll = uselect.poll()
poll.register(uart, uselect.POLLIN)

while True:
  ch = uart.read(1) if poll.poll() else None
  uart.write(ch)
    
  ## HERE YOU NEED TO ADD A TEST FOR END OF TEXT YOU'RE EXPECTING
  ## OR YOU'LL BE STUCK IN A LOOP
  if ???:
    poll.unregister(uart)
    uos.dupterm(UART(0, 115200), 1)
   break
Hopefully that will help get you on track. I found that doing the uos.dupterm() right before and right after you finish using the uart is best, that way you're more likely to see any error messages from the REPL.

frepe360
Posts: 2
Joined: Sat Apr 10, 2021 6:08 pm

Re: Trying to read MBUS data through UART

Post by frepe360 » Sat Apr 17, 2021 4:37 pm

Thank you for your help.

I found one problem; the serial device was not connected properly to my Wemos D1 mini. Now it is, and I see tons of characters flowing in to my webrepl console. It becomes unusable, I have to disconnect the RX wire in order to be able to use the python console.

Anyway, I have tried detaching the uart with dupterm, do a read(), and reattaching, but I never get anything that way. (Also, there is no TX wire connected so write() will do nothing, I guess.)

I do not really understand what the write() does? Why is that needed?

microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

Re: Trying to read MBUS data through UART

Post by microBoa » Sat Apr 17, 2021 8:36 pm

write() is functioning like print(). When the uart is detached from dupterm, at least in my case, print() doesn't display anything, you need to use write() to see your output.

I'm using an ESP01 (8266) which just has the 1 set of uart pins, read() gets the input from your sensor or whatever, and write() spits it out to my screen like print().

If you're using print() to view your output and you're not seeing anything, you need to use write() until you reattach dupterm. I think your Wemos has 2 uarts, so if the TX pin isn't connected, its probably for the other uart.

In my example code, if you don't put any condition/if statement above the poll.unregister line, it'll uncontrollably spit out characters. You need a buffer to collect the characters and some way to let it know you have enough.

Hope that helps!

microBoa
Posts: 13
Joined: Mon Apr 05, 2021 6:09 am

Re: Trying to read MBUS data through UART

Post by microBoa » Sat Apr 17, 2021 8:54 pm

Update:

This is what I suspect is going on. You're using uart0 for the REPL, and uart1 to read your sensor. I don't know the pinout for the Wemos, or the default setup, so you'll have to verify.

You probably don't need to detach the REPL and reattach, try commenting out those lines and just reading your sensor with my example.
You'll still need to add a condition/if like I mentioned above.

Post Reply