1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Control Hyperion through a small web server

Discussion in 'Hyperion General' started by milhouse, 19 May 2018.

  1. milhouse

    milhouse New Member

    Messages:
    8
    Hardware:
    RPi3
    Looked all over for a way to control Hyperion through a web call, primarily so I could control from WebCore in SmartThings. It can't send raw TCP. Could find anything, so rolled my own. Thought I'd share in case in helps anyone else.

    1) Add a couple python modules on your Raspi that runs hyperion.

    pip install bottle
    pip install bottledaemon

    2) The python script that runs the web server and add endpoints for color and effects:

    from bottledaemon import daemon_run
    from bottle import route, run, template
    import subprocess


    @route('/color/<colorid>')
    def index(colorid):
    subprocess.check_output(['/usr/bin/hyperion-remote', '--color', colorid])
    return "done"

    @route('/effect/<effectname>')
    def index(effectname):
    subprocess.check_output(['/usr/bin/hyperion-remote', '--effect', effectname])
    return "done"

    @route('/clearall')
    def index():
    subprocess.check_output(['/usr/bin/hyperion-remote', '--clearall'])
    return "done"

    if __name__ == "__main__":
    daemon_run(host="0.0.0.0", port="8080")

    3) Start the python script using nohup so you can close your connection and it keep running.

    nohup /usr/bin/python /home/pi/Documents/hyperionweb.py start

    4) make a standard http request to control.

    http://192.168.2.71:8080/effect/Cold mood blobs
    http://192.168.2.71:8080/color/ff0000
    http://192.168.2.71:8080/clearall



    Hopefully this is useful to someone. And hopefully I didn't overlook something completely obvious and do it the hard way.

    milhouse
     
  2. milhouse

    milhouse New Member

    Messages:
    8
    Hardware:
    RPi3
    Can I insert a code block so the python code isn't reformatted? It lost all the indents, and python really cares about that.
     
  3. mrl72

    mrl72 New Member

    Messages:
    6
    Hardware:
    RPi3
    Thanks for this was looking for the exact thing when time permits.

    So in webcore I know you can call a URL using post/get or JSON. Is this what you did?
     
  4. milhouse

    milhouse New Member

    Messages:
    8
    Hardware:
    RPi3
    You got it. I use a random expression so it's one of several effects. Make a web request, GET method, type JSON, no header or authorization. Obviously this is all inside my LAN.

    Make a GET request to {"http://192.168.2.71:8080/effect/" + random("Cold%20mood%20blobs","Full%20color%20mood%20blobs","Warm%20mood%20blobs","Rainbow%20swirl")} with type JSON;
     
  5. mrl72

    mrl72 New Member

    Messages:
    6
    Hardware:
    RPi3
    I finally got around to trying this but I'm running OpenElec and looks like you can't install much in this OS. Is there any other solution to making remote requests to Hyperion via URL?
     
  6. mg3point

    mg3point New Member

    Messages:
    14
    Hardware:
    RPi3
    I think this is exactly what i am looking for, but am struggling to get it working. I'm wanting to tie this into to google home assistant. What I am looking for is a web address to give to the home assistant to hit so that it will change the lights. I roughly understand what is going on in the script written above, but am a little lost when it comes to the nohup section. The other issue i have is that port 8080 is already applied to another device in my household. Would i be okay to use 8081? Can i just swap that in the code? Also do i need to put my pi's ip address in the 0.0.0.0 section of the code?

    Any help you can provide would be greatly appreciated. I've had an ambilight setup around my tv for awhile now. I am now in the process of building a couch rail that i am embedding 9m of LED into. This is what I would like to set with google assitant ("Hey google, set couch to Green Mood Blobs.").
     
  7. mg3point

    mg3point New Member

    Messages:
    14
    Hardware:
    RPi3
    So i did eventually pseudo get this working. It was having issues when i put both the effect route and the color route in. If i left it at 1 it ran fine and i was able to hit the url in a web browser and the lights would change. I thought at that point i was golden to hook it into ifttt to do it via voice control. However I quickly realized my port was only open on my local network as my ip was only static to my router and not to my ISP which is giving my router a dynamic ip. Therefore the ifttt servers were unable to talk to my pi to change the lights :( . Oh well, it was really just a "nice to have" anyways.