Well after my last success to make a accessories for my homebridge, I want to see if I can make a RGB Light.

First an need a RGB LED strip so I found this one.

 

For those who haven’t read my previous post, you must first install HomeBridge on a Raspberry Pi, and configure it as a HomeKit bridge device. For who did’t read my previous  post about the Raspian Pi update, there are a few thing changed! first read this post https://www.studiopieters.nl/raspberry-pi-a-security-update-for-raspbian-pixel/

So once again we have to alter a few things in the installation process. For a headless installation we need to add the ssh file to the root of the SD-Card. This in order to connect to the Raspberry Pi. When we have don this we can install Xrdp as described in this post https://www.studiopieters.nl/raspberry-pi-remote-desktop-connection/

But then I found a new problem it has to do with the fact that the Raspbian Jessie with pixel, which officially stands for “Pi Improved Xwindows Environment, Lightweight”, now comes pre-packaged with RealVNC, which conflicts with both xrdp (which uses vnc4server) and tightvncserver.

So I needed a solution: Google me this google me that….

The fix:

Let’s assume you already did sudo apt-get update and sudo apt-get install xrdp.

Now, let’s disable the RealVNC install. I have tested it and follow these steps to disable the RealVNC install and allow xrdp to work:

sudo apt-get remove xrdp vnc4server tightvncserver

sudo apt-get install xrdp

Remote logins using xrdp now work. Here after you can make a remote connection and follow the steps as described in this post https://www.studiopieters.nl/raspberry-pi-apple-homebridge/

When you have installed the homebridge you need to install the better-http-RGB package in order to let the homebride communicate with your ESP8266 over http.

Edit the HomeBridge /var/homebridge/config.json file on the Raspberry Pi HomeBridge. the file may alternatively be in /home/.homebridge or /root/home/./homebridge.

add this code to the config.json file.

sudo nano ~/.homebridge/config.json

mind the last comma, you may or may not need it if you have other accessories, or Homebridge is crashing on load.

 

{
   "bridge": {
      "name": "Homebridge",
      "username": "CC:22:3D:E3:CE:30",
      "port": 51826,
      "pin": "031-45-154"
   },

   "description": "Example configuration file for NodeMCU-RGB-Controller and better-http-rgb plugin",

   "platforms": [
        
    ],
    
   "accessories": [
   {    "accessory": "HTTP-RGB",
        "name": "RGB Strip",

        "switch": {
           "status": "http://192.168.1.10:80/status",
           "powerOn": "http://192.168.1.10:80/on",
           "powerOff": "http://192.168.1.10:80/off"
      },

        "color": {
           "status": "http://192.168.1.10:80/color",
           "url": "http://192.168.1.10:80/set/%s"
      },

        "brightness": {
           "status": "http://192.168.1.10:80/bright",
           "url": "http://192.168.1.10:80/set/%s"
      }
   }
   
   ]
}

 

The Hardware

Once you have HomeKit / HomeBridge working on your Pi and your iPhone, we can build a RGB LED that can be controlled by Siri and the HomeKit app.

At first we need to setup the FTDI programmer to program the ESP8266-E12 with the Arduino IDE.

upload the program below to your ESP8266.

 

/* Project name: Apple Homebridge - RGB Lightel  
*  Project URI: https://www.studiopieters.nl/Apple Homebridge - RGB Light   
*  Description: Apple Homebridge - RGB Light
*  Version: 6.0.3 
*  License: GNU General Public License V2 or later  */


#include <ESP8266WiFi.h>
#include <math.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)

int state;

int r;
int g;
int b;

float R;
float G;
float B;

int x;
int V;



///// 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
IPAddress gateway(192,168,1,1);  // Your router IP
IPAddress subnet(255,255,255,0); // Subnet mask
////////////////////////////////////////////////////////////////////

void WiFiStart() {
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  WiFi.config(ip, gateway, subnet);
  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
void setHex() {
  state = 1;
  long number = (long) strtol( &hexString[0], NULL, 16);
  r = number >> 16;
  g = number >> 8 & 0xFF;
  b = number & 0xFF;
  analogWrite(redPin, (r));
  analogWrite(grnPin, (g));
  analogWrite(bluPin, (b));
}

//Compute current brightness value
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);
}

//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);
  setHex(); //Set initial color after booting. Value defined above
  WiFi.mode(WIFI_STA);
  WiFiStart();
  //showValues(); //Uncomment for serial output
}

void loop() {
  //Reconnect on lost WiFi connection
  if (WiFi.status() != WL_CONNECTED) {
    WiFiStart();
  }
 
  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);
          client.stop();
          readString="";
        }
      }
    }
  }
}

 

Now we can modify our setup so we can see if everything is working. In the fritting digram below you can see that now I have added a LED Strip plus three TIP122 (The TIP120 is an NPN Power Darlington Transistor. It can be used with an Arduino to drive motors, turn lights on, and drive other high power gadgets.) and keeping the RX and TX connected so we can see in the monitor from the Arduino IDE if everything is working as we want to.

Now we have to connect our iPhone to the home bridge and add the new accesoires.

Hooray! It works!

Download all Files for this project on github

Do you have any questions? Leave a comment down below!

 

Pin on Pinterest1Share on Facebook0Share on Google+0Tweet about this on TwitterShare on LinkedIn0Buffer this pageDigg thisEmail this to someone
Flattr the authorShare on Reddit0Share on StumbleUpon0Share on Tumblr0Share on VKShare on Yummly0
  • Todd

    How would I wire a physical pushbutton switch into this. I was thinking of shorting a pin to ground and that would send an update to home bridge some how. I want the switch to either turn the lights on if they are off or turn them off if they are on. Please help because I cannot figure this out. I want to do this all on one board if possible. Please post code if you have a solution.
    Thanks

  • Hey Todd,

    Maybe the best thing to do is to go to the basics first: https://www.arduino.cc/en/Tutorial/Button
    And maybe this post will help: http://www.studiopieters.nl/arduino-multiway-switching/
    After reading these blog’s let me see your code, you have created for so far.

  • Todd

    Thank you for the reply. I have successfully coded it so I can manually turn on/off the lights, but that will not update itself in HomeKit. My code is the same as you have above. Do you have any solutions for updating the lights on HomeKit so then they will turn off? For example, send the request out to home bridge and then have it come back and turn off the lights? Any solution that will make this work would be greatly appreciated. Thanks so much.

  • Todd

    Okay after reviewing my code, I seem to have a problem. The program will not check for the main loop in the code until it doesn’t have a connection to a device anymore. For example, I put in a println(“loop”) every time a loop executes and it only executes when it looses connection to the phone(when I don’t have the home app open I presume). I have only initialized GPIO 5 to high in my code. I do need some help sending out requests as I am not experienced with this at all. All of my code is the same as you have above so no need to paste it(with the exception of a println and initializing GPIO to HIGH). If you could write up some code for this button, that would be greatly appreciated.
    Thank you once again for the continued support.

  • Maybe you can merge this Plugin (https://www.npmjs.com/package/homebridge-http-simple-switch) with this one, then you can switch the light on and off, and it will be updated on the home-bridge / home kit. With this solution you can integrate everything on one single board, or make more boards that enable you to turn on and off the light manually.
    Success!

UA-29014574-1