How to run multiple MQTT lights / switches from one ESP8266 (NodeMCU) board?

Been installing Sonoff’s around the house to control lights with HA and they’re working great. I have a switchbox in the basement with 4 switches for 4 different lights (2 of them dimmable) and don’t have enough room in the box to place 4 Sonoffs. I can, however, fit an ESP (NodeMCU) board & small power supply in there, but I’m having trouble getting the code to work. I’m using KmanOz’s ino (Thank you!! It works flawlessly!!) on all my other Sonoffs and trying to modify it for use with multiple lights. Can I have multiple lights / switches in HA that point to the same ESP board? I know the “MQTT Client ID” must be unique for each Sonoff but I don’t see where that’s even shown in the HA configuration file… Been searching and searching and can’t come up with a solution - can anyone help??

KmanOz’s Sonoff page if it helps - https://github.com/KmanOz/Sonoff-HomeAssistant/

The client id is set in the sonoff code here

#define MQTT_CLIENT "Sonoff_Living_Room_v2.0t" // mqtt client_id (Must be unique for each Sonoff)

The author has missed a trick, in that you can avoid editing it for each machine by using the node id, which is unique for esp8266. From memory, in Arduinio this is accessed from something like ESP.get_node_id()

To have multiple switches, you need to subscribe to multiple topics, one for each switch, and then have an if statement checking for each topic in the callback routine.

@johnny_mnemonic If you want to switch firmwares, Tinkerman’s espurna firmware for Sonoff and NodeMCU already supports multiple relays on a single module.

I would like to but his code is so complex I don’t know where to begin. Just starting out with HA and not much coding experience…

Like this?

// MQTT: ID, server IP, port, username and password
const PROGMEM char*     MQTT_CLIENT_ID    = "Downstairs Lights";      // (All 4 downstairs area lights)
const PROGMEM char*     MQTT_SERVER_IP    = "192.168.0.180";
const PROGMEM uint16_t  MQTT_SERVER_PORT  = 1883;
const PROGMEM char*     MQTT_USER         = "USER";
const PROGMEM char*     MQTT_PASSWORD     = "PASSWORD";

// MQTT: topics
// state
const PROGMEM char*     MQTT_TV_LIGHT_STATE_TOPIC       = "home/tv_area_light_switch/stat";
const PROGMEM char*     MQTT_TV_LIGHT_COMMAND_TOPIC     = "home/tv_area_light_switch";
const PROGMEM char*     MQTT_DOOR_LIGHT_STATE_TOPIC     = "home/downstairs_back_door/light_switch/stat";
const PROGMEM char*     MQTT_DOOR_LIGHT_COMMAND_TOPIC   = "home/downstairs_back_door_light_switch";
const PROGMEM char*     MQTT_ENTRY_LIGHT_STATE_TOPIC    = "home/downstairs_entry/light_switch/stat";
const PROGMEM char*     MQTT_ENTRY_LIGHT_COMMAND_TOPIC  = "home/downstairs_entry/light_switch";
const PROGMEM char*     MQTT_TABLE_LIGHT_STATE_TOPIC    = "home/card_table/light_switch/stat";
const PROGMEM char*     MQTT_TABLE_LIGHT_COMMAND_TOPIC  = "home/card_table/light_switch";

// brightness
const PROGMEM char*     MQTT_TV_LIGHT_BRIGHTNESS_STATE_TOPIC      = "home/tv_area/light_switch/brightness";
const PROGMEM char*     MQTT_TV_LIGHT_BRIGHTNESS_COMMAND_TOPIC    = "home/tv_area/light_switch/brightness/set";
const PROGMEM char*     MQTT_TABLE_LIGHT_BRIGHTNESS_STATE_TOPIC   = "home/card_table/light_switch/brightness";
const PROGMEM char*     MQTT_TABLE_LIGHT_BRIGHTNESS_COMMAND_TOPIC       = "home/card_table/light_switch/brightness/set";

// payloads by default (on/off)
const PROGMEM char*     TV_LIGHT_ON           = "ON";
const PROGMEM char*     TV_LIGHT_OFF          = "OFF";
const PROGMEM char*     DOOR_LIGHT_ON         = "ON";
const PROGMEM char*     DOOR_LIGHT_OFF        = "OFF";
const PROGMEM char*     ENTRY_LIGHT_ON        = "ON";
const PROGMEM char*     ENTRY_LIGHT_OFF       = "OFF";
const PROGMEM char*     TABLE_LIGHT_ON        = "ON";
const PROGMEM char*     TABLE_LIGHT_OFF       = "OFF";

bool door_light_state = false;
bool entry_light_state = false;
bool tv_light_state = false;
bool table_light_state = false;

int tv_light_brightness = 255;
int table_light_brightness = 255;

unsigned long count = 0;

bool tv_dim_status = false;
bool table_dim_status = false;

int tv_dim_amount = 5;
int table_dim_amount = 5;

const PROGMEM uint8_t TV_PIN = 12;
const PROGMEM uint8_t TABLE_PIN = 14;
const PROGMEM uint8_t DOOR_PIN = 13;
const PROGMEM uint8_t ENTRY_PIN = 15;
const PROGMEM uint8_t LED_PIN = 4;

That looks right. Then in the callback routine you will need something like

void callback(const MQTT::Publish& pub) {
   if (pub.topic() == MQTT_TV_LIGHT_COMMAND_TOPIC) {
      if (pub.payload_string() == TV_LIGHT_ON) {
          digitalWrite(RELAY, HIGH);   // You need to define which relay here
     } else {
          digitalWrite(RELAY, LOW);
    }
   } else if (pub.topic() == MQTT_DOOR_LIGHT_COMMAND_TOPIC) {
     // do DOOR commands
   } else .....
1 Like

Awesome, I’ll give it a shot. Thanks for your help!!

Hi @johnny_mnemonic, I am about to create something very similar to you and am just starting out with NodeMCU / Arduino / C++ for the first time. Did you get this working? I’d be hugely appreciative of seeing the rest of your code if you don’t mind. I basically want to have the NodeMCU control two relays as outputs, monitor four digital inputs and one analogue input and have all this in HA via MQTT. I have started writing the code by using what is above as a guide but some parts are a little confusing.

Hi Dave,

I couldn’t get this working and abandoned the project. Sorry, wish I could help. Good luck with yours!

Hello,

@johnny_mnemonic @sparkydave
I figured it out.
I used it in a bigger project, but you should be able to get your parts out of it.
It is a wemos with MQQT, 4 buttons, 3 relays and a temperature sensor.
But you can use it on any ESP8266 based device.

Checkout my nodes at github
Or go directly to the .INO

Have fun,
Bouwy

1 Like

Hi there, stumbled across this thread, similar to what i’m trying to achieve, i would be so grateful if you could help me modify this code to achieve multiple lights/switches?!
Thankyou!!

couldnt paste code in its too long so here is link to code source -