Reputation: 1049
Hello I have this firebase set up that is connected to a raspberry pi that speaks text according the firebase database. I have the function running every 5 seconds, and I'd like to check if there is a new message. I store the time of each message in the database, and the way i have it going now is that it's checking to see if the time is the same on new and old messages.
My two questions are:
Is there a better way to checking the messages time, to see if messages are new?
How can I fix this code so I am not getting a UnboundLocalError: local variable 'the_time' referenced before assignment"
error
Here is my code
import time
import subprocess
from firebase import firebase
firebase = firebase.FirebaseApplication('----', None)
message = firebase.get('/message', None)
name = firebase.get('/name', None)
the_time = firebase.get('the_time',None)
speak_message = message+" from "+ name
def showmessage():
message=firebase.get('/message',None)
name=firebase.get('/name',None)
current_time = firebase.get('/the_time',None)
speak_message=message+' from '+name
#this is to set the audio jack on raspi
subprocess.call(['amixer','cset','numid=3','1'])
if current_time == the_time:
#message is NOT new
print 'message is NOT new'
elif current_time != the_time:
#message IS new
#Shell script to run text-to-speech
subprocess.call(['/home/pi/./speech.sh',speak_message])
the_time = current_time
time.sleep(5)
while True:
showmessage()
Upvotes: 1
Views: 1111
Reputation: 28292
Your script should works just fine, except one little, tiny mistake in this part:
elif current_time != the_time:
#message IS new
#Shell script to run text-to-speech
subprocess.call(['/home/pi/./speech.sh',speak_message])
the_time = current_time
You're assigning to the_time
, which is a global variable. In your showmessage
function, you didn't declare it, right?
To fix this, you must declare the_time
to be a global variable.
def showmessage():
global the_time
#all other stuff
That's all you need to change :) Hope this helps!
Upvotes: 1