OS - Phidget SBC: Difference between revisions

From Phidgets Support
mNo edit summary
No edit summary
Line 161: Line 161:
You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.
You're now ready to begin programming! Continue through this guide for code examples and directions on where to go next.


==Programming with the Phidget SBC==
==Developing applications with the Phidget SBC==
===Writing a Phidget Program===
If you are interested in developing on an external computer, keep reading. If you are planning on developing directly on the SBC, you can jump ahead to [[#Developing directly on the Phidget SBC | developing directly on the SBC]].


There are three ways to write and upload a Phidget program:
===Developing on an external computer===
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. To demonstrate, we will use the Hello World example for Java. Download the following on your development machine before we get started:
*[{{SERVER}}/downloads/examples/JavaJNI.zip Java example package]
*[{{SERVER}}/downloads/examples/phidget22.jar phidget22.jar]


#Using the SBC Web Interface:
#*This is useful for simple projects written in C or Java that you want to start at boot.
#Using SSH '''OR''' Using the SBC via keyboard and monitor
#*Useful for projects that run at scheduled times (e.g. once per minute).
#*Useful for projects that use languages other than Java or ARM-compiled C.
#*Can also be used to start programs at boot, however, a startup script is needed.


If you aren't sure which method you prefer, keep reading. Otherwise, you can jump ahead here:
Next, follow these steps:
*Jump ahead to [[#Program with SSH|programming with SSH]].
*Jump ahead to [[#Developing Code on the SBC|developing code on the SBC]].


====Program in Java with the Web Interface====
1. Place phidget22.jar on your development machine in a directory that you will use to compile your Java files.
To show how to write, compile, and install Java programs on the SBC, we'll use the Hello World example for Java. Download the [{{SERVER}}/downloads/examples/JavaJNI.zip Java example package] and make sure you have the same version of Java on both your development machine and on the SBC (how to check your version number was covered [[#Installing C/C++ and Java| above]]).


The first step to getting the HelloWorld code running on on your SBC starts with your external development machine:
2. Copy the HelloWorld.java file from the example package to the same directory.
#Download [{{SERVER}}/downloads/examples/phidget22.jar phidget22.jar]
 
#Place the SBC version of phidget22.jar on your development machine in the directory that you will use to compile the Java files.
3. Compile the HelloWorld.java file. If you are using a Windows machine, type the following into the command prompt:
#Copy the HelloWorld.java file into that working directory.
<syntaxhighlight lang=bash>
#Compile the HelloWorld.java file from within that working directory. From the command line prompt on Windows, this will be:
javac -classpath .;phidget22.jar HelloWorldExample.java
#:<syntaxhighlight lang=bash>
javac -classpath .;phidget22.jar HelloWorld.java
</syntaxhighlight>
</syntaxhighlight>
#:In a terminal on Linux or Mac OS, this will be:
If you are using a Linux or macOS machine, type the following into the terminal:
#:<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
javac -classpath .:phidget22.jar HelloWorld.java
javac -classpath .:phidget22.jar HelloWorldExample.java
</syntaxhighlight>
</syntaxhighlight>
#You should now have three compiled class files:
 
#*HelloWorld.class
You should now have the following class file:
#*HelloWorld$1.class
*HelloWorldExample.class
#*HelloWorld$2.class
 
#Using the SBC Web Interface, create a new project called HelloWorld:
 
#:[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]
4. Using the SBC Web Interface, create a new project called HelloWorld:
#On the next screen, you will be prompted to upload your files. We will upload the three Java class files, and then click the Start button:
#:[[File:Phidgetsbc_runprogram.PNG|link=|alt=|center]]
#You'll note that as it runs, there are two links below the Stop button:
#*stdout: view the program output like you would in a terminal or command prompt
#*stderr: view the program error output




Success! The HelloWorld example is running on your SBC. If you aren't interested in programming with SSH, or developing directly on the Phidget SBC, jump ahead to [[#Running a program automatically|running a program automatically]].
[[File:Phidgetsbc_createproject.PNG|link=|alt=|center]]


====Program with SSH====
To show how to write, compile, and install Java programs on the SBC, we'll use the Hello World example for Java. Make sure you have Java installed before starting (this was covered [[#Installing C/C++ and Java| above]]).


#Connect to the SBC using SSH.
5. On the next screen, you will be prompted to upload your files. We will upload the Java class file, and then click the Start button:
#Download the [{{SERVER}}/downloads/examples/JavaJNI.zip Phidget Java Examples] to the SBC.
 
#:<syntaxhighlight lang=bash>
 
wget http://copied_link
[[File:Phidgetsbc_runprogram.PNG|link=|alt=|center]]
</syntaxhighlight>
 
#Unpack the examples
 
#:<syntaxhighlight lang=bash>
6. You'll note that as it runs, there are two links below the Stop button:
unzip filename
*stdout: view the program output like you would in a terminal or command prompt
</syntaxhighlight>
*stderr: view the program error output
#phidget22.jar is located at:
 
#:{{Code|/usr/share/java/phidget22.jar}}
#Compile the HelloWorld.java example:
#:<syntaxhighlight lang=bash>
javac -classpath .:/usr/share/java/phidget22.jar HelloWorld.java
</syntaxhighlight>
#To run the HelloWorld program, use:
#:<syntaxhighlight lang=bash>
java -classpath .:/usr/share/java/phidget22.jar HelloWorld
</syntaxhighlight>


Success! The HelloWorld 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]].
Success! The HelloWorld 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]].


====Developing Code on the SBC====
===Developing directly on the Phidget SBC===
When you're developing directly on the SBC, you will need to use a terminal editor:
When developing directly on the SBC, you will do so in one of the following ways:
*nano - ''recommended for beginners''  
*Access the terminal by connecting directly to the SBC using a keyboard and monitor.
*vi  
*Access the terminal by using SSH.
*emacs - ''needs to be installed''
 
Regardless of how you choose to access the terminal, the process is the same. First, you will need to pick a terminal editor in order to write and edit your files. Here are some options we recommend:
To install emacs, run this command on the SBC (ensure ''Include full Debian Package Repository'' is checked on the SBC Web Interface in System->Packages):
*nano - ''recommended for beginners, already installed''  
*vi - ''advanced, already installed''
*emacs - ''advanced, needs to be installed''
 
Next, follow these steps:
 
1. If you haven't already, connect to the SBC using SSH or a keyboard and monitor.
 
2. Download the [{{SERVER}}/downloads/examples/JavaJNI.zip Phidget Java Examples] to the SBC.
<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
apt-get install emacs
wget http://examples_link
</syntaxhighlight>
</syntaxhighlight>


Other than creating/editing files directly on the terminal, your experience will be identical to someone using SSH, so if you haven't read the section directly above, do so now.
3. Unpack the examples
<syntaxhighlight lang=bash>
unzip examples_filename
</syntaxhighlight>


===Running a Program Automatically===
4. Compile the HelloWorldExample.java example:
After testing your program, you will likely want it to run on boot, or on a schedule, without your input.  
<syntaxhighlight lang=bash>
javac -classpath .:/usr/share/java/phidget22.jar HelloWorldExample.java
</syntaxhighlight>


Running on boot:
5.Run the HelloWorldExampleprogram:
*Never miss an event. As long as the SBC is running, your code will be running.  
<syntaxhighlight lang=bash>
java -classpath .:/usr/share/java/phidget22.jar HelloWorldExample
</syntaxhighlight>


Running on a schedule:
Success! The HelloWorld example is running on your SBC. Now that you have a program running on your SBC, you may be interested in having it run automatically when the SBC boots, or maybe even on a schedule. Keep reading for more information.
*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.


If you have made your choice and want to run your program on a schedule, jump ahead to [[#Run on a schedule| running on a schedule]]. Otherwise, keep reading to learn how to run on boot!
===Running a program automatically===
After testing your program, you will likely want it to run on boot, or on a schedule, without your input.  


====Run on boot====
====Run on boot====
This section deals with programs written in either Java or ARM-compiled C. If you are using a different type of program, jump ahead to [[#Via a Boot Script|using a boot script]].
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 deals with programs written in either Java or C/C++. If you are using a different type of program, jump ahead to [[#Via a Boot Script|using a boot script]].
 


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




Line 268: Line 260:
*Startup Order: lower numbers boot first. Booting later means more programs are available for use, booting earlier means other programs can use your program.   
*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.
*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
*Executable/Class name: your main Java class or C/C++ file.
*Arguments: any command line arguments the program needs
*Arguments: any command line arguments the program needs.
 
After saving your changes, your program will run automatically whenever your SBC boots.


'''Warning:''' Ensure your program is stable before taking the above steps. A program that is unstable (i.e. has memory leaks or similar) will cause your SBC to crash after running for days, or months on end.
 
'''Warning:''' Ensure your program is stable before taking the above steps. A program that is unstable (i.e. has memory leaks or similar) will eventually cause your SBC to crash.


====Run on a schedule====
====Run on a schedule====
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.
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!
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.


#Set your editor to nano:  
 
#:<syntaxhighlight lang=bash>
First, set your editor to nano:  
<syntaxhighlight lang=bash>
export EDITOR=nano
export EDITOR=nano
</syntaxhighlight>
</syntaxhighlight>
#Edit your crontab file:
Next, edit your crontab file:
#:<syntaxhighlight lang=bash>
<syntaxhighlight lang=bash>
crontab -e
crontab -e
</syntaxhighlight>
</syntaxhighlight>
#Schedule your cron job
Finally, schedule your cron job:
#:<syntaxhighlight lang=text>
minute hour dayOfMonth month dayOfWeek  YOUR_COMMAND
</syntaxhighlight>
 
Done! Here is an example of a cron job that will run at 5 AM every week:
<syntaxhighlight lang=text>
<syntaxhighlight lang=text>
#cron job that will run at 5AM every week:
0 5 * * 1 /root/code/myprogram argument1
0 5 * * 1 /root/code/myprogram argument1
</syntaxhighlight>
</syntaxhighlight>
[[File:phidgetsbc_cronjob.PNG|link=|alt=|center]]
After entering your task, simply save and exit the file.


====Run using a boot script====
====Run using a boot script====

Revision as of 23:18, 19 May 2017

Quick Downloads

Already know what you're doing? Here you go:

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 the SBC like any other Linux computer, simply connect a monitor and a keyboard and begin your development.
  • Use a more powerful external computer to develop your code, and then simply copy the files to the SBC.


If you will not be using an external computer, jump ahead to learn about installing packages for development, otherwise, keep reading!

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.1.123) 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@168.254.3.0

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

At this point you have connected to the SBC through one or more these three options:

  • SBC Web Interface
  • SSH
  • Directly via monitor and keyboard

Now that you are connected, you may want to start developing on/for the SBC. Before you do this, you need to install some packages. Let's start with C/C++ and Java.

C/C++ and Java

The simplest way to install C/C++ and Java support on the SBC is via the install buttons on located on the SBC Web Interface (System->Packages). Check Include full Debian Package Repository before installing.


When developing for Java, ensure your development machine and your SBC have the same version of Java. Check your Java version by entering this command:

Java -version

If you need to update the version of Java on your SBC, use the following commands:

apt-get install openjdk-7-jre-headless
su
update-alternatives --config java

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

Installing Python

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.

Basic Python

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

apt-get install python

Next, install the Phidget Python module.

Method 1: Use 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

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

Developing applications with the Phidget SBC

If you are interested in developing on an external computer, keep reading. If you are planning on developing directly on the SBC, you can jump ahead to developing directly on the SBC.

Developing on an external computer

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. To demonstrate, we will use the Hello World example for Java. Download the following on your development machine before we get started:


Next, follow these steps:

1. Place phidget22.jar on your development machine in a directory that you will use to compile your Java files.

2. Copy the HelloWorld.java file from the example package to the same directory.

3. Compile the HelloWorld.java file. If you are using a Windows machine, type the following into the command prompt:

javac -classpath .;phidget22.jar HelloWorldExample.java

If you are using a Linux or macOS machine, type the following into the terminal:

javac -classpath .:phidget22.jar HelloWorldExample.java

You should now have the following class file:

  • HelloWorldExample.class


4. Using the SBC Web Interface, create a new project called HelloWorld:



5. On the next screen, you will be prompted to upload your files. We will upload the Java class file, and then click the Start button:



6. You'll note that as it runs, there are two links below the Stop button:

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


Success! The HelloWorld 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.

Developing directly on the Phidget SBC

When developing directly on the SBC, you will do so in one of the following ways:

  • Access the terminal by connecting directly to the SBC using a keyboard and monitor.
  • Access the terminal by using SSH.

Regardless of how you choose to access the terminal, the process is the same. First, you will need to pick a terminal editor in order to write and edit your files. Here are some options we recommend:

  • nano - recommended for beginners, already installed
  • vi - advanced, already installed
  • emacs - advanced, needs to be installed

Next, follow these steps:

1. If you haven't already, connect to the SBC using SSH or a keyboard and monitor.

2. Download the Phidget Java Examples to the SBC.

wget http://examples_link

3. Unpack the examples

unzip examples_filename

4. Compile the HelloWorldExample.java example:

javac -classpath .:/usr/share/java/phidget22.jar HelloWorldExample.java

5.Run the HelloWorldExampleprogram:

java -classpath .:/usr/share/java/phidget22.jar HelloWorldExample

Success! The HelloWorld example is running on your SBC. Now that you have a program running on your SBC, you may be interested in having it run automatically when the SBC boots, or maybe even on a schedule. Keep reading for more information.

Running a program automatically

After testing your program, you will likely want it to run on boot, or on a schedule, without your input.

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 deals with programs written in either Java or C/C++. If you are using a different type of program, 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/C++ file.
  • Arguments: any command line arguments the program needs.

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


Warning: Ensure your program is stable before taking the above steps. A program that is unstable (i.e. has memory leaks or similar) will eventually cause your SBC to crash.

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.

Run using a boot script

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.

Programming Languages

Ready to write some code? Select one of the programming languages below:

Network Server

The SBC comes with the Phidget Network Server installed, and the SBC automatically starts the Network Server at boot.

To practice using the Network Server, and to learn more about it, check out the Network Server section of the Linux page.

Advanced Information

Checking System Logs

The SBC maintains two logs: a kernel log and a system log.

The kernel log is for low-level occurrences, such as devices attaching and leaving the USB hub, recording what drivers are being used, and so on.

The system log (syslog) is for normal chatter from the operating system. Any program with the right permissions can use it (though you need to know the method to write to it, information all around the Internet can help) and it contains everything from the Ethernet going up and down, to webserver requests, and so on. If you don't run many programs or services on the SBC, the syslog will essentially be a mirror of the kernel log, because the kernel is the only thing talking.

You can check these logs by using the web interface in the System → Logs tab.

Or you can perform more powerful filtering and displaying via an SSH terminal. For example, dmesg is the command to display the kernel log, and tail prints the last ten lines of input. So, if you are trying to see if you can get a device to be detected on USB, you can run dmesg | tail to print the latest ten lines of kernel log data.

The actual locations of the log files (for filtering and reading) are:

  • /var/log/syslog
  • /var/log/dmesg

But don't edit them directly! Always follow the advice and procedures around the Internet on how to properly log items to syslog.

Further Reading