Skip to content

Instantly share code, notes, and snippets.

@tomconte
Created December 14, 2014 11:44
Show Gist options
  • Save tomconte/97bc405f23b0f863b11f to your computer and use it in GitHub Desktop.
Save tomconte/97bc405f23b0f863b11f to your computer and use it in GitHub Desktop.
This little sketch allows to turn Hue lamps on or off using a push button.
#include <Bridge.h>
#include <Console.h>
#include <FileIO.h>
#include <HttpClient.h>
#include <Mailbox.h>
#include <Process.h>
#include <YunClient.h>
#include <YunServer.h>
/*
** Constants etc.
*/
// Pin for the LED
const int ledPin = 13;
// Input pin for the push button
const int inPin = 7;
// The IP address of your Hue bridge; find it at https://www.meethue.com/api/nupnp
const char *hueIp = "192.168.0.132";
// The user for the Hue bridge; more info at http://www.developers.meethue.com/documentation/getting-started
const char *hueUser = "3e395757167e1dbfb5235472713dcf";
void setup() {
// declare LED as output
pinMode(ledPin, OUTPUT);
// declare pushbutton as input
pinMode(inPin, INPUT);
Bridge.begin();
Console.begin();
/*
while (!Console) {
; // wait for Console port to connect.
}
Console.println("You're connected to the Console!!!!");
*/
}
/*
** Variables
*/
int buttonState = 0;
int lastButtonState = 0;
int ledState;
bool lightState = false;
/*
** Loop and watch button state.
*/
void loop() {
bool buttonPressed = false;
buttonState = digitalRead(inPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
Console.println("up");
} else {
Console.println("down");
lightState = !lightState;
ledState = lightState ? HIGH : LOW;
Console.print("send command: ");
Console.println(lightState);
// Send Hue commands
sendHueCommand(1, lightState);
sendHueCommand(2, lightState);
sendHueCommand(3, lightState);
digitalWrite(ledPin, ledState);
}
}
lastButtonState = buttonState;
}
/*
** Send an on/off command to the light
*/
void sendHueCommand(int light, bool lightOn)
{
Process p;
char cmd[128];
if (lightOn) {
sprintf(cmd, "curl -XPUT http://%s/api/%s/lights/%d/state -d '{\"on\":true}'", hueIp, hueUser, light);
} else {
sprintf(cmd, "curl -XPUT http://%s/api/%s/lights/%d/state -d '{\"on\":false}'", hueIp, hueUser, light);
}
Console.println(cmd);
p.runShellCommand(cmd);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment