Comments: Raspberry Pi Twitter Monitor

Pages

Looking for answers to technical questions?

We welcome your comments and suggestions below. However, if you are looking for solutions to technical questions please see our Technical Assistance page.

  • Member #381819 / about 10 years ago / 2

    Thanks for posting this project. I have used the foundation to create two variants on the concept. Build page links include source code, etc.

    http://whiskeytangohotel.com/rgb Pretty basic. It looks for a color to be mentioned on Twitter then displays that color on a RGB LED.

    http://whiskeytangohotel.com/hashvote Little more difficult. This program tracks two Twitter terms and graphs how often the terms are mentioned.

    Thanks again, Sparkfun; for posting up the original project!

    • These were cool projects. Thanks for sharing! The Hash Vote project reminds me of the Twitter race we attempted to do a while ago :)

  • Guido M / about 5 years ago * / 1

    Can i ask a second question please? My "Raspberry Pi Twitter Monitor" is working ok, except when the Raspberry pi Zero W (with the configuration LED=22 and the reboot 'pi_code.py') is on for more then 3 houres, then if i test it , it gives no signal anymore to the led. At the first houre, it is working perfectly....Am i doing something wrong?.... What can i do about this please? grts from Guido m Belgium I am using a Raspberry pi Zero W .

    ps: i forgot to tell you thanks for this beautiful project

    • santaimpersonator / about 5 years ago / 1

      Hi there, it sounds like you are looking for technical assistance. Please use the link in the banner above, to get started with posting a topic in our forums. Our technical support team will do their best to assist you.

      Just a heads up, this tutorial is over 6 years old, so there might be limited support on it. Additionally, your modifications to the project might be outside the scope of our TS team; filing a GitHub issue to the repository might be a better option for getting help with your specific setup.

  • Guido M / about 5 years ago * / 1

    I did make the project "Raspberry Pi Twitter Monitor" and it is working perfectly...... Now i want to use some sound when someone is tweeting me (play a sound.mp3). I have bought the "Speaker pHat" and installed on my Raspberry pi Zero W: and it is working also... (i can play the 'sound.mp3' with the command in the terminal : cvlc sound.mp3).

    Now i must , i think , change the 'TwinkBlinky.py' for starting the 'sound.mp3' one time. I have searched all around and did not found the solution. So i gave it up and my last hope for help is you.... Please, can you help me with that? Guido M Belgium

    • santaimpersonator / about 5 years ago / 1

      Hi there, it sounds like you are looking for technical assistance. Please use the link in the banner above, to get started with posting a topic in our forums. Our technical support team will do their best to assist you (as a note, your project might be outside the scope of our TS team).

  • Member #802785 / about 8 years ago / 1

    So I am trying to do this - copied the code and put in my auth tokens etc -- it will not blink or pick up the TERMS=# Does this only work when someone I am following does one of these hashtags? I want it to blink when I hashtag something - im using it for like a remote notification for someone when I am gone I can hash tag them and the light blinks..

    Help and or thoughts would be greatly appreciated.

    Thanks -Rick

    • There's a good chance that the Twitter API has changed in the last year, and I have not revisited this project in a long time. That being said, it should pick up any hashtag. What hashtag are you trying? I've had issues with the Pi not being fast enough to pick up an obscure hashtag within a few minutes when Twitter is particularly busy. Try a common hashtag, like #lol, and see if you can blink or print it to the console.

      • Member #802785 / about 8 years ago / 1

        Shawn: I have tried random hashtags, I have tried posting from my account, I have tried on my wall from another account.. I have tried from accounts that I am following.. its not picking up ANYTHING :( I know the OAUTH codes are working - because the post twitter commands post tweets to my account from my PI.. so .. any help would be sincerely appreciated - I really want this working as its part of a project for my Girlfriend.. Thanks

        • Sounds like a fun project :) I'll see if I can help you out.

          First, regenerate your Access Token and Token Secret on apps.twitter.com. This seemed to help me, as I had a old code from months/years ago.

          Next, create a new Python file and enter this:

          from twython import TwythonStreamer
          
          TERMS = 'lol'
          
          APP_KEY = '<YOUR API KEY>'
          APP_SECRET = '<YOUR API SECRET>'
          OAUTH_TOKEN = '<YOUR ACCESS TOKEN>'
          OAUTH_TOKEN_SECRET = '<YOUR ACCESS TOKEN SECRET>'
          
          class MyStreamer(TwythonStreamer):
                  def on_success(self, data):
                          if 'text' in data:
                                  print data['text'].encode('utf-8')
          
                  def on_error(self, err, data):
                          print err, data
          
          stream = MyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
          stream.statuses.filter(track=TERMS)
          

          Run it, and you should see tons of Tweets fly past ('lol' is pretty common). I just tried this on my laptop and it worked.

  • Member #215359 / about 8 years ago / 1

    Confused: I was under the impression that GPIO.setmode(GPIO.BOARD) means you use the numbers printed on the board, yet the diagram shows the LED hooked up to Pin 25 and the code reads LED = 22

    Researching this made me feel that the tutorial was using BCM numbering where pin 22 on the board is actually GPIO 25 in the code

    I'm using a Pi 2 in this case.
    The tutorial works but I was expecting the PINS to match the board due to the use of GPIO.setmode(GPIO.BOARD) in the code Confused?

    • The GPIO.BOARD means you are using the actual pin number (1-40) whereas GPIO.BCM refers to the GPIO designator (e.g. GPIO2, GPIO3, etc.). If you count the pins, pin 22 refers to GPIO25. The code is correct, as GPIO.BOARD means that the LED is attached to pin 22, which is GPIO25. For a better explanation, see here.

  • Member #773040 / about 8 years ago * / 1

    Hi

    I am attempting to adapt the code provided in this article to search for multiple hashtags which then activate different LEDs. E.g. if #lol is tweeted, LED1 lights; if #yes is tweeted, LED2 lights, etc.

    I am a Python newbie and have the following code adapted from the article. At present on the first term ('#yes') in the code returns any tweets. Could you please advise on the best way of achieving this?

    import time
    import RPi.GPIO as GPIO
    from twython import TwythonStreamer
    
    # Search terms
    TERMS1 = '#yes'
    TERMS2 = '#lol'
    
    # GPIO pin number of LED
    
    LED1 = 22
    LED2 = 32
    
    # Twitter application authentication
    
    APP_KEY = ''
    APP_SECRET = ''
    OAUTH_TOKEN = ''
    OAUTH_TOKEN_SECRET = ''
    
    # Setup callbacks from Twython Streamer
    
    class BlinkyStreamer(TwythonStreamer):
            def on_success(self, data):
                if 'text' in data:
                print data['text'].encode('utf-8')
                print
                GPIO.output(LED1, GPIO.HIGH)
               time.sleep(0.5)
                GPIO.output(LED1, GPIO.LOW)
    
    class BlinkyStreamer2(TwythonStreamer):
        def on_success(self, data):
            if 'text' in data:
            print data['text'].encode('utf-8')
            print
            GPIO.output(LED2, GPIO.HIGH)
            time.sleep(0.5)
            GPIO.output(LED2, GPIO.LOW)
    
    # Setup GPIO as output
    
    GPIO.setmode(GPIO.BOARD)
    GPIO.setup(LED1, GPIO.OUT)
    GPIO.output(LED1, GPIO.LOW)
    GPIO.setup(LED2, GPIO.OUT)
    GPIO.output(LED2, GPIO.LOW)
    
    # Create streamer
    
    try:
        stream1 = BlinkyStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
        stream1.statuses.filter(track=TERMS1)
    
        stream2 = BlinkyStreamer2(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
        stream2.statuses.filter(track=TERMS2)
    except KeyboardInterrupt:
        GPIO.cleanup()
    

    • m.meadows / about 8 years ago / 2

      Were you able to get it working? I can swap the pin numbers and both LEDs work. It just seems like it doesn't run the second stream code.

      • I get the feeling the API won't let you connect with 2 streamers at the same time. As a result, I recommend using 1 streamer and setting the track terms to a list: ['#yes', '#lol']. The on_success() function will be called whenever either of those tags are found. Then, you can search through the tweet and turn on the LED depending on which tag is found.

        • Member #1203826 / about 6 years ago / 0

          Could you explain that idea better? I am very new in this thanks

          • Are you able to get the basic example code running? If so, try changing the line

            TERMS = '#yes'
            

            to

            TERMS = ['#yes', '#lol']
            

            This will allow you to look for multiple hashtags in real time on Twitter.

  • Member #745755 / about 8 years ago / 1

    This may be far too late, but to anyone trying to get to the twitter developer page to make the app...

    http://lmgtfy.com/?q=twitter+developer+create+app

  • Member #694306 / about 9 years ago / 1

    Hi. I got the first part of my problem solved. There were a few things which I was doing wrong. Firstly, the resistor was too strong for my LED. Changed it to 50 ohms. Secondly, updated the RPi GPIO library. Worked like a charm.

    Now trying to figure how to customize this as a personal timeline hashtag based blink.

    • When you say someone tweets you, you mean they include your Twitter handle with @<your name>? If so, then you can look to see if you were mentioned in the tweet with if "@<your name>" in data['text'].encode('utf-8'): See this post.

  • Member #684475 / about 9 years ago / 1

    Has anyone received the following error message? Any suggestions on a fix? The program runs fine for several minutes and then I receive the following:

    Traceback (most recent call last): File "/home/pi/TweetBlinky/TweetBlinky.py", line 27, in <module> stream.statuses.filter(track=TERMS) File "/usr/local/lib/python3.2/dist-packages/twython/streaming/types.py", line 65, in filter self.streamer._request(url, 'POST', params=params) File "/usr/local/lib/python3.2/dist-packages/twython/streaming/api.py", line 141, in _request for line in response.iter_lines(self.chunk_size): File "/usr/local/lib/python3.2/dist-packages/requests/models.py", line 715, in iter_lines for chunk in self.iter_content(chunk_size=chunk_size, decode_unicode=decode_unicode): File "/usr/local/lib/python3.2/dist-packages/requests/models.py", line 673, in generate for chunk in self.raw.stream(chunk_size, decode_content=True): File "/usr/local/lib/python3.2/dist-packages/requests/packages/urllib3/response.py", line 303, in stream for line in self.read_chunked(amt, decode_content=decode_content): File "/usr/local/lib/python3.2/dist-packages/requests/packages/urllib3/response.py", line 450, in read_chunked chunk = self._handle_chunk(amt) File "/usr/local/lib/python3.2/dist-packages/requests/packages/urllib3/response.py", line 411, in _handle_chunk value = self._fp._safe_read(amt) File "/usr/lib/python3.2/http/client.py", line 592, in _safe_read raise IncompleteRead(b''.join(s), amt) http.client.IncompleteRead: IncompleteRead(0 bytes read, 1 more expected)

  • Member #239583 / about 9 years ago / 1

    Two questions; odds are at least one is a dumb one.

    Does "TERMS" have to be a hashtag, or can "stream.statuses.filter" track for any text?

    I take it that in the line "if 'text' in data", 'text' is one part of the "data" array, so it should say "'text'" and not "'whatyouwanttosearchfor'". Can someone point me to documentation on what all is in "data"?

    TIA

    • To answer your first question, TERMS can be any text.

      As for your second question, 'text' is an actual keyword inside of the data "dictionary." Here is the layout of a Twitter status object. Notice that "text" is the second field within the object. You can print all of "data" if you want to see what's in the Tweet.

      Hope that helps!

  • Member #666902 / about 9 years ago / 1

    tried using code but python said the third "data" right after "print" and before "['text']" was invalid syntax. does anyone know what is wrong or something else i need to download or update on my pi.

    • Member #239583 / about 9 years ago / 1

      I'm brand new to this myself, but my suggestion: Maybe you're using Python 3 rather than Python 2? In 3, print() is a function rather than a direct command. Try putting the argument in parentheses, thus:

      print(data['text'].encode('utf-8'))

  • Member #659055 / about 9 years ago / 1

    For some reason, my monitor will not show tweets that are sent from mobile apps. Can anyone help me figure this out? Thanks!

    • Is the Twitter Monitor printing tweets from other sources?

      The one thing I found was that the Raspberry Pi can't keep up with the volume of tweets if you use a common search phrase. Try something that's uncommon or no one would use. Additionally, you could try running the Twitter Monitor from a computer to see if that helps.

      • Member #659055 / about 9 years ago / 1

        I tried tweeting a less common search phrase from my own Twitter account and it still didn't show up. This is driving me crazy! Any other ideas?

      • Member #659055 / about 9 years ago / 1

        It's definitely printing tweets from other sources. I will try that and let you know if it helps. Thank you!

  • panjas / about 10 years ago / 1

    Hello,

    I just copy /past the code , addapted the APIkey, Auth key... But nothing appears. No error message.

    While I tried the bellow SendMessage worked. :=> API keys and tweeter account are OK

    Any Idea?

    Lol is indeed very common !

    • I know that recently, Twitter has been requiring a mobile phone number to be associated with your account before you can use the API. You're saying that you can post things to your Twitter account, right?

      • panjas / about 10 years ago / 1

        Yes this works.., I can update status... twitter.update_status(status=“Ca marche! :D”)

        But your nice code does not responds I did try this code yesterday night:

        from twython import TwythonStreamer
        
        class TweetStreamer(TwythonStreamer):
            def on_success(self, data):
                if 'text' in data:
                    print data['text'].encode('utf-8')
            def on_error(self, status_code, data):
                print status_code
                self.disconnect()
        
        APP_KEY = "xxxxxx"
        APP_SECRET = "xxxxxxxx"
        OAUTH_TOKEN = 'xxxxxxxxx'
        OAUTH_TOKEN_SECRET = 'xxxxxxxxx'
        
        streamer = TweetStreamer(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
        
        streamer.statuses.filter(track = 'python')
        

        Then I get 401

        • That actually looks right to me. It might be a weird, esoteric problem like your system time being off. See this StackExchange post:

          http://stackoverflow.com/questions/17438943/twython-oauth1-issues-401-error-using-example-code

  • panjas / about 10 years ago * / 1

    I needed sudo pip install -U distribute==0.6.45

    before sudo pip install Adafruit_BBIO

  • panjas / about 10 years ago / 1

    While installing * pip install Adafruit_BBIO*

    I get this error message: InstallationError: Command python setup.py egg_info failed with error code 2 in /home/pi/build/Adafruit-BBIO

  • panjas / about 10 years ago / 1

    second try

    from twython import Twython 
    APP_KEY = “xxxx” 
    APP_SECRET = “xxxxx”
    OAUTH_TOKEN = ‘xxxx’ 
    OAUTH_TOKEN_SECRET = ‘xxxxx’
    
    #startTime = datetime.now()
    
    twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    
    #Sends a private message
    twitter.send_direct_message(user_id=“FromUserNo@”, screen_name=“FromUserNo@”, text= ‘ bonjour ’)
    
    #sends a public message
    twitter.update_status(status=“Ca marche! :D”)
    
    print (“DM SENT IN”)
    #print (datetime.now()-startTime)
    

  • panjas / about 10 years ago * / 1

    here just a small code to make the opposit using the same Twython. Has to be improved to link it to the Gpio: Once the gpio gets an input: sends a tweet:

    from twython import Twython
    APP_KEY = "xxxx"
    APP_SECRET = "xxxxx"
    
    
    OAUTH_TOKEN = 'xxxx'
    OAUTH_TOKEN_SECRET = 'xxxxx'
    #startTime = datetime.now()
    
    twitter = Twython(APP_KEY, APP_SECRET, OAUTH_TOKEN, OAUTH_TOKEN_SECRET)
    
    #Sends a private message
    twitter.send_direct_message(user_id="FromUserNo@", screen_name="FromUserNo@", text= ' bonjour ')
    
    #sends a public message
    twitter.update_status(status="Ca marche! :D")
    
    print ("DM SENT IN")
    #print (datetime.now()-startTime)
    

    P.S. Sorry for the layout of this comment.

  • panjas / about 10 years ago / 1

    A big thanks anyhow..

  • panjas / about 10 years ago / 1

    I had to add

    sudo pip install rpi.gpio

    And there is a small error between thje code Led pin and the schema (Pin 22 vs 25)..

    • Interesting....good to know. The version of Raspbian I used in the tutorial came with RPi.GPIO already installed.

      Also, If you do

      GPIO.setmode(GPIO.BOARD)

      You should be able to reference the pin by pin number (e.g. 22) as opposed to the GPIO number (e.g. 25).

  • Waltermixxx / about 10 years ago / 1

    Thanks for the explanation. I understand. I will have to look closer at the code you linked to, the tweeting game, that might help me... in the mean time I will experiment with: terms = ['#rasberrypi', '#Helpful', '@ShawnHymel']

    Thanks again for your help... :)

    cheers.

  • Waltermixxx / about 10 years ago * / 1

    Thank you Shawn,

    So I should be able to use the following?: terms = [‘#omg OR #hello OR #diy’]

    Would that be the correct syntax? As a result, any tweets that contain any one of the above three words would be displayed?

    Would the following also work?: terms = ['#omg AND #hello AND #diy'] Would this display tweets that only include those three words?

    I'm at the office and cannot test, but i hope it's that simple :)

    • Sort of. The code looks for strings that are contained between the single quotes: ' '. So, your code would be looking for the entire phrase "#omg OR #hello OR #diy" to appear in a tweet (which probably won't happen). To do ORs like in your first example, you need to set terms to an array of strings (like in my example below): terms = ['#omg', '#hello', '#diy']

      Each term in the array is its own string because of the ' '. For example, '#omg' is one search term, '#hello' is another, and '#diy' is a third. The program will call back to the on_success(self, data) function when any one of those strings appears in a tweet (which is what you are trying to do with the OR statements).

      There is no way (that I'm aware of) to set AND statements for your search terms. You will need to perform a check in on_success(self, data) that all of the search terms are present in the tweet.

  • Waltermixxx / about 10 years ago / 1

    Cool project... I'm hoping to expand on it, but I'm very new to Python...

    in the line: TERMS = '#lol' in the dissecting code, it says TERMS holds a string (or Strings)

    how would i change it if I wanted to search for more than one Term? for instance, say I want to find tweets to @RobFord that also say "drunk" how would i change that line? The above example is just for fun, but should help with my project...

    I'm hoping to either increment a counter, if Drunk is used, increment another counter if Drugs is used. then either adjust the brightness or the color of an rgb led.... i tried a couple of things like TERMS = '#lol' AND '#Haha" but nothing came of it...

    help? :)

    • To use multiple search terms, you can use an array:

      terms = ['#omg', '#hello', '#diy', '#robot', '#fun']
      

      I actually worked on something similar to what you're describing... It was a game that counted the number of times a term appeared in a specific time. You can see my example code here:

      https://github.com/ShawnHymel/TweetRace/blob/master/test/game_test_01.py

      • Kamiquasi / about 10 years ago * / 1

        Note that although Twitter has some pretty lenient API restrictions, if you do start approaching your limits you can get the number of accesses down by using e.g. '#omg OR #hello OR #diy' to get all tweets that match any one of those terms, and then use device-side code to figure out which was/were matched.

  • Mark A. Yoder / about 10 years ago * / 1

    Oh, and here's what I had to do to get it working:

    apt-get update
    apt-get install python-pip
    apt-get install python-dev
    pip install twython
    pip install Adafruit_BBIO
    

    • Awesome! Thanks, Prof. Yoder :) If you don't mind, I'll include a section in the tutorial about what to install for the BeagleBone Black.

  • Mark A. Yoder / about 10 years ago / 1

    Shawn, what a fun project. Yesterday I got it working on a BeagleBone Black.

    --Mark

  • Member #112393 / about 10 years ago / 1

    Works great, except I thought I was getting duplicate tweets, so I added printing the screen_name from the tweet. Turns out it was not duplicate tweets, but re-tweets.

  • Member #498371 / about 10 years ago / 1

    I got nothing when running it... ;-(

    • You made sure to run as superuser (sudo), right? Were there any errors that appeared?

      Also, what search term did you use (e.g. #lol)?

  • Parizival / about 10 years ago / 1

    On the "Required Components" page, you forgot the led.

  • Nothing happens when I run the command: sudo python TweetBlinky.py

    My Pi just sits and thinks...until I kill it w/ ctrl+c. Nothing prints to the terminal. What am I doing wrong?

  • Member #694306 / about 9 years ago / 0

    Hi Shawn. Thank you for this project. I am using a RasPi B+ and with the 40 pin breakout board to connect to the breadboard using the bus cable. I have connected 3.3V pin and the GND pin to the LED using 390 ohm resistor. I do get the tweets with the hashtag on my console bu the LED doesn't blink. I have double checked the circuit and it looks fine to me. Do you have any suggestions?

    Also, how do I modify the code wherein I receive the tweets only sent to my timeline and not universal tweets with the hashtag ? i.e. if someone tweets me with the hashtag it should appear on my timeline and the LED should blink.

    Thanks in advance.

    Cheers!


If you've found an issue with this tutorial content, please send us your feedback!