I2C Python readBytes
I am trying to use Python i2c module with a IR temperature sensor of address 0x5A.
i2cdetect -y 0 detects 0x5a correctly
i2cget -y 0 0x5a 0x06 reads the value of ambient temperature correctly
i2cget -y 0 0x5a 0x07 reads the value of object temperature correctly.
I installed the foll
opkg install python-light pyOnionI2C
The expansion and I2C module version is 0.9-1
My code is as follows
from OmegaExpansion import onionI2C
i2c = onionI2C.OnionI2C()
amb_temp = i2c.readBytes(0x5a, 0x6, 1)
obj_temp = i2c.readBytes(0x5a, 0x7, 1)
print (amb_temp) , (obj_temp)
I keep getting ,
Request some help in making the python module work.
Please check if your device uses the SMBus protocol or I2C.
SMBus and I2C can be interchangeable, but there are hardware and software differences between the two buses that can lead to incompatibility.
The SMBus & I2C incompatibility looks to be causing the issues you're seeing here.
The command-line I2C tools were written to support SMBus and basic I2C (see the source code here). While the Onion I2C library was only written with I2C in mind.
This would explain why the command-line tools are able to read your sensor, and the onion I2C library cannot.
Two ways you can get around this:
1) Use the python smbus package
Install the smbus module:
- First you'll need to enable the openwrt package repos, see this article for details: http://docs.onion.io/omega2-docs/using-opkg.html#using-opkg-switch-to-lede-repos
- Then run
- And then
opkg list | grep -i smbus
You can then choose between the python2 and python3 smbus module
For references on how to use the python smbus module, you can check out these resources:
2) Use the command-line tools within python
This would involve changing your python program to run the i2c-get command and read the output as a string.
See this resource for more details: https://cmdlinetips.com/2014/03/how-to-run-a-shell-command-from-python-and-get-the-output/