Tuesday, May 31, 2016

Arduino Life 5: Arduino Yun and ftp client/server

One of the main reasons I purchased an Arduino Yun as my first Arduino board was it’s inclusion of WiFi right out of the box. This means you can start making Internet of Things projects immediately. As I have introduced myself to Arduino, I have also looked to learn more about the Linux side of the Yun, how it bridges to the Arduino side and how I can interact with it using standard Internet tools, like SSH and FTP.

Arduino yun

An Arduino with SSH

Once the Arduino Yun is connected to a network, it is possible to use the Macintosh Terminal or other SSH client to log into the Yun and use it as you would any Linux computer.

You’ll need an SD Card on the Arduino Yun to do much with SSH or ftp access, though, as you need a place to store the files you will be uploading or creating.

Arduino ssh 1

This is fine if you just want to view files on the SD Card or other basic terminal operations, but as I was playing around, I realized i wanted to do more than use the built-in vi editor. I use a Mac OS X text editor program called, TextWrangler for a lot of things, including coding. My first thought was to use an ftp client on the Yun to connect to my local computer, so that I could push data over to the Mac from projects like my temperature, humidity and light level sensor. After many searches, it looked like there wasn’t an ftp client for the Yun, but you could install an ftp server and then access that from the Mac. I also had another user for this ftp server, which I’ll show in a moment,

Textwrangler icon

TextWrangler for Mac OS X

An ftp server for the Arduino Yun

Installing an ftp server on the Arduino Yun is actually quite simple. SSH into the Yun and execute these two commands:

opkg update
opkg install openssh-sftp-server

You may need to reset the Yun using the reset button or by cycling the power in order to get the server started. After that, it will be automatically started on each boot. You’’ll now be able to use your favorite ftp client to connect to the Yun. 

Here’s a view of the Yun’s ftp server using Cyberduck on the Mac.

Ftp screen 1

One of the reasons I was so keen to have direct file access to the Yun was that I was trying to develop a short Python program to run on the Yun, but using my ancient (i.e. 30+ year old) knowledge of vi was quite limiting. TextWrangler supports direct reading and writing of text files to any FTP server, and I have used that when editing files on my web server, so I figured there had to be a way to install an ftp server on the Yun and then use Textwrangler to write code directly onto the Arduino.

Textwrangler 1

I can read and write directly to the server using TextWrangler. This means I can write my code, click Save and then immediately switch to my Terminal window and execute the code to test it. That makes it very convenient.

Going Further - Pushing files to ftp server using Python

Of course, once you find one use for the ftp server, more are sure to follow. One of the common beginner projects for the Arduino Yun is a motion-activated security camera. You an find complete instructions in this post from Adafruit.

Wireless Security Camera with the Arduino Yun
Build your own wireless security camera using the Arduino Yun & a USB webcam!

This example using the Temboo service to forward the photos and stream video from the Yun directly to the Internet. When I was looking at the project, though, I was thinking that, since I have my own web server available, I would rather forward the photos to a directory on the server where I could view them at my leisure. This, in fact, is what sent me on the original search for an ftp client for the Yun. While I didn’t find a full-blown client, though, I did find a programatic way to ftp files to the server of your choice using a Python program. This was a little more manual, but it got the job done.

Here is my test code — with no error checking or any other niceties — but I figured you might find the example useful. Many thanks for the folks at EFFBot that provided the great — and easily modified — code which I could build on.

import datetime
import ftplib
import os
# Function to provide easy uploads of appropriate type (Text/Binary)
# Taken from http://effbot.org/librarybook/ftplib.htm
def upload(ftp, file):
ext = os.path.splitext(file)[1]
if ext in (".txt", ".htm", ".html"):
ftp.storlines("STOR " + file, open(file))
ftp.storbinary("STOR " + file, open(file, "rb"), 1024)
# Get Date
today = datetime.date.today()
# Get Time
now = datetime.datetime.now()
# Build webcam capture filename in YYYYMMDDHHMMSS format
# Call fswebcam capture with resolution parameter
from subprocess import call
call(["fswebcam","-r 1280x720",fname])
# Open FTP session and change directory
ftp = ftplib.FTP(“yourserver.com")
ftp.login(“user", “password")
# Upload picture file
upload(ftp, fname)
# End FTP session

You might have noticed in the ftp screen shot above, this code does work. It snaps a picture from the webcam, stores it locally on the SD Card with a unique date and time--stamped name and  then uploads it to my web site. I plan on setting up this security camera as a test in one of my upcoming learning sessions and replacing the Temboo calls with this ftp script (or perhaps an improved version of it.) I’ll report back on how well it works in an upcoming post.

There is so much to learn about the Arduino world and the Arduino Yun especially. It’s obvious now that many people have wanted a board like the Yun, as Arduino has now introduced a shield that can turn an Arduino Uno — one of the most popular boards — into a functionally equivalent Arduino Yun.

Arduino Yun Shield on eBay

Arduino Yun Shield on Amazon.com

I hope you’ve found this edition of Arduino Life useful. Please send along your questions and comments.

No comments: