Did the oAuth update break Arduino code for reading tweets?

arduino
api

#1

OK so I found this code that would be perfect for what I want to do but I heard that an update to the API broke a lot of arduino code involving twitter. I only want to read tweets not write them and only tweets from one user would this code still work or do I need to use the overly complicated way that from what I read requires being hardwired (instead of wifi). I mean if the change was for security I don’t think it should break things because you don’t need to secure reading tweets.

Here is the link to the code that I want to use: Arduino controlled by Twitter commands


Arduino WiFi Twitter Read Only, App Read Permission
#2

Hi @willstr1, great question! I’m an Arduino enthusiast too.

Twitter introduced OAuth several years ago when we transitioned to API v1.1 and all of Twitter’s API endpoints require authentication, even if they are read-only operations. There are actually three levels of authenticated access - read-only, read/write (for posting Tweets), and read/write with DMs (for access to a user’s DMs). The code in the post you’ve found there is using the older v1 of the API and you’d get a 401 or 410 error if you tried to use it.

OAuth is a bit complicated to deal with on an Arduino due to the limited resources. The Twitter library that is available on the Arduino Playground actually delegates all the work to a server, which does the hard work of authentication and signing requests. Unfortunately it only provides the ability to post Tweets, not read them…

One thing to note is that there’s no requirement for your Arduino to be hardwired, you can absolutely use a wifi shield - I’ve done that before many times. All you’ll really want is a TCP/IP + HTTP stack and that would work fine with the library I just pointed you at.

OK - so how can you display / read Tweets in your Arduino project? there are a bunch of examples around. What they have in common is largely that the Twitter communication piece is done on another device (in Java, Perl, PHP etc). Here are a couple I found… if you replaced the “read from serial line” piece to have your Arduino grab the results via Wifi then they would fit the bill.


I’m also a huge fan of Node-RED, which would be another way of doing this (have a Node-RED flow get the Tweets, and send them to the Arduino)


#3

I really wanted this to work without having it plugged into my laptop. I am actually trying to use twitter as a method for sending commands to my Arduino for home automation so having to have it connected to my computer kind of defeats the purpose. But it is good to hear that I can use the wifi shield to access twitter. Also I already have a Mega 2560 so I don’t want to have to go and buy a new Arduino (a Yun) to do this.

Also how long ago did this change happen? Because the code that I really want to use was posted in November 2012 and the guy said it was working then, and the code for processing that was linked to in one of the articles you sent me to is marked as over 4 years old (so 2011).

Let me know if you have any ideas, if not I think I might go ahead and order a wifi shield and just test the old code I have.

Thank you


#4

OAuth / API v1.1 has been around since 2011, but we ran it in parallel with v1 until mid-2013, to give folks a period of time to migrate. The older code that hits the v1 endpoints simply won’t work at this stage. Actually that Processing code uses the Twitter4J library, which has been updated a lot since 2011, so it might need to be tweaked (although since the blog post was this year, it probably does still work).

Again, even in the case of the Processing code, the interaction with Twitter happens on the main computer, and the Arduino gets the data from there (in that case, over Serial). You’d need to rewrite that to talk to your computer over wifi instead. Certainly possible, but not as simple as having the Twitter client code running directly on the Arduino.

Here’s how I would do what you’re trying to achieve (but then, I’ve been doing these home automation and IoT things for a while…)

  1. Install Node-RED on your server. This could be your computer, or some cloud service like EC2.
  2. Create a flow that listens to Twitter for the commands you want to send, and then publishes a message over MQTT with the command for the Arduino (you could use a public MQTT broker, or install and run e.g. mosquitto on your server)
  3. On the Arduino with wifi shield, write a sketch that connects to your network, and subscribes to the MQTT topic waiting to receive the command; then act on it.

I’ve done the reverse myself many times - here’s a video from our developer conference last year, where I did something similar using a Raspberry Pi and Node-RED to publish air quality information in Tweets. Totally achievable on an Arduino with wifi shield, too. You just need to do the reverse, i.e. read the commands from the Tweets, and send the messages to the device.


#5

One more thing. The Arduino wifi shield draws quite a lot of power, so you’ll definitely need to have the Arduino on a direct power source - battery packs will last no time at all (speaking from experience!)


#6

I know you’ve said you would rather not purchase a new micro-controller but I’ve had great success using a BeagleBone Black (http://beagleboard.org/BLACK). It’s $35 and can use any Node.js, even ones that talk to the Twitter API. You also use Node.js to speak to the hardware.

I used it with the Twitter streaming API to hit a gong whenever I was @-mentioned.