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条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 微信会员卡接入微信支付商户号收款
    • ¥15 如何获取烟草零售终端数据
    • ¥15 数学建模招标中位数问题
    • ¥15 phython路径名过长报错 不知道什么问题
    • ¥15 深度学习中模型转换该怎么实现
    • ¥15 HLs设计手写数字识别程序编译通不过
    • ¥15 Stata外部命令安装问题求帮助!
    • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
    • ¥15 TYPCE母转母,插入认方向
    • ¥15 如何用python向钉钉机器人发送可以放大的图片?