source: rtos_arduino/trunk/arduino_lib/libraries/Bridge/examples/Temboo/ToxicFacilitiesSearch/ToxicFacilitiesSearch.ino

Last change on this file was 136, checked in by ertl-honda, 8 years ago

ライブラリとOS及びベーシックなサンプルの追加.

File size: 6.2 KB
Line 
1/*
2 ToxicFacilitiesSearch
3
4 Demonstrates making a request to the Envirofacts API using Temboo from an Arduino Y炭n.
5 This example retrieves the names and addresses of EPA-regulated facilities in the
6 Toxins Release Inventory (TRI) database within a given zip code.
7
8 Check out the latest Arduino & Temboo examples and support docs at http://www.temboo.com/arduino
9
10 A Temboo account and application key are necessary to run all Temboo examples.
11 If you don't already have one, you can register for a free Temboo account at
12 http://www.temboo.com
13
14 This example assumes basic familiarity with Arduino sketches, and that your Y炭n is connected
15 to the Internet.
16
17 Looking for another API to use with your Arduino Y炭n? We've got over 100 in our Library!
18
19 This example code is in the public domain.
20*/
21
22#include <Bridge.h>
23#include <Temboo.h>
24#include "TembooAccount.h" // contains Temboo account information
25 // as described in the footer comment below
26
27// the zip code to search for toxin-emitting facilities
28String US_ZIP_CODE = "11215";
29
30int numRuns = 1; // execution count, so that this doesn't run forever
31int maxRuns = 10; // max number of times the Envirofacts FacilitiesSearch Choreo should be run
32
33void setup() {
34 Serial.begin(9600);
35
36 // for debugging, wait until a serial console is connected
37 delay(4000);
38 while(!Serial);
39 Bridge.begin();
40}
41
42void loop()
43{
44 // while we haven't reached the max number of runs...
45 if (numRuns <= maxRuns) {
46
47 // print status
48 Serial.println("Running ToxicFacilitiesSearch - Run #" + String(numRuns++) + "...");
49
50 // we need a Process object to send a Choreo request to Temboo
51 TembooChoreo FacilitiesSearchByZipChoreo;
52
53 // invoke the Temboo client
54 // NOTE that the client must be reinvoked and repopulated with
55 // appropriate arguments each time its run() method is called.
56 FacilitiesSearchByZipChoreo.begin();
57
58 // set Temboo account credentials
59 FacilitiesSearchByZipChoreo.setAccountName(TEMBOO_ACCOUNT);
60 FacilitiesSearchByZipChoreo.setAppKeyName(TEMBOO_APP_KEY_NAME);
61 FacilitiesSearchByZipChoreo.setAppKey(TEMBOO_APP_KEY);
62
63 // identify the Temboo Library choreo to run (EnviroFacts > Toxins > FacilitiesSearchByZip)
64 FacilitiesSearchByZipChoreo.setChoreo("/Library/EnviroFacts/Toxins/FacilitiesSearchByZip");
65
66 // set choreo inputs; in this case, the US zip code for which to retrieve toxin release data
67 // the Temboo client provides standardized calls to 100+ cloud APIs
68 FacilitiesSearchByZipChoreo.addInput("Zip", US_ZIP_CODE);
69
70 // specify two output filters, to help simplify the Envirofacts API results.
71 // see the tutorials on using Temboo SDK output filters at http://www.temboo.com/arduino
72 FacilitiesSearchByZipChoreo.addOutputFilter("fac", "FACILITY_NAME", "Response");
73
74 FacilitiesSearchByZipChoreo.addOutputFilter("addr", "STREET_ADDRESS", "Response");
75
76 // run the choreo
77 unsigned int returnCode = FacilitiesSearchByZipChoreo.run();
78 if (returnCode == 0) {
79 String facilities;
80 String addresses;
81
82 // when the choreo results are available, process them.
83 // the output filters we specified will return comma delimited
84 // lists containing the name and street address of the facilities
85 // located in the specified zip code.
86 while(FacilitiesSearchByZipChoreo.available()) {
87 String name = FacilitiesSearchByZipChoreo.readStringUntil('\x1F');
88 name.trim();
89
90 String data = FacilitiesSearchByZipChoreo.readStringUntil('\x1E');
91 data.trim();
92
93 if (name == "fac") {
94 facilities = data;
95 } else if (name == "addr") {
96 addresses = data;
97 }
98 }
99 FacilitiesSearchByZipChoreo.close();
100
101 // parse the comma delimited lists of facilities to join the
102 // name with the address and print it to the serial monitor
103 if (facilities.length() > 0) {
104 int i = -1;
105 int facilityStart = 0;
106 int addressStart = 0;
107 String facility;
108 String address;
109 do {
110 i = facilities.indexOf(',', facilityStart);
111 if (i >= 0) {
112 facility = facilities.substring(facilityStart, i);
113 facilityStart = i + 1;
114 }
115
116 i = addresses.indexOf(',', addressStart);
117 if (i >= 0) {
118 address = addresses.substring(addressStart, i);
119 addressStart = i + 1;
120 }
121
122 if (i >= 0) {
123 printResult(facility, address);
124 }
125
126 }while (i >= 0);
127 facility = facilities.substring(facilityStart);
128 address = addresses.substring(addressStart);
129 printResult(facility, address);
130 } else {
131 Serial.println("No facilities found in zip code " + US_ZIP_CODE);
132 }
133 } else {
134 while(FacilitiesSearchByZipChoreo.available()) {
135 char c = FacilitiesSearchByZipChoreo.read();
136 Serial.print(c);
137 }
138 }
139 }
140 Serial.println("Waiting...");
141 Serial.println("");
142 delay(30000); // wait 30 seconds between calls
143}
144
145// a simple utility function, to output the facility name and address in the serial monitor.
146void printResult(String facility, String address) {
147 Serial.print(facility);
148 Serial.print(" - ");
149 Serial.println(address);
150}
151
152/*
153 IMPORTANT NOTE: TembooAccount.h:
154
155 TembooAccount.h is a file referenced by this sketch that contains your Temboo account information.
156 You'll need to edit the placeholder version of TembooAccount.h included with this example sketch,
157 by inserting your own Temboo account name and app key information. The contents of the file should
158 look like:
159
160 #define TEMBOO_ACCOUNT "myTembooAccountName" // your Temboo account name
161 #define TEMBOO_APP_KEY_NAME "myFirstApp" // your Temboo app key name
162 #define TEMBOO_APP_KEY "xxx-xxx-xxx-xx-xxx" // your Temboo app key
163
164 You can find your Temboo App Key information on the Temboo website,
165 under My Account > Application Keys
166
167 The same TembooAccount.h file settings can be used for all Temboo SDK sketches.
168
169 Keeping your account information in a separate file means you can share the main .ino file without worrying
170 that you forgot to delete your credentials.
171*/
Note: See TracBrowser for help on using the repository browser.