I2C Python readBytes
-
Hello
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 update
opkg install python-light pyOnionI2CThe expansion and I2C module version is 0.9-1
My code is as follows
import os
from OmegaExpansion import onionI2C
i2c = onionI2C.OnionI2C()while True:
amb_temp = i2c.readBytes(0x5a, 0x6, 1)
obj_temp = i2c.readBytes(0x5a, 0x7, 1)
print (amb_temp) , (obj_temp)I keep getting [255],[255]
Request some help in making the python module work.Thanks
Ajit
-
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
opkg update
- 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:
- https://learn.sparkfun.com/tutorials/python-programming-tutorial-getting-started-with-the-raspberry-pi/experiment-4-i2c-temperature-sensor
- https://git.kernel.org/pub/scm/utils/i2c-tools/i2c-tools.git/tree/py-smbus
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/