Moode Forum

Full Version: MQTT support
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Would love to be able to control MoOde by sending MQTT messages it.

At a minimum:

Vol+
Vol-
Mute
Next/Skip
Pause

As a bonus:
Play radio station
And what would be sending these messages? (This is a different way of asking Tim’s favorite question, what’s the use case?)

Regards,
Kent
Hi

I had a quick look at this, as I am also interested in remote access, and it is straighforward to set up.

I installed an MQTT broker on the Moode machine, and added a user and password

   https://www.vultr.com/docs/how-to-instal...untu-16-04

Then adjusted the python script in the following link to use the local broker

      https://funprojects.blog/2016/12/13/house-music/

Code:
client.subscribe("MoodeRadio")
...

client = mqtt.Client()
client.username_pw_set("my_username", "my_password")
client.connect("localhost",1883,60)


I can then play a radio station from a different debian-based machine, by installing "mosquitto-clients" and running
Code:
mosquitto_pub -t "MoodeRadio" -m "http://51.15.169.131:80/stream/3/"  -u my_username -P "my_password" -h moode_player_ip_address

You could use the Python script as a starting point for adding the features you want, and then create a systemctl service file so the script is started at boot.

My interest in this is that I have been thinking about creating a standalone screen, connected to some small board, to display status and control the player. There are some very cheap TFT touchscreens

   https://www.ebay.es/itm/240x320-2-4-SPI-...EBIDX%3AIT

Adrian.
MQTT messages can also span over different networks. Just a few quick examples:

Home Automation system could mute audio if it needs to, for example when someone presses the door bell or someone pulls into the driveway.

Arming security system could stop player, Disarming could re-start playback

Alarm clock but controlled from different remote device

Remote MQTT publisher can push a certain stream (for example if you have multiple locations)

I'm sure others could come up with better examples, but could be very handy.

Adrian, will investigate that
It would prolly be helpful to list each moOde function that would be desirable to control via MQTT.

Play and volume are obvious but are there others?
As @adrii demonstrates, using mqtt is easy. It also demonstrates that implementing pub-sub technology requires more than just installng the mosquitto packages. The devil is in the details and IMHO it's premature to freeze the definition of the "more" so Tim can officially support it.

The use case suggested by the OP essentially duplicates what we already can do with IR/BT remote controllers. Should be easy to lash up. Why not just write a HowTo?

Regards,
Kent
"The robots are coming for humanity "


They've been and gone and come again....!
(11-22-2019, 07:28 AM)DRONE7 Wrote: [ -> ]"The robots are coming for humanity "


They've been and gone and come again....!

Anno Domini 2120... an Android called Greta AI will raise awareness that there were still some living beings called 'humans' on the Planet... and will call for their elimination.
duracell80 - I like your example use case
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()
Pages: 1 2