Thank you for your donation!


Cloudsmith graciously provides open-source package management and distribution for our project.


MQTT support
#10
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()
Reply


Messages In This Thread
MQTT support - by normand - 11-19-2019, 02:51 AM
RE: MQTT support - by TheOldPresbyope - 11-19-2019, 04:30 AM
RE: MQTT support - by adrii - 11-19-2019, 11:59 AM
RE: MQTT support - by normand - 11-19-2019, 12:54 PM
RE: MQTT support - by Tim Curtis - 11-19-2019, 02:07 PM
RE: MQTT support - by TheOldPresbyope - 11-19-2019, 02:33 PM
RE: MQTT support - by DRONE7 - 11-22-2019, 07:28 AM
RE: MQTT support - by CallMeMike - 11-23-2019, 07:51 AM
RE: MQTT support - by normand - 11-23-2019, 04:42 PM
RE: MQTT support - by normand - 03-03-2020, 12:08 AM
RE: MQTT support - by normand - 03-16-2020, 11:39 PM

Forum Jump: