问题
I am Linux user and I have those devices:
USB-I2C convertor:
https://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
GY-30 I2C module:
https://5.imimg.com/data5/TY/AK/MY-1833510/gy-30-bh1750-intensity-digital-light-sensor-module.pdf
And GY-30 is with BH1750 Ambient light sensor:
https://www.mylms.cz/wp-content/uploads/2017/07/bh1750-datasheet.pdf
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
回答1:
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))
来源:https://stackoverflow.com/questions/61028083/how-to-get-data-from-i2c-device-bh1750-over-usb-i2c-module-in-python3