Introduction: How to Tweet From an Arduino Using the Wifi Sheild

About: Find my maker profile on alpha.makeystreet.com/maker/alex/

Hey, I am a big fan of Instructables. I have consistently used it for the past 3 years and now its time for me to write one myself. 


Here we go. This instructable is for those who want to make some inanimate object tweet automatically. Like say for example you are building a robot and you want the robot to tweet to you or to the world. This obviously means your robot needs to have access to the internet and should have a logic controller. This instructable is specifically about how to use an arduino (logic controller) with wifi shield (the stuff that connects to the internet) to tweet to the world.

Hope you will find it useful.

Step 1: Arduino - Whats It? (skip If You Know)

Arduino is the quickest way you can attach logic to your project. Say for example you want your light outside your room to switch on automatically when the sun goes down. How do you do it. You want some sort of sensor that detects sunlight. You also want your light that you want to turn on/off. Most importantly you want a controller where which can read data from sensor and appropriately command the light to turn on/off . Arduino is one such controller. In fact arduino is the most popular and easiest way to implement your controller. 

If you are new to arduino you might want to check out this instructable - https://www.instructables.com/id/Intro-to-Arduino/

If you want to get a feel for what are arduino used for and want to spark some ideas - check this out - https://www.instructables.com/technology/arduino/

Here the official arduino website- http://www.arduino.cc/         (you might be a little overwhelmed if you are a complete newbie with electronics. In which case you might want to check out the intro to arduino instructable first)

The picture shows two of the most popular arduinos

Step 2: Wifi Shield - Whats It? (skip If You Know)

The awesome thing about arduino is that the capabilities of arduino is very modular. What this means is that the main arduino board contains the basic functionality. If you want some extra capabilities you add something known as a "shields" to your arduino. Some examples of shields are if you want an SD card or ethernet port or wifi capabilities. 

The wifi shield is something that was released recently by the arduino community. This is what we are going to be using. 

Step 3: Testing Wifi Shield

You will need the latest arduino software for this. Download arduino 1.02 from the arduino website - http://arduino.cc/en/Main/Software

Connect your wifi shield to your arduino controller.
I am going to be using an arduino uno r3 for this instructable. With the arduino uno r3 all you have to do is press fit the shield on uno and you are good to go. If you are using a mega or an older version of uno - you might want to follow instruction on the official wifi shield getting started page - http://arduino.cc/en/Guide/ArduinoWiFiShield

You can test your wifi shield running one of the example codes on wifi library. Run the "scan network" example code which searches for available wifi signals and displays them. 

Step 4: Get Twitter Library

First you need to get the twitter library and add that to your arduino software. You can get it from here.  http://www.arduino.cc/playground/Code/TwitterLibrary 

When you go through that you will notice that specific library works with an ethernet shield not with the wifi shield. The community has not updated it yet. The wifi shield is a new player. Dont worry go ahead and install the library as instructed in the link.
if you are on linux - go to this folder - /home/<user name>/sketchbook/libraries. If libraries folder does not exist dont panic. Just create it. 

On restarting the arduino IDE , if you can see the twitter library, then your twitter library is successfully installed. You can see the twitter library under sketch tab import library-> twitter (listed at the bottom under contributed)

In order to make the library work for your wifi shield you need to make some modifications to the twitter.cpp and twitter.h files in your library. These changes are as mentioned in this link - http://arduino.cc/forum/index.php?topic=121483.0

Note this hack will leave your twitter library incompatible with ethernet shield. What the heck - who needs compatibility with ethernet shield when you have the wifi shield. What you need to do is download these two files - twitter.cpp and twitter.h and replace the original once inside your library folder. Remember the library folder is "/home/<username>/sketchbook/libraries/twitter/ on ubuntu ~/Documents/Arduino/libraries (Mac) or My Documents\Arduino\libraries\ (Windows)" . Just in case you want to use the twitter library with the ethernet shield you might want to create a backup copy of twitter.cpp and twitter.h before over writting.

Once you are done restart the arduino IDE.

Step 5:

Now we are all set to tweet. The twitter library works by connecting your arduino to a specific website which in turn will connect to the twitter server. This workaround is because the twitter authentication is a bit code heavy which will be difficult to implement on the arduino. Instead this code heavy part is implemented on the website and arduino instructs the website to tweet on your behalf. 

So this is the website that does it - http://arduino-tweet.appspot.com/

You need to go to the website link above and do the first step. In this step you are authenticating the website to tweet on your behalf. Enter your username and password and you should be redirected back to a page which contains a token ... Copy and save this token .. you will need this. 

You can skip step 2. We already did that.

Step 3 mentioned on the website works only for the ethernet shield. Here is the work around for wifi shield. I am assuming that you are using wifi WPA protocol. I dont have access to a WEP wifi protocol. So you will have read through the arduino wifi library examples to work with wifi WEP protocol.

Open the arduino IDE.Paste this code in a new sketch.
#include <SPI.h> // needed in Arduino 0019 or later
#include <WiFi.h>
#include <Twitter.h>


char ssid[] = "Micromax A52";  //  your network SSID (name)
char pass[] = "password";  // your network password

// Your Token to Tweet (get it from http://arduino-tweet.appspot.com/)
Twitter twitter("Your token here"); 

// Message to post
char msg[] = "Automatic tweet!";

void setup()
{
  delay(1000);
  WiFi.begin(ssid, pass);
  // or you can use DHCP for automatic IP address configuration.
  // WiFi.begin(mac);
  delay(10000);
  Serial.begin(9600);

  Serial.println("connecting ...");
  if (twitter.post(msg)) {
    // Specify &Serial to output received response to Serial.
    // If no output is required, you can just omit the argument, e.g.
    // int status = twitter.wait();
    int status = twitter.wait(&Serial);
    if (status == 200) {
      Serial.println("OK.");
    } else {
      Serial.print("failed : code ");
      Serial.println(status);
    }
  } else {
    Serial.println("connection failed.");
  }
}

void loop()
{
}

In the code edit 4 things - your wifi networks name, the network password the token you got from http://arduino-tweet.appspot.com/
and the text that you want to tweet. 

Upload the sketch and check the serial monitor of the arduino. If you see something similar to that seen on the image in 10 secs then your tweet has been successfully posted. Check your twitter account.

Get creative and make something out of this. I am working on making a tweeting tree. This instructable is something that I am using for the project. Maybe once I am done with the project, I will write an instructable on how to make a tweeting tree.