Develop on your PC, Deploy on your Yun

INTRODUCTION

It is much easier to develop a program using Python rather than C/C++
It is much easier to develop a program on a PC rather than on the Linux side of a Yun.
It is much easier to do the “heavy lifting” using Python and on the Linux side of the Yun rather C/C++ than on the Arduino side.

When I first heard of the Yun I assumed it would be like a Raspberry Pi with WiFi and an Arduino but without sound and graphics. And it is. But the way it is presented by the Yun developers you would be forgiven for thinking it was a Linux attachment for an Arduno rather than an Arduino attachment for Linux.

It is easy to write a combination of Python and Arduino code that works on a PC connected to an Uno and which will run with little or no change on a Yun.

The only change neeeded for the Arduino code is to change from Serial to Serial1
And on the Yun the Python code connects to the Arduino side via /dev/ttyATH0 rather than /dev/ttyACM0 (or COMxxx on Windows)

SIMPLE DEMONSTRATION

The attached ZIP file includes the Python and Arduino code containing a minimal example.

When you extract the ZIP file on your PC it will create a directory called SimpleDemo.
It contains a file called SimpleDemo.ino which should be uploaded to the Arduino side of the Yun
The complete directory should be uploaded to the Linux side of the Yun. The file SimpleDemo.ino is not needed on the Linux side, but it won’t do any harm.

With very few changes (all noted in the comments) the code will also work on a PC connected to an Uno, Mega or Leonardo

PYTHON

In this note I am using Python because I am familiar with it. I like Python because most code runs unchanged on Linux, Mac or Windows. You can use other programming languages on the Yun and much of what I say applies to them all.

ROOT USER ON THE YUN

In most Linux systems a user does not have full ROOT privileges for obvious security reasons, although s/he can temporarily acquire the extra privilieges when required.

Because the Yun is only intended to be a single user system the normal user is called ROOT and has all of the privileges to destroy everything on the system :slight_smile:

PREPARING THE YUN

Out-of-the-box the Yun uses /dev/ttyATH0 for communication using the Bridge library and that needs to be disabled to allow Python to use that serial connection.

The file /etc/inittab contains a line
ttyATH0::askfirst:/bin/ash --login

which needs to be commented out (so it looks like this)

ttyATH0::askfirst:/bin/ash --login

and the the Yun needs to be restarted.

I have also attached two short Python programs that will make this change (and undo it) if you don’t want to edit /etc/inittab yourself. These programs are in pythonBridgeDisable.zip.

VIEWING FILE CONTENTS

If you want to check the contents of a text file on a Linux system it is as simple as
cat filename
for example
cat /etc/inittab

UPLOADING PYTHON PROGRAMS TO THE YUN

In spite of the fact that the Yun comes with Python installed the developers did not provide any convenient way to upload Python programs to the Yun. However the process is not at all complicated. A Python program is just a text file, or a collection of text files.

UPLOADING FILES TO THE YUN USING SCP (also downloading from the Yun)

When you connect to the Yun using SSH your terminal ONLY has access to the Yun. For copying files to the Yun you must obviously have access both to the Yun and the to PC that contains the files. That is what the program scp (Secure CoPy) is for. (I believe there is WINscp for Windows users).

To use SCP you need to open a terminal on your PC (not the terminal you may be using for SSH) and call scp from it - for example
scp myFile.txt root@192…:/mnt/sdxxxxxx
will copy the file “myFile.txt” to the directory /mnt/xxxxxxxx

You will notice that when you run scp it will ask you for the password for your Yun.

If you want to copy a directory and all of its files the command is only slightly different
scp -r myDirectory root@192…:/mnt/sdxxxxxx
the “-r” tells it to copy recursively

For the SimpleDemo program the command will be
scp -r ./SimpleDemo root@192.168.x.x:/root
assuming your terminal is open at the Directory that contains the directory called SimpleDemo
and, of course, you need to use the correct IP address

UPLOADING ARDUINO SKETCHES TO THE YUN

I am assuming you will create and upload Arduino sketches using a PC connected to the Yun via the small USB connector.

RUNNING PYTHON PROGRAMS USING AN SSH TERMINAL CONNECTION TO THE YUN

SSH (standing for Secure SHell) is a standard way for securely controlling a Linux PC via the command line over an internet connection. SSH is already installed on the Yun and will also be installed on Linux PCs. The puTTY terminal program can be used to make an SSH connection from Windows (and Linux).

To connect to the Yun with SSH the command is
ssh root@192xxxxxxxxxx
It will ask you for the Yun password

When you have connected using SSH you can run programs and do all the usual things that you could do if your screen was directly connected to the Yun.

cd SimpleDemo # to change to the project Directory
python SimpleDemo.py # to run the program

=================

As usual, comments and corrections will be welcome.
And it would be very helpful if a kind Windows user could try it and add any necessary additional info.

…R

SimpleDemo.zip (89.1 KB)

pythonBridgeDisable.zip (1.1 KB)

Nice writeup! 8)

When I get a chance, I'll try this out under Windows, and also give you the info on running WinSCP to copy files.

sounds good - it took me several months to figure this out when I started with the Yun :-)

A few remarks: - When we develop on a PC, the resources are endless, compared to a Yun - Not every package / distribution will even install on a Yun, not to talk a program will run on it

So along your way, always make sure everything installs and runs on the Yun, before you spend three weeks developing fancy stuff on your PC that won't run anymore on your Yun.

I'm very careful about using new, not yet on the Yun installed software modules, but with that in mind, I found almost 100% compatibility between python on my PC and python on the Yun.

I even wrote (or better: stripped down the original bridge to) a fake bridge, so I can develop programs for the Atheros and test proper communication with an on my PC non-present Atmel :-)

jafrei

jafrei:
A few remarks:

  • When we develop on a PC, the resources are endless, compared to a Yun
  • Not every package / distribution will even install on a Yun, not to talk a program will run on it

So along your way, always make sure everything installs and runs on the Yun, before you spend three weeks developing fancy stuff on your PC that won’t run anymore on your Yun.

I’m very careful about using new, not yet on the Yun installed software modules, but with that in mind, I found almost 100% compatibility between python on my PC and python on the Yun.

Very wise.

…R

Robin2: But the way it is presented by the Yun developers you would be forgiven for thinking it was a Linux attachment for an Arduno rather than an Arduino attachment for Linux.

My thoughts too, glad to know I was one of many confused by the whole bridge thing for one reason or another.

Although maybe it just one of those high barrier to entry things that make a lot more sense once you get used to them? I really hate to put down something someone obviously took quite some time to put together.

Thinking python first is probably a better way to go. If we were softballing around how an Arduino "linux" platform should be I think a write one bout of code is a good way to go for Arduino people. I think bridge takes that idea into account and come up with an "odd to us" solution. My concept would have been more like just put firmata on the at32u4 and push out / select a python script to be used a part of a larger python routine on the Yun that abstracts away some of the gritty parts about serial communication and the like. But you know the Arduino people are supposedly familiar with "Arduino C" right? Why change the language, so lets just pretend the micro is the host controller. There are compromises with the python first approach too though, so whose to say anything is a good idea or not.

Looked at your code and tried it out, works for me. I'll let you know if I do anything else with it. Thanks!