Template:PT3 PYTHON SBC TRM: Difference between revisions

From Phidgets Support
(Created page with "<div class="phd-slide-deck" data-deck="PT3_PYTHON_SBC_TRM"> <div class="phd-slide-deck-header"><h3>Language - Python</h3></div> <div class="phd-slides"> <div class="phd-slide"...")
 
No edit summary
 
Line 23: Line 23:
</p>
</p>


<p>● Networking</p>
<p>● Connection to local Network</p>


<p>● Administrator password</p>
<p>● Administrator password</p>
Line 36: Line 36:


<div class="phd-slide">
<div class="phd-slide">
<h2>Introduction</h2>
<h2>Installing Python</h2>
 
<p>
The quickest way to install Python is through the SBC web interface. Open the Phidget Control Panel on your external machine and click on the
<b>Network Phidgets</b> tab.
</p>


<p>To begin, this video will help you get started:</p>
<p>Double click on your SBC in the device list.</p>


<div class="phd-video-embed">{{#ev:youtube|OhFbGzalBFw}}</div>
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_PYTHON_SBC_TRM/-x260/50/pysbc1.png"></div>
</div>
 
<div class="phd-slide">
<h2>Installing Python</h2>
 
<p>Once you're logged in to the web interface, click on <b>System</b> and <b>Packages</b>.</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_PYTHON_SBC_TRM/-x127/50/pysbc2.png"></div>
 
<p>It may take a minute or two to load this page.</p>
</div>
 
<div class="phd-slide">
<h2>Installing Python</h2>
 
<p>Finally, click on the <b>Install</b> button next to Python Support.</p>
 
<div class="phd-lazy-image" data-src="https://www.phidgets.com/decks/PT3_PYTHON_SBC_TRM/-x152/50/pysbc3.png"></div>
 
<p>It will take a few minutes to install. Once it's complete you can click on the <b>Back</b> button.</p>
</div>
</div>


Line 54: Line 79:
<br />
<br />


<p>
<p>Since the <span>[https://www.phidgets.com/docs/OS_-_Phidget_SBC SBC OS Guide]</span> covers the web interface in detail, this guide will cover SSH.</p>
Since the <span>[https://www.phidgets.com/docs/SBC3003_User_Guide#Getting_Started SBC User Guide]</span> covers the web interface in detail, this guide will
cover SSH.
</p>
</div>
</div>


Line 138: Line 160:


<div class="phd-command-line"><code>scp /path/to/source root@phidgetsbc.local:/path/to/destination</code></div>
<div class="phd-command-line"><code>scp /path/to/source root@phidgetsbc.local:/path/to/destination</code></div>
</div>
<div class="phd-slide">
<h2>Installing Packages For Development</h2>
<p>Installing support for Python has three steps:</p>
<ol>
<li>Ensure Include full Debian Package Repository is checked on the SBC Web Interface (<b>System->Packages</b>)</li>
<li>Install Python</li>
<li>Install Phidget Python module</li>
</ol>
<p>
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.
</p>
</div>
<div class="phd-slide">
<h2>Installing Python</h2>
<p>The base Python functionality can be downloaded and installed in one step:</p>
<div class="phd-command-line"><code>apt-get install python</code></div>
</div>
<div class="phd-slide">
<h2>Installing the Phidgets Module</h2>
<p>Next, you need to install the Phidget Python module. You have three options:</p>
<p>{{SBC_Python_Tabber}}</p>
<p>You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.</p>
</div>
</div>


Line 201: Line 188:
<h2>Setting up a New Project</h2>
<h2>Setting up a New Project</h2>


<p>
<p>When developing on an external computer, you should write and test your programs on that machine. When you are ready, upload your program to the SBC.</p>
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.
</p>
</div>
</div>


Line 248: Line 232:


<p>
<p>
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
To run a Python script as a standalone application or to run it in the web interface, you will need to add a line to the top of the script with the path to your
executable. If you have followed the steps in this guide, the line will be:
Python executable. If you have followed the steps in this guide, it will be:
</p>
</p>



Latest revision as of 22:14, 17 December 2024

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:

● Connection to local Network

● 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.

Installing Python

The quickest way to install Python is through the SBC web interface. Open the Phidget Control Panel on your external machine and click on the Network Phidgets tab.

Double click on your SBC in the device list.

Installing Python

Once you're logged in to the web interface, click on System and Packages.

It may take a minute or two to load this page.

Installing Python

Finally, click on the Install button next to Python Support.

It will take a few minutes to install. Once it's complete you can click on the Back button.

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 OS 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

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 should write and test your programs on that machine. When you are ready, upload your program to the SBC.

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 or to run it in the web interface, you will need to add a line to the top of the script with the path to your Python executable. If you have followed the steps in this guide, it 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.

«
»