Project tutorial
Locate toxic facilities with EPA data using Temboo

Locate toxic facilities with EPA data using Temboo © GPL3+

We'll show you how to get your Arduino Yún to check for EPA-regulated facilities in the Toxics Release Inventory (TRI). You'll be able to...

  • 222 views
  • 0 comments
  • 0 respects

Components and supplies

Apps and online services

About this project

1. Get Set Up

  1. Make sure you have a Temboo account. If you don't already have one, you can register for a free account here.
  2. Make sure that your Yún is connected to the Internet.

2. Write the Sketch

Copy the sketch code below into a new tab in your Arduino IDE. This code calls the FacilitiesSearchByZip Choreo, and you can replace the ZIP code with any valid US ZIP code.

/*
  ToxicFacilitiesSearch
  
  Demonstrates making a request to the Envirofacts API using the Temboo Arduino Yun SDK.
  This example retrieves the names and addresses of EPA-regulated facilities in the 
  Toxins Release Inventory (TRI) database within a given zip code.
  
  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information

// the zip code to search for toxin-emitting facilities
String US_ZIP_CODE = "11215";

int numRuns = 1;   // execution count, so that this doesn't run forever
int maxRuns = 10;  // max number of times the Envirofacts FacilitiesSearch Choreo should be run

void setup() {
  Serial.begin(9600);
    
  // for debugging, wait until a serial console is connected
  delay(4000);
  while(!Serial);
  Bridge.begin();
}

void loop()
{
  // while we haven't reached the max number of runs...
  if (numRuns <= maxRuns) {
      
    // print status
    Serial.println("Running ToxicFacilitiesSearch - Run #" + String(numRuns++) + "...");

    // we need a Process object to send a Choreo request to Temboo
    TembooChoreo FacilitiesSearchByZipChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked and repopulated with
    // appropriate arguments each time its run() method is called.
    FacilitiesSearchByZipChoreo.begin();
        
    // set Temboo account credentials
    FacilitiesSearchByZipChoreo.setAccountName(TEMBOO_ACCOUNT);
    FacilitiesSearchByZipChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    FacilitiesSearchByZipChoreo.setAppKey(TEMBOO_APP_KEY);
  
    // identify the Temboo Library choreo to run (EnviroFacts > Toxins > FacilitiesSearchByZip)
    FacilitiesSearchByZipChoreo.setChoreo("/Library/EnviroFacts/Toxins/FacilitiesSearchByZip");
        
    // set choreo inputs; in this case, the US zip code for which to retrieve toxin release data
    // the Temboo client provides standardized calls to 100+ cloud APIs
    FacilitiesSearchByZipChoreo.addInput("Zip", US_ZIP_CODE);
    
    // specify two output filters, to help simplify the Envirofacts API results.
    // see the tutorials on using Temboo SDK output filters at http://www.temboo.com/arduino
    FacilitiesSearchByZipChoreo.addOutputFilter("fac", "FACILITY_NAME", "Response");

    FacilitiesSearchByZipChoreo.addOutputFilter("addr", "STREET_ADDRESS", "Response");

    // run the choreo 
    unsigned int returnCode = FacilitiesSearchByZipChoreo.run();
    if (returnCode == 0) {
      String facilities;
      String addresses;

      // when the choreo results are available, process them.
      // the output filters we specified will return comma delimited
      // lists containing the name and street address of the facilities
      // located in the specified zip code.
      while(FacilitiesSearchByZipChoreo.available()) {
        String name = FacilitiesSearchByZipChoreo.readStringUntil('\x1F');
        name.trim();

        String data = FacilitiesSearchByZipChoreo.readStringUntil('\x1E');
        data.trim();

        if (name == "fac") {
          facilities = data;
        } else if (name == "addr") {
          addresses = data;
        }
      }
      FacilitiesSearchByZipChoreo.close();
      
      // parse the comma delimited lists of facilities to join the 
      // name with the address and print it to the serial monitor
      if (facilities.length() > 0) {
        int i = -1;
        int facilityStart = 0;
        int addressStart = 0;
        String facility;
        String address;
        do {
          i = facilities.indexOf(',', facilityStart);
          if (i >= 0) {
            facility = facilities.substring(facilityStart, i);
            facilityStart = i + 1;
          }

          i = addresses.indexOf(',', addressStart);
          if (i >= 0) {
            address = addresses.substring(addressStart, i);
            addressStart = i + 1;
          }
          
          if (i >= 0) {
            printResult(facility, address);
          }

        }while (i >= 0);
        facility = facilities.substring(facilityStart);
        address = addresses.substring(addressStart);
        printResult(facility, address);
      } else {
        Serial.println("No facilities found in zip code " + US_ZIP_CODE);
      }
    } else {
      while(FacilitiesSearchByZipChoreo.available()) {
        char c = FacilitiesSearchByZipChoreo.read();
        Serial.print(c);
      }
    }
  }
  Serial.println("Waiting...");
  Serial.println("");
  delay(30000); // wait 30 seconds between calls
}

// a simple utility function, to output the facility name and address in the serial monitor.
void printResult(String facility, String address) {
  Serial.print(facility);
  Serial.print(" - ");
  Serial.println(address);
}

3. Create Your Header File

The sketch above references the TembooAccount.h header file, which contains your Temboo account information.

If you are currently logged in, you'll see your account details in the code snippet below (otherwise you'll see placeholder values). Copy the code snippet into a new tab in Arduino and call it TembooAccount.h.

With both files in place you are ready to upload the sketch and check for toxic facilities from your Yún. Now you're free to put your thoughts elsewhere, like worrying about astroid strikes.

#define TEMBOO_ACCOUNT "accountName"  // your Temboo account name 
#define TEMBOO_APP_KEY_NAME "myFirstApp"  // your Temboo app key name
#define TEMBOO_APP_KEY  "abc123xxxxxxxxxxxxxx"  // your Temboo app key

4. What's Next?

Now that all that unpleasant business is out of the way and you're (hopefully) not in any danger, why not check out the other 2000+ Choreos in our Library and start thinking about all the possibilities for your next Yún project.

5. Need Help?

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

Code

Code snippet #1Arduino
/*
  ToxicFacilitiesSearch
  
  Demonstrates making a request to the Envirofacts API using the Temboo Arduino Yun SDK.
  This example retrieves the names and addresses of EPA-regulated facilities in the 
  Toxins Release Inventory (TRI) database within a given zip code.
  
  This example code is in the public domain.
*/

