Using Arduino Yún settings files


We’ll show you how the use of settings files can simplify organizing and managing commonly used configurations for running Choreos.

The Temboo client for the Arduino Yún allows you to easily store any set of parameters used to call the client as a settings file. This includes input values, output filters, Temboo app key information, third-party credentials, and even the name of the Choreo to run. Settings files provide default arguments when running Choreos, so you don’t need to track and specify things like output filters on a per-sketch basis.

Creating a Settings File

Where Credentials are stored by Temboo, settings files are stored right on your Arduino Yún. To create a settings file, you can simply update the normal code you'd use to run a Choreo by adding a call to the setSettingsFileToWrite(path) method. For example:

SomeTembooChoreo. setSettingsFileToWrite("/mnt/sda1/mySettingsFilename");

In this example, we’ll create a settings file to store a configuration for the Google > DistanceMatrix > DrivingDistanceMatrix Choreo.

1Log in to Temboo.

2In the Library, navigate to Google > DistanceMatrix > DrivingDistanceMatrix Choreo.

3Enter a Destination (like Albuquerque, NM, USA) and an Origin (like Portland, OR, USA) and click Run. After a moment you should see the response JSON returned from Google displayed in the Output section of the page.

Google Distance Inputs

Specifying inputs for the DrivingDistanceMatrix Choreo

4Select Arduino from the drop down menu at the top of the Choreo page, then specify whether you're working with the Yún or another Arduino board paired with the Yún Shield.

Selecting your Arduino configuration

5Scroll down to the Code section of the page. Copy the snippet and paste into the Arduino IDE, and copy and paste the Header File snippet into a new tab saved as TembooAccount.h.

6At this point, you should have a working sketch that will query Google for the driving distance between Portland and Albuquerque.

7Next, we’ll modify the sketch to store a settings file that holds all the inputs to the Temboo client needed to run the DrivingDistanceMatrix Choreo, rather than specifying them directly in the sketch. To do so, before calling run() we’ll add the line:

DrivingDistanceMatrixChoreo.setSettingsFileToWrite("/mnt/sda1/distanceSettings");

We’ll also update the value of maxRuns to be 1 (since there’s no point in resaving the settings file 10 times), and remove the lines in which the results of the Choreo are printed to the Serial Monitor (since there will be no results to print).

The final sketch should look something like:

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information, as described below

int numRuns = 1;    // execution count, so this doesn't run forever
int maxRuns = 1;    // maximum number of times the Choreo should be executed
                                  // (in this case, just one since there's no point in resaving the settings file) 

void setup() {
  Serial.begin(9600);
  
  // For debugging, wait until a serial console is connected.
  delay(4000);
  while(!Serial);
  Bridge.begin();
}
void loop()
{
  if (numRuns <= maxRuns) {
    Serial.println("Running DrivingDistanceMatrix - Run #" + String(numRuns++));
    
    TembooChoreo DrivingDistanceMatrixChoreo;

    // invoke the Temboo client
    DrivingDistanceMatrixChoreo.begin();
    
    // set Temboo account credentials
    DrivingDistanceMatrixChoreo.setAccountName(TEMBOO_ACCOUNT);
    DrivingDistanceMatrixChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    DrivingDistanceMatrixChoreo.setAppKey(TEMBOO_APP_KEY);
    
    // set choreo inputs
    DrivingDistanceMatrixChoreo.addInput("Origins", "Portland, OR, USA");
    DrivingDistanceMatrixChoreo.addInput("Destinations", "Albuquerque, NM, USA");
    
    // identify choreo to run
    DrivingDistanceMatrixChoreo.setChoreo("/Library/Google/DistanceMatrix/DrivingDistanceMatrix");
    
    // specify where to save the settings
    DrivingDistanceMatrixChoreo.setSettingsFileToWrite("/mnt/sda1/distanceSettings");

    // run choreo to save the settings file
    DrivingDistanceMatrixChoreo.run();
  }

  Serial.println("Waiting...");
  delay(30000); // wait 30 seconds between DrivingDistanceMatrix calls
}

