Arduino Python Twitter (Windows)

In this instructable we'll modify my simpleTweet_00 and rewrite the Processing code with Python. You can expect an ignorant walkthrough by an illiterate noob, but maybe that's just what you're looking for.

Before you begin:
If you need help installing Python, navigating Windows from the Command Prompt, or installing packages, then take a look at my instructable How to install Python packages on Windows 7 . It's a quick and dirty walkthrough but it should get you going asap.

And because simpleTweet_00 already explains the concept, I'll be brief:  Arduino sends a message via Serial to Python; Python sends a tweet. The idea here is to create a simple Arduino Python Twitter communication. For beginners.

Let's do it.
 
Remove these adsRemove these ads by Signing Up

Step 1: Concept

simpleTweet_00.jpg
The idea for this project was to develop generic code allowing you to automatically generate tweets based on input to your Arduino. This example uses a magnetic reed switch, which is your basic on / off switch. You can use this code as is for any two-state switch or modify it for more diversity.

We attach the magnetic reed sensor to my office door and door-frame. A wire runs from the magReed sensor to a pin in the Arduino circuit. Arduino watches that pin's status, HIGH or LOW, and when the status changes from one to the other Arduino reports on that change through Serial(). The Python sketch picks up the Serial() call and checks to see if the current state is the same as the last one posted to Twitter. If the two states are the same then it will not post, but if the new state is different from the previous state, then we're in business.

Python uses python-twitter and OAuth to post the new state to your Twitter account. Done and done.
jwestenberg says: Nov 3, 2012. 5:25 AM
Hey,

I was able to install all packages but the python-oauth2.
When i tried to "python setup.py install" python-oauth2 I get the error on line 2 import setuptools no module named setuptools.

I almost gave up when after inspecting all the setup.py files, ouath2 is the only one trying to import setuptools. So i googled it and found out this is just something that you can install.

Most of you probably already know this or can find this yourselves, but I just wanted to give you a heads up in case you would get stuck.

Good luck!
Cyclone1764 says: Aug 23, 2012. 4:36 AM
this is pretty irrelevent, but that picture is save as "smile.jpg" which is a famous internet myth, about a picture of a dog, that has the power to kill whoever see his photo
alexforey says: Feb 20, 2012. 1:39 PM
Uh oh. It's not working for me. This error makes me think that the python-twitter's not installed properly, but I think it is...

Traceback (most recent call last):
File "D:\Arduino\Projects\twitter.py", line 8, in
import twitter
File "D:\Arduino\Projects\twitter.py", line 16, in
api = twitter.Api(consumer_key='imnotposingthishere',
AttributeError: 'module' object has no attribute 'Api'
>>>
Dominion-Network says: Jun 28, 2011. 3:14 AM
Ignore my last comment I missed a step, I've got it working now, but I have a different issue.

running... simpleTweet_01_python
arduino msg: Closed door

Traceback (most recent call last):
File "C:\Users\XXX\Documents\Python\Tweet", line 45, in
listenToArduino()
File "C:\Users\XXX\Documents\Python\Tweet", line 22, in listenToArduino
compareMsg(msg.strip())
File "C:\Users\XXX\Documents\Python\Tweet", line 29, in compareMsg
pM = ""+prevMsg[0]+""
IndexError: list index out of range
pdxnat (author) in reply to Dominion-NetworkJun 28, 2011. 10:15 AM
Dominion-Network: Is it possible that there is no existing previous message to populate the prevMsg array? I mean, have you ever posted a status update to that account before? If not, then it'd throw this error. Otherwise I can't think of why it'd break on prevMsg[0].

Try this: Change pM = ""+prevMsg[0]+"" to pM = str(prevMsg[0]) and see if you get the same error. The first version is a dumb way to turn an array item into a string, the second version is smarter. Hey, work in progress. ;-)

