Philips的hue灯泡算是智能灯泡的鼻祖了,此次就来玩玩这个哈。html
meethue.com是hue的门户啦,有各类的使用说明。开发者的入口在哪呢...找了半天发现右边有竖着的developer...json
好吧,进去之后直奔Get started。大概扫一圈之后发现这个hue的bridge设计的还真不错,直接经过http就能访问到api的测试后台。segmentfault
/api/newdeveloper/lights/1/state
来控制这个id为1的灯泡啦。很标准的JSON RESTful API,设计的不错!POST http://<bridge ip address>/api {"devicetype":"test user","username":"newdeveloper"}
url -X PUT -H 'Content-Type: application/json' -d '{"on":false, "sat":255, "bri":255,"hue":10000}' http://192.168.31.xxx/api/newdeveloper/lights/1/state
哈,MYO又出现了!上次只拿了pose的数据,此次我想让灯泡跟着挥手变换颜色,那就要得到当前手臂的角度啦。代码的区别只是多加个回调就好啦。能拿到一个4维的数据。xyzw。api
@Override public void onOrientationData(Myo myo, long timestamp, Quaternion rotation) { ((TextView) findViewById(R.id.hello)).setText( "Z:" + rotation.z() + " W:" + rotation.w() + " X:" + rotation.x() + " Y:" + rotation.y()); }
经过观察发现挥手的时候变换的不少的是w轴,并且右手挥动的过程正好是-0.5到0.5的过程。好啦,如今把这个数据随时记录下来,每秒钟发个PUT过去改灯泡的状态。咱就用OkHttp搞好啦,相似这样。app
String updateHue(int bright, int hue) throws IOException { OkHttpClient client = new OkHttpClient(); RequestBody body = RequestBody.create(JSON, "{\"on\":true,\"bri\":150}"); Request request = new Request.Builder() .url("http://192.168.31.xxx/api/newdeveloper/lights/1/state") .post(body) .build(); Response response = client.newCall(request).execute(); return response.body().string(); }
好啦,就这样!curl
Party呀ide