Skip to content

Commit

Permalink
New config and features for rpi.
Browse files Browse the repository at this point in the history
  • Loading branch information
ben174 committed Jan 23, 2014
1 parent d7feb35 commit dbfc3d4
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 12 deletions.
4 changes: 2 additions & 2 deletions automate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def record_command():
'''
from pygsr import Pygsr
speech = Pygsr()
speech.record(3, 1)
speech.record(3)
result = speech.speech_to_text('en-US')
if result:
return result[0].lower()
Expand Down Expand Up @@ -57,9 +57,9 @@ def setup_logging():


def main():
line = record_command()
setup_logging()
control = Control()
line = record_command()
if not line:
logger.warn('No command recorded.')
return
Expand Down
2 changes: 1 addition & 1 deletion control.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def execute_command(self, command):
return True
elif command.action == 'temperature':
if command.value:
self.nest.set_temperature(degrees)
self.nest.set_temperature(command.value)
return True
else:
logger.error('Could not determine a temperature.')
Expand Down
2 changes: 2 additions & 0 deletions example_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@

FLAC_CONVERT = 'sox audio -t wav -r 48000 -t flac audio.flac'
# or if you want to use flac command use 'flac -f audio'

AUDIO_DEVICE = 'pulse' # sysdefault, pulse
24 changes: 24 additions & 0 deletions mainloop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/usr/bin/env python

import RPi.GPIO as GPIO
import automate


def main():
GPIO.setmode(GPIO.BCM)
GPIO.setup(3, GPIO.IN)
while True:
if GPIO.input(3):
automate.main()


def oldmain():
GPIO.setmode(GPIO.BCM)
GPIO.setup(24, GPIO.IN)
GPIO.setup(25, GPIO.OUT)
GPIO.output(25, True)
while True:
print GPIO.input(24)

if __name__ == '__main__':
main()
32 changes: 24 additions & 8 deletions pygsr.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ class Pygsr:
def __init__(self, file="audio"):
self.format = paInt16
audio = PyAudio()
if hasattr(settings, 'AUDIO_DEVICE_INDEX'):
self.device_index = settings.AUDIO_DEVICE_INDEX
elif hasattr(settings, 'AUDIO_DEVICE'):
for i in range(audio.get_device_count()):
curr_device = audio.get_device_info_by_index(i)
print 'Found device: %s' % curr_device['name']
if curr_device['name'] == settings.AUDIO_DEVICE:
print 'Assigning %s (Index: %s)' % (settings.AUDIO_DEVICE, i)
self.device_index = i
elif not hasattr('device_index', self):
print 'No Audio device specified. Assuming index 1'
self.device_index = 1
print audio.get_device_info_by_index(self.device_index)
try:
calc_rate = audio.get_device_info_by_index(1)['defaultSampleRate']
print 'Discovered Sample Rate: %s' % calc_rate
Expand All @@ -27,12 +40,16 @@ def convert(self):
fh = open("NUL","w")
subprocess.call(settings.FLAC_CONVERT.split(), stdout=fh, stderr=fh)

def record(self, time, device_i):
def record(self, time):
audio = PyAudio()
#print audio.get_device_info_by_index(1)
stream = audio.open(input_device_index=device_i,output_device_index=device_i,format=self.format, channels=self.channel,
rate=self.rate, input=True,
frames_per_buffer=self.chunk)
stream = audio.open(input_device_index=self.device_index,
output_device_index=self.device_index,
format=self.format,
channels=self.channel,
rate=self.rate,
input=True,
frames_per_buffer=self.chunk
)
print "Recording..."
frames = []
for i in range(0, self.rate / self.chunk * time):
Expand Down Expand Up @@ -61,6 +78,5 @@ def speech_to_text(self, language):
response = post.read()
phrase = loads(response)['hypotheses'][0]['utterance']
return phrase, response
except urllib2.HTTPError, err:
if err.code == 400:
return "Error: No se pudo reconocer", "400"
except:
return None
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
PyAudio==0.2.7
phue==0.7
py==1.4.19
pynest==1.0
Expand Down

0 comments on commit dbfc3d4

Please sign in to comment.