Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU General Public License as published by
- # the Free Software Foundation, either version 3 of the License, or
- # (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU General Public License for more details.
- #
- # You should have received a copy of the GNU General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- #
- #
- # Python/MQTT volume controller (ALSA).
- #
- # Every 2 second it checks the current system volume. If it has
- # changed, it is reported via MQTT. If a setvolume command is
- # received through MQTT, the system volume is set to the incoming
- # value.
- #
- # This script can run as a service, for a user that has the necessary
- # rights to control the alsa volume (f.ex. logged in user). No
- # guarantees for security holes/bugs if run as root.
- import paho.mqtt.client as mqtt
- import alsaaudio
- import time
- import socket
- mqtt_broker = 'raaserv'
- mqtt_port = 1883
- sleeptime = 2 # seconds
- topic = socket.gethostname() + "/volume"
- currentvolume = 0
- def on_connect(client, userdata, flags, rc):
- print "Connected to MQTT"
- client.subscribe(socket.gethostname() + '/setvolume')
- def on_message(client, userdata, msg):
- print "Got message " + str(msg.payload)
- alsaaudio.Mixer().setvolume(int(msg.payload))
- client = mqtt.Client()
- client.on_connect = on_connect
- client.connect(mqtt_broker, mqtt_port)
- client.on_message = on_message
- time.sleep(1)
- while True:
- newvolume = int(alsaaudio.Mixer().getvolume()[0])
- print newvolume
- if newvolume != currentvolume:
- client.publish(topic, str(newvolume))
- currentvolume = newvolume
- print "changed: " + str(newvolume)
- client.loop(sleeptime)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement