How do you read and write twitter messages?

I told you I'd be back, but the issue I'm having now is that I saw on arduino.cc that the twitter client library is not ever going to work b/c of something with twitter. So, I wanted to read and write messages from twitter, like or exactly the temperature in my room, when request from a manual twitter post. So, how would I go about that now? Please help.

Portal:
I saw on arduino.cc that the twitter client library is not ever going to work b/c of something with twitter.

The "something" is called https, or more generally, secure sockets (SSL/TLS).

You can read this thread to understand the problem:

http://forum.arduino.cc/index.php?topic=197862.0

You have to use some sort of proxy system (like RFXduino) to connect to twitter (or to any https server) from an Arduino-class device.

(This probably belongs in "Networking, Protocols, and Devices")

Is it acceptable to have your solution rely on a PC - preferably one in your room? Reading and writing twitter messages from a PC is relatively simple, and it's not hard to have an Arduino and a PC work together.

I have go no experience in any sort of Networking at all. So I can't use twitter and my arduino to talk, as illustrated in the twitter client library.

Here is my arduino sketch, that posts to another server that is under my control. that's your first task, get that working. Then on your other computer (it's a raspberry pi in my case) I have some ruby code like this:

#!/bin/env ruby
require 'rubygems'
require 'twitter'
require 'cgi'

cgi = CGI.new
puts cgi.header

Twitter.configure do | config |
  config.consumer_key = "my consumer key from dev.twitter.com"
  config.consumer_secret = cgi['c']
  config.oauth_token = "my oauth token from dev.twitter.com"
  config.oauth_token_secret = cgi['a']
end
Twitter.update cgi['t']

puts '<html><body>OK!</body></html>'

I have apache on that server so I had to add in

AddHandler cgi-script .rb

to let it know tweet.rb was a cgi script.

so my arduino does something like

POST myhost.com/tweet.rb c=mysecret&a=myoauthsecret&t=air+quality+reading+is+666

the script on myhost.com then picks up those params and passes them on to twitter. It's simple, yet convoluted at the same time... I put some of the config params into the arduino sketch for some vague idea of security -if I put them all in the arduino sketch that's too much data really, and if I put them all in the ruby script (or environment vars that that reads) that leaves the ruby script open to accidentally being hit and posting on its own. I can still test just the ruby script independently by using something like

curl -v --data 'c=mysecret&a=myoauthsecret&t=air+quality+reading+is+666' myhost.com/tweet.rb

on the command line...

My results are being spammed posted at http://twitter.com/ourduino if you are interested... This is not really a useful long term thing, nor very well put together but it is nice as a demo of "when this changes I can trigger something out there"

APC’s May 2023 issue is on sale now! | TechRadar - I could not previously get the Twitter library working, so I went with my proxy. This has helped me out a load, though all these libraries make the script quite heavy...

My version below:

// From http://apcmag.com/how-to-tweet-with-your-arduino.htm

#include <SPI.h>
#include <Ethernet.h>
#include <sha1.h>
#include <Time.h>
#include <EEPROM.h>
#include <Twitter.h>
#include "Tweet.h"

#define TIME_BETWEEN_NAGS 60

#define AIRQUALITYPIN A0
int smell = 0;  // air quality value

byte mac[] = { 0x64, 0xA7, 0x69, 0x0D, 0x21, 0x21 };

IPAddress twitter_ip( 199, 59, 150, 39 );
uint16_t twitter_port = 80;

unsigned long last_tweet = 0;

#define TWEET_DELTA (5L * 60L)

char buffer[512];

Twitter twitter( buffer, sizeof( buffer ));

void setup ( ) {
  Serial.begin( 115200 );
  Serial.println( F( "Sniff tweeter" ));

  if ( Ethernet.begin( mac )) {
    Serial.print( F( "DHCP: " ));
    Ethernet.localIP( ).printTo( Serial );
    Serial.println( "" );
  }
  else {
    Serial.println( F( "DHCP configuration failed" ));
  }
  twitter.set_twitter_endpoint( PSTR( "api.twitter.com" ), PSTR( "/1.1/statuses/update.json" ), twitter_ip, twitter_port, false );
  twitter.set_client_id( consumer_key, consumer_secret );
  twitter.set_account_id( oauth_token, access_token_secret );
  delay( 500 );
}

