Controlling your garage doors with an ESP32 - Part 2

ESP32 - Relay - Sensor wiring

*ESP32 - Relay - Sensor wiring*

In this second post of this series, we’ll see how to connect a couple of magentic switches, one for each door, that will allow us to see if a door is closed or not.

I’m using this one in particular: https://www.adafruit.com/product/375.

This sensor has two wires, one goes to ground (GND) and the second one to an output GPIO pin. In my case I’m using pins 25 and 26 for the left and right door respectively.

Now our schematics will look like this:

ESP32 - Relay - Sensor schematics

*ESP32 - Relay - Sensor schematics*

I also updated the code with the parts to interact with these sensors. All the changes were made in the main/app_main.c file. Here are the relevant snippets:

static const char *LEFT_DOOR_STATUS_TOPIC = "garage/door/left/status";
static const char *RIGHT_DOOR_STATUS_TOPIC = "garage/door/right/status";
...
#define LEFT_DOOR_SENSOR_GPIO 25
#define RIGHT_DOOR_SENSOR_GPIO 26

We added four new constants, two to define the new topics were the sensor will be receiving/sending the traffic and another two to define the pin numbers where we are connecting the sensors.

In the initialization code:

gpio_set_direction(LEFT_DOOR_SENSOR_GPIO, GPIO_MODE_INPUT);
gpio_set_direction(RIGHT_DOOR_SENSOR_GPIO, GPIO_MODE_INPUT);
gpio_pullup_en(LEFT_DOOR_SENSOR_GPIO);
gpio_pullup_en(RIGHT_DOOR_SENSOR_GPIO);

Here we are settng the two GPIO as input mode, since in this case will be reading from them, and also we pull them up.

case MQTT_EVENT_CONNECTED:
    ...
    msg_id = esp_mqtt_client_subscribe(client, LEFT_DOOR_STATUS_TOPIC, 0);
    ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);

    msg_id = esp_mqtt_client_subscribe(client, RIGHT_DOOR_STATUS_TOPIC, 0);
    ESP_LOGI(TAG, "sent subscribe successful, msg_id=%d", msg_id);

In this snippet, once we are connected to the MQTT server, we subscribe to the new topics. And the last thing I added was the code that handles the messages:

if (strcmp(topic, LEFT_DOOR_TOPIC) == 0) {
  ...
} else if (strcmp(topic, RIGHT_DOOR_TOPIC) == 0) {
  ...
} else if (strcmp(topic, LEFT_DOOR_STATUS_TOPIC) == 0) {
    if (strcmp(data, "get") == 0) {
        if (gpio_get_level(LEFT_DOOR_SENSOR_GPIO)) {
            esp_mqtt_client_publish(client, LEFT_DOOR_STATUS_TOPIC, "status:open", 0, 0, 0);
        } else {
            esp_mqtt_client_publish(client, LEFT_DOOR_STATUS_TOPIC, "status:closed", 0, 0, 0);
        }
    }
} else if (strcmp(topic, RIGHT_DOOR_STATUS_TOPIC) == 0) {
    if (strcmp(data, "get") == 0) {
        if (gpio_get_level(RIGHT_DOOR_SENSOR_GPIO)) {
            esp_mqtt_client_publish(client, RIGHT_DOOR_STATUS_TOPIC, "status:open", 0, 0, 0);
        } else {
            esp_mqtt_client_publish(client, RIGHT_DOOR_STATUS_TOPIC, "status:closed", 0, 0, 0);
        }
    }
}

Here as you see we are responding only when we receive the get payload or message. Then we read the sensor gpio_get_level(….), and if the result is true (not equal to 0), then we send back to the topic the message status:open or status:closed otherwise.

Here is a little video showing this functionality with just one sensor:

If you missed part one of this series, here you have Part 1

Esta entrada también está disponible en español.