{"id":1135,"date":"2012-12-08T03:43:28","date_gmt":"2012-12-08T08:43:28","guid":{"rendered":"http:\/\/www.tigoe.com\/pcomp\/code\/?p=1135"},"modified":"2012-12-08T03:46:03","modified_gmt":"2012-12-08T08:46:03","slug":"sending-tweets-from-arduino-through-pachube-com","status":"publish","type":"post","link":"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/1135\/","title":{"rendered":"Sending Tweets from Arduino through Pachube.com"},"content":{"rendered":"<p>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 <a href=\"http:\/\/en.wikipedia.org\/wiki\/OAuth\">OAuth<\/a> to manage authentication, which is difficult to program on an Arduino due to its limited program memory size. \u00a0Most libraries that send tweets from an Arduino do so by handling the authentication through another site.\u00a0There are a couple of libraries that attempt to do it directly from the Arduino, but I haven&#8217;t seen one I like yet that sticks to the Arduino API style and\/or doesn&#8217;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.<\/p>\n<p>My first thought was to use If This Then That, but I don&#8217;t know the details of their API well enough, so I went with something I already knew: <a href=\"http:\/\/cosm.com\/\">Pachube<\/a>. Yes I know they have a <a title=\"Cosm.com\" href=\"http:\/\/www.cosm.com\">newer name<\/a>, but due to some complications I&#8217;m not sure are resolved, I&#8217;m using the old name. Here&#8217;s how to do it:<\/p>\n<p><!--more--><\/p>\n<h2>Understanding Triggers<\/h2>\n<p>Pachube has a nice feature called \u00a0a 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 &#8220;Show triggers&#8221; and you&#8217;ll get a button to add a new trigger below the feed&#8217;s graph. If you set up a trigger to send a twitter message, you&#8217;ll be asked to authenticate through your twitter account, then when you configure the tweet, it&#8217;ll give you an editable string that looks like this:<\/p>\n<pre>Datastream {datastream} of feed {feed} changed to {value} at {time} on #cosm at {feed_url}<\/pre>\n<p>You can edit that string however you want.<\/p>\n<p>You&#8217;re probably thinking, &#8220;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&#8221; It turns out that there&#8217;s a loophole. Those things in curly brackets are variables.<\/p>\n<h2>Understanding the Pachube HTTP CSV Request<\/h2>\n<p>Basically, they expect you to post data to the site using an HTTP request like this:<\/p>\n<pre>PUT \/v2\/feeds\/FEEDNUMBER.csv HTTP\/1.1\r\nHost: api.cosm.com\r\nX-ApiKey: YOUR_API_KEY_HERE\r\nUser-Agent: Cosm Arduino Example (FEEDID)\r\nContent-Length: 9\r\n\r\ndata, 234<\/pre>\n<p>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.<\/p>\n<p>If you&#8217;ve used Pachube with Arduino before, you probably used one of the <a href=\"http:\/\/arduino.cc\/en\/Tutorial\/PachubeClientString\">examples<\/a> that come with Arduino to do it. \u00a0In that example, you would have used this method. But it turns out that Pachube doesn&#8217;t care if the value that follows the name is a number or a string. So you can do things like this:<\/p>\n<pre>PUT \/v2\/feeds\/FEEDNUMBER.csv HTTP\/1.1\r\nHost: api.cosm.com\r\nX-ApiKey: YOUR_API_KEY_HERE\r\nUser-Agent: Cosm Arduino Example (FEEDID)\r\nContent-Length: 19\r\n\r\ntweet, Hello world!<\/pre>\n<p>So the key of this pair is &#8220;tweet&#8221; and the value is &#8220;Hello World!&#8221; Isn&#8217;t that nice? So if you edit the feed&#8217;s trigger so that it tweets on any new value of the datastream, and you change the tweet string to this:<\/p>\n<pre>{value}<\/pre>\n<p>then Pachube will tweet whatever you send as the value for {tweet}. Neat!<\/p>\n<p><span style=\"color: #000000;\">With the <a href=\"https:\/\/cosm.com\/docs\/v2\/\">CSV API for Pachube<\/a>, you&#8217;ll hit some problems. Your strings all need to be<\/span>\u00a0<a href=\"http:\/\/en.wikipedia.org\/wiki\/Urlencode\">URLencoded<\/a>. If anyone knows of a good URLencoder for Arduino, let me know. However, if you use the JSON API, it&#8217;s a little more forgiving. The HTTP request looks like this instead:<\/p>\n<pre>PUT \/v2\/feeds\/FEEDNUMBER.json HTTP\/1.1\r\nHost: api.cosm.com\r\nX-ApiKey: YOUR_API_KEY_HERE\r\nUser-Agent: Cosm Arduino Example (FEEDID)\r\nContent-Length: 79\r\n\r\n{\"id\":FEEDNUMBER,\"datastreams\":[{\"current_value\":\"Hello World!\",\"id\":\"tweet\"}]}<\/pre>\n<p>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 <a href=\"http:\/\/arduino.cc\/en\/Tutorial\/PachubeClientString\">PachubeClientString<\/a> and <a href=\"http:\/\/arduino.cc\/en\/Tutorial\/WiFiPachubeClientString\">WiFiPachubeClientString<\/a> examples were my starting point because they were already working with Strings in the Arduino sketch, so they were easy to modify.<\/p>\n<p><strong>Tip:<\/strong>\u00a0<em>You may want to get to know some of Arduino&#8217;s String functions if you&#8217;re not aware of them. They&#8217;re handy. I used String.replace() to make my sketch much simpler, as you&#8217;ll see.<\/em><\/p>\n<p>My resulting sketch (which you can find on <a href=\"https:\/\/github.com\/tigoe\/PachubeTweet\">my gitHub account<\/a>) 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.<\/p>\n<h2>Give That To Me Step By Step Again?<\/h2>\n<p><strong>Create a <a href=\"https:\/\/cosm.com\/\">Pachube<\/a> account.<\/strong><\/p>\n<p><strong>Set up a new feed by clicking on +Device\/Feed.<\/strong> Choose &#8220;Arduino&#8221;. Name your feed, and enter any descriptive tags you want (this is optional).<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddFeed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1137\" title=\"cosmAddFeed\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddFeed.png\" alt=\"\" width=\"163\" height=\"56\" \/><\/a><\/p>\n<p><strong>Click create.<\/strong> You&#8217;ll get a nice sample Arduino sketch which you can modify if you don&#8217;t feel like using my sketch (hint: this sample code is where I started my sketch).<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmCreateFeed.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1138\" title=\"cosmCreateFeed\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmCreateFeed.png\" alt=\"\" width=\"759\" height=\"358\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmCreateFeed.png 759w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmCreateFeed-300x141.png 300w\" sizes=\"auto, (max-width: 706px) 89vw, (max-width: 767px) 82vw, 740px\" \/><\/a><\/p>\n<p><strong>Note the feed ID number<\/strong> and put it into your Arduino sketch as FEEDID.<\/p>\n<p><strong>Click Datastream to edit your feed.<\/strong> Add a datastream and call it &#8220;tweet&#8221;.<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddDatastream.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1139\" title=\"cosmAddDatastream\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddDatastream.png\" alt=\"\" width=\"118\" height=\"41\" \/><\/a><\/p>\n<p><strong>Save the Feed.<\/strong><\/p>\n<p><strong>Click Show Triggers and add a new trigger.<\/strong> Choose &#8220;Twitter&#8221; and authenticate Pachube with your twitter account.<\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmShowTriggers.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1141\" title=\"cosmShowTriggers\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmShowTriggers.png\" alt=\"\" width=\"305\" height=\"43\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmShowTriggers.png 305w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmShowTriggers-300x42.png 300w\" sizes=\"auto, (max-width: 305px) 100vw, 305px\" \/><\/a><\/p>\n<p><a href=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddTrigger.png\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1142\" title=\"cosmAddTrigger\" src=\"http:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddTrigger.png\" alt=\"\" width=\"400\" height=\"43\" srcset=\"https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddTrigger.png 400w, https:\/\/www.tigoe.com\/pcomp\/code\/wp-content\/uploads\/2012\/12\/cosmAddTrigger-300x32.png 300w\" sizes=\"auto, (max-width: 400px) 100vw, 400px\" \/><\/a><\/p>\n<p>&nbsp;<\/p>\n<p><strong>Set the trigger to activate on Any New Value<\/strong> from the drop-down menu.<\/p>\n<p><strong>Configure the trigger<\/strong> to send the following:<\/p>\n<pre>{value}<\/pre>\n<p><strong>Click Finish<\/strong> to create your new trigger.<\/p>\n<p><strong>Program your Arduino<\/strong> with <a href=\"https:\/\/github.com\/tigoe\/PachubeTweet\/tree\/master\/WiFiPachubeTweeter\">this sketch<\/a> if you&#8217;re using a WiFi shield. If you&#8217;re using an Ethernet Shield, use <a href=\"https:\/\/github.com\/tigoe\/PachubeTweet\/tree\/master\/EthernetPachubeTweeter\">this sketch<\/a> instead. Change the configuration values to match your network.<\/p>\n<p><strong>Once you&#8217;ve uploaded the sketch, open the Serial Monitor<\/strong> in the Arduino IDE. Make sure it&#8217;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.<\/p>\n<p><em>Tip:<\/em>\u00a0<em>If you don&#8217;t want to annoy your followers, send yourself a direct message or an @message, like this:<\/em><\/p>\n<pre>d myTwitterAccount Hi me! I'm sending me a direct message<\/pre>\n<p><em>or<\/em><\/p>\n<pre>@myTwitterAccount Hi me! I'm sending me a message<\/pre>\n<p>You should get a reply something like this:<\/p>\n<pre>HTTP\/1.1 200 OK\r\nDate: Sat, 08 Dec 2012 06:42:10 GMT\r\nContent-Type: text\/plain; charset=utf-8\r\nConnection: keep-alive\r\nSet-Cookie: _pachcore_app_session=ds;flks;dlfk;sldfk;lskdfdsf;lksd;fl; domain=.cosm.com; path=\/; expires=Sat, 22-Dec-2012 06:42:10 GMT; HttpOnly\r\nX-PachubeRequestId: dsf;lksd;flksd;flks;dlfks;dlfk;sldfk\r\nContent-Length: 1\r\nX-Runtime: 255\r\nCache-Control: max-age=0\r\nX-PachubePurgeCache: t:feeds\/11111,web:feed:11111\r\nX-Pachube-Logging-Key: logging.dfkjsldkfjsldkfjsd\r\nVary: Accept-Encoding<\/pre>\n<p><strong>Finally, check your Twitter feed<\/strong> and see if your Arduino tweeted. If so, do a little dance now! And welcome to the wonderful world of physical-to-web mashups!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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. \u00a0Most libraries that send &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.tigoe.com\/pcomp\/code\/arduinowiring\/1135\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Sending Tweets from Arduino through Pachube.com&#8221;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-1135","post","type-post","status-publish","format-standard","hentry","category-arduinowiring"],"_links":{"self":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1135","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/comments?post=1135"}],"version-history":[{"count":4,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1135\/revisions"}],"predecessor-version":[{"id":1145,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/posts\/1135\/revisions\/1145"}],"wp:attachment":[{"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/media?parent=1135"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/categories?post=1135"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tigoe.com\/pcomp\/code\/wp-json\/wp\/v2\/tags?post=1135"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}