MQTT – My simple switch for wardrobe and bed

In the summer 2017 my wife and I furnished our bedroom with new furniture. The new wardrobe has now LED lightning and the bed has also lightning integrated. But the bed has only one switch for the two LED’s. Switching the LED right and left separately was not possible and switch the wardrobe LED’s off while in bed was not possible. I doesn’t want a simple solution. I want something switchable with the smartphones. Because I’ve experience with the ESP8266 from my water level gauge I decide to build a switch for smartphones.

I want make use of the MQTT protocol. It’s lightweight enough to implement into a small device like the ESP8266.

The hardware

The LED’s are supplied via a 12 volt transformer. The ESp8266 needs 3.3 volts. So I decided to use a LF33CV for a stable 3.3 volts supply. For switching the LED’s I use a BD237 transistor.

From this schematics I’ve designed a small double layer PCB.

I’ve packaged the eagle schematics and board into a zip file: MqttSwitch

I’ve ordered three PCB on and after a few day I had three professional manufactured PCB’s in hand.

With all the parts soldered on the PCB it looks like this.

The software

Before this project I was not a big fan of the adruino develepment environment. But I’ve found that are many libraries out there for supporting the ESP8266 in general and also the MQTT protocol on the ESP8266. So I used this setup:

I’ve started with the  example „mqtt_esp8266“ that’s that of the PubSubClient. Because I would go with a secure MQTT transport I’ve changed the example to support TLS/SSL. And here is the complete sketch:

 Matthias Jentsch - December 2017


// Update these with values suitable for your network.
#define wifi_ssid ".........."
#define wifi_password ".........."
#define mqtt_server ".........."
// secure connection
#define mqtt_port .....
// certificate fingerprint
#define mqtt_server_fingerprint ".. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .. .."
// mqtt account
#define mqtt_username ".........."
#define mqtt_password ".........."

// Constants for wardrobe switch
#define MAX_LAMPS 4
#define LAMP1_PIN 12
#define LAMP2_PIN 13
#define LAMP3_PIN 14
#define LAMP4_PIN 16
#define mqtt_clientName "SwitchWardrobe"
#define mqtt_topic "switchWardrobe"

// Constants for bed switch
/*#define MAX_LAMPS 2
#define LAMP1_PIN 12
#define LAMP2_PIN 13
const int LAMP_PINS[] = {LAMP1_PIN, LAMP2_PIN};
#define mqtt_clientName "SwitchBed"
#define mqtt_topic "switchBed"*/

// normal connection
//WiFiClient espClient;
// secure connection
WiFiClientSecure espClient;
PubSubClient client(espClient);

void setup() {
  // Initialize the LED pins as outputs
  for (int i = 0; i < MAX_LAMPS; i++) {
    pinMode(LAMP_PINS[i], OUTPUT);
  // switch LEDs off
  for (int i = 0; i < MAX_LAMPS; i++) {
    digitalWrite(LAMP_PINS[i], LOW);
  client.setServer(mqtt_server, mqtt_port);

void setup_wifi() {
  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(wifi_ssid, wifi_password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");
  Serial.println("IP address: ");

  // Synchronize time useing SNTP. This is necessary to verify that
  // the TLS certificates offered by the server are currently valid.
  Serial.print("Setting time using SNTP");
  configTime(8 * 3600, 0, "", "");
  time_t now = time(nullptr);
  while (now < 1000) {
    now = time(nullptr);

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print("] ");
  for (int i = 0; i < length; i++) {

  if (String(topic) == mqtt_topic) {
    for (int i = 0; i < MAX_LAMPS; i++) { if (length > i) {
        Serial.print(i + 1);
        if ((char)payload[i] == '0') {
          digitalWrite(LAMP_PINS[i], LOW);
          Serial.println(" off");
        else if ((char)payload[i] == '1') {
          digitalWrite(LAMP_PINS[i], HIGH);
          Serial.println(" on");
        else {
          Serial.println(" unchanged");
      else {
  else {
    Serial.println("Unknown topic!");

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(mqtt_clientName, mqtt_username, mqtt_password)) {
      if (espClient.verify(mqtt_server_fingerprint, mqtt_server)) {
        Serial.print("verified tls!");
      } else {
        Serial.print("unverified tls");
      // resubscribe
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
void loop() {
  if (!client.connected()) {


2 Gedanken zu „MQTT – My simple switch for wardrobe and bed

  1. Hi Matthias, thanks for sharing this nice article. This integrates wonderful with Home Assistant (HASS). How much would this PCB (parts or assembled) cost? Why did you choose the BD237 power transistor and not an(other) logical FET with a very low Ron? And how did you solder the ESP board on your PCB? And how about using PWM (soft-pwm)?

    1. Hi Dennis,
      thanks for you comment. The cost for three PCB’s was 10.65 €. I had the parts already at home. I’ve chosen the BD237 because I had this transistor already. I did no deeper thinking about which transistor or FET I should use. I simply used the sort that I had. Soldering the ESP board to the PCB was easy. I took a little piece of double sided adhesive tape and taped the ESP on the board. Then I soldered on pin at one corner and then the pin on the opposite corner. The looked with magnifying glass if the ESP was in place and soldered the rest of the pins.
      Btw. the MQTT broker runs since a few days on my home on a Raspberry Pi. It was super easy to install the mosquitto MQTT broker.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht.