Page 1 of 1

Trying to read MBUS data through UART

Posted: Sat Apr 10, 2021 7:00 pm
by frepe360

I am trying to read some stuff from a power meter through the UART of a Wemos D1 mini (

I have wired it up as suggested here:

(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)
... gives nothing.

I have also tried the trick mentioned in the section "UART (serial bus)" here: ... ckref.html

Like this:

Code: Select all

That gives me no data from the power meter either.

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

Re: Trying to read MBUS data through UART

Posted: Mon Apr 12, 2021 1:43 am
by microBoa
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 = if poll.poll() else None
  if ???:
    uos.dupterm(UART(0, 115200), 1)
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.

Re: Trying to read MBUS data through UART

Posted: Sat Apr 17, 2021 4:37 pm
by frepe360
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?

Re: Trying to read MBUS data through UART

Posted: Sat Apr 17, 2021 8:36 pm
by microBoa
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!

Re: Trying to read MBUS data through UART

Posted: Sat Apr 17, 2021 8:54 pm
by microBoa

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.