Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

HC2 & iTunes on OS X integration ... any idea ?


gucio1234

Question

I have found several topics on HC2 and iTunes.  However there were only brief ideas and I did not find any complete, well documented solution.

 

Is there anybody who managed to integrate  iTunes running on OS X system and Fibaro HC2 box in the way that VD would be able to control iTunes ?

 

It would be great to have VD with buttoons to play, stop, select playlist, etc.  directly from HC2.

 

Any hint is highly appreciated.

 

Link to comment
Share on other sites

Recommended Posts

  • 0

I'm interested in same solution. Haven't find time to play with it yet, but if there is already solution out there will be nice for sharing!

Link to comment
Share on other sites

  • 0

I did a POC for this a few years back but never got around to using it. IIRC the setup was as follows.

 

1. Run the built in web server on the mac that also runs iTunes. 

2. Create applescripts for each command needed. One for play, one for next track and so on.

3. Create a simple php api that triggers the applescripts.

4. Trigger the php api from HC2.

 

Should be easy enough to recreate. I'll have a look at it.

Link to comment
Share on other sites

  • 0
5 minutes ago, StarkJohan said:

I did a POC for this a few years back but never got around to using it. IIRC the setup was as follows.

 

1. Run the built in web server on the mac that also runs iTunes. 

2. Create applescripts for each command needed. One for play, one for next track and so on.

3. Create a simple php api that triggers the applescripts.

4. Trigger the php api from HC2.

 

Should be easy enough to recreate. I'll have a look at it.

 

"Bring it on" ..... (encouragement ) 

Link to comment
Share on other sites

  • 0

Found my old php files. It's really simple. The issue is getting the permissions right for exec in php.

Ha! I can't post the code to the forum because of some lame protection. Not even in code brackets. Lets try an image.

Please login or register to see this attachment.

Please login or register to see this code.

I  other words, no applescript files needed. Just build a php api to exec the osascript commands and you're done. Getting track info and playlists and info like that is possible as well as setting system volume and a bunch of other stuff. 

 

Here's some command references. 

Please login or register to see this link.

Edited by StarkJohan
  • Like 1
Link to comment
Share on other sites

  • 0

Quick and dirty POC php api for getting basic track info to a VD. Adding player controls is easy and lots of stuff in iTunes is scriptable using Applescript.

 

Can't attach code because of forum "security" (probably because of the exec command). Will have to attach images.

Please login or register to see this attachment.

Please login or register to see this attachment.

Link to comment
Share on other sites

  • 0

OK. Did some further adding this morning. Using this example you'll be able to do basic controls. Adding more advanced functionality should be rather easy if needed but I have no use for anything else right now. Playing specific playlists or specific songs, enable shuffle, anything applescript can do really. Have a look in the Script Editor -> Window -> Library -> iTunes for a full list of available commands.

 

VD attached. 

PHP API: 

Please login or register to see this link.

 

 

Please login or register to see this attachment.

Please login or register to see this attachment.

