MQTT LED-STRIPE WITH A WEMOS D1 MINI

To control two of my LED-Strips i update my “Control box”. The current Version based on two OPEN-SMART Full-color RGB LED Strip Driver Module for Arduino and a WeMos D1 mini. I use two 12 V power supply and a voltage regulator to connect on of the power supply also to the WeMos.


To controll two LED-Strips with one WeMos and the I2C Bus i connect the WeMos with the first LED Strip Driver Module with the following pins:

WeMosLED Strip Driver
D5CIN
D6DIN
GGND

Between the first LED Strip Driver and the second LED Strip Driver i connect all out pins to the in pins at the next board. To test its also necessary to connect the 12V power supply and a RGB-LED-Stripe to the LED Strip Driver. I download the example for the LED Strip Driver (its also in the GIT-Repro of the project) and change the Ports of the Script to D5 and D6:

RGBdriver Driver(D6,D5);

And than the RGB-LED-Strip start to blink. Magic \o/

in the loop or setup function the Strip can be control with the following command:

Driver.begin(); // begin
Driver.SetColor(255, 0, 0); //Red. first LED strip
Driver.SetColor(0, 0, 255); //Blue. second LED strip
Driver.end();

To control the LED-Strip with MQTT i look at some MQTT-Libarys for Ardunio and decide to use PubSubClient.

In the Setup we can connect to the MQTT Server and subscribe to the topics for the LED-Strips:

WiFiClient wifiClient;
PubSubClient client("mqtt.handler.cloud", 1883, callback, wifiClient);
if ("ledname", "username", "password")) {
  client.subscribe("topic/apartment/light/ledstripe/bed");
  client.subscribe("topic/apartment/light/ledstripe/couch");
};

in the callback function i save the message based on the topic:

void callback(char* topic, byte* payload, unsigned int length) {
  String msg = "";
  for (int i=0;i<length;i++) {
    msg = msg + String((char)payload[i]);
  }
  if(String(topic)=="soeren/apartment/light/ledstripe/bed") {
    led_bed = msg;
  } else if(String(topic)=="soeren/apartment/light/ledstripe/couch") {
    led_couch = msg;
  } else {
    Serial.println("Unknown Topic");
  }
  doLEDS();
}

Over MQTT I send a number with 9 digits. So in the doLEDS() function I split them and send them over the I2C Bus to the controller:

 

void doLEDS() {
  Driver.begin();
  Driver.SetColor(led_couch.substring(0,3).toInt(), led_couch.substring(3,6).toInt(), led_couch.substring(6,9).toInt());
  Driver.SetColor(led_bed.substring(0,3).toInt(), led_bed.substring(3,6).toInt(), led_bed.substring(6,9).toInt());
  Driver.end();
}

 

the complete code is in the Github Repro.

After I test everything in the box and make some small code changes i finish the box by removing to long wire.