Display tweets with a certain hashtag on a led-matrix using an Arduino/Genuino Yùn or Yùn-Shield. We will use a Python script to log into twitter and check for updates.
In order to connect your board to internet you first have to configure some parameters.
Open your Arduino IDE and go to
example -> Bridge -> YunFirstConfig
If you are using a YunShield be sure to select the name of the board to which it is attached and not a Yun board!
Upload the sketch and open the Serial monitor. You will see a message as the one in the screenshot below.
Follow a few simple steps and set up a name, a password and a network for your board.
Done! Now you can connect your Yun to internet!
In order to access your own data, you must create an application, and generate your own access token.
Go to this page and click on the Create New App button.
Fill everything but leave the Callback URL empty if you want call the API without setting up a web server. The API will give us an URL to get an identifier, so we don't need callback.
Now if you click on the Keys and Access Tokens tab you should see your own
CONSUMER_SECRET. Save those values, we will need them in the next step.
Great, now we only have to generate the OAUTH tokens.
To do so you can run the python script you'll find in the software section of this project. It is the same script that we will upload on our Yun101/yunShield but first we need to make some changes.
Download the script and open it in an advanced text editor. Set the two constants at the top of the code with the
Now, to generate also the OAUTH tokens we have to run the script. Open the terminal and use the following commands
sudo pip install requests requests-oauthlib python YunTwitter.py Arduino
Now you should have received the ID and the content of the last tweet with hashtag #Arduino.
If you run this script for the first time you could get an URL as a response. Follow that link and copy and paste the identification number you get in the terminal. Now you have your
OAUTH_TOKEN_SECRET, set them in the code to complete the authorization process.
In order to to download the python libraries we are going to use we can connect to the board via ssh ( be sure your computer is connected to the same network of the yun-shield).
Open your terminal and use the following command to download the libraries.
ssh root@name_of_your_yun.local opkg update opkg install python-pip pip install requests requests-oauthlib pip install sys
For the next step we will need the IP address of our YunShield. We can obtain it by using the command
Now that we have the IP we can proceed uploading the script. You can use this command replacing your_path with the folder's path to reach the python script.
scp /your_path/YunTumblr.py root@your_Yun_ip_address:
We are done!!!
To test it out you can digit in the terminal while you are connected via ssh:
python YunTwitter.py Arduino
For this project we used an 8x32 led matrix.
Normally all led matrix comes with three wires. The black one for the ground, the red one for the 5V and the coloured one for the data.
The led matrix needs some libraries to work. You can easily install them from the Library Manager.
In the Arduino IDE go to
Sketch -> include library -> manage libraries
And search for:
For a deepen understanding of the sketch have a look at the NeoMatrix example!
# -*- encoding: utf-8 -*- from __future__ import unicode_literals import requests from requests_oauthlib import OAuth1 from urlparse import parse_qs import sys REQUEST_TOKEN_URL = "https://api.twitter.com/oauth/request_token" AUTHORIZE_URL = "https://api.twitter.com/oauth/authorize?oauth_token=" ACCESS_TOKEN_URL = "https://api.twitter.com/oauth/access_token" CONSUMER_KEY = "****" CONSUMER_SECRET = "****" OAUTH_TOKEN = "" OAUTH_TOKEN_SECRET = "" def setup_oauth(): """Authorize your app via identifier.""" # Request token oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET) r = requests.post(url=REQUEST_TOKEN_URL, auth=oauth) credentials = parse_qs(r.content) resource_owner_key = credentials.get('oauth_token') resource_owner_secret = credentials.get('oauth_token_secret') # Authorize authorize_url = AUTHORIZE_URL + resource_owner_key print 'Please go here and authorize: ' + authorize_url verifier = raw_input('Please input the verifier: ') oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET, resource_owner_key=resource_owner_key, resource_owner_secret=resource_owner_secret, verifier=verifier) # Finally, Obtain the Access Token r = requests.post(url=ACCESS_TOKEN_URL, auth=oauth) credentials = parse_qs(r.content) token = credentials.get('oauth_token') secret = credentials.get('oauth_token_secret') return token, secret def get_oauth(): oauth = OAuth1(CONSUMER_KEY, client_secret=CONSUMER_SECRET, resource_owner_key=OAUTH_TOKEN, resource_owner_secret=OAUTH_TOKEN_SECRET) return oauth if __name__ == "__main__": ashtag = str(sys.argv) oauth = get_oauth() part1 = "https://api.twitter.com/1.1/search/tweets.json?q=%23" part2 = "&count=1" r = requests.get(url=part1+ashtag+part2, auth=oauth) message_id = r.json()["statuses"]["id"] text = r.json()["statuses"]["text"] print message_id print text