from pyaudio import PyAudio, paInt16
from thunderDetector import ThunderDetector
from serial import Serial

pa = PyAudio()
info = pa.get_host_api_info_by_index(0)

numdevices = info.get('deviceCount')
for i in range(0, numdevices):
        if (pa.get_device_info_by_host_api_device_index(0, i).get('maxInputChannels')) > 0:
            print "Input Device id ", i, " - ", pa.get_device_info_by_host_api_device_index(0, i).get('name')

chunk = 1024
on_threshold = 4
off_threshold = 2
counter=0

detector = ThunderDetector(chunk)


stream = pa.open(format = paInt16,
                 channels = 5,
                 rate = 44100,
                 input=True,
                 )

data=stream.read(chunk)

ser = None
#ser = Serial('/dev/ttyUSB2')

while len(data)>0:
    #check if there is thunder in the data
    detector.detect(data, on_threshold, off_threshold)
    #get new data
    data=stream.read(chunk)
    #change this print statement to output
    intensity = int(detector.intensity)
    print intensity
    if ser:
        ser.write([intensity])

stream.stop_stream()
stream.close()
if ser:
    ser.close()
pa.terminate()


@@ -99,16 +99,52 @@
 t Serial
+%0Afrom glob import glob%0Aimport random
 %0A%0Apa = P
@@ -261,20 +261,16 @@
 vices):%0A
-    
     if (
@@ -349,20 +349,16 @@
 )) %3E 0:%0A
-    
         
@@ -511,17 +511,19 @@
 %0Acounter
-=
+ = 
 0%0A%0Adetec
@@ -728,33 +728,106 @@
 k)%0A%0A
-ser = None%0A#ser
+num_devices_to_output_to = 0%0Aserial_device_names = glob(%22/dev/ttyUSB*%22)%0Aserial_devices
  =
+ %5B
  Serial(
 '/de
@@ -826,31 +826,69 @@
 ial(
-'/
 dev
-/ttyUSB2')
+ice_name) for device_name in serial_device_names %5D
 %0A%0Awhile 
 len(
@@ -887,19 +887,12 @@
 ile 
-len(
 data
-)%3E0
 :%0A  
@@ -1116,16 +1116,104 @@
 ensity)%0A
+%0A    # Switch which devices to output to everytime it's quiet%0A    if intensity %3E 0:%0A    
     prin
@@ -1224,16 +1224,20 @@
 tensity%0A
+    
     if s
@@ -1234,24 +1234,35 @@
       if ser
+ial_devices
 :%0A        se
@@ -1259,19 +1259,252 @@
         
+ 
-ser
+   random.shuffle(serial_devices)%0A            num_devices_to_output_to = random.randint(0, len(serial_devices)-1)%0A%0A    # Write data to relevant serial devices.%0A    for device in serial_devices%5B:num_devices_to_output_to%5D:%0A        device
 .write(%5B
@@ -1556,23 +1556,48 @@
 e()%0A
-if ser:%0A    ser
+for device in serial_devices:%0A    device
 .clo

Handle multiple USB serial devices.