Hello.
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):
import json
import urllib
from pprint import pprint
import time
import serial
#to collect the first tweet without vending
countTweet = 0
tweet= 0
noTweet= 0
#the infinate loop
while True:
#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')
try:
response = urllib.urlopen('http://search.twitter.com/search.json?q=%23happy&result_type=recent&rpp=1&filter:retweets')
except IOError:
pprint ('internet or twitter is down. This program will continue working when the connection opens again.')
time.sleep(15)
continue
j =json.loads(response.read())
#Check that j contains the correct json, else twitter has sent another broken one.
if j['results']:
text = j['results'][0]['text']
id = j['results'][0]['id']
pprint(text)
pprint(id)
tweet+= 1
else:
#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
ser.write('1')
ser.write('0')
#loop until the arduino tells us it is done vending by sending us a '0'
while ser.read() == '1':
ser.read()
#Make lastID equal to ID
lastID = id
pprint ('lastID updated')
#if no new tweets, print
else:
pprint ('no new tweets')
#If it's the first loop, confirm by printing to the screen
else:
pprint("First loop complete")
lastID = id
pprint(lastID)
countTweet += 1
#make count not equal to 0 after first loop
pprint ('closing arduino connection')
ser.close()
#wait
pprint('waiting 15 seconds')
pprint ('Number of Tweets')
pprint (countTweet)
pprint('Working JSON')
pprint(tweet)
pprint('Broken JSON')
pprint(noTweet)
time.sleep(15)
And here is the simple Arduino sketch:
void setup(){
Serial.begin(9600);
pinMode(12, OUTPUT);
//Tell python we are done with setup
Serial.write('1');
}
void loop(){
if(Serial.available() >0){
//Vend pin high
digitalWrite(12,HIGH);
delay(1000);
digitalWrite(12,LOW);
//Tell Python we have flashed
Serial.write('0');
}
}