Interfacing EQSP32 with Home Assistant (Industrial ESP32 IoT Controller)

In this tutorial, we will explain how to create simple interfaces between the EQSP32 and Home Assistant. The EQSP32 uses the MQTT protocol to communicate with Home Assistant. For the two systems to exchange information, we need to have some control and monitoring parameters, which we call interfaces.

EQSP32 - Din rail mount industrial IoT controller - ESP32 PLC. EQSP32 is introducing itself to Home Assistant and it is ready to execute commands.

Each interface has a unique name that will appear in Home Assistant, either as a control variable or a displayed sensor value. To create such parameters, we need to use the createInterface() function. This function takes two arguments: the first is the interface name, and the second can be a pin number or a custom entity type.

Prerequisites

Before we begin, ensure you have the following:

  • An EQSP32 device.
  • MQTT device discovery enabled.
  • The Home Assistant server running an MQTT broker (Mosquitto broker)

Check out our tutorial on how to set up your MQTT broker and enable MQTT device discovery in your EQSP32.

Create an ADIO pin interface – Monitoring interface

Let’s start with the first case where we create an interface for a temperature sensor using the ADIO pin directly. The interface will automatically assign the sensor’s type based on the corresponding pin mode during creation.

Here is a code example:

#include "EQSP32.h"

EQSP32 eqsp32;

void setup() {
    // Initialize EQSP32 module with system ID
    EQSP32Configs myEQSP32Configs;
    myEQSP32Configs.devSystemID = "InteraceTest";

    eqsp32.begin(myEQSP32Configs, true); // Verbose enabled

    // Configure pin 1 as Temperature Input (TIN)
    eqsp32.pinMode(1, TIN);
    eqsp32.configTIN(1, 3988, 10000); // Beta and reference resistance values

    // Create a standard interface for the temperature sensor on pin 1
    createInterface("Temperature Sensor", 1); // Pin 1 is automatically updated
}

void loop() {
    delay(100); // Small delay to allow other tasks to run
}

In this example, we:

  1. Initialize the EQSP32 module with a system ID.
  2. Configure pin 1 as a Temperature Input (TIN) with our specific NTC thermistor parameters.
  3. Create an interface named “Temperature Sensor” for pin 1.

Creating the ADIO pin interface

  • Create the Interface:
    • The createInterface function is used to define a new interface for Home Assistant.The function takes two arguments:
      • The first argument is the name of the interface, which will be displayed in Home Assistant. In this case, it is “Temperature Sensor”.
      • The second argument is the pin number associated with this interface. Here, it is pin 1.
  • Update the Interface’s value:
    • By creating an ADIO interface, the EQSP32 library handles the updating of the temperature value in Home Assistant automatically.
    • Home Assistant will display the readings from the sensor connected to pin 1 as temperature values due to the configured mode of the ADIO pin.

Real-time monitoring in Home Assistant

Let’s take a look at our discovered EQSP32 device.

  • Navigate to Settings, then Devices & Services.
  • Click on Device inside the MQTT integration.
EQSP32 - Din rail mount industrial IoT controller - ESP32 PLC. Example of creating ADIO pin entity for our Home Assistant automation. The respective pin is set in TIN mode for directly converting the analog reading to temperature. The temperature values are automatically updated in Home Assistant.
EQSP32 device discovered in Home Assistant – Monitoring system parameters and ADIO temperature entity

Adding EQSP32 in Home Assistant’s dashboard

Since our device is available, we can now add it to our dashboard as a system or as separate entities. This allows us to integrate it with existing automations to expand its capabilities.

  • Click Add to dashboard.
EQSP32 - Din rail mount industrial IoT controller - ESP32 PLC. Example of creating ADIO pin entity for our Home Assistant automation. We can click Add to dashboard to add the EQSP32 device to our Home Assistant (HA) dashboard for monitoring.
  • Choose the dashboard we want EQSP32 to be added.
EQSP32 - Din rail mount industrial IoT controller - ESP32 PLC. Example of creating ADIO pin entity for our Home Assistant automation. Select the target dashboard for EQSP32 to be added to.
  • Our device is successfully added in our dashboard.
EQSP32 - Din rail mount industrial IoT controller - ESP32 PLC. Example of creating ADIO pin entity for our Home Assistant automation. EQSP32 device successfully added to our desired dashboard view.

