RGB LED strip control using Arduino and MQTT

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:

  1. 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.

  2. 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 
  1. 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!

Nice, I will look into reproducing this during christmas break :smile:

I know it’s a pretty basic control right now just sending the color wheel selection to the light, but I would be interested if anyone implements any good code for effects or scripts, etc…this could be a good thread for variations and ideas on this type of lighting

ESP8266 modules can be used to control LED strips, controller with WiFi for a fraction of the cost of most other alternatives. They come in various forms mostly based upon on the number of GPIO pins.

I have about a half dozen, I can control each indidually through a web interface or send them RGB commands over MQTT via openHab.

great suggestion, looking into this for this and some other applications. Looked at them a while back but didn’t realize the full potential. Thanks

Ok, so that means that you dont have to use a Velleman in between?

You’d still need a velleman or another driver in line, this just removed the arduino with Ethernet or wifi…so you could save $15-$20 perhaps

Ok!
I’m looking into building on mysensors so I would “just” hook the Velleman up to an arduino nano with an rf-chip.

That should work fine as long as libraries and pins are the same or make adjustments, haven’t worked with the nano but believe it is the same

Good Day

Is it possible to run same coding as above on arduino instead on ESP8266 to get more io’s?
My coding works great with Ethernet (Arduino Ethernet), but want to use arduino mini with ESP-05 as a “modem” to wifi, but cant find any coding to do so.

Any help will be appreciated.

Thank You

Albertus Geyser

Id be interested to see what you come up with, please share here if you do implement successfully. I do not have an ESP-05 to play with yet.

Digging up an old thread, but I have just assembled a Velleman RGB shield, have an ethernet shield but cannot get anything to work

All I want is simple control of my rgb strip by OpenHAB but it is proving to be an impossible job for me :frowning:

Are you attempting control via MQTT? There may be an issue with MQTT depending on the openhab version you are using, have you tested for messages?

I am, I cannot even get it to compile. I have made a new topic to discuss it with more details.

Look for H801 rgb Wifi controller based on esp8266. Very cheap and easily hackable! I can share my arduino ide code!

Will google that now. If you could share it anyway that would be great, I’m sure it would be of some use.

Please feel free to share the new thread link here in case any others are having issues.

I have not used the code for a while so it could be something to do with a library dependency or updated version, but I will take a look when I get home.

This year I have installed digitally addressable WS2811 chip based strips and have been having fun with increased possibilities, however I had planned to reuse some of this code for MQTT control.

I have found the WS2811 easier to work with since there is no PWM to contend with, only data, ground and power…and more fun so far too!

RGB Strip Arduino MQTT control is my thread.

Spent most of the evening messing around with it and googling but got nowhere. I do have a few reels of ws2812b but that is a project for another day and I want to use what I have now for the analog strips.

I’m having the same issues. Did you manage to get it working?

I had the same Issue.
You have to put the void callback above “PubSubClient client(server, 1883, callback, ethClient);” Then it should work.