I2C Switch Help
26/09/2020
Posted by:
Ermahiho
https://github.com/abelectronicsuk/ABElectronics_Python_Libraries/tree/master/I2CSwitch
I'm able to set the channel 1,2,3,4 but now the main issue that i have is how to read the value on my BH1750 sensors connected to the 4 channels available on the board. With the previous code i was able to read data from my sensor directly connected to the GPIO of the Raspebrry but now i need to pass through the board and after set channel my previous code not work and show error in connection.
How i can ask, one by one, the value of sensors passing through switch board?
Thanks in advance
Here is the code that works perfectly when sensor is connected directly ithout board :
#!/usr/bin/python
import smbus
import time
import requests
# Define some constants from the datasheet
DEVICE = 0x23 # Default device I2C address
POWER_DOWN = 0x00 # No active state
POWER_ON = 0x01 # Power on
RESET = 0x07 # Reset data register value
ONE_TIME_HIGH_RES_MODE = 0x20
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
def convertToNumber(data):
# Simple function to convert 2 bytes of data
# into a decimal number
return ((data[1] + (256 * data[0])) / 1.2)
def readLight(addr=DEVICE):
data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE)
return convertToNumber(data)
def main():
while True:
#print ("Light Level : " + str(readLight()) + " lux")
payload = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight())}
#print(payload);
r = requests.post('http://192.168.1.14/take_params.php', data=payload)
if r.ok :
if r.json()["data"]["response"] == "OK":
print("Dati lux aggiornati");
time.sleep(0.5)
if __name__=="__main__":
main()
26/09/2020
Posted by:
andrew
The code below should switch between each I2C channel and read the sensor values.
I added a channel parameter into the readLight function and used this to set the i2c switch channel. There is a short delay added after switching the i2c channel, this may not be needed.
In the main loop, I duplicated the payload variable and added the channel parameter to each readLight function call.
This should give you an idea of how you can use the I2C switch in your code.
#!/usr/bin/python
import smbus
import time
import requests
from I2CSwitch import I2CSwitch
# Define some constants from the datasheet
DEVICE = 0x23 # Default device I2C address
POWER_DOWN = 0x00 # No active state
POWER_ON = 0x01 # Power on
RESET = 0x07 # Reset data register value
ONE_TIME_HIGH_RES_MODE = 0x20
bus = smbus.SMBus(1) # Rev 2 Pi uses 1
i2cswitch = I2CSwitch(0x70) # I2C switch on address 0x70
def convertToNumber(data):
# Simple function to convert 2 bytes of data
# into a decimal number
return ((data[1] + (256 * data[0])) / 1.2)
def readLight(channel=1, addr=DEVICE):
# Set the I2C channel
i2cswitch.switch_channel(channel)
time.sleep(0.1)
data = bus.read_i2c_block_data(addr,ONE_TIME_HIGH_RES_MODE)
return convertToNumber(data)
def main():
i2cswitch.reset()
while True:
#print ("Light Level : " + str(readLight()) + " lux")
payload1 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=1))}
payload2 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=2))}
payload3 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=3))}
payload4 = {'door_psw':'YLGC202008','id_procedure':'0','id_door':'1','external_lux':str(readLight()),'internal_lux':str(readLight(channel=4))}
#print(payload);
r = requests.post('http://192.168.1.14/take_params.php', data=payload1)
if r.ok :
if r.json()["data"]["response"] == "OK":
print("Dati lux aggiornati")
time.sleep(0.5)
if __name__=="__main__":
main()
27/09/2020
Posted by:
Ermahiho
27/09/2020
Posted by:
Ermahiho
27/09/2020
Posted by:
andrew
Try removing the delay and see if it still works correctly.
Note: documents in Portable Document Format (PDF) require Adobe Acrobat Reader 5.0 or higher to view.
Download Adobe Acrobat Reader or other PDF reading software for your computer or mobile device.