For my old setup I haven't had to change anything to have it still working fine, I'm on iOS 12 now.
For my old setup I haven't had to change anything to have it still working fine, I'm on iOS 12 now.
Explore... Chat... Share...
//NodeMCU RGB-Controller for Homebridge & HomeKit (Siri) with nonblocking color fade
#include <ESP8266WiFi.h>
#define redPin 13 //D7
#define grnPin 12 //D6
#define bluPin 14 //D5
#define max(a,b) ((a)>(b)?(a):(b)) //added to make max() work with different data types (int | float)
WiFiServer server(80);
String readString;
String hexString = "080100"; //Define initial color here, i.e.: (hex 080100 == RGB 8.1.0) | (hex FFFFFF == RGB 255.255.255) | ...
String offString = "000000";
int state;
int r, g, b, x, V;
float R, G, B;
//Color fade variables
int dltR, dltG, dltB;
int curR, curG, curB, i = 0;
int prvR = curR;
int prvG = curG;
int prvB = curB;
//Fade timer variables
unsigned long fdeTimer;
unsigned long fdeDelay = 1UL;
// WiFi SETTINGS - Replace with your values:
const char* ssid = "YOUR_ROUTER_SSID";
const char* password = "YOUR_ROUTER_PASSWORD";
IPAddress ip(192, 168, 1, 10); //set a fixed IP for the NodeMCU - comment if you prefer to set it in your router instead.
IPAddress gateway(192, 168, 1, 1); //Your router IP - comment if you prefer to set it in your router instead.
IPAddress subnet(255, 255, 255, 0); //Subnet mask - comment if you prefer to set it in your router instead.
void WiFiStart() {
WiFi.begin(ssid, password);
WiFi.config(ip, gateway, subnet); //Sets your fixed IP - comment if you prefer to set it in your router instead.
while (WiFi.status() != WL_CONNECTED) delay(100);
server.begin();
//Serial.print(WiFi.macAddress()); //Uncomment to print the ESP's MAC-address
//Serial.print(" | "); //
//Serial.println(WiFi.localIP()); //Uncomment to print the ESP's actual IP-address
}
void allOff() {
//Transform hex-color from 'offString' into rgb-color for the LED-Strip:
long number = (long) strtol(&offString[0], NULL, 16);
r = number >> 16;
g = number >> 8 & 0xFF;
b = number & 0xFF;
setLED(r, g, b);
state = 0;
}
void setHex() {
//Transform requested hex-color from HomeKit ('hexString') into rgb-color for the LED-Strip:
long number = (long) strtol(&hexString[0], NULL, 16);
r = number >> 16;
g = number >> 8 & 0xFF;
b = number & 0xFF;
setLED(r, g, b);
state = 1;
}
//Handle color values before fade, then set the fade timer:
void setLED(int r, int g, int b) {
dltR = calcStep(prvR, r);
dltG = calcStep(prvG, g);
dltB = calcStep(prvB, b);
fdeTimer = millis();
prvR = curR;
prvG = curG;
prvB = curB;
}
int calcStep(int prvVal, int endVal) {
int step = endVal - prvVal;
if (step) step = 1027/step; //in relation with 'fdeTimer' in loop()
return step;
}
int calcVal(int step, int val, int i) {
if ((step) && i % step == 0) {
if (step > 0) val += 1;
else if (step < 0) val -= 1;
}
if (val > 255) val = 255;
else if (val < 0) val = 0;
return val;
}
//get actual brightness in %
void getV() {
R = roundf(r/2.55);
G = roundf(g/2.55);
B = roundf(b/2.55);
x = max(R, G);
V = max(x, B);
}
//Debug
void showValues() {
Serial.print("Status on/off: ");
Serial.println(state);
Serial.print("RGB color: ");
Serial.print(r);
Serial.print(".");
Serial.print(g);
Serial.print(".");
Serial.println(b);
Serial.print("Hex color: ");
Serial.println(hexString);
getV();
Serial.print("Brightness: ");
Serial.println(V);
Serial.print("Current RGB: ");
Serial.print(curR);
Serial.print(".");
Serial.print(curG);
Serial.print(".");
Serial.print(curB);
Serial.println(". (should match 'RGB color')");
Serial.println();
}
void setup() {
Serial.begin(115200);
pinMode(redPin, OUTPUT); //declaration added (though actually not needed with 'analogWrite'?!)
pinMode(grnPin, OUTPUT);
pinMode(bluPin, OUTPUT);
WiFi.hostname("LED-Light"); //Rename your ESP for better assignment within your router's clients list
WiFi.mode(WIFI_STA);
WiFiStart();
setHex();
}
void loop() {
//if fade-timer is set and expired, fade to new color:
if (fdeTimer > 0 && millis() - fdeTimer >= fdeDelay) {
fdeTimer = millis();
curR = calcVal(dltR, curR, i);
curG = calcVal(dltG, curG, i);
curB = calcVal(dltB, curB, i);
analogWrite(redPin, map(curR, 0, 255, 0, 1023));
analogWrite(grnPin, map(curG, 0, 255, 0, 1023));
analogWrite(bluPin, map(curB, 0, 255, 0, 1023));
prvR = curR;
prvG = curG;
prvB = curB;
if (++i > 1023) { //in relation with 'step' in calcStep()
i = 0;
fdeTimer = 0;
}
}
//Handle Homebridge HTTP requests:
WiFiClient client = server.available();
if (!client) return;
while (client.connected() && !client.available()) delay(1);
if (client) {
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (readString.length() < 100) {
readString += c;
}
if (c == '\n') {
//Serial.print("Received: "); //uncomment for serial debugging
//Serial.println(readString); //uncomment for serial debugging
//Send response:
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
//ON:
if(readString.indexOf("on") >0) {
setHex();
}
//OFF:
if(readString.indexOf("off") >0) {
allOff();
}
//Set Hex-Color:
if(readString.indexOf("set") >0) {
hexString = "";
hexString = (readString.substring(9,15));
setHex();
}
//ON/OFF state:
if(readString.indexOf("status") >0) {
client.println(state);
}
//Color state (hex):
if(readString.indexOf("color") >0) {
client.println(hexString);
}
//Brightness state (%):
if(readString.indexOf("bright") >0) {
getV();
client.println(V);
}
delay(1);
while (client.read() >= 0); //added: clear remaining buffer to prevent ECONNRESET
client.stop();
//showValues(); //uncomment for serial debugging
readString.remove(0); //reset readString
}
}
}
}
}
It takes about 20-25 seconds for home assistant c[…]
I tried to upgrade tof my sonoff basic R2 with the[…]
a problem Perhaps you want to define "Probl[…]
Rebooting your router will not give you a faster I[…]
There are no other notifications from esptool.py i[…]
Using the Arduino IDE, you'll learn how to set up […]
In this project, you will post to Twitter using an[…]
In this project, we will build a water level contr[…]
I guess I'm late, but I had the same problem and f[…]
Last night I received my first D1 Minis for a lear[…]
Although I am aware that this is an old post, I fe[…]