Small example of how to post to Twitter

edited September 2012 in Planner
I imagine posting to Twitter will be part of the Planner eventually, but in case anyone is interested, here's how to get the Imp to post to Twitter using http://open.sen.se

Create an account on http://open.sen.se and then create a new channel – choose "Arduino & other"

On the custom device screen name your device and choose "other" for "device is"

The other settings should be fine (HTTP Post etc).

You'll then add a feed. Give it a name and choose "input - data flows from device to sen.se"

In the drop down for "what type of data" choose "text".

Save the feed and click finish.

In the device settings choose "anything else" from the side menu and you'll see instructions of how to POST to sen.se

Make a note of the feed id and then copy your API key.

Now go to this gist, copy the php code and save it as "opensense.php"- we need this to handle the post from the Planner and to then construct json to send to sen.se


Open the opensense.php file in the text editor of your choice and change the feed id and api key settings to the ones you noted down before. Upload it to your server and remember the url - we'll use this in the plan in a moment.

Now in the Planner create a new piece of code - and paste in the Squirrel code from the gist above. It's a very basic example that has a button on pin 1 and GND. 

In the Planner add a HTTP Request vimp and drag a noodle from the Imp node to this HTTP Request vimp. You should be able to just press ok. Click the settings icon in the vimp and paste in the url of the location of the php file on your server that you created before.

Finally, back in open.sen.se find the device you created earlier and add a new app. Choose the twitter app, authenticate it and in the template just simply type %VALUE.

Now when you press the button it should tweet.

Hope that's useful!

Comments

  • I was wondering if you were @dawesduino ;)  Very nice. Thanks for sharing, Brendan.
  • Thanks Nong. 
  • @brendandawes

    I agree, thanks for sharing! I've modified parts of the code to hit ThingSpeak to get something posted to Twitter. The code in itself was an extremely helpful "learn by example" of how to make a push button web connected thing. I got far enough to prove to myself a working prototype...
  • Thanks for this. Helpful to get going.

    I was able to knock the intermediate PHP out of the loop by manually constructing the JSON in the Squirrel code like follows:

    channelOutput[0].set("{\"feed_id\":\"xxxxx\",\"value\":\"tweet\"}");

    And POSTing from the vimp to api.sen.se with content type application/json.

    Sean
  • Jumped the gun...doesn't work...figuring it out.
  • edited October 2012
    Since doing this I've now removed the need to go through open.sen.se and instead use this library by Matt Harris: https://github.com/themattharris/tmhOAuth. I'll post some code when I get chance.
  • edited November 2012
    Thanks for all the documentation Brendan. I've just got an imp and trying to get experimenting with it.
  • The Electric Imp whitepaper claims that they provide email and twitter gateways -- is that just a lie?
  • We did provide email/twitter on an early planner revision, back when that was written. The whole IDE is being updated now, and agents will allow you the flexibility you need to connect to arbitrary services.
  • Thank you Hugo. I look forward to when the planner again allows me to both post to Twitter and react to tweets without third party services or self-hosted scripts. In the mean time I've been enjoying working through the examples and imagining what my impee could do.
  • Brendan (and/or Hugo):

    I have attached a Planner screenshot to help...

    I am trying to implement the above with the goal having a cat door that Tweets (basically sensing a switch closure). With the help of you and Hugo I have gotten pretty far, but now seem stuck. Probably due, in part, to the fact that I don't fully understand the fundamentals of what is actually going on. That said, here is where I am at:

    - on Sen.se I have the Twitter API working. I can manually enter a value from Sen.se and it will Tweet on my page @OurCatDoor. I have established my Imp as a device in Sen.se and have the FEEDID and API Code.

    - In Imp Planner I have switch closure (PIN1 to GND) code that works.

    - In Imp Planner I have the switch code 'noddled' to the "OPENSENSE.PHP" file on my server (with proper FEEDID and API codes). I modified the Imp code some for debug by adding a timestamp to the switch closure (see pic)

    - The HTTP node with the FEEDID/API modified PHP returns "200" which in my book means "All OK".

    But (and of course you could have guessed this), a switch closure on the Imp does not force the Sen.se to send the tweet.

    I'm "shotgunning" the problem now, so thought I would pause for some help from those that are wiser. Any thoughts on what I am missing. Something tells me it is with the 'noodle' setup.

    Planner screenshot attached at and at:
    http://3.bp.blogspot.com/-RLBKWi_k520/UNilC2eFEhI/AAAAAAAAMZc/Lrs6-QRz6p4/s1600/imp-706818.png

    Thanks,
    www.WhiskeyTangoHotel.com
    imp.png
    650 x 320 - 110K
  • Update: I got to thinking.... I believe the issue is with server (ISP) not allowing script to run on their site. I should have thought of this straight away.

    I'll dust off an old PC an create a little server, I guess. I wish Electric Imp would support access to Twitter directly. Seems like one of the most convient ready made solutions for broadcasting status easily.

    I welcome comments...
  • Agents will allow you to do this; right now you tend to need to cobble stuff together for external services I'm afraid :(
  • edited July 2013
    OK. Update here.... First off a few 'grounding' statements:

    - this is my first Imp
    - first use of Squirrel code
    - first PHP exposure.
    - 75% of the info came from this forum or the devwiki. 25% from other random web pages.

    Result:
    It's working. I connect PIN1 to GND. The Imp sees it, the PHP script is triggered, and our cat door twitter page gets updated.

    Well, for now anyway.

    Pretty impressive piece of kit and pretty impressive support forum.

    Build page at: http://whiskeytangohotel.com/ourcatdoor
  • Thanks brendandawes for the comment and thanks for your help!
  • edited May 2013
    Source on build page updated to correct some error hits.
  • Use two magnetic reed switches on the door so you know if it swung from the inside out, or the outside in. One switch will trigger a few milliseconds before the other.

    And debounce them so the door swinging back and forth after use doesn't trigger.

    These types of projects are always amusing.
  • With Agent it is a few lines of code to do Facebook updates...
  • Recently the IFTTT folks re-enabled Twitter support, apparently having worked through the API cap issue. I hope that Electric Imp can come to a similar agreement / workaround with Twitter, re-enabling easier tweet posting support without using third-party services.

    Alternately, if there were a way for a user to input their consumer API key into a twitter node in the planner, this would go a long way toward making Imp -> Twitter easier for your users.
  • Any idea why the open.sen.se "tweet it" app got stucked at loading settings??
  • Is it possible to post facebook updates on my own profile using agents? If yes, can someone help me with the code? I couldn't find any documentation for that.
  • tracer:

    Say more about your error. The following may not help, but....

    My experience after running a tweeting cat door for about a year is that the Imp is almost never the weak link; I think I had to reboot it only once or twice. :)
    Open.sen.se does goes down from time to time. Before you spend debug effort check their demo feed at http://twitter.com/SenseDemo and make sure the twitter stream is updating every minute or so.

    Hope that helps some.
  • edited November 2013
    yeshabavishi:

    I have not tried this, but IFTTT.com and their FaceBook agent may provide some ideas.

    Good luck!
Sign In or Register to comment.