03-03-2020, 12:08 AM
Got more features to add but here is my working MQTT script as it is at this moment.
Code:
#!/usr/bin/env python
'''
Getting Started
---------------
1.edit settings section of this script using your mqtt broker settings
2.copy mqtt.py to /home/pi folder
3.edit /etc/rc.local, add the following just before "exit 0":
# mosquitto
/home/pi/mqtt.py > /dev/null 2>&1 &
4.save changes and reboot
5.test
Basic Commands
--------------
volume up 10
volume dn 10
volume 25
mute
stop
pause
toggle
stream http://stream.radioparadise.com/mp3-320
alert ~/doorbell.wav
reboot
Advanced Commands
-----------------
Any valid mpc command in the format of:
mpc command
'''
# settings
mqttHost = "192.168.xxx.xxx"
mqttPort = 1883
mqttKeep = 60 #keep alive seconds
mqttUser = "test"
mqttPass = "password"
subTopic = "moode/player1/command"
pubTopic = "moode/player1/status"
alertVol = "70%"
# don't change anything below unless you know what you are doing ;-)
import paho.mqtt.client as mqtt
import subprocess
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
client.subscribe(subTopic)
client.publish(pubTopic, "Online", qos=0, retain=True)
ip = subprocess.check_output("hostname -I", shell=True)
host = subprocess.check_output("hostname", shell=True)
volume = subprocess.check_output("/var/www/vol.sh", shell=True)
client.publish(pubTopic + "/ip", ip, qos=0, retain=True)
client.publish(pubTopic + "/hostname", host, qos=0, retain=True)
client.publish(pubTopic + "/volume", volume, qos=0, retain=True)
#TODO add log error
def on_message(client, userdata, msg):
action = msg.payload.decode("utf-8")
if "volume" in action:
vol = action.replace("volume ", "")
subprocess.check_output("/var/www/vol.sh " + vol, shell=True)
vol = subprocess.check_output("/var/www/vol.sh", shell=True)
client.publish(pubTopic + "/volume" , vol, qos=0, retain=True)
elif "mpc " in action:
#mpc = action.replace("mpcvolume ", "")
subprocess.check_output(action, shell=True)
#client.publish(pubTopic + "/volume" , "Muted", qos=0, retain=False)
elif action == "mute":
subprocess.check_output("/var/www/vol.sh mute", shell=True)
client.publish(pubTopic + "/volume" , "Muted", qos=0, retain=False)
elif action == "stop":
subprocess.check_output("mpc stop", shell=True)
elif action == "pause" or action == "toggle":
subprocess.check_output("mpc toggle", shell=True)
elif action == "status":
status = subprocess.check_output("mpc status", shell=True)
client.publish(pubTopic + "/mpc-status" , status, qos=0, retain=True)
elif action == "sync":
pass
elif action == "reboot":
subprocess.check_output("sudo reboot now", shell=True)
elif "stream " in action:
url = action.replace("stream ", "")
subprocess.check_output("mpc clear && mpc add " + url + " && mpc play", shell=True)
elif "alert " in action:
url = action.replace("alert ", "")
subprocess.check_output("mpc toggle && amixer set Digital " + alertVol + " && aplay " + url, shell=True)
subprocess.check_output("amixer set Digital 100% && mpc toggle", shell=True)
else:
pass
def on_publish(client, userdata, mid):
print("mid: " + str(mid))
def on_subscribe(client, userdata, mid, granted_qos):
print("Subscribed: " + str(mid) + " " + str(granted_qos))
def on_log(client, userdata, level, buf):
print(buf)
client = mqtt.Client()
client.username_pw_set(mqttUser,mqttPass)
# last will
client.will_set(pubTopic, "Offline", qos=0, retain=True)
# event callbacks
client.on_message = on_message
client.on_connect = on_connect
client.on_publish = on_publish
client.on_subscribe = on_subscribe
# connect
client.connect(mqttHost,mqttPort,mqttKeep)
# keep listening for messages
client.loop_forever()