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:
- 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.
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:
Or, something like this (you will need to know the IP address of your SBC):
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 email@example.com:/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 firstname.lastname@example.org:/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.
The base Python functionality can be downloaded and installed in one step:
apt-get install python
Next, you need to install the Phidget Python module. To do so, you have to main options:
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):
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:
Finally, change directories to the 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. We describe how in the general Using USB Data Keys section. 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.
Setting up a New Project
For simplicity, if you have not used Phidgets before, we recommend first 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.
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.
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 run them.
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
3. Use SSH to access the SBC terminal
4. Navigate to the project folder using the command:
5. You can now run the program with the command:
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:
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
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
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. If you are not using the SBC Web Interface, jump ahead to using a boot script.
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.
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:
Next, edit your crontab file:
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.
If you want your program to run on boot, you can install your program into the boot order using a script. This process is covered here.
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.