Book Image

Internet of Things with the Arduino Yún

By : Marco Schwartz
Book Image

Internet of Things with the Arduino Yún

By: Marco Schwartz

Overview of this book

<p>Internet of Things (IoT) is currently a growing trend in the technology space, and the Arduino Yún is the perfect board to get started with building IoT projects. This book covers many of the powerful features of the Arduino Yún via four exciting projects. The first project is all about sending weather measurements data to a Google Docs spreadsheet for easy online visualization. The second one is about building an energy consumption meter and controlling devices remotely. The third focuses on the field of security, by helping you to build a camera that is triggered by motion and that uploads pictures automatically to Dropbox. Finally, the last project is in relation with the field of robotics, and focuses on building a robot that is controlled via Wi-Fi. <br /><br />The main focus of this book is to teach everything you need to know to build complex projects using the Arduino Yún, organized around the fields of home automation, security, and robotics.</p>
Table of Contents (11 chapters)

Making your Arduino Yún board tweet sensor data


Finally, in the last part of this project, we will make your Arduino Yún board send its own messages on Twitter. You can even create a new Twitter account just for your Yún board, and you can tell people you know to follow it on Twitter so that they can be informed at all times about what's going on in your home!

The project starts on the Twitter website because you have to declare a new app on Twitter. Log in using your Twitter credentials and then go to https://apps.twitter.com/.

Now, click on Create New App to start the process, as shown in the following screenshot:

You will need to give some name to your app. For example, we named ours MyYunTemboo. You will need to get a lot of information from the Twitter website. The first things you need to get are the API key and the API secret. These are available in the API Keys tab, as shown in the following screenshot:

Make sure that the Access level of your app is set to Read, Write, and Direct messages. This might not be active by default, and in the first tests, my Arduino board did not respond anymore because I didn't set these parameters correctly. So, make sure that your app has the correct access level.

Then, you are also going to need a token for your app. You can do this by going to the Your access token section. From this section, you need to get the Access token and the Access token secret. Again, make sure that the access level of your token is correctly set.

We can now proceed to write the Arduino sketch, so the Arduino Yún board can automatically send tweets. The Twitter Choreo is well known for using a lot of memory on the Yún, so this sketch will only tweet data without logging data into your Google Docs account. I also recommend that you disable any debugging messages on the serial port to preserve the memory of your Yún. In the sketch, you first need to define your Twitter app information, as shown in the following code:

const String TWITTER_ACCESS_TOKEN = "yourAccessToken";
const String TWITTER_ACCESS_TOKEN_SECRET = " yourAccessTokenSecret";
const String TWITTER_API_KEY = " yourApiKey";
const String TWITTER_API_SECRET = " yourApiKeySecret";

Then, the sketch will regularly tweet the data about your home with the following function:

tweetAlert(lightLevel, temperature, humidity);

This function is repeated every minute using a delay() function, as follows:

delay(60000); 

Of course, this delay can be changed according to your needs. Let's see the details of this function. It starts by declaring the correct Choreo to send updates on Twitter:

TembooChoreo StatusesUpdateChoreo;

Then, we build the text that we want to tweet as a string. In this case, we just formatted the sensor data in one string, as shown in the following code:

String tweetText = "Temperature: " + String(temperature) + ", Humidity: " + String(humidity) + ", Light level: " + String(light);

The access token and API key that we defined earlier are declared as inputs:

StatusesUpdateChoreo.addInput("AccessToken", TWITTER_ACCESS_TOKEN);
StatusesUpdateChoreo.addInput("AccessTokenSecret", TWITTER_ACCESS_TOKEN_SECRET);
StatusesUpdateChoreo.addInput("ConsumerKey", TWITTER_API_KEY);  
StatusesUpdateChoreo.addInput("ConsumerSecret", TWITTER_API_SECRET);

The text that we want to tweet is also simply declared as an input of the Twitter Choreo with the string variable we built earlier:

StatusesUpdateChoreo.addInput("StatusUpdate", tweetText);

The complete code for this part can be found at https://github.com/openhomeautomation/geeky-projects-yun/tree/master/chapter1/temboo_twitter.

Now that the Arduino sketch is ready, we can test it. You can simply upload the code to your Arduino Yún, and wait for a moment. Your board should automatically connect to the Twitter feed that you chose and print the data as a new message, as shown in the following screenshot:

If nothing shows up on your Twitter account, there are several things that you can check. I already mentioned memory usage; try to disable the debug output on the serial port to free some memory. Also, make sure that you have entered the correct information about your Twitter app; it is quite easy to make a mistake between different API keys and access tokens.

For this project, I used the Twitter account of my website dedicated to home automation, but of course, you can create a dedicated Twitter account for the project so that many people can follow the latest updates about your home!

You can also combine the code from this part with the idea of the previous section, for example, to create automated alerts based on the measured data and post messages on Twitter accordingly.