OS - Phidget SBC

From Phidgets Support
Revision as of 21:10, 17 May 2017 by Lmpacent (talk | contribs)

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.


Warning: Regardless of how you decide to use the SBC, you will be running on the SBC as root (super-user). This means you are able to overwrite important system files without the system asking for additional permissions.


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:

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

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.

Programming with the Phidget SBC

Writing a Phidget Program

There are three ways to write and upload a Phidget program:

  1. Using the SBC Web Interface:
    • This is useful for simple projects written in C or Java that you want to start at boot.
  2. 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:

Program in Java with the Web Interface

To show how to write, compile, and install Java programs on the SBC, we'll use the Hello World example for Java. Download the 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 above).

The first step to getting the HelloWorld code running on on your SBC starts with your external development machine:

  1. Download phidget22.jar
  2. Place the SBC version of phidget22.jar on your development machine in the directory that you will use to compile the Java files.
  3. Copy the HelloWorld.java file into that working directory.
  4. Compile the HelloWorld.java file from within that working directory. From the command line prompt on Windows, this will be:
    javac -classpath .;phidget22.jar HelloWorld.java
    
    In a terminal on Linux or Mac OS, this will be:
    javac -classpath .:phidget22.jar HelloWorld.java
    
  5. You should now have three compiled class files:
    • HelloWorld.class
    • HelloWorld$1.class
    • HelloWorld$2.class
  6. Using the SBC Web Interface, create a new project called HelloWorld:
  7. 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:
  8. 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.

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

  1. Connect to the SBC using SSH.
  2. Download the Phidget Java Examples to the SBC.
    wget http://copied_link
    
  3. Unpack the examples
    unzip filename
    
  4. phidget22.jar is located at:
    /usr/share/java/phidget22.jar
  5. Compile the HelloWorld.java example:
    javac -classpath .:/usr/share/java/phidget22.jar HelloWorld.java
    
  6. To run the HelloWorld program, use:
    java -classpath .:/usr/share/java/phidget22.jar HelloWorld
    

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 Code on the SBC

When you're developing directly on the SBC, you will need to use a terminal editor:

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

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):

apt-get install emacs

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.

Running a Program Automatically

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

Running on boot:

  • Never miss an event. As long as the SBC is running, your code will be running.

Running on a schedule:

  • 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 running on a schedule. Otherwise, keep reading to learn how to 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 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:



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

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.

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.


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!

  1. Set your editor to nano:
    export EDITOR=nano
    
  2. Edit your crontab file:
    crontab -e
    
  3. Schedule your cron job
    minute hour dayOfMonth month dayOfWeek  YOUR_COMMAND
    

Done! Here is an example of a cron job that will run at 5 AM every week:

0 5 * * 1 /root/code/myprogram argument1

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