Page 1 of 3

PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Sat Jan 20, 2018 2:14 am
by testpresta2
Hello,

I want to connect a PhidgetInterfaceKit 0/0/4 (4 relays) via USB to a raspberry pi3.

I want to drive the 4 relays with a python program.

Here is what i've done:

$ wget http://www.phidgets.com/downloads/libra ... Python.zip
(I've unzip and changed directory)
$ sudo python setup.py install
At this point, everything is okay, i have no error message.

Here is my very basic python program:

Code: Select all

from Phidget22.Devices.DigitalOutput import *
from Phidget22.Phidget import *

ch = DigitalOutput()
ch.openWaitForAttachment(5000)
ch.close()
I get this error:

Code: Select all

Traceback (most recent call last):
  File "test3.py", line 4, in <module>
    ch = DigitalOutput()
  File "/usr/local/lib/python2.7/dist-packages/Phidget22/Devices/DigitalOutput.py", line 28, in __init__
    __func = PhidgetSupport.getDll().PhidgetDigitalOutput_create
  File "/usr/local/lib/python2.7/dist-packages/Phidget22/PhidgetSupport.py", line 17, in getDll
    PhidgetSupport.__dll = cdll.LoadLibrary("libphidget22.so.0.0.0")
  File "/usr/lib/python2.7/ctypes/__init__.py", line 440, in LoadLibrary
    return self._dlltype(name)
  File "/usr/lib/python2.7/ctypes/__init__.py", line 362, in __init__
    self._handle = _dlopen(self._name, mode)
OSError: libphidget22.so.0.0.0: cannot open shared object file: No such file or directory
Exception OSError: OSError('libphidget22.so.0.0.0: cannot open shared object file: No such file or directory',) in <bound method DigitalOutput.__del__ of <Phidget22.Devices.DigitalOutput.DigitalOutput instance at 0x76bb3f08>> ignored
Maybe this is because libphidget22 is not designed for ARM architecture ? I've read some tutorials about phidget and raspberry pi

I have successfully compiled and installed this library on my raspberry
http://www.phidgets.com/downloads/libra ... get.tar.gz

But this library contains a different version: libphidget21.so

So i do not know what to do.

Thanks

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Sat Jan 20, 2018 11:52 am
by LucasP
You should follow the Getting Started section on Linux OS Page:
https://www.phidgets.com/docs/OS_-_Linu ... with_Linux

After you have the libraries installed correctly, you should then follow the Linux section on the Python page:
https://www.phidgets.com/docs/Language_-_Python#Linux

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Sat Jan 20, 2018 1:29 pm
by testpresta2
Thanks it seems to work

Just one more question: This line can set on/off a relay but is there a way to read the current state of a relay ?

Code: Select all

ch.setState(1)  # ON
ch.setState(0)  # OFF

Thanks

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Sat Jan 20, 2018 4:49 pm
by LucasP
There is a getState method available. Navigate to the API tab on the product page (link below) and select Python from the drop-down list for all available methods and descriptions.

https://www.phidgets.com/?tier=3&catid= ... rodid=1020

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Sun Jan 21, 2018 3:31 am
by testpresta2
Thanks but there is no full example for 4 relay board.

How can i select the relay from the 4 ?

Can i create 4 digitaloutput at the same time this way: ?

Code: Select all

from Phidget22.Devices.DigitalOutput import *
from Phidget22.Phidget import *

# relay 0
ch0 = DigitalOutput()
ch0.openWaitForAttachment(5000)
ch0.setChannel(0)

# relay 1
ch1 = DigitalOutput()
ch1.openWaitForAttachment(5000)
ch1.setChannel(0)

# relay 2
ch2 = DigitalOutput()
ch2.openWaitForAttachment(5000)
ch2.setChannel(0)

# relay 3
ch3 = DigitalOutput()
ch3.openWaitForAttachment(5000)
ch3.setChannel(0)

# Some stuff
ch0.getState()

ch0.setState(1)
ch1.setState(1)
ch2.setState(0)
ch3.setState(0)

time.sleep(1)

ch0.setState(0)
ch1.setState(0)
ch2.setState(1)
ch3.setState(1)

# clean up
ch0.close()
ch1.close()
ch2.close()
ch3.close()

I have not buy the board. I will do it next week but i need to know before if this code is correct and if it will work on raspebrry pi3

Thanks

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Sun Jan 21, 2018 1:58 pm
by LucasP
Under the API tab there is a Software Objects section that will tell you the API to use, and the channels associated with it.
Screen Shot 2018-01-21 at 1.04.04 PM.png
Your code should then look like this:

Code: Select all

...
# relay 0
ch0 = DigitalOutput()
ch0.setChannel(0)
ch0.openWaitForAttachment(5000)

# relay 1
ch1 = DigitalOutput()
ch1.setChannel(1)
ch1.openWaitForAttachment(5000)
...
Note that I am setting the channel before opening. For more information on addressing/channel matching try this page:
https://www.phidgets.com/docs/Phidget_P ... l_Matching

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Mon Jan 22, 2018 12:58 am
by testpresta2
Thanks but the sample you told me does not show a multi relay case.
So i want to be sure it will work...

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Mon Jan 22, 2018 10:42 am
by LucasP
The code sample you provided for the multiple relays was correct, you just need to make the modification I suggested in my previous comment.

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Wed Jan 31, 2018 6:07 am
by testpresta2
I have received my relay boards.

I have tried this code.

When i try to set state to 1 of relay 3 (channel3), the relay 0 (channel0) goes from 1 to 0 whereas i do not touch anything on channel0.

Please note i am maybe changing value too fast. Does the setState method async ? I mean if setState returns something, does it mean the operation is done ?

Any idea ?

Thanks

Re: PhidgetInterfaceKit 0/0/4 USB on raspberry pi3 - Python

Posted: Wed Jan 31, 2018 10:40 am
by LucasP
There are both synchronous and asynchronous versions of setState available, check the API for more information.

If you are setting the state of one channel and another is responding, make sure your channel matching/addressing is correct. Also note that when a channel is closed, it will reset. There is a forum post that describes this: (viewtopic.php?f=7&t=8471)