Last active
March 4, 2019 14:39
-
-
Save bphermansson/7c34693c073b1f7191008dcae558e841 to your computer and use it in GitHub Desktop.
Detects sound playing on a Linuxsystem. Sends messages via Mqtt when sound starts and stops.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
''' | |
Send a Mqtt message if system is playing sound, another if it's stopped | |
Put Mqtt server password in a separate file named "mqttpass.txt". | |
''' | |
import os, time, json, sys | |
import paho.mqtt.client as mqtt | |
MQTT_HOST = '192.168.1.79' | |
MQTT_PORT = 1883 | |
MQTT_USER = 'emonpi' | |
MQTT_TOPIC = 'HallwaySound' | |
# Mqtt server password in a separate file | |
f = open('mqttpass.txt', 'r') | |
MQTT_PASS = f.readline().strip() | |
soundstatus=0 | |
def on_connect(client, userdata, flags, rc): | |
if rc==0: | |
client.connected_flag=True #set flag | |
print("connected OK Returned code=",rc) | |
else: | |
print("Bad connection Returned code=",rc) | |
client.bad_connection_flag=True | |
try: | |
mqtt.Client.connected_flag=False | |
mqtt.Client.bad_connection_flag=False | |
client = mqtt.Client("Sounddetector") | |
client.username_pw_set(MQTT_USER, MQTT_PASS) | |
client.on_connect=on_connect #bind call back function | |
client.loop_start() | |
print("Start Mqtt loop") | |
client.connect(MQTT_HOST, MQTT_PORT) | |
while not client.connected_flag and not client.bad_connection_flag: #wait in loop | |
print("In wait loop") | |
time.sleep(1) | |
if client.bad_connection_flag: | |
client.loop_stop() #Stop loop | |
print("Mqtt connection failed, exiting.") | |
sys.exit() | |
client.publish(MQTT_TOPIC, "Sound detector started") | |
print ("Connected to Mqtt server") | |
#except: | |
# print "Unexpected error:", sys.exc_info()[0] | |
except Exception as e: | |
exc_type, exc_obj, exc_tb = sys.exc_info() | |
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] | |
print(exc_type, fname, exc_tb.tb_lineno) | |
while 1: | |
if not (os.system("pacmd list-sink-inputs | grep -c 'state: RUNNING' >/dev/null")): | |
if (soundstatus == 0): | |
print("Sound on") | |
try: | |
mqtt_client = mqtt.Client() | |
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASS) | |
mqtt_client.connect(MQTT_HOST, MQTT_PORT) | |
mqtt_client.publish(MQTT_TOPIC, "on") | |
except: | |
print ("Unexpected error:", sys.exc_info()[0]) | |
soundstatus = 1 | |
else: | |
if (soundstatus == 1): | |
print("Sound off") | |
try: | |
mqtt_client = mqtt.Client() | |
mqtt_client.username_pw_set(MQTT_USER, MQTT_PASS) | |
mqtt_client.connect(MQTT_HOST, MQTT_PORT) | |
mqtt_client.publish(MQTT_TOPIC, "off") | |
except: | |
print ("Unexpected error:", sys.exc_info()[0]) | |
soundstatus = 0 | |
time.sleep(5) | |
mqtt_client.loop_stop() #Stop loop | |
mqtt_client.disconnect() # disconnect |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment