Blog Home

Hands on: the Arduino Yún’s Bridge

Federico FissoreSeptember 5th, 2013

arduino yun - handson

The other day, we gave you an overview of the Yún’s hardware. Today, we are going to talk about the Bridge library, describing how it facilitates communication between the two processors. The Arduino Yún has two different processors on-board: an Atheros AR9331 running Linino (a customized OpenWRT GNU/Linux distribution maintained by Dog Hunter) linked through its serial port with an Atmel ATMega32U4 (the same processor as the Leonardo).
The Bridge concerns itself with communication between these two parts of the Yún.

The Bridge is made of two different parts
One part, written in Python, runs on the GNU/Linux processor and has three functions:

  1. it executes programs on the GNU/Linux side, when asked by Arduino.
  2. it provides a shared storage space, useful for sharing data like sensor readings between the Arduino and the Internet
  3. it receives commands coming from the Internet and passes them directly to the Arduino

The other part of Bridge is the library that runs on the 32U4. The library allows you to access the Linino parts of Bridge through your sketches.

The awesomeness of the Bridge
With the Bridge you can do some awesome things by communicating between the 32U4 and the AR9331 processors. Some examples could be commanding and controlling your sketch over the internet from a remote location, accessing remote APIs to get data for your sketch to process, or executing programs or scripts too complex to write in an Arduino sketch.

For example, if your shop is on the other side of the house, and you wanted to know if it was comfortable enough to work in there, you can connect a LDR sensor and thermistor to your Yún, which is also connected to your home wireless network. Your sketch can access the board’s shared storage to publish the readings every second to a webpage running on the AR9331. By accessing the URL http://arduino.local/data/get you can call up those readings, letting you know if it’s bright enough but not too hot to get to work on your next project.

Your sketch could also store the sensor readings on a Google Drive spreadsheet or publish them on Facebook. The Temboo library relies on the Bridge to access all the internet based services and APIs provided by Temboo.

Using Bridge, you would no longer need to upload a new version of a sketch to change text on an LCD screen: your sketch can use the shared storage to read the text to display, changing it remotely from a browser using REST based calls. If the text to be displayed is identified by the label “lcd_text”, accessing the URL http://arduino.local/data/put/lcd_text/Hello%20World will show “Hello World” on the LCD.

Finally, you can send your own command to your sketch remotely. You can create a firmata-like access to every single pin of the board, so that calling URL http://arduino.local/arduino/digital/13 will report the current state of pin 13, while http://arduino.local/arduino/digital/13/1 will turn it on. We’ve actually made such a sketch as part of the examples: you’ll find it in the upcoming Arduino IDE release.

Here’s one of the examples that come with the library, the TemperatureWebPanel. It gets the current reading of a temperature sensor and displays it on a web page in your browser. It demonstrates a number of Bridge’s more advanced features like Process (for executing Linux processes), the YunServer and YunClient (for server and client communication), and the ability to upload additional files for serving up to connected clients.

In “setup()”, start the Bridge with “Bridge.begin()”. This ensures the Python part of the Bridge on the GNU/Linux processor is up & running. Next, toggle pins to use a TMP36 temperature sensor so that it can be plugged directly into the board’s headers. YunServer is part of Bridge that enables Linino to pass URLS formatted with “/arduino/” to the 32U4. To listen to commands from connected clients, you need to start the server by calling “server.begin()”. “server.listenOnLocalhost()” forwards all local server communication to port 5555. Process allows you to call Linux commands through your sketch. Here, you’ll execute the shell command “date” by calling “startTime.runShellCommand(“date”)”, which will report the date and time the sketch started running.

In “loop()”, the sketch listens for incoming client connections (requests from the Internet) by creating an instance of YunClient. You can read incoming commands with “client.readString()”. If a connected client sends the command “temperature”, the Bridge executes the “date” command on GNU/Linux to report the time of the reading, then reads the sensor on A1 and calculates the temperature. The date and temperature information is combined into a HTML formatted string and sent back as a response.

Sketches for the Yún can now contain all the files needed to create a web application that can talk to your sketches through a browser. In the TemperatureWebPanel directory on your computer, there is another folder named “www”. This folder contains a basic webpage and a copy of zepto.js, a minimized version of jQuery.

You need a micro SD card plugged in to your Yún with a folder named “arduino” at the root. Inside the “arduino” folder, there must be a directory called “www”. You need to upload the sketch via WiFi to transfer the contents of the local “www” folder. You cannot transfer files via USB. Once uploaded, you can open your favorite browser and go to http://arduino.local/sd/TemperatureWebPanel. There you’ll see the browser reporting back the sensor readings, with the time information and total number of requests.

The browser calls the url http://arduino.local/arduino/temperature in the background. URLs that start with “/arduino/” are special: everything from the “t” of “temperature” is sent to the sketch via Bridge. You can send any command you like, as long as your sketch understands them.

The next post about the Yún will focus on Temboo and how the Arduino Yún can easily grab all sorts of data and interact with tons of web-based services. Stay tuned!