Well after my last successes to make a accessory for my HomeBridge, I want to see if I can make a controllable RGB Light accessory. First, you must install HomeBridge on a Raspberry Pi, and configure it as a HomeKit bridge device.How to do that, you can read here. Then I need a RGB Light, so I found this one, a RGB LED strip .
RGB LED strip
The LED strip Uses trinity-SMD LED as lighting source, low voltage with resistance and IC constant current made the shining stable, bright and uniform. FPC circuit board made it easily to bend, with a RGB Controller, you can control the color and brightness easily! Besides, self adhesive back makes it convenient to stick them to any flat surface. These LED strips are sold by standard ( 1m , 2m, 3m .. ) lengths and comes with connectors. the Specification of the strips are: 10.0mm wide, 2mm thick, 50mm long per segment (3 LED’s). 0.24w per LED. Maximum 12V per strip segment. Max 1.2 Amps per meter (all LED’s lit full brightness). 3 common-anode RGB LED’s per segment, 20 segments per meter. No micro controller or chip controller (‘analog’ only!)
Installing the Plugin on your HomeBridge
When you have installed the HomeBridge you need to install the better-http-RGB package in order to let the HomeBridge communicate with your ESP8266 over HTTP
sudo npm install -g homebridge-better-http-rgb --unsafe-perm
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.
sudo nano ~/.homebridge/config.json
Add this code to the config.json file. 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 Temperature & Humidity sensor that can be read by Siri and the HomeKit app. At first we need to setup the FTDI programmer to program the ESP8266-E12 with the Arduino IDE.
The Code
The we can 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.
Yes! It works!
Download all Files for this project on github.
Do you have any questions? Leave a comment down here.
Reference
Nick Farina (Nov 14 2017), Homebridge, Homebridge is a lightweight NodeJS server you can run on your home network that emulates the iOS HomeKit API., https://github.com/nfarina/homebridge Justin J. Novack ( Dec 17 2017), homebridge-better-http-rgb, Supports RGB http(s) devices on the HomeBridge Platform, https://github.com/jnovack/homebridge-better-http-rgb