Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remote control with MQTT #581

Closed
codegrau opened this issue Sep 26, 2019 · 66 comments
Closed

Remote control with MQTT #581

codegrau opened this issue Sep 26, 2019 · 66 comments

Comments

@codegrau
Copy link

Hi there, thank you @MiczFlor and all contributors for this wonderful project.

Since mqtt is very common for smart home and home automation stuff I was wondering: wouldn't it be cool for phonieboxes to connect to a mqtt broker for other devices to know, which tune is playing and control phoniebox remotely?

  • Publish "now playing" to a selected mqtt topic
  • Subscribe to a mqtt topic for remote control

This way you could easily integrate phoniebox to openHAB or HomeAssistant or even use cheap ESP8266 to control or visualize phoniebox contents.

Looking forward to your thoughts.

@andreasbrett
Copy link
Contributor

Awesome idea! Would love to use this as well with my openHAB setup.

@MiczFlor
Copy link
Owner

@codegrau @andreasbrett
It's a great idea. I have no smart home, it's nice here, but not very smart ;) so I can't help with this idea. Makes sense though and I will be hovering over the pull requests - possibly you can create a small task group.

@andreasbrett
Copy link
Contributor

I'll see if I can contribute to this feature. I need to dig into the phoniebox infrastructure first and see how and where to insert the mqtt stuff.

@codegrau any ideas on what exact features we would need to control/monitor via mqtt?

@andreasbrett
Copy link
Contributor

@codegrau
I think we can close this issue as it is implemented now.

@codegrau
Copy link
Author

codegrau commented Oct 4, 2019

Sorry for my late response, but what you've achieved @andreasbrett is incredible! This is exactly what I meant! Thank you for this one!

@andreasbrett
Copy link
Contributor

@codegrau thanks, it was easier than I thought. Let me know if there's something else that could be done with regards to MQTT.

@whenthelight
Copy link

Hey @ all i had massive problems with Openhab and Mosquito to get and read States (Commands are without Errors and i have it run till google Home can control my Box :-) , thats so awesome), so test step by step and figured some errors (dont know its only me on rasbian buster?) :
1st: its not phoniebox/attributes/attributeName ist phoniebox/attribute/attributeName
2nd: with sample Conf for service in systemd the working Directory must be WorkingDirectory=/home/pi/RPi-Jukebox-RFID/scripts
not
WorkingDirectory=/home/pi/RPi-Jukebox-RFID
without the directory scripts you wont get attribute Values

Hope this help someone

Thanks to dev for this awesome script and Idea of Mqtt

@andreasbrett
Copy link
Contributor

andreasbrett commented Oct 4, 2019

1st: its not phoniebox/attributes/attributeName ist phoniebox/attribute/attributeName

Fixed the typo in the wiki, thanks for pointing that out!

2nd: with sample Conf for service in systemd the working Directory must be WorkingDirectory=/home/pi/RPi-Jukebox-RFID/scripts
not
WorkingDirectory=/home/pi/RPi-Jukebox-RFID

Okay, I think I found the bug with that configuration. Typically my scripts are independent from the working directory as I use the script path. Thanks to your hint I found 3 attributes where I did NOT do so. maxvolume, idletime and volstep. Did you have issues with other attributes as well? Those 3 bugs will be fixed once @MiczFlor merges the pull request (#593) into master. It contains some new features and I also added the fixes a minute ago.

@whenthelight
Copy link

whenthelight commented Oct 4, 2019

1st: its not phoniebox/attributes/attributeName ist phoniebox/attribute/attributeName

Fixed the typo in the wiki, thanks for pointing that out!

2nd: with sample Conf for service in systemd the working Directory must be WorkingDirectory=/home/pi/RPi-Jukebox-RFID/scripts
not
WorkingDirectory=/home/pi/RPi-Jukebox-RFID

Okay, I think I found the bug with that configuration. Typically my scripts are independent from the working directory as I use the script path. Thanks to your hint I found 3 attributes where I did NOT do so. maxvolume, idletime and volstep. Did you have issues with other attributes as well? Those 3 bugs will be fixed once @MiczFlor merges the pull request (#593) into master. It contains some new features and I also added the fixes a minute ago.

Hey, so fast :-) i couldn't drink my Pale and have to sit down for testing .
The Problem with the "scripts" path is wenn i start the service the "get/all" doesnt get Values in al Attributes. Not only these 3 but when it has merged i test some Attributes. actually this error appears only in the "get" and "attribute" topics , the cmd have no error and delivers without error. Also in the Syslog i see "get/all" empty Value . When start directly with python3 "script" no error and all topics become their Values

PS: the new Function for Folderplay(which is incredible when i get this work as Switch and then in Google Home :-)) need some Parameters , are these the directory Path to Musicalbums ? like : /RPi-Jukebox-RFID/shared/audiofolders/Album1 or only Album1 ?

