To proceed with our project, use the following steps to create a Twitter App and obtain the API keys.
http://www.example.com
as a placeholder), accept the Developer Agreement by clicking on the Yes, I agree checkbox.The Tweepy library provides a set of easy functions to interface with the Twitter API. Our Python script provides the following operations and services:
@PacktPub
, every 30 seconds (if you want, you can change the delay period)#switchon
, then print the tweet on the console and write 1
on the serial port#switchoff
, then print the tweet on the console and write 0
on the serial portThe 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:
Windows Command Prompt will display any incoming Tweets and actions against them.
18.221.241.116