dongliu5475 2014-02-03 17:25
浏览 115

Hue API和php打开/关闭灯

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.

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题
    • ¥15 请完成下列相关问题!
    • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
    • ¥15 求daily translation(DT)偏差订正方法的代码
    • ¥15 js调用html页面需要隐藏某个按钮