main.py 630 B

1234567891011121314151617181920212223242526272829303132
  1. #!/usr/bin/env python3
  2. import time
  3. import paho.mqtt.client as mqtt
  4. import audioHandler
  5. mqtt_host = "localhost"
  6. mqtt_port = 1883
  7. interval = 20.0 # sec
  8. use_audio_sink = False
  9. audio = audioHandler.Listener(dataTime=interval if interval<5 else 5, agcTime=interval, input=not use_audio_sink)
  10. def main():
  11. audio.start()
  12. mqttc = mqtt.Client()
  13. mqttc.connect_async(mqtt_host, mqtt_port)
  14. mqttc.loop_start()
  15. while True:
  16. if audio.hasNewData():
  17. vol = audio.getAgc()
  18. mqttc.publish("Room/noise", "{:.4f}".format(vol))
  19. time.sleep(interval)
  20. else:
  21. time.sleep(.1)
  22. if __name__ == "__main__":
  23. main()