Language - Python SBC Linux
Language - Python SBC Linux with Python Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events. |
Getting Started With the Phidget SBC
Welcome to using the Phidget SBC. If you haven't already, check out the user guide in order to set up the following:
- Networking
- Administrator password
If you are ready to go, the first step will be deciding how you will use the SBC:
- Use a more powerful external computer to develop your code, and then simply copy the files to the SBC.
- Use the SBC like any other Linux computer, simply connect a monitor and a keyboard and begin your development.
This guide will cover development using an external machine. For development using the SBC itself, check the Terminal (Linux) page.
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)
The user guide covers the SBC Web Interface in detail, so we will review SSH below.
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 to know the following things about the SBC:
- IP address (e.g. 192.168.3.195) or the link local address (e.g. phidgetsbc.local)
- The administrator password
You must also enable SSH on the SBC. You can do this via the SBC Web Interface which is shown in the image 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):
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:
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, simply open the terminal and type the following:
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:

To copy a file from the SBC to your development machine using SCP, simply open the terminal and type the following:
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:
- Ensure Include full Debian Package Repository is checked on the SBC Web Interface (System->Packages)
- Install Python
- 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.
Basic Python
The base Python functionality can be downloaded and installed in one step:
apt-get install python
Next, install the Phidget Python module.
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
You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.
Use Our Examples
For simplicity, if you have not used Phidgets before, we recommend trying them out directly on an external development machine like a desktop computer. For Python development, check the Command Line (Windows), Terminal (MacOS), and Terminal (Linux) pages.
One of the best ways to start programming with Phidgets is to use our example code as a guide.
Select an example that will work with your Phidget:
When developing on an external computer, you will write, and test your programs on that machine. When you are ready, you will then upload your programs to the SBC to run them.
Follow these steps to get our examples running on an SBC:
1. Download the example files onto the development machine.
2. Using the SBC Web Interface, create a new project:
3. Transfer all the example files from the development machine to the SBC, either using the SBC Web Interface or a tool like WinSCP.
4. Use SSH to access the SBC terminal
5. Navigate to the project folder using the command:
cd /usr/userapps/ProjectName
6. You can now run the program with the command:
python ExampleName.py
Success! The example is running on your SBC. If you aren't interested in developing directly on the Phidget SBC, jump ahead to running a program automatically.
You should now have the example up and running for your device. Your next step is to look at the Editing the Examples section below for information about the example and important concepts for programming Phidgets. This would be a good time to play around with the device and experiment with some of its functionality.
Editing the Examples
To get our example code to run in a custom application, simply remove the calls to AskForDeviceParameters and PrintEventDescriptions, and hard-code the addressing parameters for your application.
If you are unsure what values to use for the addressing parameters, check the Finding The Addressing Information page.
For instance:
channelInfo = AskForDeviceParameters(ch)
ch.setDeviceSerialNumber(channelInfo.deviceSerialNumber)
ch.setHubPort(channelInfo.hubPort)
ch.setIsHubPortDevice(channelInfo.isHubPortDevice)
ch.setChannel(channelInfo.channel)
if(channelInfo.netInfo.isRemote):
ch.setIsRemote(channelInfo.netInfo.isRemote)
if(channelInfo.netInfo.serverDiscovery):
Net.enableServerDiscovery(PhidgetServerType.PHIDGETSERVER_DEVICEREMOTE)
else:
Net.addServer("Server", channelInfo.netInfo.hostname,
channelInfo.netInfo.port, channelInfo.netInfo.password, 0)
Might become:
ch.setDeviceSerialNumber(370114)
ch.setHubPort(2)
ch.setIsHubPortDevice(1)
Notice that you can leave out any parameter not relevant to your application for simplicity.
You can then manipulate the rest of the code as your application requires. A more in-depth description of programming with Phidgets can be found in our guide on Phidget Programming Basics.
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 C library.
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.
To include the Phidget Python library, add the following imports to your code:
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
Then, you will also have to import the class for your particular Phidget. For example, you would include the following line for a DigitalInput:
from Phidget22.Devices.DigitalInput import *
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:
2. Transfer all the example 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
3. Use SSH to access the SBC terminal
4. Navigate to the project folder using the command:
cd /usr/userapps/ProjectName
5. You can now run the program with the command:
python ExampleName.py
Success! The program is running on your SBC. Next, you may want to learn about running a program automatically.
Running a Program Automatically
After testing your program, you will likely want it to run on boot, or on a schedule, without your input.
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
What's Next?
Now that you have set up Phidgets to work with your programming environment, we recommend you read our guide on Phidget Programming Basics to learn the fundamentals of programming with Phidgets.