I have several units build from scratch to work in my home domotica.
All my units work with the I²C pins (SDA/SCL)
You can check if a device is present on the bus by using the i2cdetect program from the i2ctools package- i2cdetect 0 -y(raspberry pi R1) or i2cdetect 1 -y (raspberry pi R2).
Now my question is if it is possible to make a python code that first checks if there are units connected on the I²C pins and if so show it on screen.
Thx.
Re: Make controle script.
you could try something like this, just a bunch of try/except statements:
Code: Select all
locations=['locations that you'd scan for addresses', 'end']
bus = smbus.SMBus(1)
for device in locations:
try:
(commands to try location on the i2c bus.)
break
except:
try:
another try statement stuff to try to search the i2c bus
except:
try:
and on and on....
if device == 'end':
print "Nothing found, Please check cable connections."
exit()
If it isnt smoking and blown into pieces, it's not pushed far enough yet. 
http://cae2100.wordpress.com

http://cae2100.wordpress.com
Re: Make controle script.
I don't realy understand your code.
Maybe you have a working example?
Maybe you have a working example?
Re: Make controle script.
no working example, I was just giving a concept. you have a list of all addresses used by the i2c, and what the code does is search through them using a for loop to loop through all of the combinations, and if it finds one, you can assign a varable to 1 or something like that to tell that the location is being used, then after all locations have been checked, it'd print out whatever locations it shows something at.
If it isnt smoking and blown into pieces, it's not pushed far enough yet. 
http://cae2100.wordpress.com

http://cae2100.wordpress.com
Re: Make controle script.
In your code
The word 'end' do you mean the last address or just the word 'end' ?
Code: Select all
locations=['locations that you'd scan for addresses', 'end']
Re: Make controle script.
just the word end, I use it as kinda a stopper for my projects. here's an example of what I mean, this is for auto searching and connecting to a serial port so it works on any system.
Code: Select all
import serial
locations=['/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3','/dev/ttyACM4', '/dev/ttyACM5','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyUSB4', '/dev/ttyUSB5', '/dev/ttyUSB6', '/dev/ttyUSB7', '/dev/ttyUSB8', '/dev/ttyUSB9', '/dev/ttyUSB10', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'com10', 'com11', 'com12', 'com13', 'com14', 'com15', 'com16', 'com17', 'com18', 'com19', 'com20', 'com21', 'com1', 'end']
for device in locations:
try:
#print "Trying...",device
serialport = serial.Serial(device, 2400, timeout = 0)
break
except:
#print "Failed to connect on",device
if device == 'end':
print "Unable to find Serial Port, Please plug in cable or check cable connections."
exit()
If it isnt smoking and blown into pieces, it's not pushed far enough yet. 
http://cae2100.wordpress.com

http://cae2100.wordpress.com
Re: Make controle script.
Hmm, as long as it works... But might be better to code without the 'end' string - something along this:
...just to avoid doing the call serial.Serial('end', ...)
Code: Select all
import serial
locations=['/dev/ttyACM0', '/dev/ttyACM1','/dev/ttyACM2', '/dev/ttyACM3','/dev/ttyACM4', '/dev/ttyACM5','/dev/ttyUSB0','/dev/ttyUSB1','/dev/ttyUSB2','/dev/ttyUSB3', '/dev/ttyUSB4', '/dev/ttyUSB5', '/dev/ttyUSB6', '/dev/ttyUSB7', '/dev/ttyUSB8', '/dev/ttyUSB9', '/dev/ttyUSB10', 'com2', 'com3', 'com4', 'com5', 'com6', 'com7', 'com8', 'com9', 'com10', 'com11', 'com12', 'com13', 'com14', 'com15', 'com16', 'com17', 'com18', 'com19', 'com20', 'com21', 'com1']
deviceFound = False
for device in locations:
try:
#print "Trying...",device
serialport = serial.Serial(device, 2400, timeout = 0)
deviceFound = True
break
except:
#print "Failed to connect on",device
if not deviceFound:
print "Unable to find Serial Port, Please plug in cable or check cable connections."
exit()
http://raspberrycompote.blogspot.com/ - Low-level graphics and 'Coding Gold Dust'