Language - Python: Difference between revisions

From Phidgets Support
No edit summary
 
(195 intermediate revisions by 9 users not shown)
Line 1: Line 1:
[[Category:Language]]
[[Category:Language]]
[[File:icon-Python.png|64x64px]] Python is an object oriented programming language developed by the [http://www.python.org/psf/summary/ Python Software Foundation], is powerful and easy to learn.
__NOTOC__


__TOC__
We provide support for Python in all major operating systems. We also provide instructions on how to get your project started in a number of common development environments. Select your operating system and preferred development environment below, and follow the instructions to get your project running with Phidgets.


==Introduction==
If you do not know which development environment you want to use, or your development environment of choice is not listed, we recommend starting with command line or terminal as the simplest path to getting your code running.


{{LanguageSupport|Python|the complete Phidget API, including events|all Phidget devices.|Windows, Mac OS X and Linux. We provide instructions on how to set up your environment/compilers for [[#Visual Studio 2005/2008/2010 | Visual Studio 2005/2008/2010]], [[#Visual Studio 2003 | Visual Studio 2003]], [[#MonoDevelop | MonoDevelop]] and the [[#Mono | Mono command line compilers]]|}}
Once you have set up your development environment to run with Phidgets, we recommend you follow our guide on [[Phidget Programming Basics]]. The guide will showcase the fundamentals of programming with Phidgets, with examples in Python.


==Quick Downloads==
== Setup Guide ==


Just need the Python documentation, drivers, libraries, and examples?  Here they are:
<div class="phd-deck-sequence">
 
{{PT3_PYTHON_CHOOSE}}{{PT3_PYTHON_WIN_PYCHARM}}{{PT3_PYTHON_WIN_PYCHARM_1}}{{PT3_PYTHON_WIN_PYCHARM_2}}{{PT3_PYTHON_WIN_PYCHARM_3}}{{PT3_PYTHON_WIN_CMD}}{{PT3_PYTHON_MAC_TRM}}{{PT3_PYTHON_LNX_TRM}}{{PT3_PYTHON_SBC_TRM}}{{PT3_PYTHON_WIN_LIC}}{{PT3_PYTHON_WIN_VS}}{{PT3_PYTHON_WIN_VS_1}}{{PT3_PYTHON_WIN_VS_2}}{{PT3_PYTHON_WIN_IDLE}}
API Documentation:
*[http://www.phidgets.com/documentation/PythonDoc.zip API Manual]
 
Example Code:
 
*[http://www.phidgets.com/downloads/examples/Python_2.1.8.20120131.zip Example Code]
 
Libraries and Drivers:
*[http://www.phidgets.com/downloads/libraries/PhidgetsPython_2.1.8.20120131.zip Python Library]
*[http://www.phidgets.com/downloads/libraries/Phidget-x86_2.1.8.20111220.exe 32-bit Windows Drivers Installer]
*[http://www.phidgets.com/downloads/libraries/Phidget-x64_2.1.8.20111220.exe 64-bit Windows Drivers Installer]
*[http://www.phidgets.com/downloads/libraries/libphidget_2.1.8.20111028.tar.gz Linux Source]
*[http://www.phidgets.com/downloads/libraries/Phidget_2.1.8.20111103.dmg General Mac OS X Drivers Installer]
 
==Getting started with Python==
 
If you are new to writing code for Phidgets, we recommend starting by running, then modifying existing examples. This will allow you to:
{{ExampleCodeReasons}}
 
Instructions are divided up by operating system. Choose:
*[[#Windows(2000/XP/Vista/7)|Windows 2000 / XP / Vista / 7]]
*[[#Mac OS X |Mac OS X]]
*[[#Linux | Linux]] (including PhidgetSBC)
 
==Windows(2000/XP/Vista/7)==
 
===Description of Library Files===
Python programs on Windows depend on the following files, which the installers above put onto your system:
* <b>{{Code|phidget21.dll}}</b> contains the actual Phidgets library, which is used at run-time.  By default, it is placed in {{Code|C:\Windows\System32}}.
You will also need one of the following two files, depending on the .NET framework version you are targeting:
* <b>{{Code|PhidgetsPython}} Module</b> is the Phidgets library for Python <i><b>2.5</b></i> or higher. Your compiler has to know where this file is.
 
If you do not want to use our installer, you can download the five [http://www.phidgets.com/downloads/libraries/Phidget21-windevel_2.1.8.20111220.zip file files].
 
 
Running the examples and writing your own code can be fairly compiler-specific, so we include instructions for each compiler below.
 
===Visual Studio 2005/2008/2010===
 
Microsoft makes free versions of Visual Studio available known as Express Editions.  The Express editions are suitable for most applications, but are limited in features for more complex applications. Please see [http://www.microsoft.com/visualstudio Microsoft Visual Studio] for more information.
 
=====Use Our Examples=====
 
Please start by downloading the [http://www.phidgets.com/downloads/examples/CSharp_2.1.8.20110615.zip examples] and unpack them into a foler.  While these examples were written in Visual Studio 2005 and 2008, Visual Studio 2010 will easily open and upgrade them. To load all projects in Visual Studio, go to File &rarr; Open &rarr; Project, and open {{Code|AllExamples/AllExamples.sln}} or {{Code|AllExamples/AllExamples_vs2008.sln}} for Visual Studio 2005 and 2008, respectively.
 
If you are opening the Phidget examples in Visual Studio 2010, you will need to go through the Visual Studio Conversion Wizard to convert the 2005 or 2008 project.
[[File:VS2005 Conversion Wizard.PNG|link=|alt=Conversion Wizard]]
 
This will load all of the examples available for C#, and then you can set your main project to be the one that matches your device.  If you aren't sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started guide for your device]].
 
The only thing left to do is to run the examples! Click on Debug &rarr; Start Debugging. Please note that the projects, by default try to find the {{Code|Phidget21.NET.dll}} in the {{Code|C:\Program Files\Phidgets}}. If you have it installed in another location, please change the path to the file's location accordingly. If you are receiving an error message regarding that the namespace Phidgets cannot be found, please re-add the reference to {{Code|Phidget21.NET.dll}}. Please see the [[#Write Your Own Code | Write Your Own Code ]] section for details.
 
[[File:CSharp VS2005 Run.PNG|link=|alt=Run‎]]
 
Once you have the C# examples running, we have a [[#Follow The Examples|teaching]] section below to help you follow them.
 
=====Write Your Own Code=====
 
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your environment to properly link the Phidget C# libraries. To begin:
 
1. Generate a new Visual C# Windows Applications project with a descriptive name such as PhidgetTest.
 
[[File:CSharp VS2005 New Project.PNG|link=|alt=New Project]]
 
2. Add a reference to the .NET Phidgets library.
 
[[File:CSharp VS2005 Add Reference.PNG|link=|alt=Add Reference]]
 
3. Under the .NET tab, select {{Code|Phidget21.NET.dll}}.
If you used our installer, these files are installed in {{Code|C:\Program Files\Phidgets}}, by default. If it does not appear in this list, then you can browse to the Phidget Framework installation directory and add the file.
 
[[File:CSharp VS2005 Add Reference 2.PNG|link=|alt=Add Reference]]
 
4. Then, in your code, you will need to include the Phidget library:
 
<div class="source">
<syntaxhighlight lang=csharp>
  using Phidgets;
  using Phidgets.Events;
</syntaxhighlight>
</div>
 
 
The project now has access to the Phidget21 function calls and you are ready to begin coding.
 
The same [[#Follow The Examples|teaching ]] section which describes the examples also has further resources for programming your Phidget.
 
===Visual Studio 2003===
 
=====Use Our Examples=====
 
1. Download the [http://www.phidgets.com/downloads/examples/CSharp_2.1.8.20110615.zip examples] and unpack them into a folder. Here, you can find example programs for all the devices. If you are not sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started guide for your device]]. As the examples were written in newer versions of Visual Studio, Visual Studio 2003 is not able to open the examples. Fortunately, you can import the simple examples to a Visual Studio 2003 project. It will be difficult to import the full examples as you will need to recreate the GUI components. In the [[#Use Our Examples 2 | Use Our Examples]] section, it will be assumed that the simple examples are used. You will need this example source code to be copied into your C# project later on.
 
2. Next, a new project will need to be created. Generate a new Visual C# console application project with a descriptive name such as PhidgetTest.
 
[[File:CSharp_VS2003 New Project.PNG|link=|alt=New Project]]
 
3. Add a reference to the .NET Phidgets library.
 
[[File:CSharp_VS2003 Add Reference 1.PNG|link=|alt=Add Reference]]
 
4. Under the .NET tab, select {{Code|Phidget21.NET1.1.dll}}. If you used our installer, by default, this file is placed in {{Code|C:\Program Files\Phidgets}}. If it is in another location, please change the path to the file's location accordingly.
 
[[File:CSharp_VS2003 Add Reference 2.PNG|link=|alt=Add Reference]]
 
5. To import the simple example program into your project, please: open up {{Code|Class1.cs}}.
 
6. Traverse to the example in Windows Explorer and locate the {{Code|Program.cs}} file.
 
[[File:CSharp VS2003 Source Code.PNG|link=|alt=Source Code]]
 
7. Copy and paste the contents from that file into {{Code|Class1.cs}}.
 
8. Comment out the following line as it is not supported in .NET 1.1:
<div class="source">
<syntaxhighlight lang=csharp>
  using System.Collections.Generic;
</syntaxhighlight>
</div>
</div>


[[File:CSharp VS2003 Source Code 2.PNG|link=|alt=Source Code]]
9. Then, in your code, you will need to include the Phidget library:
<div class="source">
<syntaxhighlight lang=csharp>
  using Phidgets;
  using Phidgets.Events;
</syntaxhighlight>
</div>
10. Now, you can run the example. Click on Debug &rarr; Start.
[[File:CSharp VS2003 Run.PNG|link=|alt=Run]]
Once you have the C# examples running, we have a [[#Follow The Examples|teaching ]] section below to help you follow them.
=====Write Your Own Code=====
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your development environment to properly link the Phidget C# libraries. Please see the [[#Use Our Examples 2 | Use Our Examples ]] section for instructions.
===Mono===
This section will provide instructions on how to compile using the {{Code|mcs}} compiler. Other compilers such as {{Code|gmcs}}, {{Code|smcs}}, and {{Code|dmcs}} all work in the same way. Start by downloading the C# Examples.
=====Use Our Examples=====
Download the [http://www.phidgets.com/downloads/examples/CSharp_2.1.8.20110615.zip examples] and unpack them into a folder. Here, you can find example programs for all the devices. If you aren't sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started guide for your device]]. Please only use the simple examples. The full examples uses Windows Forms, which Mono and the Gtk# toolkit are not completely compatible with. Locate the {{Code|Program.cs}} file as this contains the example source code. Copy the file into your working directory, and rename it to {{Code|example.cs}}.
<br/>
To compile and build an executable, run:
<div class="source">
<syntaxhighlight lang=bash>
mcs /out:example.exe /lib:"C:\Program Files\Phidgets" /r:phidget21.NET.dll example.cs
</syntaxhighlight>
</div>
If you have the {{Code|Phidget21.NET.dll}} installed in another location, please change the path to the file's location accordingly.
Afterwards, you will have an executable named {{Code|example.exe}} that you can run. Place the {{Code|Phidget21.NET.dll}} in the same directory as the executable and type the following to run the program:
<div class="source">
<syntaxhighlight lang=bash>
mono example.exe
</syntaxhighlight>
</div>
Once you have the C# examples running, we have a [[#Follow The Examples|teaching]] section below to help you follow them.
=====Write Your Own Code=====
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your development environment to properly link the Phidget C# libraries. Please see the [[#Use Our Examples 3 | Use Our Example ]] section for instructions.
In your code, you will need to include the Phidget library:
<div class="source">
<syntaxhighlight lang=csharp>
  using Phidgets;
  using Phidgets.Events;
</syntaxhighlight>
</div>
The project now has access to the Phidget21 function calls and you are ready to begin coding.
The same [[#Follow The Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.
===MonoDevelop===
=====Use Our Examples=====
Download the [http://www.phidgets.com/downloads/examples/CSharp_2.1.8.20110615.zip examples] and unpack them into a folder. Here, you can find example programs for all the devices. These examples were written in Visual Studio 2005 and 2008, but are also compatible with MonoDevelop.
To load all projects in MonoDevelop, go to File &rarr; Open, and open {{Code|AllExamples/AllExamples.sln}}
This will load all of the examples available for C#, and then you can set your main project to be the one that matches your device.  If you aren't sure what the software example for your device is called, check the software object listed in the [[Device List | Getting Started guide for your device]]. If you are running under the .NET framework, you can use either the full or simple examples. Otherwise, if you are running under the Mono framework, please only use the simple examples. The full examples uses Windows Forms, which is not completely compatible with Mono's Gtk#.
[[File:CSharp MonoDevelop Win Start Up.PNG|link=|alt=Start Up Project]]
The only thing left to do is to run the examples! Right click the project, and click on {{Code|Run With}} and select the target framework. Please note that the projects, by default try to find the {{Code|Phidget21.NET.dll}} in the {{Code|C\Program Files\Phidgets}}. If you have it installed in another location, please change the path to the file's location accordingly. If you are receiving an error message regarding that the namespace Phidgets cannot be found, please re-add the reference to {{Code|Phidget21.NET.dll}}. Please see the [[#Write Your Own Code 4 | Write Your Own Code]] section for details.
[[File:CSharp MonoDevelop Win Run As.PNG|link=|alt=Run As]]
Once you have the C# examples running, we have a [[#Follow The Examples|teaching]] section below to help you follow them.
=====Write Your Own Code=====
When you are building a project from scratch, or adding Phidget function calls to an existing project, you'll need to configure your  development environment to properly link the Phidget C# libraries. To begin:
1. Create a new C# empty project with a descriptive name such as PhidgetTest.
[[File:CSharp MonoDevelop Win New Project.PNG|link=|alt=New Project]]
2. Add a reference to the .NET library.
[[File:CSharp MonoDevelop Win Reference.PNG|link=|alt=Add Reference]]
3. Select {{Code|Phidget21.NET.dll}}. If you used our installer, by default, this file is placed in {{Code|C:\Program Files\Phidgets}}. If it is in another location, please change the path to the file's location accordingly.
[[File:CSharp MonoDevelop Win Reference 2.PNG|link=|alt=Add Reference]]
4. Then, in your code, you will need to include the Phidget library:
<div class="source">
<syntaxhighlight lang=csharp>
  using Phidgets;
  using Phidgets.Events;
</syntaxhighlight>
</div>
The project now has access to the Phidget21 function calls and you are ready to begin coding.
The same [[#Follow The Examples|teaching]] section which describes the examples also has further resources for programming your Phidget.
==Mac OS X==
Python has excellent support on Mac OS X.
The first step in using Python on Mac is to install the Phidget libraries.  Compile and install them as explained on the [[Device List|getting started guide for your device]].  Then, the [[OS - Mac OS X]] page also describes the different Phidget files, their installed locations, and their roles....
==Linux==
Python has support on Linux. 
The first step in using Python on Linux is to install the Phidget libraries.  Compile and install them as explained on the main [[OS - Linux | Linux page]].  That Linux page also describes the different Phidget files, their installed locations, and their roles.
==Follow The Examples==


By following the instructions for your operating system and compiler above, you probably now have a working example and want to understand it better so you can change it to do what you want.  This teaching section has resources for you to learn from the examples and write your own.
== Quick Downloads ==


Next, comes our API information. These resources outline the Python Phidget functions:
If you already know what you're doing and just need the files, you can find them all below.


{{UsingAPhidgetInCodeGeneral|both of which are available in Python|[http://www.phidgets.com/documentation/PythonDoc.zip Python API]}}
=== Python Module ===
* [https://cdn.phidgets.com/downloads/phidget22/libraries/any/Phidget22Python.zip Phidget Python module]


===Example Flow===
=== Documentation ===


{{ExamplePseudocode|In Python, you can name these '''event''' functions whatever you like. You will then pass them as function pointers to the Phidget library below in the Main Code section.  This hooks them into the actual events when they occur. <br>
*{{Phidget22API}} (select Python from the drop-down menu)
In the example code, the event functions common to all Phidgets are called things like '''AttachHandler()''' and '''DetachHandler()''', etc.<br><br>
Some event functions will be specific to each device, like when a tag is read on an RFID board, or when a sensor value changes on an Interface Kit.
Other functions are given in the examples to show you more detail on using your Phidget.  For example, '''DeviceInitialize()''' will show what needs to be set up for your Phidget before using it.
|Creating a Phidget software object in Python is specific to the Phidget.  For a Phidget Spatial, for example, this would involve creating a {{Code|Spatial}} object.  The examples show how to do this and other API functions.<br><br>
The object provides device specific methods and properties which are available from the API for your specific Phidget.|
[http://www.phidgets.com/documentation/PythonDoc.zip Python API]}}


==Code Snippets==
=== Example Code ===


==Common Problems and Solutions/Workarounds==
*{{SampleCode|Python|Python Examples}}


Here you can put various frequent problems and our recommended solutions.
===Libraries===
{{AllQuickDownloads}}

Latest revision as of 22:06, 17 October 2023


We provide support for Python in all major operating systems. We also provide instructions on how to get your project started in a number of common development environments. Select your operating system and preferred development environment below, and follow the instructions to get your project running with Phidgets.

If you do not know which development environment you want to use, or your development environment of choice is not listed, we recommend starting with command line or terminal as the simplest path to getting your code running.

Once you have set up your development environment to run with Phidgets, we recommend you follow our guide on Phidget Programming Basics. The guide will showcase the fundamentals of programming with Phidgets, with examples in Python.

Setup Guide

Python - Select Development Environment

Select your Development Environment:

Windows

MacOS

Linux

PhidgetSBC

Language - Python

Windows with Pycharm

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

PyCharm is an integrated development environment for Python by JetBrains.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for Windows (see Part 1 of this user guide)

Python (2.7 and 3.6+ are both compatible with Phidgets)

Installing the Phidget Python Module

Select your preferred installation method below:

The recommended way to install the Phidget22 Python module is using the PIP package manager.

«
»

Language - Python

Installing the Phidget Python Module

You can install the Phidget22 libraries for your current PyCharm project with PIP by opening File > Settings

Installing the Phidget Python Module

Next, navigate to Project > Project Interpreter and click on the + symbol located on the right:

Installing the Phidget Python Module

Type Phidget22 into the search bar, select the package named Phidget22 and click Install Package:

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

Click the Download Example button to download the sample code:

Using the Code Samples

Add the example you just downloaded by dragging it into the project:

Using the Code Samples

Finally, run the project:

The project is now running with Phidgets!

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Installing the Phidget Python Module

Python versions 2.7.9+ and 3.4+ include PIP by default. To install the Phidget22 Python module with PIP, simply open the Command Prompt (press the Windows key and search for cmd), and enter the command:

python -m pip install Phidget22

To install the Phidget22 libraries to a specific Python version, you can use the Python Windows Launcher from the Command Prompt as follows (replace -X.X with your Python version, e.g. -2.7 or -3.6):

py -X.X -m pip install Phidget22

Installing the Phidget Python Module

To use the global Phidget22 library in your PyCharm project, select Inherit global site-packages when creating a new project.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

Click the Download Example button to download the sample code:

Using the Code Samples

Add the example you just downloaded by dragging it into the project:

Using the Code Samples

Finally, run the project:

The project is now running with Phidgets!

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Installing the Phidget Python Module

To install the Phidget22 Python module without PIP, you need to download it here:

Phidget22 Python Module

Unpack the Phidget22 Python module and open the Command Prompt (press the Windows key and search for cmd). Find the folder where you downloaded the Python module and enter this command:

python setup.py install

This will build the module and install the Python module files into your site-packages directory.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

Click the Download Example button to download the sample code:

Using the Code Samples

Add the example you just downloaded by dragging it into the project:

Using the Code Samples

Finally, run the project:

The project is now running with Phidgets!

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Windows with Command Line

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

The command line is the default environment to make Python programs in Windows, since it's available immediately after installing Python.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for Windows (see Part 1 of this user guide)

Python (2.7 and 3.6+ are both compatible with Phidgets)

Installing the Phidget Python Module

Python versions 2.7.9+ and 3.4+ include PIP by default. To install the Phidget22 Python module with PIP, simply open the Command Prompt (press the Windows key and search for cmd), and enter the command:

python -m pip install Phidget22

To install the Phidget22 libraries to a specific Python version, you can use the Python Windows Launcher from the Command Prompt as follows (replace -X.X with your Python version, e.g. -2.7 or -3.6):

py -X.X -m pip install Phidget22

If you don't use PIP, follow the manual installation instructions on the next slide.

Installing the Phidget Python Module

To install the Phidget22 Python module without PIP, you need to download it here:

Phidget22 Python Module

Unpack the Phidget22 Python module and open the Command Prompt (press the Windows key and search for cmd). Find the folder where you downloaded the Python module and enter this command:

python setup.py install

This will build the module and install the Python module files into your site-packages directory.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

Click the Download Example button to download the sample code:

Using the Code Samples

To run the example, open the command prompt at the location of the example and enter the following command:

python example.py

The script is now running with Phidgets!

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

MacOS with Terminal

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

The terminal is the default environment to make Python programs in macOS, since it's available immediately after installing Python.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for MacOS (see Part 1 of this user guide)

Python (2.7 and 3.6+ are both compatible with Phidgets)

Installing the Phidget Python Module

Python versions 2.7.9+ and 3.4+ include PIP by default. To install the Phidget22 Python module with PIP, simply open the Terminal and enter the command:

python pip install Phidget22

If you don't use PIP, follow the manual installation instructions on the next slide.

Installing the Phidget Python Module

To install the Phidget22 Python module without PIP, you need to download it here:

Phidget22 Python Module

After unpacking the Phidget Python module, open the terminal (Applications -> Utilities -> Terminal). Locate the folder containing the Python module and enter the following command:

python setup.py install

This will build the module and install the Python module files into your site-packages directory.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

Click the Download Example button to download a Java file with the sample code:

Using the Code Samples

To run the example, open the command prompt at the location of the example and enter the following command:

python example.py

Success! Your program is now running with Phidgets!

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Linux with Terminal

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

The terminal is the default environment to make Python programs in Linux, since it's available immediately after installing Python.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for Linux (see Part 1 of this user guide)

Python (2.7 and 3.6+ are both compatible with Phidgets)

Installing the Phidget Python Module

Python versions 2.7.9+ and 3.4+ include PIP by default. To install the Phidget22 Python module with PIP, simply open the Terminal and enter the command:

Python 2.x :

pip install Phidget22

Python 3.x :

pip3 install Phidget22

If you don't use PIP, follow the manual installation instructions on the next slide.

Installing the Phidget Python Module

To install the Phidget22 Python module without PIP, you need to download it here:

Phidget22 Python Module

After unpacking the Phidget Python module, open the terminal at folder location and enter the following command:

python setup.py install

This will build the module and install the Python module files into your site-packages directory.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

Click the Download Example button to download a Java file with the sample code:

Using the Code Samples

To run the example, open the command prompt at the location of the example and enter the following command:

Python 2.x :

python Python_Example.py

Python 3.x :

python3 Python_Example.py

Success! Your program is now running with Phidgets!

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

PhidgetSBC with Python

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

Requirements

If you haven't already, check out the user guide in order to set up the following:

● Networking

● Administrator password


This guide will cover development using an external machine. For development using the SBC itself, go back and select Terminal - Linux as your environment.

Introduction

To begin, this video will help you get started:

Developing With An External Computer

There are two main ways in which you can access your SBC from an external computer:

● SBC Web Interface

● Secure Shell (SSH)


Since the SBC User Guide covers the web interface in detail, this guide will cover SSH.

SSH

If you are unfamiliar with SSH, it is a simple yet powerful tool that allows you to log into a remote machine in order to execute commands. You can also transfer files using the associated SCP tool.

In order to use SSH, you need the following:

● The SBC's IP address (e.g. 192.168.3.195) or the link local address (e.g. phidgetsbc.local)

● The administrator password

● SSH enabled on the SBC

SSH

You can enable SSH on the SBC Web Interface as shown below:

SSH on Windows

To use SSH on Windows, we recommend PuTTY. Use the images below as a guide for configuring PuTTY (use the IP address or the link local address interchangeably):

SSH on Windows

After clicking open, simply login as root and provide the administrator password:

To transfer files between your SBC and Windows machine, we recommend either of these programs:

WinSCP

PuTTY PSCP

You will follow a similar process to access the SBC as described for SSH.

SSH on Linux and macOS

SSH is available on Linux and macOS by default. To run SSH, open the terminal and type:

ssh root@phidgetsbc.local

Or, something like this (you will need to know the IP address of your SBC):

ssh root@192.168.3.195

You will then be prompted for the password in order to gain access to the SBC:

SSH on Linux and macOS

To copy a file from the SBC to your development machine using SCP, open the terminal and type:

scp root@phidgetsbc.local:/path/to/source /path/to/destination

You can reverse this if you want to transfer a file from your development machine to your SBC:

scp /path/to/source root@phidgetsbc.local:/path/to/destination

Installing Packages For Development

Installing support for Python has three steps:

  1. Ensure Include full Debian Package Repository is checked on the SBC Web Interface (System->Packages)
  2. Install Python
  3. Install Phidget Python module

You will need to run commands on the SBC to install support for Python. You can either use SSH to issue the commands, or you can connect directly to the SBC via a monitor and keyboard.

Installing Python

The base Python functionality can be downloaded and installed in one step:

apt-get install python

Installing the Phidgets Module

Next, you need to install the Phidget Python module. You have three options:

Using PIP

The recommended way to install the Phidget22 Python module is using the PIP package manager.

Python versions 2.7.9+ and 3.4+ include PIP by default.

To install the Phidget22 Python module with PIP, simply run the command:

python -m pip install Phidget22

Manual Install Using the Internet

First, install wget and unzip:

apt-get install wget
apt-get install unzip

Next, copy the web link address for the Python Libraries and use it in the following command (right click to copy into a terminal):

wget http://copied_link

The Phidget Python libraries should now be downloaded in the folder you ran the previous command in. The next step is to unzip the file:

unzip filename

Finally, change directories to the unzipped folder:

cd /path/to/unzipped/folder

and install the Phidget Python libraries:

python setup.py install

Using a USB Key

Copy the Python Libraries onto a USB key. Unpack the zip file into a folder on the USB key. Insert the key into the SBC.

You will have to figure out where the USB key (and the Phidget Python library folder) is now located. Next, run the following commands (be sure to modify the usb directory number if necessary):

cd /media/usb0/
python setup.py install

You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

If it's unclear what any of the options do, click on the nearby '?' for more info.

Once you've made your selections, click the Download Example button under Downloads.

Setting up a New Project

When developing on an external computer, you will write, compile, and test your programs on that machine. When you are ready, you will then upload your programs to the SBC to compile and run them.

Setting up a New Project

Once your code is written, follow these steps to get your program running on the SBC:

1. Using the SBC Web Interface, create a new project:

Setting up a New Project

2. Transfer all the project files from the development machine to the SBC, either using the SBC Web Interface or a tool like WinSCP.

The project directory will be:

/usr/userapps/ProjectName

Setting up a New Project

3. Use SSH to access the SBC terminal and go to the project folder:

cd /usr/userapps/ProjectName

You can now run the program with the command:

python ExampleName.py

Success! The program is running on your SBC.

Running a Program Automatically

To run a Python script as a standalone application, you will need to add a line called a "shebang" to the top of the script, with the path to your Python executable. If you have followed the steps in this guide, the line will be:

#!/usr/bin/python

Click on the sections below for various automation options:

-----

Running a Program from the SBC Web Interface

To quickly test whether a program can be run automatically, you can try starting it from the SBC Web Interface.

1. To start the program, navigate to Projects->ProjectName->Startup Settings in the SBC Web Interface.

2. Select your program in the drop-down menu labeled Executable/Class Name.



3. Click the Start button on the SBC web interface.


4. You'll note that as it runs, there are two links below the Stop button which can be used to view the program output:

  • stdout: view the program output like you would in a terminal or command prompt
  • stderr: view the program error output

Run on Boot

Running on boot ensures that your program will never miss an event. As long as the SBC is running, your code will be running. This section assumes you have written and compiled your program on an external computer, and have uploaded it to the SBC Web Interface.


To have your program run on boot, navigate to Projects->ProjectName->Startup Settings in the SBC Web Interface. After selecting your project, copy the settings from the image below:



We will review some of the options that are shown in the image above:

  • Startup Order: lower numbers boot first. Booting later means more programs are available for use, booting earlier means other programs can use your program.
  • Run as a daemon: starts the program as a daemon. Unless you have explicitly written your program as a daemon, leave this checked, or else your SBC may hang on boot.
  • Executable/Class name: your main Java class or C file.
  • Arguments: any command line arguments the program needs.

After saving your changes, your program will run automatically whenever your SBC boots.


Run on a Schedule

Running your program on a schedule allows you to perform your task once a week, or once a minute without worrying about memory management issues or instability problems that may arise. It executes, and then gets cleaned up. To run your program on a schedule, we recommend using Cron. Cron can automatically schedule programs (known as jobs, or cron jobs). Cron simply reads a crontab file and runs whatever programs are listed, with whatever timing they are listed with. Cron runs continuously in the background, but the cron jobs only run as long as they naturally would, and then they exit.


Let's set up your first cron job. We will use nano to edit the crontab file, but feel free to use whatever editor you prefer.


First, set your editor to nano:

export EDITOR=nano

Next, edit your crontab file:

crontab -e

Finally, schedule your cron job:

#cron job that will run at 5AM every week:
0 5 * * 1 /root/code/myprogram argument1


After entering your task, simply save and exit the file.


What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Windows with LiClipse

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

LiClipse is a closed-source development environment based on Eclipse, with support for Python.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for Windows (see Part 1 of this user guide)

Python (2.7 and 3.6+ are both compatible with Phidgets)

Installing the Phidget Python Module

Python versions 2.7.9+ and 3.4+ include PIP by default. To install the Phidget22 Python module with PIP, simply open the Command Prompt (press the Windows key and search for cmd), and enter the command:

python -m pip install Phidget22

To install the Phidget22 libraries to a specific Python version, you can use the Python Windows Launcher from the Command Prompt as follows (replace -X.X with your Python version, e.g. -2.7 or -3.6):

py -X.X -m pip install Phidget22

If you don't use PIP, follow the manual installation instructions on the next slide.

Installing the Phidget Python Module

To install the Phidget22 Python module without PIP, you need to download it here:

Phidget22 Python Module

Unpack the Phidget22 Python module and open the Command Prompt (press the Windows key and search for cmd). Find the folder where you downloaded the Python module and enter this command:

python setup.py install

This will build the module and install the Python module files into your site-packages directory.

Setting Up a New Project

When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget Python library.

To start, create a new Python project:

Setting Up a New Project

Next, add a new file to the project:

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

If it's unclear what any of the options do, click on the nearby '?' for more info.

Once you've made your selections, click the copy button and paste the code into your new project.

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Windows with Visual Studio

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

Visual Studio is an IDE provided by Microsoft that can be used to develop code in a wide variety of programming languages, including Python.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for Windows (see Part 1 of this user guide)

Python (2.7 and 3.6+ are both compatible with Phidgets)

Installing the Phidget Python Module

Select your preferred installation method below:

The recommended way to install the Phidget22 Python module is using the PIP package manager.

«
»

Language - Python

Installing the Phidget Python Module

You can install the Phidget22 libraries for your current Visual Studio project with PIP by opening View > Other Windows > Python Environments

Installing the Phidget Python Module

Next, select Packages (PyPI) from the drop-down menu:

Installing the Phidget Python Module

Type Phidget22 into the search bar, then click Install Phidget22:

Success! Your project now has access to Phidget22.

Setting Up a New Project

When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget Python library.

To start, create a new Python project:

Setting Up a New Project

Then open the Python file that was generated with the project.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

If it's unclear what any of the options do, click on the nearby '?' for more info.

Once you've made your selections, click the copy button and paste the code into your new project.

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Installing the Phidget Python Module

To install the Phidget22 Python module without PIP, you need to download it here:

Phidget22 Python Module

Unpack the Phidget22 Python module and open the Command Prompt (press the Windows key and search for cmd). Find the folder where you downloaded the Python module and enter this command:

python setup.py install

This will build the module and install the Python module files into your site-packages directory.

Setting Up a New Project

When you are building a project from scratch, or adding Phidget functionality to an existing project, you'll need to configure your development environment to properly link the Phidget Python library.

To start, create a new Python project:

Setting Up a New Project

Then open the Python file that was generated with the project.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

If it's unclear what any of the options do, click on the nearby '?' for more info.

Once you've made your selections, click the copy button and paste the code into your new project.

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»

Language - Python

Windows with IDLE

Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.

IDLE is a cross-platform development environment for Python targeted at newcomers to the language.

WARNING: We do not recommend using Idle with Phidgets. It has known problems dealing with multiple threads, which is a required feature if your program uses events.

Requirements

First, make sure you have the following installed:

● Phidgets Drivers for Windows (see Part 1 of this user guide)

Python (2.7 and 3.6+ are both compatible with Phidgets)

Installing the Phidget Python Module

Python versions 2.7.9+ and 3.4+ include PIP by default. To install the Phidget22 Python module with PIP, simply open the Command Prompt (press the Windows key and search for cmd), and enter the command:

python -m pip install Phidget22

To install the Phidget22 libraries to a specific Python version, you can use the Python Windows Launcher from the Command Prompt as follows (replace -X.X with your Python version, e.g. -2.7 or -3.6):

py -X.X -m pip install Phidget22

If you don't use PIP, follow the manual installation instructions on the next slide.

Installing the Phidget Python Module

To install the Phidget22 Python module without PIP, you need to download it here:

Phidget22 Python Module

Unpack the Phidget22 Python module and open the Command Prompt (press the Windows key and search for cmd). Find the folder where you downloaded the Python module and enter this command:

python setup.py install

This will build the module and install the Python module files into your site-packages directory.

Finding Code Samples

To find the code sample to use for your Phidget, navigate to the Code Samples page and select your device from the drop-down menu.

Once you select your device, the code sample generator will give you a working code sample, and a selection of options to customize it to your needs.

Using the Code Samples

If it's unclear what any of the options do, click on the nearby '?' for more info.

Once you've made your selections, click the copy button.

Using the Code Samples

Paste the sample code into a new Python script file in IDLE:

Using the Code Samples

To run the example, simply click on Run and select Run Module:

What's Next?

Now that you've set up Phidgets in your programming environment, you should read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.

Continue reading below for advanced information and troubleshooting for your device.

«
»


Quick Downloads

If you already know what you're doing and just need the files, you can find them all below.

Python Module

Documentation

Example Code

Libraries