Updating my last Arduino based ESP to ESPHome

I have most of my ESP based IoT devices running ESPHome by now, but there was one left that I hadn’t spent the time figuring out how to adapt.

That was the one that lives inside my wall, and opens the buildings door by shorting two contacts on the intercom system.

My requirements for this were backwards compatibility so that my scripts and automations that were setup to control the door via MQTT could still function, this way I can continue to operate as normal but with the addition of OTA updates and telemetry from the node thats hardest to reach!

The original code involved subscribing to a topic, and waiting for a payload, then turning the relay on and then off again, for a momentary press.

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

#define CLIENT_ID "buildingdoor-singlerelay"

#define RELAY_PIN 0


// Update these with values suitable for your network.
const char* ssid = "WiFiSSID";
const char* password = "WiFiPassword";
const char* mqtt_server = "MQTTServerIP";

WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(RELAY_PIN, OUTPUT);
  Serial.begin(115200);
  setup_wifi(); 
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
  digitalWrite(RELAY_PIN, HIGH);
  pinMode(D3, OUTPUT);
  pinMode(D1, OUTPUT);
  digitalWrite(D1, LOW);
}

void setup_wifi() {
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid); //We don't want the ESP to act as an AP
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);

  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived [");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();

  // Switch on the LED if an 1 was received as first character
  if ((char)payload[0] == '1') {
    client.publish("building/door/relay", "1");
    delay(100);
    digitalWrite(RELAY_PIN, LOW);
    delay(200);
    digitalWrite(RELAY_PIN, HIGH);
  }  } else {
    digitalWrite(RELAY_PIN, HIGH);
    delay(100);
    client.publish("building/door/relay", "0");
  }

}

void reconnect() {
  // Loop until we're reconnected
  digitalWrite(LED_BUILTIN, LOW);
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(CLIENT_ID)) {
      Serial.println("connected");
      client.subscribe("building/door/relay/set");
      digitalWrite(LED_BUILTIN, HIGH);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void loop()
{
  if (!client.connected()) {
    reconnect();
  }
  client.loop();
}

The new ESPHome code, does this, in addition to supporting the Home Assistant API and reporting back some values such as the WiFi Signal Strength!

esphome:
  name: buildingdoor
  platform: ESP8266
  board: esp01_1m

wifi:
  ssid: 'WiFiSSID'
  password: 'WiFiPassword'
  domain: .local
  fast_connect: true
  manual_ip:
    static_ip: 172.16.0.XX
    gateway: 172.16.0.1
    subnet: 255.255.255.0
mqtt:
  broker: 172.16.0.XX
  username: MQTTUsername
#  password: MyMQTTPassword
  on_message:
    topic: building/door/relay/set
    then:
      - switch.turn_on: building_door_switch
api:

# Enable logging
logger:

ota:

switch:
  - platform: gpio
    pin:
      number: 0
      inverted: yes
    icon: "mdi:office-building"
    name: "Building Door Open Switch"
    id: building_door_switch
    retain: false
    discovery: false
    availability:
      topic: building/door/status
      payload_available: online
      payload_not_available: offline
    state_topic: building/door/relay
    command_topic: building/door/relay/set
    on_turn_on:
    - logger.log: "Building Door Relay Activated!"
    - delay: 0.2s
    - switch.turn_off: building_door_switch
    on_turn_off:
    - logger.log: "Building Door Relay Deactivated!"

sensor:
  - platform: wifi_signal
    name: "Building Door WiFi Signal"
    update_interval: 60s
text_sensor:
  - platform: wifi_info
    ip_address:
      name: Building Door ESP IP Address
    ssid:
      name: Building Door ESP Connected SSID
    bssid:
      name: Building Door ESP Connected BSSID

I first connected it externally next to the wall, and listened to hear that they indeed, both fired when they were supposed to, and once confirmed, I opened up the wall, switched out the ESP-01S modules and closed it all back up!

Leave a Reply

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