pythonnetwork-programmingphilips-hue

How to control Philips Hue lights using Python script


I am trying to write a Philips Hue script using python that needs to be able to communicate with the bridge from a different network. I have a VPS that the scripts will be running on.

I have already tried thinks like phue but these libraries are only able to control lights in the same network.

I have tried solving the error by adding this:

import logging
logging.basicConfig()

but this didn't work. This is the script:

from phue import Bridge
import time
b = Bridge('192.168.2.3')
b.connect()

b.get_api()

b.set_light(1, 'on', True)

I wanted the lights to turn on using this script, but it gives this error, indicating that it can not find the bridge.

No handlers could be found for logger "phue"
Traceback (most recent call last):
  File "hue.py", line 3, in <module>
    b = Bridge('192.168.2.3')
  File "/usr/local/lib/python2.7/dist-packages/phue.py", line 628, in __init__
    self.connect()
  File "/usr/local/lib/python2.7/dist-packages/phue.py", line 751, in connect
    self.register_app()
  File "/usr/local/lib/python2.7/dist-packages/phue.py", line 705, in register_app
    response = self.request('POST', '/api', registration_request)
  File "/usr/local/lib/python2.7/dist-packages/phue.py", line 660, in request
    raise PhueRequestTimeout(None, error)
phue.PhueRequestTimeout

Solution

  • It's not actually a network problem ;)

    Firstly 192.168.2.3 doesn't exist out on the big bad internet, it's a in a 'private address range' for internal use only so you ain't hitting it from outside of your network.

    You would need to be hitting your public facing address (the one assigned to your routers WAN0 port, or what ever device calls the public facing interface)

    It also depends on your ISP permitting you to hit your external address from the outside world (my ISP doesn't NAT that address but yours may.)

    I will also add a caveat which is speculation as I only got my HUE (and IKEA) hubs today and I haven't had time to check yet but a lot of embedded devices will only happily talk to things on the same IP subnet.

    As the previous respondent hinted at "HTTP access from the internet, that way lies madness"

    Essentially HTTPS is the only sane option to use.

    At a pinch could use a VPN connection back to your own network as some sort of minimal security. I say minimal because I wouldn't even choose to pass HTTP traffic even on my own home network.

    I'm not paranoid BTW, network data leaks regardless of vlans, access lists, firewalls and all the best intentions and you don't want credentials to anything flying around unencrypted (at any point.)