Edited by StarkJohan
Link to comment
Share on other sites

  • 0
  • Inquirer
  • @StarkJohan,

     

    Thank you very much for your contribution. I will follow your way, however I need some more explanation/guidance.

     

    I found the guide to enable apache web server on  OS X :

     

    Please login or register to see this link.

     

    I managed to activate apache so my web server is up and running.

     

    I have set the IP of my MAC an port 80 for the VD settings.

    I understood from your VD that "/testzon/itunesremote/" is the path to the files on the OS X web server. That is OK. One has to modify this according to the file location.

     

    I have placed your .php file into the location of the files for my web server.

     

    And the question is how to link .php file with the buttons of VD so the proper command is called.

     

    Presently when I press PLAY button of the VD I see the response in the debug window of the example index.html file.

     

    thnak you in adavance for further guidance ...

     

    Link to comment
    Share on other sites

    • 0
    1 hour ago, gucio1234 said:

    I have set the IP of my MAC an port 80 for the VD settings.

    I understood from your VD that "/testzon/itunesremote/" is the path to the files on the OS X web server. That is OK. One has to modify this according to the file location.

     

    That's correct. This should obviously be more dynamically scripted for a "proper" VD... I never said it was pretty :)

     

    You also need to activate PHP for OSX. Loading the php5 module in apache should do the trick. Edit the apache config: "/etc/apache2/httpd.conf"

     

    Uncomment this line:

    Please login or register to see this code.

     

    And restart to reload config:

    Please login or register to see this code.

     

    To check if it works, just create a simple php file like this pastebin code in your webroot and browse to it. It should display info on your php installation (a huge purple table).

    Please login or register to see this link.

     

    To "link" my php api just name the file index.php and put in the location you prefer. In my case this is <ip-of-my-mac>/testzon/itunesremote/index.php

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @StarkJohan,

     

    Thanks for the fast response. The trick I missed was to rename your .php file to index.php

     

    I tested it with PLAY button and works like a charm.  No I will modify the rest of the VD buttons

     

    :):):)

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @StarkJohan,

     

    One more thing :

     

    All is working fine for me, but ...

    I have no refresh of the 'status'  for the first label of VD.

     

    When I see debug of "GET INFO" button I see the proper response received from iTunes (Artist, Track, Current Position, Current Track Time, Current Volume).

    Unfortunately these data is not updated for the first label "STATUS".

    I see the updated "STATUS" label only if I modify the VD (eg. by adding not significant space in the code) and save the VD.

     

    the line :

    fibaro:call(id, "setProperty", "ui.status.value", response.cur_artist.." - "..response.cur_track .." "..response.cur_pos .." of "..response.cur_track_time)

    should update the label "status" but it is not.

     

    Any hint ... ?

     

     

    UPDATE:

    I have replaced all icons of "blue box" for this VD with the nice iTunes icon and "GET INFO" is updated as expected.

     

     

     

     

    Edited by gucio1234
    Link to comment
    Share on other sites

    • 0

    So it all works now? Including the status label? If so, thanks for letting me know and good job :) 

     

    Any other specific function you're looking for that you can't figure out on your own, please let me know and I might be able to help out.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I have added a volume slider. That makes good start for integration with @jompa68's "Alarm Clock and Wake Up Timer" :

    Now the iTunes VD may be driven by the alarm scene in the same way as sonos speaker.

     

    I have few comments to ACWUT solution but I will share them on @jompa68's thread.

     

     

    Please login or register to see this attachment.

    • Like 1
    Link to comment
    Share on other sites

    • 0

    Hi!

    Please could you help me:

    1)If i use VD nothing happens, in debugger  got 400 code

    2)if i put /testzon/itunesremote/?cmd=..urlencode("play")  in safari, then i get "cmd not accepted"

    3)

    What do i wrong?

     

    Thank you

     

    solved

     

    add in address  /~username/testzon/itunesremote/?cmd=

     

     

    Edited by Trr
    Link to comment
    Share on other sites

    • 0

    Please login or register to see this code.

    Apple script for choose airplay speakers. Added to VD this option, works good.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @Trr  It is good you control airplay speakers from iTunes,  however I have a little different approach (which I believe is more flexible). It was detailed described in one of my threads.

     

    I have created Audio Mixer so I can adjust volumes separately for each AirPlay speaker and I can play TTS messages and/or music from iTunes. 

    • Like 1
    Link to comment
    Share on other sites

    • 0

    @gucio1234, @StarkJohan

     

    I am having some trouble with getting this to work. Can someone help me figure it out?

     

    Here is my setup.

     

    1. have Mac OS X server setup

    2. OSX server handles the .php files

    3. enabled php via terminal

    4. put the .php file from stark in the OS X website folder (renamed it itunes.php)

    5. imported VD from stark

    6. updated file path in the fibaro VD

     

     

    when I run the debug in VD it gives 400 error

     

    when I enter <IP address(hostname)>/itunes.php into a web browser I get the error(s) below.

     

    "Notice: Use of undefined constant cmd - assumed 'cmd' in /Library/Server/Web/Data/Sites/Default/itunes.php on line 26

    Notice: Use of undefined constant cmd - assumed 'cmd' in /Library/Server/Web/Data/Sites/Default/itunes.php on line 38
    cmd not accepted"

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • @amatt,

     

    I have path to PHP script defined in my VD as :

     

    Cytuj

    response, status, errorCode = HC2:GET("/~username/itunesremote/?cmd="..command);

     

    My PHP script to drive iTunes is index.php

     

    and it is placed on my Mac as :

     

    Macintosh HD/Users/username/Sites/itunesremote/index.php

     

    Here is my index.php :

    Please login or register to see this code.

     

     

    Link to comment
    Share on other sites

    • 0
    17 minutes ago, gucio1234 said:

    @amatt,

     

    I have path to PHP script defined in my VD as :

     

     

    My PHP script to drive iTunes is index.php

     

    and it is placed on my Mac as :

     

    Macintosh HD/Users/username/Sites/itunesremote/index.php

     

    Here is my index.php :

    Please login or register to see this code.

     

     

     

    @gucio1234

    in your path in the VD for the php is does not indicate the "/Sites" folder is the on purpose or accident?

     

    I have the same .php file code as you

     

    what was this code below? apple script? do you have to have an apple script to run this?

     

    /*
    tell app "iTunes" get name of playlists
    tell app "iTunes" get count of playlists
    tell app "iTunes" to stop
    tell app "iTunes" to pause
    tell app "iTunes" to play
    tell app "iTunes" to playpause
    tell app "iTunes" to play next track
    tell app "iTunes" to play previous track
    tell app "iTunes" to get name of current track
    tell app "iTunes" to get the album of the  current track


    set current_track to the current track
    set track_album to the album of the  current track
    set track_artist to the artist of the current track
    set track_comments to the comment of the current track
    set track_name to the name of the current track
    */

     

     

    I am trying to get one button working then I can workout the rest. I am working on the pause button here is my code below for the VD still get the error 400 even if the itunes.php was changed to index.php

     

    -- READ THIS VIRTUAL DEVICE (id, ip, port)    
    local ip = fibaro:get(fibaro:getSelfId(), 'IPAddress');
    local port = fibaro:get(fibaro:getSelfId(), 'TCPPort');
    HC2 = Net.FHttp(ip,port);

    -----------------------------------------------------
    local command = urlencode("pause");
    -----------------------------------------------------


    response, status, errorCode = HC2:GET("Users/username/Library/Server/Web/Data/Sites/itunesremote/index.php/?cmd=" ..command);

    if tonumber(status) == 200 -- Command accepted
    then
          fibaro:debug("200 OK, Response: " .. response)
    else
          fibaro:debug("Errorcode: "..status)
          fibaro:log("ErrorCode: "..status);
    end

     

     

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...