LoginSignup
9
9

More than 5 years have passed since last update.

HueをブラウザなJavaScriptで操作するメモ

Last updated at Posted at 2015-10-17

SuperAgentを使ってみます。

index.html
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title></title>
  </head>
  <body>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/superagent/1.2.0/superagent.min.js"></script>
    <script src="app.js"></script>
  </body>
</html>

app.js
var request = window.superagent;
var IPURL = 'https://www.meethue.com/api/nupnp';
var hueapi = ''; //
var USERNAME = 'lig-n0bisuke';
var flag = true;

//Hue操作ようのAPIエンドポイント作成
function createEndpoint(ip){
  return 'http://'+ip+'/api/'+USERNAME+'/lights/';
}

request
  .get(IPURL)
  .set('Accept', 'application/json')
  .end(function(err, res){
    var ipadress = res.body[0].internalipaddress; //HueのIPアドレス
    hueapi = createEndpoint(ipadress);
  });

function ctrlHue(id, flag){
  if(hueapi === '')return;

  request
    .put(hueapi+id+'/state')
    .send({on: flag})
    .set('Accept', 'application/json')
    .end(function(err, res){
      console.log(res.body[0]);
    });
}

setInterval(function(){
  flag = !flag;
  ctrlHue(1, flag);
},2000);

これで2秒ごとに点滅してくれます。

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9