Commit 32fddb09 authored by Nicolai Grymer's avatar Nicolai Grymer 🐤
Browse files

Changing library folder

parent d1bff179
#include <NodeHue.h>
// SPECIFY FOR YOUR SETUP
#define IP "192.168.0.182" // Ip-adress of Philips Hue Bridge
#define PASS "8cfQRkATLSueu3Iub5fSBQpVCAeOSFEeb9lEoZaX" // also know as username
#define ID 7 // lamp ID
NodeHue hue(IP, &Serial); // Define NodeHue
void setup(){
hue.connect(PASS); // Setup serial connection
Serial.begin(115200); // Setup Serial. MUST BE 115200
hue.turnOn(ID,true); // Turn on hue (not necessary)
}
void loop(){
hue.setRGB(ID,255,0,0); // Change color to red. Takes 8-bit (0-255) values
delay(3000);
hue.setRGB(ID,0,255,0); // Change color to green
delay(3000);
hue.turnOn(ID,false); // Turn hue off
delay(3000);
hue.turnOn(ID,true); // Turn hue on
delay(3000);
hue.setBri(ID,0); // Change brightness to 0 (min)
delay(3000);
hue.setBri(ID,255); // Change brightness to 255 (max)
delay(3000);
hue.setHueSat(ID,0,0); // Change hue and sat. Hue min = 0, sat min = 0
delay(3000);
hue.setHueSat(ID,40000,255); // Hue = 40000, sat = 255 (max)
delay(3000);
hue.setHueSat(ID,65535,130); // Hue = 65535 (max), sat = 130
delay(3000);
}
#include <NodeHue.h>
#define IP "192.168.0.182"
#define PASS "8cfQRkATLSueu3Iub5fSBQpVCAeOSFEeb9lEoZaX"
#define ID 7
NodeHue hue(IP, &Serial);
void setup(){
hue.connect(PASS);
Serial.begin(115200);
}
void loop(){
hue.turnOn(ID,true);
delay(1000);
hue.turnOn(ID,false);
delay(1000);
//hue.setBri(ID,255);
//hue.setHueSat(ID,65535,255);
//hue.setRGB(ID,255,255,255);
}
name=NodeHue
version=1.0.0
author=Nicolai Grymer
maintainer=Nicolai Grymer
sentence=Node Hue
paragraph=Communicates with Node.js through Serial port to control Philips Hue Lamps
category=Other
url=https://gitlab.au.dk/au648662/arduinohue20
architectures=*
#include "ArduinoHue.h"
int ArduinoHue::turnOn(int light, boolean state){
strcpy_P(_buffer, (char*)turn_on_off);
sprintf(_lastResponse, _buffer, state ? "true" : "false");
return fillRequestAndProcess(light,true);
}
int ArduinoHue::fillRequestAndProcess(int light,boolean useState){
if(useState){
sprintf(_buffer, "lights/%i/state",light);
}
else{
sprintf(_buffer, "lights/%i",light);
}
if(makePost(_buffer, _lastResponse)){
return 1;
}
return STATUS_POST_ERROR;
}
int ArduinoHue::setHueSat(int light, int hue, int sat){
strcpy_P(_buffer, (char*)change_color_string);
sprintf(_lastResponse, _buffer, hue, sat);
return fillRequestAndProcess(light,true);
}
int ArduinoHue::setRGB(int light, byte red, byte green, byte blue){
auto rd = static_cast<double>(red) / 255;
auto gd = static_cast<double>(green) / 255;
auto bd = static_cast<double>(blue) / 255;
auto max = max(rd, max(gd, bd));
auto min = min(rd, min(gd, bd));
double h, s, l = (max + min) / 2;
if (max == min)
{
h = s = 0; // achromatic
}
else
{
auto d = max - min;
s = l > 0.5 ? d / (2 - max - min) : d / (max + min);
if (max == rd)
{
h = (gd - bd) / d + (gd < bd ? 6 : 0);
}
else if (max == gd)
{
h = (bd - rd) / d + 2;
}
else if (max == bd)
{
h = (rd - gd) / d + 4;
}
h /= 6;
}
strcpy_P(_buffer, (char*)change_rgb);
sprintf(_lastResponse, _buffer, static_cast<int>(h*65536),static_cast<int>(s*255),static_cast<int>(max*255));
return fillRequestAndProcess(light,true);
}
int ArduinoHue::setBri(int light, int bri){
strcpy_P(_buffer, (char*)set_bri_string);
sprintf(_lastResponse, _buffer, bri);
return fillRequestAndProcess(light,true);
}
\ No newline at end of file
#ifndef ARDUINOHUE_H
#define ARDUINOHUE_H
#include <Arduino.h>
#include <avr/pgmspace.h>
const char turn_on_off[] PROGMEM = "{\"on\":%s}";
const char change_color_string[] PROGMEM = "{\"hue\":%i,\"sat\":%i}";
const char change_rgb[] PROGMEM = "{\"hue\":%i,\"sat\":%i,\"bri\":%i}";
const char set_bri_string[] PROGMEM = "{\"bri\":%i}";
const char set_alert_string[] PROGMEM = "{\"alert\":\"%s\"}";
typedef void (* ActionFunctionPtr) (char*);
#define MAX_LENGTH_STATION_IDENTIFIER 32
#define STATUS_SUCCESS 1
#define STATUS_POST_ERROR -1
#define STATUS_SERVER_KNOWN_ERROR 2
#define STATUS_SERVER_UNKNOWN_ERROR -2
#define STATUS_OUT_OF_BOUNDS_ERROR -3
class ArduinoHue{
public:
int turnOn(int light, boolean state);
int setBri(int light, int bri);
int setHueSat(int light, int hue, int sat);
int setRGB(int light, byte red, byte green, byte blue);
protected:
int fillRequestAndProcess(int light,boolean useStatus);
virtual boolean makePost(char* request, char* data);
char _lastResponse[255];
char _buffer[100];
};
#endif
\ No newline at end of file
#include "NodeHue.h"
NodeHue::NodeHue(char* ipAddress, Stream* serial){
_ipAddress = ipAddress;
_serial = serial;
}
boolean NodeHue::connect(char* username){
_username = username;
return true;
}
boolean NodeHue::makePost(char* request, char* data){
_serial -> flush();
_serial->print("http://");
_serial->print(_ipAddress);
_serial->print("/api/");
_serial->print(_username);
_serial->print("/");
_serial->print(request);
_serial->print("|");
_serial->print(data);
_serial->println();
return true;
}
\ No newline at end of file
#ifndef SERIALHUE_H
#define SERIALHUE_H
#include <Arduino.h>
#include <Stream.h>
#include <ArduinoHue.h>
class NodeHue: public ArduinoHue{
public:
NodeHue(char* ipAddress, Stream* serial);
boolean connect(char* username);
protected:
char* _ipAddress;
char* _username;
boolean makePost(char* request, char* data);
Stream* _serial;
};
#endif
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment