How to Tweet From an Arduino Using the Wifi Sheild

73K6325

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

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.


24 Comments

Hey everyone, i need a little modification about the content of the tweet.I have a txt file which contain 1000 word and i want to tweet them daily as 3-4 of them at once, per day.How can i import my data in to the program. Any ideas would be very appreciated.

Does anyone know if it is still possible to tweet from an Arduino with WiFi connectivity? All the solutions appear to be from 2012 or so and I read that the Twitter API no longer supports this capability. Any insights?

is anyone else getting a 404 on the URL request?

to specify, I'm receiving a 404 Error on the twitter code above:

connecting ...

HTTP/1.0 404 Not Found

Date: Fri, 16 May 2014 16:18:28 GMT

Server: Check Point SVN foundation

Content-Type: text/html

Connection: Close

Last-Modified: Mon, 14 Mar 2011 14:45:46 GMT

Content-Length: 204

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<TITLE> 404 File Not Found </TITLE>

</HEAD>

<BODY>

The URL you requested could not be found on this server.

</BODY>

</HTML>

failed : code 404

Is there anything more compact then arduino w wifi shield? if I was looking for a wifi capable device?

There are many but what works for you will be totally dependant on your requirements

Have you ever faced Arduino stopped from tweeting? mine goes whole day but I guess it's ram will be full and stops tweeting!

Yeah sometimes its needs to be reset. Let me know if you figured out a way to do it otherwise.

Hi ,
If I want to tweet corresponding to a situation which means I need to tweet many times, what change should I make in above code?
I have not tried with the new version but it looks like it works great with the latest version according to comments by others.

for data logging, you can use an SD for more permanent log. or just print to the debug using Serial.print(var);
Hi ... Googling I found the problem ...!

Apparently with the latest versions of the IDE does not work well the library twitter.
Download the version 1.0.3 and everything is OK ..!

http://arduino.cc/en/Main/Software

(attached screenshot)
Greetings ...!
I can verify that it was the IDE version. Downloaded 1.0.3 and solved my problem on another project. Thanks for your notes stanleyatala!

(arduino wifi shield connected but not tweeting)
Awesome !! :)

Yeah I think I had some version issues as well when I did it.. Great to see that you got it to work. Great job!

Alex
Hi,
Thank you for your comprehensive description.I did what you mentioned. But the red LED on my wifi shield is on and yet I do not get the tweet! I would be grateful if you answer.
Hello,

If the red LED is on, then you have an issue connecting to the wifi. Notice on the picture I marked red LED as an error. If your connection to the internet works you need the green LED to light up.

This could be due to the following reason
- wifi protocol missmatch - there are 3 versions of there protocol - no authentication. WEP and WPA - the code for connecting to the wifi is different for these 3 protocol. Arduino example has example code for each of these 3. Make sure you get the right protocol.
- wifi authentication - make sure you got the wifi username and password right.

Hope that helps.
Alex, thank you very much for your comments.
I'm going to be attentive to your new instructables ... (when you can).
Greetings.!
hello echoalex, I have an unrecognized problem:
(first; sorry by my english, I speak Spanish..)

-My Twitter account has the app arduino Ok (http://arduino-tweet.appspot.com).
-I use your library.
-I change in the the sketch, "Token" by indicated Ok.
-I change ssid / pass by my wlan Ok.
-Compile and send the sketch to Arduino Uno + Wifi Ok.
-The shield is connected wifi Ok (ping).
-By the way, I opened my WLAN ports Modem.

but I can not make it work ...!
I have the serial error:

connecting ...
connection failed.

(attached print)

The app still works (authentication) Ok or The library is still in force??
You have any advice for me?, Pls
Hey Stanley,

I have not used that library for about 6 months now. For the tweeting tree project I have created my own website backend instead of going through http://arduino-tweet.appspot.com. So I am not sure if that library is functional now. The reason I moved to my own backend is because arduino resets when processing large text operations. So now I am using arduino for just data logging on my site and from my site i send the tweet. so it looks as if the arduino/tree is tweeting.

Back to your problem. Obviously twitter.post(buf) inside your if condition is the one that is giving you the problem. Check the twitter.h and twitter.cpp . It is possible that with the wifi shield under active development, the code could have been altered. Check it out.

From my experience, if you can code a web page, then data dump your sensor data to your site and write your tweet logic on the site. If you can code php, I can send you my website code and new arduino code. I might write an instructable on that but that will take a while

Hope that helps. Feel free to ask if need more clarifications.

Alex
www.makeystreet.com
Dude this is cool, i want to control motors over wifi can u guide me what to do?
Thanks man ... !! If you want to control the motors wirelessly you can use zigbee. But if you still want to use wifi to control, the hard way to do that is to write a webpage which has the instructions for the motor and using the arduino wifi shield to keep polling that webpage.

You can program the webpage in any way you want. I hope that made sense. I would be glad to elaborate if necessary.
More Comments