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 ads by
Signing UpStep 1: Concept
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.







































Visit Our Store »
Go Pro Today »




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!
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'
>>>
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
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.
* 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!
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!
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!
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
:'(
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.
thanks for all!
Change this line: tweet = api.PostUpdate(newMsg+", "+localtime)
to this: tweet = api.PostUpdate(newMsg)
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!
thanks for your attention!
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