Sending Tweets from Arduino through

I get a lot of people asking me how to send tweets from an Arduino through an Ethernet or WiFi shield. It turns out to be a bit tricky, because Twitter uses OAuth to manage authentication, which is difficult to program on an Arduino due to its limited program memory size.  Most libraries that send tweets from an Arduino do so by handling the authentication through another site. There are a couple of libraries that attempt to do it directly from the Arduino, but I haven’t seen one I like yet that sticks to the Arduino API style and/or doesn’t take up a lot of memory (though I am ever hopeful someone will write one). Since not everyone knows how to write a server-side middleware program to handle the authentication, I decided to see if I could get an existing service to take care of it for me.

My first thought was to use If This Then That, but I don’t know the details of their API well enough, so I went with something I already knew: Pachube. Yes I know they have a newer name, but due to some complications I’m not sure are resolved, I’m using the old name. Here’s how to do it:

Understanding Triggers

Pachube has a nice feature called  a trigger that automatically makes an HTTP call or sends a tweet for you when incoming data on a feed meets a condition that you can set. When you set up a new feed, click “Show triggers” and you’ll get a button to add a new trigger below the feed’s graph. If you set up a trigger to send a twitter message, you’ll be asked to authenticate through your twitter account, then when you configure the tweet, it’ll give you an editable string that looks like this:

Datastream {datastream} of feed {feed} changed to {value} at {time} on #cosm at {feed_url}

You can edit that string however you want.

You’re probably thinking, “but Pachube is a site for uploading and graphing numerical data; how can I send custom tweets through them? This lets me set a set string, but what if I want a variable string” It turns out that there’s a loophole. Those things in curly brackets are variables.

Understanding the Pachube HTTP CSV Request

Basically, they expect you to post data to the site using an HTTP request like this:

PUT /v2/feeds/FEEDNUMBER.csv HTTP/1.1
User-Agent: Cosm Arduino Example (FEEDID)
Content-Length: 9

data, 234

You make a PUT request, and your data is posted as comma-separated values (CSV) with the key (i.e. the name) of the datastream first, and the value second.

If you’ve used Pachube with Arduino before, you probably used one of the examples that come with Arduino to do it.  In that example, you would have used this method. But it turns out that Pachube doesn’t care if the value that follows the name is a number or a string. So you can do things like this:

PUT /v2/feeds/FEEDNUMBER.csv HTTP/1.1
User-Agent: Cosm Arduino Example (FEEDID)
Content-Length: 19

tweet, Hello world!

So the key of this pair is “tweet” and the value is “Hello World!” Isn’t that nice? So if you edit the feed’s trigger so that it tweets on any new value of the datastream, and you change the tweet string to this:


then Pachube will tweet whatever you send as the value for {tweet}. Neat!

With the CSV API for Pachube, you’ll hit some problems. Your strings all need to be URLencoded. If anyone knows of a good URLencoder for Arduino, let me know. However, if you use the JSON API, it’s a little more forgiving. The HTTP request looks like this instead:

PUT /v2/feeds/FEEDNUMBER.json HTTP/1.1
User-Agent: Cosm Arduino Example (FEEDID)
Content-Length: 79

{"id":FEEDNUMBER,"datastreams":[{"current_value":"Hello World!","id":"tweet"}]}

Once I realized this, it was simply a matter of modifying the standard Pachube example that comes with the Arduino Ethernet or WiFi libraries to do the job. The PachubeClientString and WiFiPachubeClientString examples were my starting point because they were already working with Strings in the Arduino sketch, so they were easy to modify.

Tip: You may want to get to know some of Arduino’s String functions if you’re not aware of them. They’re handy. I used String.replace() to make my sketch much simpler, as you’ll see.

My resulting sketch (which you can find on my gitHub account) listens for new characters to come in from the serial port, and adds them to a string to be sent. If it gets a newline character, or the total length of the string is 140 characters, it posts to Pachube, and Pachube sends the string as a tweet.

Give That To Me Step By Step Again?

Create a Pachube account.

Set up a new feed by clicking on +Device/Feed. Choose “Arduino”. Name your feed, and enter any descriptive tags you want (this is optional).

Click create. You’ll get a nice sample Arduino sketch which you can modify if you don’t feel like using my sketch (hint: this sample code is where I started my sketch).

Note the feed ID number and put it into your Arduino sketch as FEEDID.

Click Datastream to edit your feed. Add a datastream and call it “tweet”.

Save the Feed.

Click Show Triggers and add a new trigger. Choose “Twitter” and authenticate Pachube with your twitter account.


Set the trigger to activate on Any New Value from the drop-down menu.

Configure the trigger to send the following:


Click Finish to create your new trigger.

Program your Arduino with this sketch if you’re using a WiFi shield. If you’re using an Ethernet Shield, use this sketch instead. Change the configuration values to match your network.

Once you’ve uploaded the sketch, open the Serial Monitor in the Arduino IDE. Make sure it’s set to send a newline when you hit enter, from the drop-down menu at the bottom. Type a message in the Serial monitor and send it.

Tip: If you don’t want to annoy your followers, send yourself a direct message or an @message, like this:

d myTwitterAccount Hi me! I'm sending me a direct message


@myTwitterAccount Hi me! I'm sending me a message

You should get a reply something like this:

HTTP/1.1 200 OK
Date: Sat, 08 Dec 2012 06:42:10 GMT
Content-Type: text/plain; charset=utf-8
Connection: keep-alive
Set-Cookie: _pachcore_app_session=ds;flks;dlfk;sldfk;lskdfdsf;lksd;fl;; path=/; expires=Sat, 22-Dec-2012 06:42:10 GMT; HttpOnly
X-PachubeRequestId: dsf;lksd;flksd;flks;dlfks;dlfk;sldfk
Content-Length: 1
X-Runtime: 255
Cache-Control: max-age=0
X-PachubePurgeCache: t:feeds/11111,web:feed:11111
X-Pachube-Logging-Key: logging.dfkjsldkfjsldkfjsd
Vary: Accept-Encoding

Finally, check your Twitter feed and see if your Arduino tweeted. If so, do a little dance now! And welcome to the wonderful world of physical-to-web mashups!

This entry was posted in arduino/wiring. Bookmark the permalink.

One Response to Sending Tweets from Arduino through

  1. Pingback: Easily send tweets from Arduino using cosm « freetronicsblog