A place users can post their projects. If you have a small project and would like your own dedicated place to post and have others chat about it then this is your spot.
User avatar
By Willem S
#78556 I have been struggling with the next issue for a while now.

When i change the brightness from a sertain level to another level, like 100% to 40% nothing happens. I have to choose an other color only then the brightness changes to 40%.

hope you can help,

Willem
User avatar
By eqsOne
#78590 Hey Willem,

so you added a color fade, tried your code and although I have no issues with changing color or brightness once it runs, I noticed the esp resets after loading up the sketch.
If I set an initial color, it does show up with a fade-in but like a bit off. If you try a ‚080100‘ for example, green has to go to its lowest ‚01‘ value - now with your sketch green just stays off.
So yes, seems you have it on a way but it quite needs some more debugging indeed.
User avatar
By tommrodrigues
#78598 Love the tutorial! It's what I've been looking for for ages!

I've got everything set up but I've run into a few issues. The first of which was fairly easy to solve. I believe you forgot to include
Code: Select all"service": "Light",
in the config.json as without it, HomeKit says it does not support that type of accessory.
However, the problem which I am still experiencing is with the brightness. I.e. whilst I can set the colour absolutely fine, when I try to change the brightness, it sets the colour to blue, and then I have to manually change the colour to another one. This also means that there are a number of issues when trying to set up Automations within Homekit. I would really appreciate it if you could look further into this as it is an otherwise awesome project, thank you!

Kind regards, Tom

P.S. Whilst it isn't really too much of an issue; after restarting the strip circuit it appears in HomeKit as "On" even though it admits that the brightness is 0%. Is this something you could see if you could fix? No worries if not as it's not a major issue.

Here is my code (I made I few modifications with the WiFi and Serial setup):

Code: Select all#include <ESP8266WiFi.h>

#define redPin 13 //D7 - Red channel
#define grnPin 12 //D6 - Green channel
#define bluPin 14 //D5 - Blue channel

WiFiServer server(80); //Set server port

String readString;           //String to hold incoming request
String hexString = "000000"; //Define inititial color here (hex value), 080100 would be a calm warmtone i.e.

int state;

int r, g, b, V;

float R, G, B, x;

///// WiFi SETTINGS - Replace with your values /////////////////
const char* ssid = "SSID";
const char* password = "PASSWORD";
////////////////////////////////////////////////////////////////////

void WiFiStart() {
  Serial.begin(115200);
  delay(10);
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.softAPdisconnect(true);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    Serial.print("_");
  }
  Serial.println();
  Serial.println("Done");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  Serial.println("");

  server.begin();
}

void allOff() {
  state = 0;
  analogWrite(redPin, 0);
  analogWrite(grnPin, 0);
  analogWrite(bluPin, 0);
}

//Write requested hex-color to the pins (10bit pwm)
void setHex() {
  state = 1;
  long number = (long) strtol( &hexString[0], NULL, 16);
  r = number >> 16;
  g = number >> 8 & 0xFF;
  b = number & 0xFF;
  r = map(r, 0, 255, 0, 1023);  //added for 10bit pwm
  g = map(g, 0, 255, 0, 1023);  //added for 10bit pwm
  b = map(b, 0, 255, 0, 1023);  //added for 10bit pwm
  analogWrite(redPin, (r));
  analogWrite(grnPin, (g));
  analogWrite(bluPin, (b));
}

//Compute current brightness value
void getV() {
  R = roundf(r / 10.23); //for 10bit pwm, was (r/2.55);
  G = roundf(g / 10.23); //for 10bit pwm, was (g/2.55);
  B = roundf(b / 10.23); //for 10bit pwm, was (b/2.55);
  x = max(R, G);
  V = max(x, B);
}

//For serial debugging only
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.println("");
}

void setup() {
  Serial.begin(9600);
  delay(1);
  pinMode(redPin, OUTPUT);  //declaration added
  pinMode(grnPin, OUTPUT);  //declaration added
  pinMode(bluPin, OUTPUT);  //declaration added
  setHex(); //Set initial color after booting. Value defined above
  WiFi.mode(WIFI_STA);
  WiFiStart();
  //showValues(); //Uncomment for serial output
  int state = 0;
}

void loop() {
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  while (client.connected() && !client.available()) {
    delay(1);
  }
  //Respond on certain Homebridge HTTP requests
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();
        if (readString.length() < 100) {
          readString += c;
        }
        if (c == '\n') {
          //Serial.print("Request: "); //Uncomment for serial output
          //Serial.println(readString); //Uncomment for serial output
          //Send reponse:
          client.println("HTTP/1.1 200 OK");
          client.println("Content-Type: text/html");
          client.println();
          //On:
          if (readString.indexOf("on") > 0) {
            setHex();
            //showValues();
          }
          //Off:
          if (readString.indexOf("off") > 0) {
            allOff();
            //showValues();
          }
          //Set color:
          if (readString.indexOf("set") > 0) {
            hexString = "";
            hexString = (readString.substring(9, 15));
            setHex();
            //showValues();
          }
          //Status on/off:
          if (readString.indexOf("status") > 0) {
            client.println(state);
          }
          //Status color (hex):
          if (readString.indexOf("color") > 0) {
            client.println(hexString);
          }
          //Status brightness (%):
          if (readString.indexOf("bright") > 0) {
            getV();
            client.println(V);
          }
          delay(1);
          while (client.read() >= 0);  //added: clear remaining buffer to prevent ECONNRESET
          client.stop();
          readString.remove(0);
        }
      }
    }
  }
}
User avatar
By eqsOne
#78634 Hey Tom, you're welcome!
tommrodrigues wrote:(...) I believe you forgot to include
Code: Select all"service": "Light",
in the config.json as without it, HomeKit says it does not support that type of accessory. (...)

(...) However, the problem which I am still experiencing is with the brightness. (...)

At the time I posted this, it was running perfectly fine as is, even until today in my case. Meanwhile Homebride has been updated to 2.0, maybe that's causing your issues with the brightness value.

As Philipp1887 posted back in june:
Philipp1887 wrote:I dont know if someone else has or had problems with the accessory entry in the config.json file, but I had:
I had to insert
Code: Select all"service": "Light",
under
Code: Select all"name": "Single Color Light",
. Otherwise Homekit would show the strip as "Not supported".
Also for a correct RGBW Output I had to insert
Code: Select all "brightness": true
in the "color" property.
Otherwise I couldnt set any White color in homekit.

You should give this a try, maybe that's all. If not, you might uncomment the serial output for debug within the sketch to see what HomeKit responds. At least this was a helpful tool when I got this together. So long, happy coding!