thx

Update: Change your Commit et voila , thumbsup all works like charm.

@andreasbrett
Copy link
Contributor

andreasbrett commented Oct 4, 2019

PS: the new Function for Folderplay(which is incredible when i get this work as Switch and then in Google Home :-)) need some Parameters , are these the directory Path to Musicalbums ? like : /RPi-Jukebox-RFID/shared/audiofolders/Album1 or only Album1 ?

You simply pass the folder name (Album1). BTW: I primarily use the Basic UI to control openHAB and therefore would implement that with a String element that is linked to the MQTT topic "phoniebox/cmd/playfolder" and make a dropdown in the sitemap with pre-set folder names. Something like that:

Selection item=phoniebox_playfolder mappings=["Album1"="Tool - Fear Inocolum", "Album2"="Slipknot - We are not your kind"]

So if I select the Slipknot album from that dropdown the value "Album2" would be sent to the playfolder command. Not sure how you would handle that with your Google Home though.

Update: Change your Commit et voila , thumbsup all works like charm.

Great!

@KingKahn123
Copy link

Hi, I still have an issue with the MQTT function. I have connected the Phoniebox to my MQTT server of my IPSymcon system, but only 4 entries are created:

image

The phoniebox/get/all attribute gets updated every 10 seconds when a music file is playing. However no data is written to this attribute. In the log files I can see that the payload for this attribute is NULL.

Any idea why no information is forwarded to this attribute?

Thx for your help...

Kai

@andreasbrett
Copy link
Contributor

@KingKahn123
You misunderstood the documentation. Attributes are sent to /attribute/$attributeName (so e.g. /attribute/volume or /attribute/elapsed).

/get/$attributeName only triggers phoniebox to publish that attribute to /attribute/$attributeName. That's only used if you want to receive the updates quicker or at a specific time rather than waiting for the next interval. What you will have to do though is set your smart home tool to monitor each attribute separately (via e.g. /attribute/volume).

@andreasbrett
Copy link
Contributor

Oh and I see that you're using the Spotify edition. Haven't tested that yet as I'm not using it. Let me know if it works like I described above.

@KingKahn123
Copy link

I have created now one additional attribute manually:

image

The details look like this:

image

However no updates are received for this attribute.

@KingKahn123
Copy link

In addition, as far as I understand the MQTT function from the Phoniebox is that all attributes are refreshed at a predefined interval, correct? That means during playback of any music file, the attributes are sent to the server every 10 seconds (depending on the config file). This would be recognized by my SmartHome system and the attributes would be created automatically. But only the four are created. So I assume that no data is transferred to my SmartHome system.

@whenthelight
Copy link

Hey, in Mqtt you have to subscribe to an Topic to receive an Attribute. You have to make an Status or SUB in your Software with phoniebox/attribute/Volume an no Value.

It seems that you set up an public with that Topic above and an Value

@KingKahn123
Copy link

But this is how my other attributes, where I receive some data from the Phoniebox, are defined.

image


Received information on startup:

image

This is how my MQTT server receives the publish get/all from the Phoniebox. The payload is empty. Should it not include some data?

 image

@whenthelight
Copy link

Sure ;-) because you use publish to publish plusSpotify to the Topic phoniebox/edition
You need an Function in your Ip_symcon that catchs the Values from the Topics not send some.
Like https://github.com/thomasf68/IPS_MQTT.
You received this in your mqtt server because you published that with your Action.

@andreasbrett
Copy link
Contributor

Should it not include some data?

No. Please read again the documentation and my description above. The get topic NEVER is filled with payload because the topic itself will trigger an action and does not need a payload as all the necessary information is already in the subtopic (attributename). The reason why your tool detects this topic is because the periodic publishing of attributes is triggered by phoniebox sending /get/all to itself. Just ignore that.

The reason you're not receiving any attributes has to do with you being on the Spotify edition. I will have to check how the management of the player (which I guess is different to the classic edition) works for the Spotify edition.

@andreasbrett
Copy link
Contributor

@whenthelight He is not publishing anything. His tool just subscribed to all possible topics and phoniebox indeed is sending 4 parameters at the beginning (independent of the player attributes).

Reason why he does not receive any attributes is because that function seems to crash due to some difference between the classic and Spotify edition.

@MiczFlor
Are there major differences on how I can retrieve player data in the Spotify edition? As you see in my code I mostly use queries to mpd as well as the payout controls.sh script.

@KingKahn123
Copy link

Sure ;-) because you use publish to publish plusSpotify to the Topic phoniebox/edition
You need an Function in your Ip_symcon that catchs the Values from the Topics not send some.
Like https://github.com/thomasf68/IPS_MQTT.
You received this in your mqtt server because you published that with your Action.

This mqtt module was now integrated in IPSymcon 5.1/5.2. No need to have this module any longer ;-) When I create this attribute initially the value field is empty... After reboot of the Phoniebox the value is filled in by the MQTT publish:

image

@KingKahn123
Copy link

@whenthelight He is not publishing anything. His tool just subscribed to all possible topics and phoniebox indeed is sending 4 parameters at the beginning (independent of the player attributes).

Reason why he does not receive any attributes is because that function seems to crash due to some difference between the classic and Spotify edition.

I think so as well as I now checked the log file of my IPSymcon MQTT server and I do not see anything else than the 4 parameters. The get/all is occurring every 10 seconds. But I now understood that it is ok, if it is empty ;-)

Thanks for looking into it

@whenthelight
Copy link

@whenthelight He is not publishing anything. His tool just subscribed to all possible topics and phoniebox indeed is sending 4 parameters at the beginning (independent of the player attributes).
Hey im blind :-) didnt see that he is RX the Publish , sorry for missing this. I read the manual of the IPsymcon :-)
Reason why he does not receive any attributes is because that function seems to crash due to some difference between the classic and Spotify edition.

@MiczFlor
Are there major differences on how I can retrieve player data in the Spotify edition? As you see in my code I mostly use queries to mpd as well as the payout controls.sh script.

What is the output of journalctl -u phoniebox-mqtt-client.service there you see whats going on

@andreasbrett
Copy link
Contributor

@KingKahn123
Could you please try the latest version of the mqtt daemon? It's not yet merged but includes some major changes that might already get rid of the errors.

You can get it here:
https://github.com/andreasbrett/RPi-Jukebox-RFID/blob/mqtt-dev/scripts/daemon_mqtt_client.py

@KingKahn123
Copy link

Where should I add it? Right at the end of the script after: print("result =", status) ?

@andreasbrett
Copy link
Contributor

Yes, right there.

@KingKahn123
Copy link

Ok then I get following error message:

image

@andreasbrett
Copy link
Contributor

Sorry change the first line of the script to

import subprocess, re

@andreasbrett
Copy link
Contributor

(sorry for all the hassle. I'm on my smartphone right now and can't properly help)

@KingKahn123
Copy link

no worries... Thx for the help:

image

@andreasbrett
Copy link
Contributor

Okay, that all looks very good. Could you try running the mqtt script?

First stop the service with sudo systemctl stop phoniebox-mqtt-client

Then run the script manually with
python3 daemon_mqtt_client.py

@KingKahn123
Copy link

image

@andreasbrett
Copy link
Contributor

Okay, this looks like the subscription to the /get/# topic did not work. Which mqtt server are you using? Could you try and send to /get/volume (any payload) from your smart home software? The daemon script needs to run while you're doing it. It should output that it received something.

@KingKahn123
Copy link

Ok that worked:

image

And my MQTT Server (IPSymcon) received the publish and saved it in the attribute:

image

@andreasbrett
Copy link
Contributor

Alright, I think I fixed it. Please download the script once again from latest dev (see link above).

@KingKahn123
Copy link

Perfect! It is working now ;-)

image

@andreasbrett
Copy link
Contributor

Awesome! Thanks for the patience and enjoy it. Let me know if there's something missing that you need/want.

@KingKahn123
Copy link

Are you kidding me?! Thanks for your patience and your help!

@MiczFlor
Copy link
Owner

MiczFlor commented Oct 8, 2019

I committed the last pull requests to master:
#600

@MiczFlor
Copy link
Owner

MiczFlor commented Oct 8, 2019

And thanks for the lively discussion and great input!

@andreasbrett
Copy link
Contributor

Thanks and sorry if we flooded your mailbox... 😜

@whenthelight
Copy link

PS: the new Function for Folderplay(which is incredible when i get this work as Switch and then in Google Home :-)) need some Parameters , are these the directory Path to Musicalbums ? like : /RPi-Jukebox-RFID/shared/audiofolders/Album1 or only Album1 ?

You simply pass the folder name (Album1). BTW: I primarily use the Basic UI to control openHAB and therefore would implement that with a String element that is linked to the MQTT topic "phoniebox/cmd/playfolder" and make a dropdown in the sitemap with pre-set folder names. Something like that:

Selection item=phoniebox_playfolder mappings=["Album1"="Tool - Fear Inocolum", "Album2"="Slipknot - We are not your kind"]

So if I select the Slipknot album from that dropdown the value "Album2" would be sent to the playfolder command. Not sure how you would handle that with your Google Home though.