Good luck and let me know how it goes.
Dominion-Network in reply to pdxnatJun 30, 2011. 1:12 AM
I ended up writing my own python code, a lot simpler, it works, but I'm getting my magnetic reed switch today, what other components will I need to set this up?
pdxnat (author) in reply to Dominion-NetworkJun 30, 2011. 9:12 AM
Study the image in Step 5 to see what components you'll need to set this up.
* Mag Reed Switch (tho' any HIGH / LOW switch will do)
* 10k ohm resistor (between Power and the Mag Reed Switch)
* Breadboard + wires (or solder your own circuit based on the drawing in step 5)
* Arduino + usb connection to pc 
* pc w/ Python & internets

In what way is your code "a lot simpler?" Do you understand why the original code did not work for you? This script is the first Python project I wrote and the Twitter Mention Mood Light (a slight improvement) is the second. I don't have a lot of experience w/ Python so I look forward to seeing how you improved upon this code when you share it here.

Thanks!
cris3D says: Jun 25, 2011. 12:40 AM
not works..

File "C:\Documents and Settings\crystal\Desktop\tweet2.py", line 37, in postMsg
tweet = api.PostUpdate(newMsg+", "+localtime)
File "build\bdist.win32\egg\twitter.py", line 2766, in PostUpdate
data = self._ParseAndCheckTwitter(json)
File "build\bdist.win32\egg\twitter.py", line 3668, in _ParseAndCheckTwitter
self._CheckForTwitterError(data)
File "build\bdist.win32\egg\twitter.py", line 3691, in _CheckForTwitterError
raise TwitterError(data['error'])
TwitterError: Timestamp out of bounds

I followed all steps, packages installed correctly (I guess), I use XP .. well.. I'm not a Python User.. but your application is really interesting :)

thanks for your efford!
pdxnat (author) in reply to cris3DJun 25, 2011. 9:24 AM
I'm not really a Python user either. Check the Python Forums for more help, but I can see that the error has something to do with Time and not with the twitter api, so let's try something. The error happens in the postMsg() function, in the line
tweet = api.PostUpdate(newMsg+", "+localtime)

Please open a new Python file (call it testTime.py or something like that.)
Paste the following into your new file:

################################################################
############ test one
import time
localtime = time.asctime(time.localtime(time.time()))
print "now: "+localtime
################################################################

Run that code. Do you get the same error message? Does it print the time?
After running the code above, then paste the following into your file and run.

################################################################
########## test two
import twitter
import time

api = twitter.Api(consumer_key='YOUR_CONSUMER_KEY',
                  consumer_secret='YOUR_CONSUMER_SECRET',
                  access_token_key='YOUR_ACCESS_TOKEN_KEY',
                  access_token_secret='YOUR_ACCESS_TOKEN_SECRET')

localtime = time.asctime(time.localtime(time.time()))
newMsg = "testing time at "
tweet = api.PostUpdate(newMsg+", "+localtime)
print "tweeted: "+tweet.text
################################################################

Save it and run it. Let me know if you get the same error message as before, or if it is a success. If successful, you will see a line printed like this: "tweeted: testing time at , Sat Jun 25 09:15:05 2011" with your date/time.

Good Luck!
cris3D in reply to pdxnatJun 25, 2011. 1:34 PM
I test those codes and...


the first print..

now: Sat Jun 25 13:24:10 2011

works!

the second..

>>> ================================ RESTART ================================
>>>

Traceback (most recent call last):
File "C:\Documents and Settings\crystal\Desktop\test.py", line 11, in
tweet = api.PostUpdate(newMsg+", "+localtime)
File "build\bdist.win32\egg\twitter.py", line 2766, in PostUpdate
data = self._ParseAndCheckTwitter(json)
File "build\bdist.win32\egg\twitter.py", line 3668, in _ParseAndCheckTwitter
self._CheckForTwitterError(data)
File "build\bdist.win32\egg\twitter.py", line 3691, in _CheckForTwitterError
raise TwitterError(data['error'])
TwitterError: Timestamp out of bounds

:'(
pdxnat (author) in reply to cris3DJun 28, 2011. 10:07 AM
cris3D: I don't know the answer. You're able to get the first timestamp test to work (yay!) but when we include python-twitter and use that with the timestamp it does not work. This suggests that the problem has something to do with python-twitter.

Try posting to twitter without the localtime variable and see if you still get an error.
Change this line:  tweet = api.PostUpdate(newMsg+", "+localtime)
to this:  tweet = api.PostUpdate(newMsg)

Also, your file path looks suspicious to me, but again I am a total Python noob. Is your Python installed at C:\Python27 with everything installed inside that file. Please see How to install Python Packages on Windows 7 for more info on installing packages on Windows. Python should be at C:\Python27 and you should "set path" to that location ( set path=%path%;C:\Python27\ )

Good luck. I hope it works. Let me know.
cris3D in reply to pdxnatJun 28, 2011. 11:12 AM
Sure, I read your HOWTO and install python packages, but.. I will try to re-install the twitter package, I'll let you know ,

thanks for all!
pdxnat (author) in reply to cris3DJun 28, 2011. 3:40 PM
Okay, and try posting to twitter without the localtime variable and see if you still get an error.

Change this line: tweet = api.PostUpdate(newMsg+", "+localtime)
to this: tweet = api.PostUpdate(newMsg)
cris3D in reply to pdxnatJun 28, 2011. 7:25 PM
noup, I guess my PC not works.

I tried to run the processing sketch and works in anoter PC.. but in my PC not!

thanks for your attention.. I'll try in laptop when I have it..

saludos!
pdxnat (author) in reply to cris3DJun 29, 2011. 12:09 PM
bummer. Good job testing on another pc. Good Luck!
cris3D in reply to pdxnatJun 25, 2011. 10:10 AM
so quick! I will try this on Monday, and I'll let you know the results..

thanks for your attention!
sheetmetalalchemist says: Jun 23, 2011. 10:12 PM
This is totally the way to go to get an arduino tweeting under linux. So much simpler. One thing that may be handy to point out...there is no COM4 if you are running *nix! Replace COM4 with /dev/ttyUSB0 or whatever is listed in Tools > Serial Port in the arduino IDE!
pdxnat (author) in reply to sheetmetalalchemistJun 24, 2011. 8:04 AM
Right, that's a good point. The COM4 line is written for use in the Windows environment. For Linux environment is should read
serial.Serial('/dev/tty.usbserial', 9600)
http://www.arduino.cc/playground/Interfacing/Python

Thanks for pointing that out. 

arduino = serial.Serial('COM4', 9600, timeout=1)   # windows
arduino = serial.Serial('/dev/tty.usbserial', 9600)    # linux
Layout says: Jun 16, 2011. 11:05 AM
I can't believe I've been a member for four years and I hadn't realized until I saw your comment that I can click the "i" in the corner of the image to see a larger size image. Thanks for that.
pdxnat (author) in reply to LayoutJun 20, 2011. 2:36 AM
Glad I could help.
Pro

Get More Out of Instructables

Already have an Account?

close

PDF Downloads
As a Pro member, you will gain access to download any Instructable in the PDF format. You also have the ability to customize your PDF download.

Upgrade to Pro today!