1

I am not an expert in php but I can do the basics so I put together a php script, which should turn on or off my hue light in the kitchen, when I run a certain URL i.e. when I enter and when I leave the kitchen:

$bulb   = $_GET['bulb'];
$status = $_GET["state"];
settype($status,"Boolean"); 

$data = array("on" => $status); 


$lat     = 48.1351253;    // North
$long    = 11.581980599999952;    // East
$offset = 1;    // difference between GMT and local time in hours

$zenith = 90+50/60;
$sunrise   = date_sunrise(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith,$offset);
$sunset = date_sunset(time(), SUNFUNCS_RET_STRING, $lat, $long, $zenith, $offset);
$now        = date("H") + date("i") / 60 + date("s") / 3600; 

if ($sunrise < $sunset)
{
    if (($now > $sunrise) && ($now < $sunset))
    {
        echo "It's daytime";
    }
    else
    {
        if (($now > $sunrise) || ($now < $sunset))
        {
            echo "It's nighttime";

            $data_string = json_encode($data);
            echo $data_string;

            $ch = curl_init("http://xx.xx.xx.xx/api/xxxxxx/lights/$bulb/state");
            curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json',
                'Content-Length: ' . strlen($data_string))
            );

            $result = curl_exec($ch);
            print_r($result);
        }
    }
}

The script should only turn on the lights between sunset and sunrise. But when I leave the kitchen it should also turn off the lights again.

The $bulb placeholder should be the bulb I put in the uRL.

Somehow it doesn't work. The script shows the right time, but turns on the lights anyway even it is daytime.

The URL for now I use is http://xxxxxx.com/hue.php?bulb=2&state=1 to turn on the state=0 to turn off.

It would be really cool if you could help me with this script. Thanks.

NXT
  • 1,981
  • 1
  • 24
  • 30

0 Answers0