Reputation: 33
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
Upvotes: 3
Views: 11280
Reputation: 26
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.)
Upvotes: 1
Reputation: 4964
This problem has nothing to do with Python actually. This is a common networking problem.
If you have control scripts running in the internet to control the lights say, at home, you need to open/redirect the TCP 80 port requests made from the VPS to your home IP, in your home router configuration, to the IP of your home "bridge", as I see from phue
module that it has an HTTP Rest API.
Then I hope the system (that I haven't used or really know of) to have some kind of authentication, or everyone else can also control it if they access to your home router IP (and they will do it). If you authenticate by using a secret key to both sides, you'll also need to encrypt the communication or the keys could be read and stolen. This requires for example using HTTPS and changing the 80 port to the 443 port, and also setting up a HTTPS server with (at least) a self-signed certificate.
There are probably other possible setups, but this should give you enough information for you to research more about the subject.
You may also find it reasonable to work the other way round. Setup a normal control app in the home network and make it connect to your VPS to periodically read a configuration or write status, and then apply it locally.
Upvotes: 0