Tutorial: Sending Tweets from Spark Core

Is there a tutorial or Topic on how to tweet form your core when motion is detected? or just to receive a notification when motion is detected by the core through a motion detector?

Okay guys, this code by SaraJo works perfectly. The instructions are pretty simple.

  1. Go to this site and follow Step 1. Save the long string you get (OAuth Key), you’ll need this later!
  2. Paste the code I’ve provided below into a new Sketch in the WebIDE.
  3. At the top of the code you’ll see a line called #define TOKEN "OAuth". Replace the word OAuth with the long string you got in Step 1. (Be sure you keep the quotes around your OAuth string!)
  4. At the very top of the code you’ll find a line called char msg[] = "Hi, I'm a tweet from a Spark Core!";. Replace that sentence with whatever you want to tweet!
  5. That’s it! Flash the sketch to your Core and check your Twitter account!

Caveats: You can only send around one tweet per minute, so don’t run this code in the main loop!

If this works for you guys I’ll wrap it up into a neat little function.

Code:

// Message to Post
char msg[] = "Hi, I'm a tweet from a Spark Core!";

// OAuth Key
#define TOKEN "OAuth"

// Twitter Proxy
#define LIB_DOMAIN "arduino-tweet.appspot.com"

TCPClient client;

void setup()
{
    delay(1000);

    client.connect(LIB_DOMAIN, 80);
    client.println("POST /update HTTP/1.0");
    client.println("Host: " LIB_DOMAIN);
    client.print("Content-Length: ");
    client.println(strlen(msg)+strlen(TOKEN)+14);
    client.println();
    client.print("token=");
    client.print(TOKEN);
    client.print("&status=");
    client.println(msg);
}

void loop() {
    
}
5 Likes

Thanks @timb! Worked first time :smile:

1 Like

Awesome! I’m glad everything made sense.

I’ll go ahead and wrap that code into a little function so you can call it from anywhere in your sketch! I’ll add some protection so you can only post once every 60 seconds (if you try to do it more often the function will return an error).

1 Like

Although…

Having said that I’ve just tried to run the same code again (well over a minute later) and this time it’s not working.

On reading the “arduino-tweet.appspot.com” page it says “Twitter seems to reject repeated tweets with the same contenet (returns error 403).”

So I ran the code again and changed the content of the tweet and it worked fine.

Is there a way to subtly change the tweet each time, by adding a timestamp maybe, to get round this problem?

Won’t a Delay (total miliseconds) in the code work?

I think it’s important to know that Twitter is really protective of its resources and api. If you hit their servers too quickly, or tweet the same thing over and over, they’ll send you ‘back off’ http error codes, and eventually if you keep tweeting too aggressively you could get banned from tweeting altogether.

I would try to be careful when using their service, and try not to send anything faster than once a minute or so, and maybe even less often if you can - so your projects can be good internet citizens. :slight_smile: They describe their rate limiting here:

https://dev.twitter.com/docs/rate-limiting/1.1

1 Like

I’m just trying to get a PIR sensor to tweet when it goes off. Which is hopefully never as that would mean someone is in my house without my say so.

But if you can’t tweet the same message (eg “Movement Detected”) more than once then how do you get it to reset so it can send another tweet at a later date?

Ah, totally, in that case a date or time stamp makes perfect sense. You could use the NTP and Time.h examples to grab a current timestamp.

Or use my SparkTime library

Here’s the general idea:

UDP UDPClient;
SparkTime rtc;

unsigned long currentTime;
unsigned long lastTime = 0UL;
String tweetStr;
char msg[128];    

void setup() {
  rtc.begin(&UDPClient, "north-america.pool.ntp.org");
  rtc.setTimeZone(-5); // gmt offset
}

void loop() {
    currentTime = rtc.now();
    if (rtc.minute(currentTime) != rtc.minute(lastTime)) {
      tweetStr = "Motion detected at ";
      tweetStr += rtc.ISODateString(currentTime);
      tweetStr.toCharArray(msg,128);
      // connect to twitter here and POST update
      lastTime = currentTime;
    }

[EDIT: removed extra close paren–sorry about that!]

2 Likes

Nice! That’s so slick!

I was with you up until “Here’s the general idea:”

But I’m getting closer…

Although when I try to compile your code I get the following error:

twitter_post.cpp: In function ‘void loop()’:
twitter_post.cpp:70:49: error: expected ‘;’ before ‘)’ token
make: *** [twitter_post.o] Error 1

EDIT: Got it working now :smile:

Thanks for your tutorial , worked the first time smoothly ! Very easy … easier than SaraJo’s tutorial. No offence…

Hi this is a great contribution but i’m having some troubles to get this works in my spark.
I follow all the steps at the moment that i flash the spark core down it says ready but it doesn’t work.
If you have any idea please help me.

@juanpa15, since this post was made, twitter now requires HTTPS-type posts instead of regular HTTP. The Core can’t handle SSL so you have to post tweets via a third-party service like IFTTT. :smile:

thank you very much for your help @peekay123. I will search how to use IFTTT, if you had some examples i would appreciate your help.

Why not try the pinned topic on the main page ;)?

3 Likes

Thanks @Moors7 you are AWESOME!! :smiley:

3 Likes

@Moors7

Hey there! Your advice is awesome for IFTTT!

So far I’ve been able to get my core to post to Twitter using spark.publish(“Event Name”, “Event Contents”).

I’ve been trying to get my core to post the Temp off a DHT11 (Temp Sensor), to twitter.
The issues is that when IFTTT has you post an event, it’s contents matter as well, thus I wish there was a way to post the contents of the event (whatever they are).

Do you have any idea how to do that?

Thanks!

Justin

1 Like

Glad you like it!
You could have IFTTT relay the event’s content to Twitter. Assuming you’ve got the “IF spark event Then Twitter”, you should be able to configure what happens on the Twitter side of things.
In the textbox, you can type what has to be tweeted. In there, you can place parameters. To see the available parameters, you’ll have to click the Erlenmeyer bottle on to top right of the text box. Once you click that, you should be able to choose “EventContents” from the dropdown menu. It should look something like this:

If you can’t get that dropdown menu to open up, you can try typing in the following:"{{EventContents}}", that should normally work as well.

Give it a try, and let me know it it worked for you! Good luck.

2 Likes