Grove 4way relay using esphome

Hi all. Hoping to find some arduino specialists here (warning noob, managed to setup a couple of other esphome based componenets like temp sensor and single relay but no real experience with arduino like devices)


I have this 4way relay board connected to an nodemcu flashed with esphome and connected to home assistant. This is the board : http://wiki.seeedstudio.com/Grove-4-Channel_SPDT_Relay/
I can’t use any of the documented esphome switch configs as it is i2c based.
current config (board flashed and ota updates working)(upload://fNTbOlxZfiU3EZJM9AU3XrsAVMA.jpeg)
esphome:
name: 4wayrelay
platform: ESP8266
board: nodemcuv2

wifi:
ssid: “xxxxxxxxx”
password: “xxxxxxxxxxxxx”

Enable fallback hotspot (captive portal) in case wifi connection fails

ap:
ssid: “4wayrelay”
password: “xxxxxxxxxxxxx”

captive_portal:

Enable logging

logger:

Enable Home Assistant API

api:

ota:

i2c:
sda: D1
scl: D2
scan: True

Log
[14:54:42][C][i2c:028]: I2C Bus:
[14:54:42][C][i2c:029]: SDA Pin: GPIO5
[14:54:42][C][i2c:030]: SCL Pin: GPIO4
[14:54:42][C][i2c:031]: Frequency: 50000 Hz
[14:54:42][I][i2c:033]: Scanning i2c bus for active devices…
[14:54:42][I][i2c:040]: Found i2c device at address 0x11
[14:54:42][C][logger:175]: Logger:
[14:54:42][C][logger:176]: Level: DEBUG
[14:54:42][C][logger:177]: Log Baud Rate: 115200
[14:54:42][C][logger:178]: Hardware UART: UART0
[14:54:42][C][captive_portal:169]: Captive Portal:
[14:54:42][C][ota:029]: Over-The-Air Updates:
[14:54:42][C][ota:030]: Address: 4wayrelay.local:8266
[14:54:42][C][api:095]: API Server:
[14:54:42][C][api:096]: Address: 4wayrelay.local:6053
[14:54:48][D][api.connection:583]: Client ‘Home Assistant 0.104.3 (192.168.0.6)’ connected successfully!
[14:56:16][I][ota:046]: Boot seems successful, resetting boot loop counter.

My guess would be I would have to use a config like this guy https://khaz.me/cheap-and-easy-control-of-8-relays-through-home-assistant/ but I have no idea how to talk to the on-board STM32F030F4P6.

Any tips?

Yeah, format your code properly if you want to post it on the forum. See How to help us help you - or How to ask a good question

Very friendly…

You’re more likely to get a helpful response if you follow the rules and make a helpful post.

Anyway, your device (assuming you have nothing else connected via i2c) is 0x11. I think you may need to make a custom i2c component, unless there is support for this already in esphome https://esphome.io/custom/i2c.html

Anyone trying the same. I’ve left the esphome route and used the grove “multi_channel_relay” library in an arduino sketch and used MQTT switches in Home Assistant to connect things.

For reference my arduino code :

// Original sketch courtesy of ItKindaWorks
//ItKindaWorks - Creative Commons 2016
//github.com/ItKindaWorks
//
//Requires PubSubClient found here: https://github.com/knolleary/pubsubclient
//
//ESP8266 Simple MQTT switch controller


#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <multi_channel_relay.h>

Multi_Channel_Relay relay;

void callback(char* topic, byte* payload, unsigned int length);

//EDIT THESE LINES TO MATCH YOUR SETUP
#define MQTT_SERVER "xxx.xxx.xxx.xxx"  //your MQTT IP Address 
const char* ssid = "wifisid";
const char* password = "wifipass";
#define mqtt_user "mymqttusername" 
#define mqtt_password "mqttuserpass"
#define mqtt_port 1883



//EJ: These are the MQTT Topic that will be used to manage the state of Relays 1 ~ 4
//EJ: Refer to my YAML component entry
//EJ: feel free to replicate the line if you have more relay switch to control, but dont forget to increment the number suffix so as increase switch logics in loop()

char const* switchTopic1 = "/house/switch1/";
char const* switchTopic2 = "/house/switch2/";
char const* switchTopic3 = "/house/switch3/";
char const* switchTopic4 = "/house/switch4/";


WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, mqtt_port, callback, wifiClient);

void setup() {
    // Set I2C address and start relay
  relay.begin(0x11); 
  //initialize the switch as an output and set to LOW (off)
 relay.turn_off_channel(1);
 relay.turn_off_channel(2);
 relay.turn_off_channel(3);
 relay.turn_off_channel(4);
  
  ArduinoOTA.setHostname("My Arduino WEMO"); // A name given to your ESP8266 module when discovering it as a port in ARDUINO IDE
  ArduinoOTA.begin(); // OTA initialization

  //start the serial line for debugging
  Serial.begin(115200);
   // Set I2C address and start relay
  relay.begin(0x11); 
  delay(100);

  //start wifi subsystem
  WiFi.begin(ssid, password);
  //attempt to connect to the WIFI network and then connect to the MQTT server
  reconnect();

  //wait a bit before starting the main loop
      delay(2000);
}


void loop(){

  //reconnect if connection is lost
  if (!client.connected() && WiFi.status() == 3) {reconnect();}

  //maintain MQTT connection
  client.loop();

  //MUST delay to allow ESP8266 WIFI functions to run
  delay(10); 
  ArduinoOTA.handle();
}

void callback(char* topic, byte* payload, unsigned int length) {

  //convert topic to string to make it easier to work with
  String topicStr = topic; 
  //EJ: Note:  the "topic" value gets overwritten everytime it receives confirmation (callback) message from MQTT

  //Print out some debugging info
  Serial.println("Callback update.");
  Serial.print("Topic: ");
  Serial.println(topicStr);

   if (topicStr == "/house/switch1/") 
    {

     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       relay.turn_on_channel(1);
       client.publish("/house/switchConfirm1/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       relay.turn_off_channel(1);
       client.publish("/house/switchConfirm1/", "0");
       }
     }

     // EJ: copy and paste this whole else-if block, should you need to control more switches
     else if (topicStr == "/house/switch2/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       relay.turn_on_channel(2);
       client.publish("/house/switchConfirm2/", "1");
       DEBUG_PRINT.print("Turn switch 2 on, State: ");
       DEBUG_PRINT.println(relay.getChannelState(), BIN);
       
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       relay.turn_off_channel(2);
       client.publish("/house/switchConfirm2/", "0");
       DEBUG_PRINT.print("Turn switch 2 off, State: ");
       DEBUG_PRINT.println(relay.getChannelState(), BIN);
       }
     }
     else if (topicStr == "/house/switch3/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       relay.turn_on_channel(3);
       client.publish("/house/switchConfirm3/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       relay.turn_off_channel(3);
       client.publish("/house/switchConfirm3/", "0");
       }
     }
     else if (topicStr == "/house/switch4/") 
     {
     //turn the switch on if the payload is '1' and publish to the MQTT server a confirmation message
     if(payload[0] == '1'){
       relay.turn_on_channel(4);
       client.publish("/house/switchConfirm4/", "1");
       }

      //turn the switch off if the payload is '0' and publish to the MQTT server a confirmation message
     else if (payload[0] == '0'){
       relay.turn_off_channel(4);
       client.publish("/house/switchConfirm4/", "0");
       }
     }    
}

void reconnect() {

  //attempt to connect to the wifi if connection is lost
  if(WiFi.status() != WL_CONNECTED){
    //debug printing
    Serial.print("Connecting to ");
    Serial.println(ssid);

    //loop while we wait for connection
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }

    //print out some more debug once connected
    Serial.println("");
    Serial.println("WiFi connected");  
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }

  //make sure we are connected to WIFI before attemping to reconnect to MQTT
  if(WiFi.status() == WL_CONNECTED){
  // Loop until we're reconnected to the MQTT server
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");

      // Generate client name based on MAC address and last 8 bits of microsecond counter
      String clientName;
      clientName += "esp8266-";
      clientName += "4wayrelay";

      //if connected, subscribe to the topic(s) we want to be notified about
      //EJ: Delete "mqtt_username", and "mqtt_password" here if you are not using any 
       if (client.connect((char*) clientName.c_str(),mqtt_user, mqtt_password)) 
        Serial.print("\tMQTT Connected");
        client.subscribe(switchTopic1);
        client.subscribe(switchTopic2);
        client.subscribe(switchTopic3);
        client.subscribe(switchTopic4);
        //EJ: Do not forget to replicate the above line if you will have more than the above number of relay switches
      }

      //otherwise print failed for debugging
      else{Serial.println("\tFailed."); abort();}
    }
  }
}

The corresponding switches in home assistant config look like this

switch 1:
  - platform: mqtt
    name: "MQTT Switch 1"
    state_topic: "/house/switchConfirm1/"
    command_topic: "/house/switch1/"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true    
    
switch 2:
  - platform: mqtt
    name: "MQTT Switch 2"
    state_topic: "/house/switchConfirm2/"
    command_topic: "/house/switch2/"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true    

switch 3:
  - platform: mqtt
    name: "MQTT Switch 3"
    state_topic: "/house/switchConfirm3/"
    command_topic: "/house/switch3/"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true    
  
switch 4:
  - platform: mqtt
    name: "MQTT Switch 4"
    state_topic: "/house/switchConfirm4/"
    command_topic: "/house/switch4/"
    payload_on: "1"
    payload_off: "0"
    qos: 1
    retain: true

2 Likes

Hi, I just got my own Grove - 4-Channel SPDT Relay. Did you give ESPHome another chance? My plan is to use 3 of the relays on the board to switch from a given time squedule. This to choose which sone the robot lawmower should cut.

nope, left the 4 way grove relay setup as is. Works fine so far.
Did add a couple of other esphome relay devices to my setup meanwhile but without the grove controller. Just used the esphome switch :

switch:
  - platform: gpio
    pin: GPIO0
    id: ketel

I was going to suggest getting one of the inexpensive 2- or 4- or even 8-relay boards with built-in ESP-01 or ESP-12F from Amazon. These are well-known and well-understood. Most are based on original designs from LC Technology in China. They are also usually found for lower prices on AliExpress.com.

1 Like