#include <Bridge.h>
#include <Temboo.h>
#include "TembooAccount.h" // contains Temboo account information

// the zip code to search for toxin-emitting facilities
String US_ZIP_CODE = "11215";

int numRuns = 1;   // execution count, so that this doesn't run forever
int maxRuns = 10;  // max number of times the Envirofacts FacilitiesSearch Choreo should be run

void setup() {
  Serial.begin(9600);
    
  // for debugging, wait until a serial console is connected
  delay(4000);
  while(!Serial);
  Bridge.begin();
}

void loop()
{
  // while we haven't reached the max number of runs...
  if (numRuns <= maxRuns) {
      
    // print status
    Serial.println("Running ToxicFacilitiesSearch - Run #" + String(numRuns++) + "...");

    // we need a Process object to send a Choreo request to Temboo
    TembooChoreo FacilitiesSearchByZipChoreo;

    // invoke the Temboo client
    // NOTE that the client must be reinvoked and repopulated with
    // appropriate arguments each time its run() method is called.
    FacilitiesSearchByZipChoreo.begin();
        
    // set Temboo account credentials
    FacilitiesSearchByZipChoreo.setAccountName(TEMBOO_ACCOUNT);
    FacilitiesSearchByZipChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
    FacilitiesSearchByZipChoreo.setAppKey(TEMBOO_APP_KEY);
  
    // identify the Temboo Library choreo to run (EnviroFacts > Toxins > FacilitiesSearchByZip)
    FacilitiesSearchByZipChoreo.setChoreo("/Library/EnviroFacts/Toxins/FacilitiesSearchByZip");
        
    // set choreo inputs; in this case, the US zip code for which to retrieve toxin release data
    // the Temboo client provides standardized calls to 100+ cloud APIs
    FacilitiesSearchByZipChoreo.addInput("Zip", US_ZIP_CODE);
    
    // specify two output filters, to help simplify the Envirofacts API results.
    // see the tutorials on using Temboo SDK output filters at http://www.temboo.com/arduino
    FacilitiesSearchByZipChoreo.addOutputFilter("fac", "FACILITY_NAME", "Response");

    FacilitiesSearchByZipChoreo.addOutputFilter("addr", "STREET_ADDRESS", "Response");

    // run the choreo 
    unsigned int returnCode = FacilitiesSearchByZipChoreo.run();
    if (returnCode == 0) {
      String facilities;
      String addresses;

      // when the choreo results are available, process them.
      // the output filters we specified will return comma delimited
      // lists containing the name and street address of the facilities
      // located in the specified zip code.
      while(FacilitiesSearchByZipChoreo.available()) {
        String name = FacilitiesSearchByZipChoreo.readStringUntil('\x1F');
        name.trim();

        String data = FacilitiesSearchByZipChoreo.readStringUntil('\x1E');
        data.trim();

        if (name == "fac") {
          facilities = data;
        } else if (name == "addr") {
          addresses = data;
        }
      }
      FacilitiesSearchByZipChoreo.close();
      
      // parse the comma delimited lists of facilities to join the 
      // name with the address and print it to the serial monitor
      if (facilities.length() > 0) {
        int i = -1;
        int facilityStart = 0;
        int addressStart = 0;
        String facility;
        String address;
        do {
          i = facilities.indexOf(',', facilityStart);
          if (i >= 0) {
            facility = facilities.substring(facilityStart, i);
            facilityStart = i + 1;
          }

          i = addresses.indexOf(',', addressStart);
          if (i >= 0) {
            address = addresses.substring(addressStart, i);
            addressStart = i + 1;
          }
          
          if (i >= 0) {
            printResult(facility, address);
          }

        }while (i >= 0);
        facility = facilities.substring(facilityStart);
        address = addresses.substring(addressStart);
        printResult(facility, address);
      } else {
        Serial.println("No facilities found in zip code " + US_ZIP_CODE);
      }
    } else {
      while(FacilitiesSearchByZipChoreo.available()) {
        char c = FacilitiesSearchByZipChoreo.read();
        Serial.print(c);
      }
    }
  }
  Serial.println("Waiting...");
  Serial.println("");
  delay(30000); // wait 30 seconds between calls
}

// a simple utility function, to output the facility name and address in the serial monitor.
void printResult(String facility, String address) {
  Serial.print(facility);
  Serial.print(" - ");
  Serial.println(address);
}

Comments

Similar projects you might like

Post Data to a Google Spreadsheet with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,289 views
  • 0 comments
  • 7 respects

Send an SMS using Twilio with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,552 views
  • 1 comment
  • 1 respect

Upload a File to your Dropbox Account with Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,044 views
  • 0 comments
  • 1 respect

Read And Update Databases With Temboo

Project tutorial by Temboo_OFFICIAL

  • 2,043 views
  • 2 comments
  • 5 respects

Send An Email From Your Gmail Account With Temboo

Project tutorial by Temboo_OFFICIAL

  • 1,455 views
  • 0 comments
  • 2 respects

Control your Yún from your Phone with Temboo

Project tutorial by Temboo_OFFICIAL

  • 958 views
  • 0 comments
  • 1 respect
Add projectSign up / Login