There are quite a few users making their Phidget SBCs do data acquisition, and perhaps this is the plan you have for yours. Depending on how much data you want to collect, and because of limited space on the SBC itself, you’ll probably end up using something like a USB drive to store your data logs. It’s really easy to set up, but there are a couple gotchas that you might want to consider before writing your program, so let’s go over them and talk about the basics of logging to the USB flash drive from the SBC.
If you’re new to working with the SBC, watch the getting started video, which will talk about the features of the SBC, uploading files, and compiling your code.
USB drives are located in the media folder on the SBC. As new USB devices are attached, they will be assigned an incremental name, starting at usb0. You can check the path and mount point of your device by running mount from the command line (through SSH). You can also use the web interface to look up this information, located under Status > USB. Once you’ve logged data, you can either use SSH or the web interface to view and download the file. On the web interface, go to System > File Editor, where you’ll find the media folder with the usb drives listed inside.
It seems pretty easy, but when you’re writing code for the purpose of logging data, keep these points in mind:
- Don’t hard-code the USB file path into your program; Device and mount points can change. Furthermore, creating a file to an unmounted USB won’t produce any errors for you to catch. You can run some system calls to prevent losing data to unmounted USBs, shown in the examples and explained later.
- If your program has control of the InterfaceKit on the SBC, then no other programs will be able to use it. We recommend setting up a cron job to run your code every x minutes (or hours, or what have you)
Feel free to download the example in C and look through it. It demonstrates how to check for the USB mount point, create and attach to a Phidget Interface Kit (that’s on the SBC), read the sensor, and write the sensor data to a log file. Most of that is pretty easy, but let’s look at a couple of those key points you want to keep in mind when writing to a USB flash drive from the SBC
It’s pretty easy to check the mount point, but you’ll need to run a system call from whatever language you’re using. The basic steps for finding the USB’s mount point are:
- Run a system command like
mountto find the mount location of your USB flash drive. The SBC will load the USB flash as a device, so pipe the output of
grep /dev/sdcommand. Redirect the output to a file, so the final command looks something like:
mount | grep /dev/sd > /usr/userapps/templog/checkusb.txt
- Then, open and read
/dev/sda1 on /media/usb0 type vfat (rw,noexec,nodev,sync,noatime,nodiratime)
. The output will look something like this:
- Check that the device is read write (i.e. search for the “rw” string) and then store the file location (
/media/usb0in this case). In the examples, the file name is appended to the file location for use later in the programs.
If you want to know more about issues with mount points, check out the OS – Phidget SBC page.
Once the USB mount location is known, writing a line of data to a file on the USB drive is pretty easy. The only other catch is scheduling the program to run every x minutes (or hours, or what have you). The best way to do this is to use Cron.
If you want to sample less often than every minute, and perhaps run some other infrequent tasks on the SBC, you can use Cron to schedule tasks. These are the instructions provided on the OS – Phidget SBC page for setting up a cron job:
- Specify your default editor to be nano. Otherwise it
will default to
viand you’ll have to figure out
viin order to add lines to your crontab:
export EDITOR nano
- To edit your crontab file, simply type: crontab -e
- Each line of the crontab file is one scheduled job. Lines that start with a hash “#” are comments and are ignored. There
is an example line in the crontab, and a reminder line at the very end. Essentially, each line should contain:
minute hour dayOfMonth month dayOfWeek commandWhere:
commandis the program you want to run (with absolute path, and arguments. For example,
./myprogram argument1won’t work, but
- Each time argument is either a number, a list of numbers separated by commas, or an asterisk. (For example, * * * * * means every minute for all days and months, */30 * * * * means every thirty minutes for all days and months
So, for Cron to run the temperature logging c program every 2 minutes I added the line:
*/2 * * * * /usr/userapps/ctemplog/ctemplog
If you already have jobs scheduled, you’ll see them in the file that comes up. You can edit, add, or delete.
More information about Cron can be found on Ubuntu.com.
- After you save, you’ll see a little message back in the terminal that says the new crontab file was installed, and it is now scheduled! Cron always starts at boot, and so if you have edited and installed your crontab as above, the scheduling of your program will start properly even after a reboot of the SBC. However, if you are having strange scheduling problems, you may want to familiarize yourself with the software details of how the SBC as a whole determines the current date and time.
In this article, we’ve covered the easiest way to collect data, and provided examples that you can quickly edit and use for your own purposes. If you’re interested in saving and retrieving data in other ways, you can read more about saving and retrieving data on the OS – Phidget SBC page. Have some other ideas? Please share your methods in the comments.