Android Core

IoT Project: Integrate Arduino with Yahoo! using Temboo – Updated

This IoT project explores how to integrate Arduino with Yahoo! Weather. Internet of things (IoT project) is the next big thing in the near feature. This technology is growing fast and there are many objects that begin talking each other using internet. Prototyping platforms like Arduino and Raspberry PI are supporting these innovations and help many developers to create interesting Internet of things projects. One pillar of this emerging technology is the cloud IoT platforms that help developers to build Internet of things projects. One great platform, that is very useful, to build great IoT project is Temboo. To explore how to develop an IoT-based project we will connect Arduino with Ethernet shield to Yahoo! Weather information, using this information this IoT project controls an RGB led changing its color. In other articles about IoT project, we explored how external events, measured by sensors connected to Arduino, can trigger actions on Temboo platform, like sending messages and so on. This Internet of things project example uses a different approach: data retrieved from an external source (like Yahoo! Weather) can control and have effects on the prototyping board connected devices.

Set up the IoT Project: Getting Started the RGB Led

Before digging into Temboo platform details, it is useful to create a simple 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 value (Red, Green, Blue).
The figure below shows the simple sketch:

Please notice that in the picture above it is represented Arduino Uno just for simplicity. This IoT project uses Arduino MRK1000.
Recently Temboo has changed the way to use to connect boards. Instead of using Arduino UNO you can use one of the compatible boards that use Yun or Wifi101.
In this tutorial, we will use MKR1000 because it supports SSL connection, a feature required by Temboo.

The code is very simple: the board has some PWM pins that can be used for this purpose, in this sketch the PWM pins used are 3, 6, 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());
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:

YAHOO! Wether details

Important: If you get only weather temperature you can use the city name instead of woeid so you can skip this paragraph. If you want to get weather information you can read this paragraph to get the woeid.

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, 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 Yahoo! API and another one much more simpler.

YAHOO! API : WOEID

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 a JSON data like this:

{
"places":{
  "place":[
           {
            "woeid":720187,
            "placeTypeName":"Cittu00e0",
            "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":"Cittu00e0",
               "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 woeid (720187) is on the top so that it is enough to copy and paste this value.

Second Method: Website

If you don’t want to waste your time using the method one and you are looking for a fast way to retrieve woeid, you can use this website: woeid lookup

Inserting the city name and clicking on lookup you will find as result the woeid list.

Integrate Arduino with Temboo

Temboo is a great platform that uses Choreos to expose services to dev boards. Choreos are useful when building Internet of things system. To get Weather information we have to use Yahoo! Weather Choreos and then GetTemperature. In this case, the project uses the Temperature to control the RGB Led, but you can use any other parameters.
Now everything is very simple, just paste the city name you like and click on run to get weather information:

There are some other optional parameters you should consider like the Temperature unit. Before getting the code to use in your sketch is important you select the right platform and the wifi:

Once you click on run, you get the Internet of things app code that you have to copy and paste in your sketch. MKR1000 is ready to get weather information, if you run the example you get as result:

As you can see the source code is available to use in the sketch.

Control RGB Led using Temperature

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 get only the Temperature value. Notice that there is the need to clean a bit the line before converting it to an integer.

Finally, you can implement your rules to convert the integer value to 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 rules, of course, you can change them as you prefer.

All done!!

In this Internet of things project, we connected Arduino to Yahoo! Weather using Temboo chores. Arduino, with the information retrieved, controls RGB led. This IoT project was useful to experiment how to use a cloud platform and connect it to a prototyping board. You can use the idea that stands behind this IoT project and extend it to control other peripherals.

What will you create using Temboo? Let me know…..

Francesco Azzola

He's a senior software engineer with more than 15 yrs old experience in JEE architecture. He's SCEA certified (Sun Certified Enterprise Architect), SCWCD, SCJP. He is an android enthusiast and he has worked for long time in the mobile development field.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button