Search code examples
pythonpython-2.7twittertweepyarduino-yun

Pass a variable on a call in Python to bisect arduino action


Total noob with Python writing, so pease be kind. I have been trying for hours to solve this and so long I am not able. The only work I have done in Python was much simpler and didn't involve libraries.

I'm using this Python code to implement an Arduino action on my Yún. It is based on Tweepy library. My main goal is to insert three hashtags and do 3 different things in Arduino depending on which hashtag has been tweeted. So, everything is working fine and my LED is blinking whenever any of the hashtag is tweeted. What I want to do now is bisect code to be run on Arduino.

This is part of my streaming.py code:

search_string = '#jekotest'

class StdOutListener(StreamListener):

"""
A listener handles tweets are the received from the stream.
This is a basic listener that just prints received tweets to stdout.
"""

def on_data(self, data):
    # I call an external script because the Arduino's Bridge library
    # confilcts with the tweepy library
    call(["/usr/bin/python", script_path + "/go.py"])
    return True

def on_error(self, status):
    # TODO: Put some error handling here
    return False

if __name__ == '__main__':
    l = StdOutListener()
    auth = OAuthHandler(consumer_key, consumer_secret)
    auth.set_access_token(access_token, access_token_secret)

    stream = Stream(auth, l)
    stream.filter(track=[search_string])

And this is go.py:

import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge/')

from bridgeclient import BridgeClient as bridgeclient

value = bridgeclient()
value.put('go','1')

I know that stream.body is a dict containing {'track': '#jekotest'} so: How the heck could I do a

if '#jekotest' in stream.body:
value.put('go', 1)
elif: 
'#anotherhashtag' in stream.body
value.put('go', 2)

And so on?

Thank you so much


Solution

  • Finally, I managed to put both files together and make it work like this:

    hashtag = status.entities['hashtags']
        for value in hashtag:
            if translateHashtag1 in value['text']:
                goBridge.put('go', 1)