Creating a Twitter app and obtaining API keys

To proceed with our project, use the following steps to create a Twitter App and obtain the API keys.

  1. Go to https://apps.twitter.com/ and sign in with your Twitter login credentials (create a new Twitter account if you don't have one). The following page will display on the browser:
    Creating a Twitter app and obtaining API keys

    apps.twitter.com, the Application Management start page

  2. Click on the Create New App button. The Create an application page will display:
    Creating a Twitter app and obtaining API keys

    Twitter's Create an application page

  3. Fill in the required fields (for the website textbox, just type http://www.example.com as a placeholder), accept the Developer Agreement by clicking on the Yes, I agree checkbox.
  4. After this, click on the Create your Twitter application button.
  5. You will be navigated to the following page:
    Creating a Twitter app and obtaining API keys

    The Twitter application settings page

  6. Click on the Keys and Access Tokens tab. Under this tab, you will find Consumer Key (API Key) and Consumer Secret (API Secret). Copy these two keys and paste them in a Notepad file, because you will require them in the next section:
    Creating a Twitter app and obtaining API keys

Writing a Python script to read Twitter tweets

The Tweepy library provides a set of easy functions to interface with the Twitter API. Our Python script provides the following operations and services:

  • Read tweets from the specified twitter screen name. For example, @PacktPub, every 30 seconds (if you want, you can change the delay period)
  • Always read the latest tweet
  • If the tweet includes the text, #switchon, then print the tweet on the console and write 1 on the serial port
  • If the tweet includes the text, #switchoff, then print the tweet on the console and write 0 on the serial port
  • Otherwise, maintain the last state

The following Python script will provide an interface between Twitter and the serial port of your computer. The sample script, twitter_test.py, can be found inside the Chapter 7 codes folder. Copy the file to your local drive and open it using Notepad or NotePad++:

import tweepy
import time
import serial
import struct

auth = tweepy.OAuthHandler('SZ3jdFXXXXXXXXXXPJaL9w4wm', 'jQ9MBuy7SL6wgRK1XXXXXXXXXXGGGGIAFevITkNEAMglUNebgK')
auth.set_access_token('3300242354-sJB78WNygLXXXXXXXXXXGxkTKWBck6vYIL79jjE', 'ZGfOgnPBhUD10XXXXXXXXXXt3KsxKxwqlcAbc0HEk21RH')

api = tweepy.API(auth)
ser = serial.Serial('COM3', 9600, timeout=1)
last_tweet="#switchoff"
public_tweets = api.user_timeline(screen_name='@PacktPub',count=1)
while True: # This constructs an infinite loop
  for tweet in public_tweets:
    if '#switchon' in tweet.text: #check if the tweet contains the text #switchon
      print (tweet.text)  #print the tweet
      if last_tweet == "#switchoff":
        if not ser.isOpen(): #if serial port is not open
          ser.open();  #open the serial port
          ser.write('1') # write 1 on serial port
        print('Write 1 on serial port')  #print message on console
        last_tweet="#switchon"
    elif "#switchoff" in tweet.text: #check if the tweet contains the text #switchoff
      print (tweet.text)  #print the tweet
      if last_tweet == "#switchon":
        if not ser.isOpen(): #if serial port is not open
          ser.open();  #open the serial port
          ser.open();  #open the serial port
          ser.write("0") # write 0 on serial port
        print('Write 0 on serial port')  #print message on console
        last_tweet="#switchoff"
    else:
      ser.close()  #close the serial port
    time.sleep(30)  #wait for 30 seconds

Now, replace the following code snippet with your Twitter Consumer Key and Consumer Secret:

auth = tweepy.OAuthHandler('SZ3jdFXXXXXXXXXXPJaL9w4wm', 'jQ9MBuy7SL6wgRK1XXXXXXXXXXGGGGIAFevITkNEAMglUNebgK')
auth = tweepy.OAuthHandler(' Consumer Key (API Key)', ' Consumer Secret (API Secret)')

Also, replace the following code snippet with Access Token and Access Token Secret:

auth.set_access_token('3300242354-sJB78WNygLXXXXXXXXXXGxkTKWBck6vYIL79jjE', 'ZGfOgnPBhUD10XXXXXXXXXXt3KsxKxwqlcAbc0HEk21RH')
auth.set_access_token(' Access Token, ' Access Token Secret ')

Next, replace the COM port number with which you wish to attach the Arduino UNO board. Also, use the same baud rate (in this case, 9,600) in Python script and Arduino sketch (you will write in the final step of this chapter):

ser = serial.Serial('Your Arduino Connected COM Port', 9600, timeout=1)

Finally, replace the Twitter screen name with your Twitter account's screen name:

public_tweets = api.user_timeline(screen_name='@PacktPub',count=1)
public_tweets = api.user_timeline(screen_name='@your_twitter_screen_name',count=1)

Now, save the file and navigate to the file location using Windows Command Prompt. Then, type the following command and press Enter:

>python your_python_script.py

Replace your_python_script with the filename. The script will continuously monitor any incoming new Twitter tweets and write data on the serial port according to the command that has been sent:

Writing a Python script to read Twitter tweets

Windows Command Prompt will display any incoming Tweets and actions against them.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.93.12