Last year after I had OpenHAB up and running and had gotten comfortable with some basics one of my first goals was to get control ((in time for the holidays) over the cheap LED rgb strips that had become so readily available for about $15 for a 16ft roll…only problem was automating with a readily available networked RGB controller (that did not cost as much as Philips HUE).
My first home automation panel was an HAI panel and I had played with some software based automation on top such as Elve, Haiku, Homeseer, etc…when looking for more versatile and custom ways to extend functionality It was suggested I might look into openhab and MQTT, which thank goodness opened up the world to me once I jumped in. Haven’t looked back since! Anyway I digress…
So in the timeliness of the season I would like to centralize the information I had pulled together from various sources in order to get everything together and working as desired. I remember looking for a complete how to for openhab–>MQTT–>arduino–>RGBs, perhaps there are more write ups now but if not here is mine (It was a good learning excercise for me though):
For this install you will need:
1. Arduino board
2. Ethernet Shield
3. Velleman KA01 RGB Shield (You couls always make your own LED driver instead but this is one of the easiest ways and they have a kit you can assemble yourself or one pre-assembled)
4. LED 3528 strip light
optional:
12V RGB amplifier for extended runs (more than 30ft)
Directions:
-
Take a running openHAB installation and make sure you have the MQTT binding working. There are many setup guides I’m sure but you can reference this. Install and configure the binding.
-
Install mosquitto (MQTT broker) on your network or openhab server. I have usually installed on my openhab server as I run Ubuntu on a capable Intel NUC and find it easier.
For Ubuntu simply add the repo, update and install
sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt-get update
sudo apt-get install mosquitto mosquitto-clients python-mosquitto
ubuntu@ubuntu:~$ mosquitto
1403588549: mosquitto version 0.15 (build date 2013-08-23 19:24:40+0000) starting
1403588549: Opening ipv4 listen socket on port 1883.
1403588549: Error: Address already in use
For Raspberry Pi you could follow this guide
It’s a good idea to test and I like to keep a terminal open subscribed to all
$ mosquitto_sub -h localhost -t \# -v
- Assemble Arduino shields and upload the sample Velleman code to check operation. I will post my Arduino code below which contains some simple MQTT triggers and code to display any color selected by the color picker item in openhab. Modify as necessary for your application.
#include <SPI.h>
#include <Ethernet.h>
#include <PubSubClient.h>
// Set the MAC address
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0F, 0x5E, 0x69 };
// Set fallback IP address if DHCP fails
IPAddress ip(x,x,x,x);
// Set the broker server IP
byte server[] = { x,x,x,x };
EthernetClient ethClient;
PubSubClient client(server, 1883, callback, ethClient);
int SoffitR;
int SoffitG;
int SoffitB;
int BLUE = 6;
int GREEN = 5;
int RED = 3;
void setup()
{
// Open serial communications
Serial.begin(9600);
// Start with a hard-coded address:
Serial.println("Assigning Static IP address:");
Ethernet.begin(mac, ip);
Serial.print("My address:");
Serial.println(Ethernet.localIP());
// Connect to Broker, give it arduino as the name
if (client.connect("arduino_LED2")) {
// Publish a message to the status topic
client.publish("status/arduino_LED","Arduino LED2 is now online");
// Listen for messages on the control topic
client.subscribe("control/arduino_LED/#");
}
}
void loop()
{
client.loop();
}
void callback(char* topic, byte* payload, unsigned int length) {
// check for messages on subscribed topics
payload[length] = '\0';
Serial.print("Topic: ");
Serial.println(String(topic));
// check topic to identify type of content
if(String(topic) == "control/arduino_LED/livingroom/color") {
// convert payload to String
String value = String((char*)payload);
//value.trim();
Serial.print (value);
//Serial.flush();
// split string at every "," and store in proper variable
// convert final result to integer
SoffitR = value.substring(0,value.indexOf(',')).toInt();
SoffitG = value.substring(value.indexOf(',')+1,value.lastIndexOf(',')).toInt();
SoffitB = value.substring(value.lastIndexOf(',')+1).toInt();
// print obtained values for debugging
Serial.print("RED: ");
Serial.println(SoffitR);
//client.publish("status/arduino_LED", SoffitR);
Serial.print("GREEN: ");
Serial.println(SoffitG);
//client.publish("status/arduino_LED", SoffitG);
Serial.print("BLUE: ");
Serial.println(SoffitB);
//client.publish("status/arduino_LED/soffit/color/blue", int SoffitB);
//Serial.flush();
analogWrite(GREEN, SoffitG);
analogWrite(RED, SoffitR);
analogWrite(BLUE, SoffitB);
while(Serial.available())
Serial.read();
}
}
OpenHAB Items
/* LED STRIPS */
Color LivSoffitLight "Living Room Soffit Color" (Lights_LivingRoom)
String LivSoffitLightColor (Lights_LivingRoom) {mqtt=">[mosquitto:control/arduino_LED/livingroom/color:command:*:default]"}
Number LivSoffitAnimation "Living Room Soffit Animation" (Lights_LivingRoom) {mqtt=">[mosquitto:control/arduino_LED/livingroom/animation:command:*:default]"}
Enjoy the millions of colors without breaking the bank!