There is a possibility that this error is not caused by an Arduino issue, therefore this post will serve as a learning curve for myself and others.
I am currently making pin12 go HIGH and LOW after receiving a new tweet on Python.
The program will run for a couple of days and then Python will crash. I have yet to see the Error code as the unit is very far away from myself. Until I am sent the error code, I'm trying to understand possible problems.
I have two queries.
- How can I ensure the Python script will not crash if Serial connection is temporarily lost?
At the moment, using a Try / Except doesn't seem to cut it as once unplugged, the computer doesn't recognise the COM port again.
- Does disconnecting and reconnecting to the Arduino from a PC use more memory each time or does it reset the Arduino?
Here is the python code (with the COM port Try/Except removed):
from pprint import pprint
#to collect the first tweet without vending
countTweet = 0
#the infinate loop
#the connection to the arduino
ser = serial.Serial('COM3',9600)
#not connected to arduino before connection is made
connected = False
#loop until the arduino is connected
while not connected:
serin = ser.read()
connected = True
#debug arduino connection
if connected == True:
pprint('connected to arduino')
response = urllib.urlopen('http://search.twitter.com/search.json?q=%23happy&result_type=recent&rpp=1&filter:retweets')
pprint ('internet or twitter is down. This program will continue working when the connection opens again.')
#Check that j contains the correct json, else twitter has sent another broken one.
text = j['results']['text']
id = j['results']['id']
#How many times the Json is false
noTweet += 1
#to isolate the first loop, if the first ID has been stored already (countTweet == 1)
if countTweet != 0:
pprint ("new tweet")
#if lastID is not equal to ID
if lastID != id:
#Tell Arduino to Vend
#loop until the arduino tells us it is done vending by sending us a '0'
while ser.read() == '1':
#Make lastID equal to ID
lastID = id
pprint ('lastID updated')
#if no new tweets, print
pprint ('no new tweets')
#If it's the first loop, confirm by printing to the screen
pprint("First loop complete")
lastID = id
countTweet += 1
#make count not equal to 0 after first loop
pprint ('closing arduino connection')
pprint('waiting 15 seconds')
pprint ('Number of Tweets')
And here is the simple Arduino sketch:
//Tell python we are done with setup
//Vend pin high
//Tell Python we have flashed