void loop ( ) {
  delay( 1000 );
  smell = analogRead( AIRQUALITYPIN );
  if ( smell > 500 ) {
    Serial.print( F( "air: " ));
    Serial.println( smell );
    if ( twitter.is_ready( )) {
      unsigned long now = twitter.get_time( );
      if ( last_tweet == 0 ) {
        Serial.print( F( "Now: " ));
        Serial.println( now );
        last_tweet = now - TWEET_DELTA + 15L;
      }
      if ( now > last_tweet + TWEET_DELTA ) {
        char msg[140];
        sprintf( msg, "Smell; air: %d #arduino", smell );
        Serial.print( F( "Tweeting: " ));
        Serial.println( msg );
        last_tweet = now;
        if ( twitter.post_status( msg )) {
          Serial.println( F( "Updated" ));
        }
        else {
          Serial.println( F( "failed" ));
        }
      }
    }
    delay( 5000 );
  }
}
// Tweet.h
const static char consumer_secret[43] PROGMEM = "";
const static char access_token_secret[46] PROGMEM = "";
const static char consumer_key[ ] PROGMEM = "";
const static char oauth_token[ ] PROGMEM = "";

How easy is it to modify the sketch so that it works with the WiFi Sheild. We're having real difficultly trying to hack it together. Any tips would be greatly appreciated :slight_smile:

sciencerewired:
How easy is it to modify the sketch so that it works with the WiFi Sheild. We're having real difficultly trying to hack it together. Any tips would be greatly appreciated :slight_smile:

In theory, if you can make it work with a standard wired Ethernet shield, you should be able to get it to work using a WiFi shield. In practice, however, the inherent instability of the WiFI shield firmware (at least at current revisions) will always be an Achilles heel, and a potential source of difficulty and frustration whatever you do.

There is currently a thread that will bring you roughly up to the state of play with the WiFi shield here:

http://forum.arduino.cc/index.php?topic=166151

Going into a few pages now, but worth a read if you have a WiFi shield, or are thinking of getting one.

The simplest, cheapest and most robust way of sending tweets wirelessly from an Arduino (or Arduino-like) device is using the RFXduino TCP/IP gateway. Disclaimer: I sell these, but I'd say that anyway, because it's true.

You can literally send a tweet with three lines of code in your sketch:

if (rfx.start_alert_request(ALERT_TWEET,5000)) 
  rfx.println("This is a Twitter alert sent from your sketch!");
rfx.stop_alert_request();

The full details, including a complete short demo sketch is here (click on "RFX Alerts: How to send a Tweet"):

http://embeddedcoolness.com/docs/

I developed the RFXduino gateway system because there was simply no reasonable, satisfactory and cost-effective way to get Arduinos wirelessly connected to the Internet. (At least not for the $$$ I was willing to spend.) And even if you spend the big bucks on something like a WiFi shield, you still don't necessarily get a good result. Whereas a WiFI shield is lucky to stay up and running for a few days, I've had Arduino clients connecting through the RFXduino gateway for weeks and months at a time. A well thought-out and rational architecture has its advantages.

Simpler, cheaper, more powerful. It's my tagline. :wink:

Thanks for the reply, looks like an interesting solution to the issue we're facing.

We can tweet relatively easily with the WiFi Shield, but when we try to understand the complex world of OAuth and Twitter, it becomes too difficult and we don't have the time to invest in understanding all the issues. Which of course leads to frustration.

We would be very interested in the solution you mention. In essense we are after a gateway to enable us to

  1. read tweets from a timeline into an Arduino sketch and act accordingly i.e. LED on, write to LCD, make a noise etc
  2. send tweets to a named Twitter account based on inputs coming from the Arduino board i.e. temperature, light, and other sensors.

If the tech you are referring to is able to provide this functiobnality, OOTB without too much coding and faffing around, we'll gladly invest time and money into purchasing.

Will the tech support our needs? We have an Arduino Duo at the moment.

ADDITION. The tech we need has to be completely Wireless and standalone - it will not connect to a LAN, but a Wireless network with a 9V battery and be out doors for most of it's time

Cheers,
Ande

PM sent. We may have to discuss some possible issues like a suitable power supply, but otherwise the specs look fine.