Create a custom control interface

In the next example we demonstrate how to create two custom interfaces: one for a button and one for a switch. Both will create control entities in Home Assistant. The button can be used to send commands, while the switch can send commands and monitor the state of the controlled object.

Our goal is for the button interface to toggle a relay on pin 1 and the switch interface to control a relay on pin 2. Additionally, the switch interface will provide feedback on the state of relay 2.

#include "EQSP32.h"

EQSP32 eqsp32;

void setup() {
    // Initialize EQSP32 module with system ID
    EQSP32Configs myEQSP32Configs;
    myEQSP32Configs.devSystemID = "RelayControlV1.0";

    eqsp32.begin(myEQSP32Configs, true); // Verbose enabled

    // Configure pin 1 as Relay
    eqsp32.pinMode(1, RELAY);
    eqsp32.configRELAY(1, 500, 1000); // Default hold value and derate delay

    // Configure pin 2 as Relay
    eqsp32.pinMode(2, RELAY);
    eqsp32.configRELAY(2, 500, 1000); // Default hold value and derate delay

    // Create a custom button interface
    createInterface("Button Command", "button");

    // Create a custom switch interface
    createInterface("Switch State", "switch");
}

void loop() {
    // Handle button interface to toggle relay 1
    if (readInterfaceBOOL("Button Command")) {
        // Toggle the state of relay 1
        if (eqsp32.readPin(1) == 0) {
            eqsp32.pinValue(1, 1000); // Turn on relay 1
        } else {
            eqsp32.pinValue(1, 0); // Turn off relay 1
        }
    }

    // Handle switch interface to control relay 2
    if (readInterfaceBOOL("Switch State")) {
        eqsp32.pinValue(2, 1000); // Turn on relay 2
    } else {
        eqsp32.pinValue(2, 0); // Turn off relay 2
    }

    // Read the state of relay 2
    bool relay2State = eqsp32.readPin(2);

    // Update the switch interface with the state of relay 2
    updateInterfaceBOOL("Switch State", relay2State);

    delay(10); // Small delay to allow other tasks to run
}

Example Explanation

  • Initialization:
    • The EQSP32 module is initialized with a system ID using eqsp32.begin().
    • Verbose mode is enabled for detailed logging.
  • Configure Pins:
    • Pin 1 is configured as a relay using pinMode and configRELAY with default hold value and derate delay.
    • Pin 2 is configured similarly as a relay.
  • Create Custom Interfaces:
    • A custom button interface named “Button Command” is created using createInterface with the type “button”.
    • A custom switch interface named “Switch State” is created using createInterface with the type “switch”.
  • Loop:
    • The loop reads the “Button Command” interface. If it returns true, it toggles the state of relay 1.
    • The loop reads the “Switch State” interface. If it returns true, it turns on relay 2; otherwise, it turns off relay 2.
    • The state of relay 2 is read and updated in the “Switch State” interface.

Real-time control in Home Assistant

For our second example, our device entities look a little bit different. Now that we have added a virtual button and a virtual switch to the interfaces, we see that we have the Controls category added to our device.

By pressing the button’s PRESS command and by toggling the switch’s state, we can now control our loads connected on the relays driven by pin 1 and 2 respectively.

EQSP32 - Din rail mount industrial IoT controller - ESP32 PLC. Example of creating control entities for our Home Assistant automation. We can send command to our EQSP32 automation system by using the virtual button and virtual switch from our Home Assistant (HA) integration.

This enhancement allows for direct interaction with the device through the Home Assistant dashboard, providing real-time control and monitoring of the connected relays.

Conclusion

We have successfully created monitoring interfaces for directly exposing ADIO pins to Home Assistant. Each pin’s value type is automatically characterized based on its mode. Furthermore, we created custom interfaces to control relays with a virtual switch and a virtual button.

It is important to mention that once each entity is available in Home Assistant (HA), it can be used internally for additional integrations and automations within Home Assistant. These entities can be combined with existing automations, sensors, and controls, enhancing the overall system’s functionality.

In the next tutorial, we will demonstrate a slightly more advanced approach, providing even more flexibility in creating custom interfaces by introducing the custom Sensor and BinarySensor types. Stay tuned for more advanced features and enhanced control capabilities.

Add a Comment

Your email address will not be published. Required fields are marked *