The contents of TembooAccount.h would be identical to what we’ve used in other examples, namely constants defining your Temboo account name and app key information as found in the My Account section, or shown below if you are logged in:

#define TEMBOO_ACCOUNT "ACCOUNT_NAME"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "APP_NAME"  // your Temboo app key name
#define TEMBOO_APP_KEY  "APP_KEY"  // your Temboo app key

Upload and run the sketch. Rather than executing the DrivingDistanceMatrix Choreo, the specified arguments for the execution have been stored at the named location (distanceSettings). Note that you could also put the code to store the settings file in the setup() method of your sketch, since it only needs to run once.

If no path was is specified (as in this case), the Settings File will be created in the default location of /tmp/temboo. Note that files stored in /tmp will be cleared when the Arduino Yún is powered down or reset. If you wish for the settings file to be persistent across power loss or resets, you should specify a directory on a micro SD card like this: /mnt/sda1/mySettingsFile.

You can store as many uniquely named settings files as you’d like. Settings files can also be viewed and created using a text editor: the data saved in the DrivingDistance settings file as a result of running the code above looks like:

-a<your Temboo account name>
-c/Library/Google/DistanceMatrix/DrivingDistanceMatrix
-u<your Temboo app key name>
-p<your Temboo app key>
-iDestinations:Albuquerque, NM, USA
-iOrigins:Portland, OR, USA

Next, we’ll see how settings files can be used to simplify Choreo execution.

Using Settings Files

The Temboo client for the Arduino Yún can be invoked with a flag (-r) that causes it to use the set of default arguments defined in a settings file.

The following code runs the DrivingDistanceMatrix Choreo, using previously stored settings:

TembooChoreo DrivingDistanceMatrixChoreo;

// invoke the Temboo client
DrivingDistanceMatrixChoreo.begin();
    
// specify where to read input settings from
DrivingDistanceMatrixChoreo.setSettingsFileToRead("/mnt/sda1/distanceSettings");

// run choreo; when results are available, print them to serial
DrivingDistanceMatrixChoreo.run();
    
while(DrivingDistanceMatrixChoreo.available()) {
  char c = DrivingDistanceMatrixChoreo.read();
  Serial.print(c);
}
DrivingDistanceMatrixChoreo.close();

Overriding Settings Files

As mentioned earlier, settings files supply default arguments for Choreo executions. Any explicitly provided arguments or inputs will override the ones stored in the settings file.

For example:

TembooChoreo DrivingDistanceMatrixChoreo;

// invoke the Temboo client
DrivingDistanceMatrixChoreo.begin();
    
// specify where to read input settings from
DrivingDistanceMatrixChoreo.setSettingsFileToRead("/mnt/sda1/distanceSettings");

// override the stored "destination" input setting
DrivingDistanceMatrixChoreo.addInput("Destinations", "New York, NY, USA");

// run choreo; when results are available, print them to serial
DrivingDistanceMatrixChoreo.run();
    
while(DrivingDistanceMatrixChoreo.available()) {
  char c = DrivingDistanceMatrixChoreo.read();
  Serial.print(c);
}
DrivingDistanceMatrixChoreo.close();

Convert the example code to work with the Yún Shield

If you're working with the Yún Shield paired with another Arduino board, you'll need to make some small changes to the Yún example code above it so that it's compatible with the Arduino Yún Shield. If you generate code specifically for the Yún Shield, you won't need to make these changes to your generated sketch.

1First, change the include statement #include <Temboo.h> to #include <TembooYunShield.h>. Your include statements should look like this:

#include <Bridge.h>
#include <TembooYunShield.h>
#include "TembooAccount.h" 

2Next, change the Temboo object name from TembooChoreo to TembooYunShieldChoreo. Your code should look like this:

TembooYunShieldChoreo  DrivingDistanceMatrixChoreo;

Now your code is ready to run on your Yún Shield!

Need Help?

We're always happy to help. Just email us at support@temboo.com, and we'll answer your questions.


Back