I need to read Luxs from BH1750 over virtual serial port in Python3, but I am not sure how to do it.
I ended with something like this:
import serial
ser = serial.Serial(port="/dev/ttyUSB0",
baudrate=19200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS,
timeout=0.500,
)
ser.flushInput()
ser.flushOutput()
ser.write(bytearray([0x55, 0x23, 0x11, 0x01]))
test = ser.read()
But I am not sure, what are the right bytes. I have the GY-30 connected directly to USB-I2C.
Thank for your eventual help ...
Jiri
解决方案
There is final solution:
import serial
import time
ser = serial.Serial(port="/dev/ttyUSB0",
baudrate=19200,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_TWO,
bytesize=serial.EIGHTBITS,
timeout=0.500,
)
ser.flushInput()
ser.flushOutput()
ADDR_BASE = 0x23 # nebo 0x5c pokud je ADDR pin HIGH
ADDR_READ = ADDR_BASE << 1 | 1
ADDR_WRITE = ADDR_BASE << 1
CMD_WRITE_1B = 0x53
CMD_READ_MULT = 0x54
LUX_RESOLUTION_1X = 0x10
LUX_RESOLUTION_4X = 0x13
# nastavení senzoru
ser.write(bytearray([CMD_WRITE_1B, ADDR_WRITE, LUX_RESOLUTION_1X]))
while True:
time.sleep(1)
# požádám převodník o 2 B
ser.write(bytearray([CMD_READ_MULT, ADDR_READ, 0x02]))
# přečtu je
raw = ser.read(2)
# interpretace bytů podle datasheetu
lx = (raw[1] << 8 | raw[0]) / 1.2
print("--> %.d lx" %(lx))