Created
April 7, 2018 04:32
-
-
Save debsahu/046b2b2683975a6dc44db3346be6ec57 to your computer and use it in GitHub Desktop.
ESP8266 reading from TCS34725 Color Sensor and changing RGB Colors in Home Assistant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ColorName.h> // https://github.com/debsahu/ColorName | |
#include "Adafruit_TCS34725.h" // https://github.com/adafruit/Adafruit_TCS34725 | |
#include <SSD1306.h> // https://github.com/ThingPulse/esp8266-oled-ssd1306 | |
#include <ESP8266WiFi.h> | |
#include <WiFiClientSecure.h> | |
#include <Bounce2.h> // https://github.com/thomasfredericks/Bounce2 | |
//#include "secret.h" | |
#define BUTTON_PIN 0 | |
#ifndef SECRET | |
const char* ssid = "WiFi SSID"; | |
const char* password = "WiFi Password"; | |
const char* HAIP = "HA IP Address"; | |
const int HAPORT = 8123; | |
const char* HAPassword = "HA Web Password"; | |
const char* HALight = "light.rgb_light"; | |
const char* HAEffect = "Static"; | |
String url = "/api/services/light/turn_on"; | |
//#define HASSL // Use this if HA uses SSL | |
#ifdef HASSL | |
// Use web browser to view and copy | |
// SHA1 fingerprint of the certificate | |
const char* fingerprint = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19"; | |
#endif | |
#endif | |
SSD1306 display(0x3c, D2, D1); | |
Adafruit_TCS34725 tcs = Adafruit_TCS34725(TCS34725_INTEGRATIONTIME_50MS, TCS34725_GAIN_4X); | |
Bounce debouncer = Bounce(); | |
byte gammatable[256]; // our RGB -> eye-recognized gamma color | |
long lastcolorchk = 0; | |
bool replyha = true; | |
const char* color_name = "-----"; | |
boolean sendHTTPSHA(uint8_t red, uint8_t green, uint8_t blue, boolean debug){ | |
#ifdef HASSL | |
WiFiClientSecure client; | |
#else | |
WiFiClient client; | |
#endif | |
boolean reply; | |
String replystr; | |
String message = "{\"entity_id\":\"" + String(HALight) + "\"," + | |
"\"rgb_color\":["+ String(red) + "," + String(green) + "," + String(blue) + "]}"; | |
client.setTimeout(1000); | |
if(debug){ | |
Serial.print("connecting to "); | |
Serial.println(HAIP); | |
} | |
if (!client.connect(HAIP, HAPORT)) { | |
if(debug) Serial.println("connection failed"); | |
return false; | |
} | |
#ifdef HASSL | |
if(debug) { | |
if (client.verify(fingerprint, HAIP)) { | |
Serial.println("certificate matches"); | |
} else { | |
Serial.println("certificate doesn't match"); | |
} | |
} | |
#endif | |
if(debug) { | |
Serial.print("requesting URL: "); | |
Serial.println(url); | |
} | |
if(debug) { | |
Serial.println("Sending: "); | |
//Serial.println(message); | |
Serial.println("POST " + url + " HTTP/1.1\r\n" + | |
"Host: " + HAIP + "\r\n" + | |
"User-Agent: ESP8266 RGB Sensor\r\n" + | |
"Accept: */*\r\n" + | |
"x-ha-access: " + String(HAPassword) + "\r\n" + | |
"Content-Type: application/json" + "\r\n" + | |
"Content-Length: " + message.length() + "\r\n\r\n" + | |
message); | |
} | |
client.print("POST " + url + " HTTP/1.1\r\n" + | |
"Host: " + HAIP + "\r\n" + | |
"User-Agent: ESP8266 RGB Sensor\r\n" + | |
"Accept: */*\r\n" + | |
"x-ha-access: " + String(HAPassword) + "\r\n" + | |
"Content-Type: application/json" + "\r\n" + | |
"Content-Length: " + message.length() + "\r\n\r\n" + | |
message); | |
if(debug) Serial.println("HTTPS HA Request Sent!"); | |
while(client.connected()){ | |
if(client.available()){ | |
String line = client.readStringUntil('\n'); | |
replystr += line + "\n"; | |
if (line == "\r") break; | |
} | |
} | |
if (replystr.startsWith("HTTP/1.1 200 OK")) { | |
if(debug) Serial.println("HA request successfull!"); | |
reply = true; | |
} else { | |
if(debug) Serial.println("HA request failed"); | |
reply = false; | |
} | |
if(debug) { | |
Serial.println("reply was:"); | |
Serial.println("=========="); | |
Serial.println(replystr); | |
Serial.println("=========="); | |
Serial.println("closing connection"); | |
} | |
client.flush(); | |
client.stop(); | |
return reply; | |
} | |
void displayInit() { | |
display.init(); | |
//display.flipScreenVertically(); | |
display.setFont(ArialMT_Plain_24); | |
} | |
void displayInt(int dispInt, int x, int y) { | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
display.drawString(x, y, String(dispInt)); | |
display.setFont(ArialMT_Plain_24); | |
display.display(); | |
} | |
void displayString(String dispString, int x, int y) { | |
display.setTextAlignment(TEXT_ALIGN_CENTER); | |
if (dispString.length() > 9) { | |
y = y - 10; | |
dispString.replace(" ", "\n"); | |
} | |
display.setFont(ArialMT_Plain_24); | |
display.drawString(x, y, dispString); | |
display.display(); | |
} | |
void setup() { | |
Serial.begin(115200); | |
Serial.println(); | |
Serial.print("connecting to "); | |
Serial.println(ssid); | |
WiFi.mode(WIFI_STA); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
Serial.println("Color View Test!"); | |
if (tcs.begin()) { | |
Serial.println("Found sensor"); | |
} else { | |
Serial.println("No TCS34725 found ... check your connections"); | |
while (1); // halt! | |
} | |
displayInit(); | |
displayString("Welcome", 64, 15); | |
pinMode(BUTTON_PIN,INPUT_PULLUP); | |
debouncer.attach(BUTTON_PIN); | |
debouncer.interval(50); | |
// Genrate Gamma Table | |
for (int i=0; i<256; i++) { | |
float x = i; x /= 255; x = pow(x, 2.5); x *= 255; | |
gammatable[i] = x; | |
} | |
} | |
void loop() { | |
uint16_t clear, red, green, blue; | |
uint32_t sum = clear; | |
float r, g, b; | |
debouncer.update(); | |
if ( replyha ) { | |
tcs.setInterrupt(false); // turn on LED | |
delay(60); // takes 50ms to read | |
tcs.getRawData(&red, &green, &blue, &clear); | |
tcs.setInterrupt(true); // turn off LED | |
// Figure out some basic hex code for visualization | |
r = red; r /= sum; | |
g = green; g /= sum; | |
b = blue; b /= sum; | |
r *= 256; g *= 256; b *= 256; | |
if (millis() - lastcolorchk > 1000) { | |
color_name = ColorNameString((int)r, (int) g, (int) b); | |
display.clear(); | |
displayString(String(color_name), 64, 15); | |
lastcolorchk = millis(); | |
} | |
} | |
if ( debouncer.fell() or !replyha ) { | |
replyha = sendHTTPSHA(gammatable[(int)r], gammatable[(int)g], gammatable[(int)b], false); | |
(replyha) ? Serial.println("HA Success!") : Serial.println("HA Failure!"); | |
Serial.print("C:\t"); Serial.print(clear); | |
Serial.print("\tR:\t"); Serial.print(gammatable[(int)r]); | |
Serial.print("\tG:\t"); Serial.print(gammatable[(int)g]); | |
Serial.print("\tB:\t"); Serial.print(gammatable[(int)b]); | |
Serial.print("\t"); | |
Serial.print(gammatable[(int)r], HEX); | |
Serial.print(gammatable[(int)g], HEX); | |
Serial.print(gammatable[(int)b], HEX); | |
Serial.print("\t"); | |
Serial.print(color_name); | |
Serial.println(); | |
//delay(10); | |
} | |
//delay(100); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define SECRET | |
#define HASSL // Use this if HA uses SSL | |
const char* ssid = "WiFi SSID"; | |
const char* password = "WiFi Password"; | |
const char* HAIP = "HA IP Address"; | |
const int HAPORT = 8123; | |
const char* HAPassword = "HA Web Password"; | |
const char* HALight = "light.rgb_light"; | |
const char* HAEffect = "Static"; | |
String url = "/api/services/light/turn_on"; | |
#ifdef HASSL | |
// Use web browser to view and copy | |
// SHA1 fingerprint of the certificate | |
const char* fingerprint = "35 85 74 EF 67 35 A7 CE 40 69 50 F3 C0 F6 80 CF 80 3B 2E 19"; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment