Integrate Arduino With Yahoo! Using Temboo
Using Temboo you can hook your Arduino up to web services. This example looks at connecting to a weather service, but lays the groundwork for you to work with your service of choice
Join the DZone community and get the full member experience.
Join For FreeBefore digging into Temboo platform details, it is useful to create a simple Arduino sketch to control the RGB led. Arduino controls this LED type using PWM (Pulse Width Modulation), so that it is possible to change the three basic color values (Red, Green, Blue). The figure below shows the simple sketch:
The code is very simple. Arduino has some PWM pins that can be used for this purpose, in this sketch the PWM pins used are 3, 6, and 5.
int delTime = 1000;
int redPin = 3;
int bluePin = 6;
int greenPin = 5;
void setup() {
pinMode(redPin, OUTPUT);
pinMode(bluePin, OUTPUT);
pinMode(greenPin, OUTPUT);
}
void loop() {
String val = "120";
Serial.print("Value " + val.toInt());
analogWrite(redPin, 255);
analogWrite(bluePin, 0);
analogWrite(greenPin, 0);
delay(delTime);
}
Running the example, Arduino controls the RGB Led as shown below:
If you are new to Yahoo! Weather, you have to know that to get weather information, Yahoo uses Woeid. This is a unique identifier assigned to all cities and areas around the world. Using this parameter, it is possible to get weather information. As you will see later, Temboo requires Woeid so that it is necessary to convert the city name to woeid.
There are two different methods: one that uses the Yahoo! API and another one that's much simpler.
This method requires you to create an account on Yahoo! and create a unique key. This can be done easily using Yahoo! developer site.
Once, the unique key is available we can get the woeid easily:
http://where.yahooapis.com/v1/places.q('City_Name')?appid=your:key&format=json
The result is JSON data like this:
{
"places":{
"place":[
{
"woeid":720187,
"placeTypeName":"Citt\u00e0",
"placeTypeName attrs":{
"code":7
},
"name":"Perugia",
"country":"Italia",
"country attrs":{
"type":"Paese",
"code":"IT",
"woeid":23424853
},
"admin1":"Umbria",
"admin1 attrs":{
"type":"Regione",
"code":"",
"woeid":7153347
},
"admin2":"Perugia",
"admin2 attrs":{
"type":"Provincia",
"code":"IT-PG",
"woeid":12591817
},
"admin3":"Perugia",
"admin3 attrs":{
"type":"Comune",
"code":"",
"woeid":12676126
},
"locality1":"Perugia",
"locality1 attrs":{
"type":"Citt\u00e0",
"woeid":720187
},
"locality2":"",
"postal":"",
"centroid":{
"latitude":43.103779,
"longitude":12.37542
},
"boundingBox":{
"southWest":{
"latitude":43.075531,
"longitude":12.32937
},
"northEast":{
"latitude":43.121311,
"longitude":12.41188
}
},
"areaRank":2,
"popRank":11,
"timezone":"Europe\/Rome",
"timezone attrs":{
"type":"Fuso Orario",
"woeid":28350914
},
"uri":"http:\/\/where.yahooapis.com\/v1\/place\/720187",
"lang":"it-it"
}
],
"start":0,
"count":1,
"total":3
}
}
The wooed (720187) is on the top so that it is enough to copy and paste this value.
If you don't want to waste your time using this method and you are looking for a fast way to retrieve woeid, you can use this website: woei lookup
Inserting the city name and clicking on lookup you will find your result.
Temboo is a great platform that uses Choreos to expose services to Arduino or other dev platforms. To get Weather information we have to use Yahoo! Weather Choreos and then GetTemperature. In this case, the project uses Temperature to control the RGB Led, but you can use any other parameters.
Now everything is very simple, just paste the woeid you got before and click on run to get weather information.
In this example woeid is used but you can get the weather information using Address, for example. However, I prefer woeid as it is much simpler.
There are some other optional parameters you should consider like the Temperature unit and the response format:
Once you click run, you get the Arduino code that you have to copy and paste in your sketch. Arduino is ready to get weather information, if you run the example you get this result:
As you can see the source code is available to use in the sketch.
Now that we have all the pieces in our hands we have to mix them to make things working.
The sketch with Temboo code, copied previously, has to be modified so that the result returned is used to change the RGB Led color.
Let's slightly modify the code so that we only get the Temperature value:
String line;
String lines[4];
int counter = 0;
while(GetTemperatureChoreo.available()) {
char c = GetTemperatureChoreo.read();
if (c == '\r' || c == '\n') {
Serial.println("Line ["+line+"]");
lines[counter++] = line;
line = "";
}
else
line += c;
}
String tempLine;
// Clean line
for (int i=1; i < lines[3].length(); i++) {
tempLine += lines[3].charAt(i);
}
int temp = tempLine.toInt();
Serial.println(temp);
Notice that there is the need to clean up the line before converting it to an integer.
Finally, you can implement your rules to convert the integer value to an RGB color:
...
// Now let's countrol the led
int r = ( f1(temp, 20) * 5 ) % 255;
int g = ( f1(temp, 10) * 5) % 255;
int b = ( f2(temp, 5) * 5) % 255;
setRGBColor(r,g,b);
GetTemperatureChoreo.close();
....
Then add the RGB function to control the LED:
void setRGBColor(int red, int green, int blue) {
analogWrite(redPin, red);
analogWrite(bluePin, blue);
analogWrite(greenPin, green);
}
The functions f1,f2
are very simple:
int f1(int val, int threshold) {
if (val < threshold)
return 0;
return val;
}
int f2(int val, int threshold) {
if (val < threshold)
return abs(val);
return 0;
}
They are just a simple set of rules, of course you can change them as you prefer.
All done!!
In this Internet of things project we connected an Arduino to Yahoo! Weather using Temboo choreos. Arduino, with the information retrieved, controls an RGB LED.
What will you create using Temboo? Let me know...
Published at DZone with permission of Francesco Azzola, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments