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??
#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.
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 .....
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.
@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.
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 -