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.
- Go to this site and follow Step 1. Save the long string you get (OAuth Key), youâll need this later!
- Paste the code Iâve provided below into a new Sketch in the WebIDE.
- 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!) - 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! - 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() {
}
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).
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. They describe their rate limiting here:
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!]
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
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.
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 ;)?
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
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.