Language - Python SBC Linux: Difference between revisions

From Phidgets Support
No edit summary
(Redirected page to Language - Python)
 
(8 intermediate revisions by 2 users not shown)
Line 1: Line 1:
[[Category:Language]]
#REDIRECT [[Language - Python]]
{{NoTitle}}
{{Language_-_Python_Dev_Environment_Table}}
{|
|style="vertical-align:middle; width: 60%;"|
<font size=6>'''Language - Python'''
 
'''SBC Linux with Python'''</font>
 
Welcome to using Phidgets with Python! By using Python, you will have access to the complete Phidget22 API, including events.
 
|{{TOC limit|2}}
|}
==Getting Started With the Phidget SBC==
Welcome to using the Phidget SBC. If you haven't already, check out the [[SBC3003 User Guide#Getting Started | 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 [[Language_-_Python_Linux_Terminal|Terminal (Linux)]] page.
 
To begin, this video will help you get started:
<center>{{#ev:youtube|y7XhShmhiEU}}</center>
 
 
===Developing with an External Computer===
{{SBC_Developing_with_an_External_Computer}}
 
 
===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:
<syntaxhighlight lang=bash>
apt-get install python
</syntaxhighlight>
 
Next, install the Phidget Python module.
 
=====Method 1: Use the Internet=====
First, install wget and unzip:
<syntaxhighlight lang=bash>
apt-get install wget
apt-get install unzip
</syntaxhighlight>
 
Next, copy the web link address for the [{{SERVER}}/downloads/phidget22/libraries/any/Phidget22Python.zip Python Libraries] and use it in the following command (right click to copy into a terminal):
<syntaxhighlight lang=bash>
wget http://copied_link
</syntaxhighlight>
 
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:
<syntaxhighlight lang=bash>
unzip filename
</syntaxhighlight>
 
Finally, change directories to the unzipped folder:
<syntaxhighlight lang=bash>
cd /path/to/unzipped/folder
</syntaxhighlight>
and install the Phidget Python libraries:
<syntaxhighlight lang=bash>
python setup.py install
</syntaxhighlight>
 
You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.
 
=====Method 2: Use a USB Key=====
 
Copy the [{{SERVER}}/downloads/phidget22/libraries/any/Phidget22Python.zip 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 | Using USB Data Keys]] section. Next, run the following commands (be sure to modify the usb directory number if necessary):
<syntaxhighlight lang=bash>
cd /media/usb0/
python setup.py install
</syntaxhighlight>
 
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 [[Language_-_Python_Windows_Command_Line|Command Line (Windows)]], [[Language_-_Python_macOS_Terminal|Terminal (MacOS)]], and [[Language_-_Python_Linux_Terminal|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:
*{{SampleCode|Python|Python Examples}}
 
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:
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
 
3. Transfer all the example files from the development machine to the SBC, either using the SBC Web Interface or a tool like [https://winscp.net/eng/download.php WinSCP].
 
4. Use [[#SSH|SSH]] to access the SBC terminal
 
5. Navigate to the project folder using the command:
 
<syntaxhighlight lang=bash>
cd /usr/userapps/ProjectName
</syntaxhighlight>
 
6. You can now run the program with the command:
 
<syntaxhighlight lang=bash>
python ExampleName.py
</syntaxhighlight>
 
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|running a program automatically]].
 
{{Edit_the_Examples}}
 
{{Language_-_Python_Editing_The_Examples}}
 
==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:
 
<syntaxhighlight lang=python>
from Phidget22.PhidgetException import *
from Phidget22.Phidget import *
</syntaxhighlight>
 
Then, you will also have to import the class for your particular Phidget. For example, you would include the following line for a DigitalInput:
 
<syntaxhighlight lang=python>
from Phidget22.Devices.DigitalInput import *
</syntaxhighlight>
 
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:
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
 
2. Transfer all the example files from the development machine to the SBC, either using the SBC Web Interface or a tool like [https://winscp.net/eng/download.php WinSCP]. The project directory will be <code>/usr/userapps/ProjectName</code>
 
3. Use [[#SSH|SSH]] to access the SBC terminal
 
4. Navigate to the project folder using the command:
 
<syntaxhighlight lang=bash>
cd /usr/userapps/ProjectName
</syntaxhighlight>
 
5. You can now run the program with the command:
 
<syntaxhighlight lang=bash>
python ExampleName.py
</syntaxhighlight>
 
Success! The program is running on your SBC. Next, you may want to learn about [[#Running a program automatically|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:
 
<syntaxhighlight lang=python>
#!/usr/bin/python
</syntaxhighlight>
 
{{SBC_Running_A_Program_Automatically}}
 
{{Language Page What's Next}}

Latest revision as of 16:45, 15 March 2021

Redirect to: