HueBlink.ino 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /* HueBlink example for ArduinoHttpClient library
  2. Uses ArduinoHttpClient library to control Philips Hue
  3. For more on Hue developer API see http://developer.meethue.com
  4. To control a light, the Hue expects a HTTP PUT request to:
  5. http://hue.hub.address/api/hueUserName/lights/lightNumber/state
  6. The body of the PUT request looks like this:
  7. {"on": true} or {"on":false}
  8. This example shows how to concatenate Strings to assemble the
  9. PUT request and the body of the request.
  10. note: WiFi SSID and password are stored in config.h file.
  11. If it is not present, add a new tab, call it "config.h"
  12. and add the following variables:
  13. char ssid[] = "ssid"; // your network SSID (name)
  14. char pass[] = "password"; // your network password
  15. modified 15 Feb 2016
  16. by Tom Igoe (tigoe) to match new API
  17. */
  18. #include <SPI.h>
  19. #include <WiFi101.h>
  20. #include <ArduinoHttpClient.h>
  21. #include "config.h"
  22. int status = WL_IDLE_STATUS; // the Wifi radio's status
  23. char hueHubIP[] = "192.168.0.3"; // IP address of the HUE bridge
  24. String hueUserName = "huebridgeusername"; // hue bridge username
  25. // make a wifi instance and a HttpClient instance:
  26. WiFiClient wifi;
  27. HttpClient httpClient = HttpClient(wifi, hueHubIP);
  28. void setup() {
  29. //Initialize serial and wait for port to open:
  30. Serial.begin(9600);
  31. while (!Serial); // wait for serial port to connect.
  32. // attempt to connect to Wifi network:
  33. while ( status != WL_CONNECTED) {
  34. Serial.print("Attempting to connect to WPA SSID: ");
  35. Serial.println(ssid);
  36. // Connect to WPA/WPA2 network:
  37. status = WiFi.begin(ssid, pass);
  38. }
  39. // you're connected now, so print out the data:
  40. Serial.print("You're connected to the network IP = ");
  41. IPAddress ip = WiFi.localIP();
  42. Serial.println(ip);
  43. }
  44. void loop() {
  45. sendRequest(3, "on", "true"); // turn light on
  46. delay(2000); // wait 2 seconds
  47. sendRequest(3, "on", "false"); // turn light off
  48. delay(2000); // wait 2 seconds
  49. }
  50. void sendRequest(int light, String cmd, String value) {
  51. // make a String for the HTTP request path:
  52. String request = "/api/" + hueUserName;
  53. request += "/lights/";
  54. request += light;
  55. request += "/state/";
  56. String contentType = "application/json";
  57. // make a string for the JSON command:
  58. String hueCmd = "{\"" + cmd;
  59. hueCmd += "\":";
  60. hueCmd += value;
  61. hueCmd += "}";
  62. // see what you assembled to send:
  63. Serial.print("PUT request to server: ");
  64. Serial.println(request);
  65. Serial.print("JSON command to server: ");
  66. // make the PUT request to the hub:
  67. httpClient.put(request, contentType, hueCmd);
  68. // read the status code and body of the response
  69. int statusCode = httpClient.responseStatusCode();
  70. String response = httpClient.responseBody();
  71. Serial.println(hueCmd);
  72. Serial.print("Status code from server: ");
  73. Serial.println(statusCode);
  74. Serial.print("Server response: ");
  75. Serial.println(response);
  76. Serial.println();
  77. }