Python Forum

Full Version: Phillips hue simple on script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Any reason this shouldnt work anymore?`
Updated my raspberry pi and since it has been unable to actually turn my phillips hue lights on with the script below.. Im unsure as to why.

import requests
import time

time.sleep(1)

headers = {
    'Accept': 'application/json',
}

data = '{"on":true,"bri":255,"sat":255,"hue":46920,"effect":"colorloop"}'

requests.put('http://192.168.1.47/api/XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/$
Do you get an error message? What happens when you execute it?
Could you show us a but more of our code?
Has the IP address changed after you've updated or rebooted your pi?
No error message at all.
That is all the code actually, it is being ran by another script. But ive tested manually running it and thru the other script. Both no good.
The IP is the same for both the pi and the hue bridge.

I can turn the lights on/off with my phone wich works fine.
It difficult to speculate without having anymore information about your infrastructure.
Try using postman to make the post request and check what return code do you get?

Are you able to access the PI, ping etc? Your request call should really look like this

requests.put('http://192.168.1.47/api/XXXXXXXXXXXXXXXXXXXXXXXXXXXXX/$', data=data, headers=headers)
My bad, was missing some of the script.

It actually looks like this:

import requests
import time

time.sleep(1)

headers = {
    'Accept': 'application/json',
}

data = '{"on":true,"bri":255,"sat":255,"hue":46920,"effect":"colorloop"}'

requests.put('http://192.168.1.47/api/XXXXXXXXXXXXXXXXXXXXXXXXX/lights/1/state', headers=headers, data=data)
I just tried this from home and it worked without issues. Try visiting http://192.168.1.47/debug/clip.html and exploring the API there.
No idea what the actual problem here was, but the script does indeed work fine.
The solution was actually to delete the lightstrip from the hue app and re-add them.
Then it started working. Thanks for pointing out the API tho, helped me out. :)