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