Skip to content

Instantly share code, notes, and snippets.

@johnty
Created April 2, 2016 03:28
Show Gist options
  • Save johnty/f109801035a297f880ac9c96dedd71f2 to your computer and use it in GitHub Desktop.
Save johnty/f109801035a297f880ac9c96dedd71f2 to your computer and use it in GitHub Desktop.
Arduino ESP8266 sketch for updating the status of a generic CLIP status sensor. hardcoded AP/password and Hue IP/API strings.
/**
Custom ESP8266 CLIP sensor for Philips Hue system
GPIO2's high/low state will emit two different status values
which can then be used to trigger Hue bridge events
You will need:
0.) Wifi network and Philips Hue bridge connected to it and set up, with an API key
1.) A custome IP sensor (CLIP) created in the Hue bridge
2.) A rule hooking up the status of the IP sensor and events we want
3.) Arduino/ESP8266 program to update said status value (This code)
// Part 1: Create the sensor:
// (reference: http://www.developers.meethue.com/documentation/how-use-ip-sensors)
// Part 2: Create rule. Here is
// a sample rule that turns off light (7). (if you want multiple
// lights, make a light group!
// if status value of custom sensor (8) is less than 50:
//{
// "name": "test01",
// "conditions": [
// {
// "address": "/sensors/8/state/status",
// "operator": "leq",
// "value": "50"
// }
// ],
// "actions": [
// {
// "address": "/lights/7/state",
// "method": "PUT",
// "body": {
// "on": false
// }
// }
// ]
//}
// SAMPLE curl command to push this update, which we want to emulate here with the esp8266:
//curl PUT --data "{\"status\" : 10}" http://192.168.100.230/api/b225912329de4371bdc4d2e18678263/sensors/8/state
*/
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
#define USE_SERIAL Serial
ESP8266WiFiMulti WiFiMulti;
int sw0;
unsigned long timePrev;
String IP_API_SENSOR_URL = "http://192.168.100.230/api/b225912329de4371bdc4d2e18678263/sensors/8/state";
char outputBuffer[32];
void sendStatus(int stats);
void setup() {
USE_SERIAL.begin(115200);
// USE_SERIAL.setDebugOutput(true);
pinMode(0, OUTPUT); //for local indicator LED
pinMode(2, INPUT);
digitalWrite(2, HIGH); //internal pullup
sw0 = digitalRead(2);
USE_SERIAL.print("initial switch state = ");
USE_SERIAL.println(sw0);
USE_SERIAL.println();
USE_SERIAL.println();
USE_SERIAL.println();
for (uint8_t t = 4; t > 0; t--) {
USE_SERIAL.printf("[SETUP] WAIT %d...\n", t);
USE_SERIAL.flush();
delay(1000);
}
WiFiMulti.addAP("AP_NAME", "AP_PASSWD");
while (WiFiMulti.run() != WL_CONNECTED) {
USE_SERIAL.printf("waiting for wifi connection...\n");
delay(1000);
}
USE_SERIAL.printf("Connected!\n");
//strcpy(outputBuffer,"/hello");
//sendBuffer();
timePrev = millis();
}
void loop() {
// wait for WiFi connection
if ((WiFiMulti.run() == WL_CONNECTED)) {
unsigned int timeElapsed = millis() - timePrev;
//do a ping once in a while
if (timeElapsed >= 10000) {
timePrev = millis();
//strcpy(outputBuffer,"/ping");
//sendBuffer();
}
if (digitalRead(2) != sw0)
{
sw0 = digitalRead(2);
if (sw0 == HIGH) {
digitalWrite(0, HIGH);
//strcpy(outputBuffer, "/light/on");
sendStatus(100);
}
else
{
digitalWrite(0, LOW);
//strcpy(outputBuffer, "/light/off");
sendStatus(10);
}
}
}
//sleep a bit
delay(50);
}
void sendStatus(int stats) {
HTTPClient http;
USE_SERIAL.print("[HTTP] begin...\n");
http.begin(IP_API_SENSOR_URL);
String req = "{ \"status\" : " + String(stats) + "}";
USE_SERIAL.print("[HTTP] PUT...\n");
// start connection and send HTTP header
int httpCode = http.sendRequest("PUT", req);
if (httpCode) {
// HTTP header has been send and Server response header has been handled
USE_SERIAL.printf("[HTTP] PUT... code: %d\n", httpCode);
// file found at server
if (httpCode == 200) {
String payload = http.getString();
USE_SERIAL.println(payload);
}
} else {
USE_SERIAL.print("[HTTP] PUT... failed, no connection or no HTTP server\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment