Compiling Phidget Relay control example in Python

Source: Internet
Author: User
Tags documentation sleep dmesg in python

During the past few days, we found Phidget, which was given by the client of the previous case. The model is 1014_2. I remember it was used for stress testing.
I forgot how to use it too long, so I will try again.
.
Because the Ubuntu 16.04 system is reinstalled, you can install phidget on the Linux platform from the beginning.
For details, refer to the Phidget official website.

Build a Linux environment

Install libusb development Library

Sudo apt-get install libusb-1.0-0-dev

Install the Phidget Library

Click the download path to download the Phidget Library.

After decompression, execute the following commands in sequence:

./Configure
Make
Sudo make install

Check the installation of the Phidget Library

Software:

Download official examples

Decompress the package and compile HelloWorld. c.

Gcc HelloWorld. c-o HelloWorld-lphidget21

Run HelloWorld


Jasper @ jasper :~ /Phidget/phidget21-c-examples-2.1.8.20151217 $ sudo./HelloWorld
Opening...
Phidget Simple Playground (plug and unplug devices)
Press Enter to end anytime...
Hello Device Phidget InterfaceKit 0/0/4, Serial Number: 381507
Goodbye Device Phidget InterfaceKit 0/0/4, Serial Number: 381507

Closing...

Normally, the Hello/Goodbye information is displayed along with the insertion/removal of the phidget device.
Hardware:

Use kernal log to view the insert/unplug information:
Insert:


Jasper @ jasper :~ $ Dmesg | tail
......
[17239.182460] usb 2-1.4: new low-speed USB device number 13 using ehci-pci
......
Unplugging:


Jasper @ jasper :~ $ Dmesg | tail
......
[17262.852520] usb 2-1.4: USB disconnect, device number 13
......
Under normal circumstances, the device number that uses ehci-pci during insertion will be disconnect when it is unplugged.
Install python package of Phidget
Download and decompress the python package of phidget
1

Jasper @ jasper :~ /Phidget/PhidgetsPython $ sudo python setup. py install
You can download the official example for verification.
Now, Phidget has been installed in the Python environment on the Linux platform.
Use Phidget
For Python APIs, refer to the official API documentation.
Step 1: initialize and enable the device

Self. device = InterfaceKit ()
Self. device. openPhidget ()

Step 2: wait for access from the phidget device

Self. device. waitForAttach (10000)

Step 3: perform operations on the device

Self. device. setOutputState (output, 1)

Step 4: disable the device

Self. device. closePhidget ()

Actual operation

Materials on hand

Phidget board: The model is 1014_2 (actually four relay) and can be used as a switch control line.
Xiaomi fan: Available on Xiaomi official website, 19 items.
USB cable: longer cables are better, saving you a lot of trouble.
Micro USB cable: 10 years later, Phidget 1014_2 was connected by Micro USB.
Practice
Because it is only to control the switch of the fan, we only need to use the power supply positive level and grounding for the four USB lines, because the data section is not used, so D ++/D-will not be taken care.
Cut the USB small end and pull out the red line and black line. Cut down the red line in half. Connect the Phidget and fan as shown in the following figure. It is troublesome to connect the USB of the fan here. It is much easier to extend the cable, but here I will be one, so I cannot afford to waste it .... Connect the two USB cables to the fan, and the two are used for data transmission.

The overall effect after the connection is shown in the figure:

The control code is as follows:


Import time

From Phidgets. PhidgetException import *
From Phidgets. Devices. InterfaceKit import *

Class TestPhidget (object ):
Def _ init _ (self ):
Pass

Def _ enter _ (self ):
Try:
Self. device = InterfaceKit ()
Self. device. openPhidget ()
Self. device. waitForAttach (10000)
Return self
Except t PhidgetException, e:
Exit (1)

Def relayOn (self, output ):
Self. device. setOutputState (output, 1)
Time. sleep (1)

Def relayOff (self, output ):
Self. device. setOutputState (output, 0)
Time. sleep (1)

Def _ exit _ (self, e_t, e_v, t_ B ):
Try:
Self. device. closePhidget ()
Except t PhidgetException, e:
Exit (1)

Def test ():
Import optparse

Parser = optparse. OptionParser ()
Parser. add_option ("-n ",
Dest = "counts ",
Action = "store ",
Help = "Number of test counts"
 )
   
(Options, args) = parser. parse_args ()
Counts = int (options. counts)

With TestPhidget () as testphidget:
Time. sleep (1)
For I in range (0, counts ):
Testphidget. relayOff (3)
Time. sleep (7)
Testphidget. relayOn (3)
Time. sleep (5)

If _ name _ = '_ main __':
Test ()
All of these can be found in the official API documentation.
The effect is to control the switch of the fan. That's all. (boring @@)
Conclusion
For more information about Phidget, visit the official website.
If you want to understand the principles of 1014_2, refer to here. As long as you have studied high school physics, I think there should be no problem.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.