Hey andreasbrett, If i select only 1 Album , i can handle this as Switch with 1 Value (Album1).
Items create as Switch et voila my Son is happy ;-) . Volume are configured as Dimmer and Lightning. The Reaction Time is awesome fast. Sleeptimer also as Switch with Value in my Case 60 for 60min. Play and Pause also works good as Switch.

@georgbachmann
Copy link

georgbachmann commented Oct 13, 2019

Thank you guys a lot for this addon! I can't wait to realize a wake-up-alarm for my kids with it!
I can run it and it also works if I run it manually like so: python3 RPi-Jukebox-RFID/scripts/daemon_mqtt_client.py.
But when I follow the instructions on the wiki page and then run sudo systemctl status phoniebox-mqtt-client at the end, I always get error messages:

pi@phoniebox:~ $ sudo systemctl status phoniebox-mqtt-client
● phoniebox-mqtt-client.service - Phoniebox MQTT Client Service
   Loaded: loaded (/etc/systemd/system/phoniebox-mqtt-client.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2019-10-13 19:59:43 BST; 1min 42s ago
  Process: 531 ExecStart=/home/pi/RPi-Jukebox-RFID/scripts/daemon_mqtt_client.py (code=exited, status=203/EXEC)
 Main PID: 531 (code=exited, status=203/EXEC)

Oct 13 19:59:42 phoniebox systemd[1]: phoniebox-mqtt-client.service: Failed with result 'exit-code'.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Service hold-off time over, scheduling restart.
Oct 13 19:59:43 phoniebox systemd[1]: Stopped Phoniebox MQTT Client Service.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Start request repeated too quickly.
Oct 13 19:59:43 phoniebox systemd[1]: Failed to start Phoniebox MQTT Client Service.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Unit entered failed state.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Failed with result 'exit-code'.

Do you guys have any idea what the problem might be?
I already tried rebooting... but didn't work.

@whenthelight
Copy link

Thank you guys a lot for this addon! I can't wait to realize a wake-up-alarm for my kids with it!
I can run it and it also works if I run it manually like so: python3 RPi-Jukebox-RFID/scripts/daemon_mqtt_client.py.
But when I follow the instructions on the wiki page and then run sudo systemctl status phoniebox-mqtt-client at the end, I always get error messages:

pi@phoniebox:~ $ sudo systemctl status phoniebox-mqtt-client
● phoniebox-mqtt-client.service - Phoniebox MQTT Client Service
   Loaded: loaded (/etc/systemd/system/phoniebox-mqtt-client.service; enabled; vendor preset: enabled)
   Active: failed (Result: exit-code) since Sun 2019-10-13 19:59:43 BST; 1min 42s ago
  Process: 531 ExecStart=/home/pi/RPi-Jukebox-RFID/scripts/daemon_mqtt_client.py (code=exited, status=203/EXEC)
 Main PID: 531 (code=exited, status=203/EXEC)

Oct 13 19:59:42 phoniebox systemd[1]: phoniebox-mqtt-client.service: Failed with result 'exit-code'.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Service hold-off time over, scheduling restart.
Oct 13 19:59:43 phoniebox systemd[1]: Stopped Phoniebox MQTT Client Service.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Start request repeated too quickly.
Oct 13 19:59:43 phoniebox systemd[1]: Failed to start Phoniebox MQTT Client Service.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Unit entered failed state.
Oct 13 19:59:43 phoniebox systemd[1]: phoniebox-mqtt-client.service: Failed with result 'exit-code'.

Do you guys have any idea what the problem might be?
I already tried rebooting... but didn't work.

Show was going on with : journalctl -u phoniebox-mqtt-client.service

@georgbachmann
Copy link

Sorry for the super late reply... so much stuff going on currently and so little time for the fun stuff...
The message I am getting was essentially:

phoniebox-mqtt-client.service: Failed at step EXEC spawning /home/pi/RPi-Jukebox-RFID/scripts/daemon_mqtt_client.py: Permission denied

So I simply ran chmod +x daemon_mqtt_client.py any everything is now working as expected... awesome :)
Excited to see how long my kids will like "Guten morgen Sonnenschein" as their wake up sound :) haha

Again... thanks for that MQTT plugin! Love it!

@andreasbrett
Copy link
Contributor

So I simply ran chmod +x daemon_mqtt_client.py any everything is now working as expected

Ah I see. I had not pushed the file via git but instead pasted its contents here on github. That's why the file attributes are missing. Thanks for pointing out!

@MiczFlor
Copy link
Owner

Hi @andreasbrett can you create a pull request, please?

@andreasbrett
Copy link
Contributor

Hi @andreasbrett can you create a pull request, please?

Of course. It's done.

@s-martin
Copy link
Collaborator

Description for MQTT in https://github.com/MiczFlor/RPi-Jukebox-RFID/wiki/Smart-Home-remote-control-with-MQTT

Closing this issue now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants