ESPHome with Existing Arduino Programs

I would love to implement Fire effect from BRUH led strip, I’m using it when turning on heating. I cannot figure out how to add that effect…

I am trying also to move from a Arduino code (made by someone else) to ESPHOME, which is really great and practical, I love it.

I solved it with SWITCH, but its not optimal since I need a LOCK, I was wondering if/how to use a binary sensor instead of SWITCH for LOCK and another SWITCH for UNLOCK

Below the Arduino code, and my first attempt with SWITCH in ESPhome

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

#define MQTT_VERSION MQTT_VERSION_3_1_1

// Wifi: SSID and password
const char* WIFI_SSID = "*****";
const char* WIFI_PASSWORD = "*****";

// MQTT: ID, server IP, port, username and password
const PROGMEM char* MQTT_CLIENT_ID = "DoorLock";
const PROGMEM char* MQTT_SERVER_IP = "*****";
const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
const PROGMEM char* MQTT_USER = "******";
const PROGMEM char* MQTT_PASSWORD = "*********";

// MQTT: topics
const char* MQTT_LOCK_STATE_TOPIC = "stat/home/door";
const char* MQTT_LOCK_COMMAND_TOPIC = "cmnd/home/door";

// payloads by default (on/off)
const char* UNLOCK = "UNLOCK";
const char* LOCK = "LOCK";

const PROGMEM uint8_t LOCK_PIN = 5;
boolean lock_state = false; 

WiFiClient wifiClient;
PubSubClient client(wifiClient);

// function called to publish the state of the light (on/off)
void publishLightState() {
  if (lock_state) {
    client.publish(MQTT_LOCK_STATE_TOPIC, UNLOCK, true);
  } else {
    client.publish(MQTT_LOCK_STATE_TOPIC, LOCK, true);
  }
}

// function called to turn on/off the light
void setLockState() {
  if (lock_state) {
    digitalWrite(LOCK_PIN, HIGH);
    delay(100);
    digitalWrite(LOCK_PIN, LOW);
    delay(100);
    digitalWrite(LOCK_PIN, HIGH);
    delay(100);
    digitalWrite(LOCK_PIN, LOW);
    delay(100);
    digitalWrite(LOCK_PIN, HIGH);
    delay(100);
    Serial.println("INFO: Door is unlocked...");
  } else {
    digitalWrite(LOCK_PIN, HIGH);
    delay(100);
    digitalWrite(LOCK_PIN, LOW);
    delay(100);
    digitalWrite(LOCK_PIN, HIGH);
    Serial.println("INFO: Door is locked...");
  }
}

// function called when a MQTT message arrived
void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  // concat the payload into a string
  String payload;
  for (uint8_t i = 0; i < p_length; i++) {
    payload.concat((char)p_payload[i]);
  }
  
  // handle message topic
  if (String(MQTT_LOCK_COMMAND_TOPIC).equals(p_topic)) {
    if (payload.equals(String(UNLOCK))) {
        lock_state = true;
        setLockState();
        publishLightState();
    } else if (payload.equals(String(LOCK))) {
        lock_state = false;
        setLockState();
        publishLightState();
    }
  }
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("INFO: Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
      Serial.println("INFO: connected");
      // Once connected, publish an announcement...
      publishLightState();
      // ... and resubscribe
      client.subscribe(MQTT_LOCK_COMMAND_TOPIC);
    } else {
      Serial.print("ERROR: failed, rc=");
      Serial.print(client.state());
      Serial.println("DEBUG: try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}

void setup() {
  // init the serial
  Serial.begin(115200);

  // init the led
  pinMode(LOCK_PIN, OUTPUT);
  digitalWrite(LOCK_PIN, HIGH);
  analogWriteRange(255);
  setLockState();

  // init the WiFi connection
  Serial.println();
  Serial.println();
  Serial.print("INFO: Connecting to ");
  WiFi.mode(WIFI_STA);
  Serial.println(WIFI_SSID);
  WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("INFO: WiFi connected");
  Serial.print("INFO: IP address: ");
  Serial.println(WiFi.localIP());

  // init the MQTT connection
  client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  client.setCallback(callback);
}

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

.
esphome:
name: 14b_office_door
platform: ESP8266
board: nodemcuv2

wifi:
  ssid: "xx"
  password: "xx"
  manual_ip:
    static_ip: 192.168.1.189
    gateway: 192.168.1.239
    subnet: 255.255.255.0 
# Enable logging
logger:

# Enable Home Assistant API
api:

ota:


web_server:
  port: 80

switch:
  - platform: gpio
    pin:
      number: D0
      inverted: yes
    id: relay
  - platform: template
    name: "open_14b_office_door"
    icon: "mdi:gate"
    turn_on_action:
    - switch.turn_on: relay
    - delay: 500ms
    - switch.turn_off: relay
  - platform: template
    name: "close_14b_office_door"
    icon: "mdi:gate"
    turn_on_action:
    - switch.turn_on: relay
    - delay: 500ms
    - switch.turn_off: relay
    - delay: 100ms
    - switch.turn_on: relay
    - delay: 500ms
    - switch.turn_off: relay

sensor:
  - platform: wifi_signal
    name: "14b BLE Bridge WiFi Signal"
    update_interval: 60s
  - platform: uptime
    name: "14b BLE Bridge Uptime"
    
text_sensor:
  - platform: version
    name: "14b ble bridge ESPHome Version"

Do you have an updated link to this code? I’m trying to do the same thing with converting BRUH’s Arduino on a NodeMCU to ESPHome.

esphome:
  name: nodemculeds
  platform: ESP8266
  board: nodemcuv2

wifi:
  ssid: 'xxxx'
  password: 'xxxx'
  domain: .xxxx
  manual_ip:
    static_ip: 172.16.0.80
    gateway: 172.16.0.1
    subnet: 255.255.255.0

api:

# Enable logging
logger:

ota:

light:
  - platform: fastled_clockless
    chipset: WS2812
    pin: D5
    num_leds: 29
    rgb_order: GRB
    name: "NodeMCU"
    effects:
      - addressable_flicker:
          name: Flicker
          update_interval: 16ms
          intensity: 5%
      - addressable_fireworks:
          name: Fireworks
          update_interval: 32ms
          spark_probability: 10%
          use_random_color: false
          fade_out_rate: 120
      - addressable_random_twinkle:
          name: Random Twinkle
          twinkle_probability: 5%
          progress_interval: 32ms
      - addressable_twinkle:
          name: Twinkle
          twinkle_probability: 5%
          progress_interval: 4ms
      - addressable_scan:
          name: Scan Effect With Custom Values
          move_interval: 100ms
      - addressable_color_wipe:
          name: Color Wipe
          colors:
            - red: 100%
              green: 100%
              blue: 100%
              num_leds: 1
            - red: 0%
              green: 0%
              blue: 0%
              num_leds: 1
          add_led_interval: 100ms
          reverse: False
      - addressable_rainbow:
          name: Rainbow
          speed: 10
          width: 50
      - strobe:
          name: Strobe
          colors:
            - state: True
              brightness: 100%
              red: 100%
              green: 100%
              blue: 100%
              duration: 50ms
            - state: True
              brightness: 100%
              red: 0%
              green: 0%
              blue: 0%
              duration: 50ms
      - strobe:
          name: Notify Red
          colors:
            - state: True
              brightness: 100%
              red: 100%
              green: 10%
              blue: 10%
              duration: 100ms
            - state: True
              brightness: 0%
              duration: 3000ms
      - strobe:
          name: Notify Green
          colors:
            - state: True
              brightness: 100%
              red: 0%
              green: 100%
              blue: 0%
              duration: 100ms
            - state: True
              brightness: 0%
              red: 0%
              green: 0%
              blue: 0%
              duration: 100ms
            - state: True
              brightness: 100%
              red: 0%
              green: 100%
              blue: 0%
              duration: 100ms
            - state: True
              brightness: 0%
              red: 0%
              green: 0%
              blue: 0%
              duration: 3000ms
      - strobe:
          name: Notify Blue
          colors:
            - state: True
              brightness: 100%
              red: 10%
              green: 10%
              blue: 100%
              duration: 100ms
            - state: True
              brightness: 0%
              red: 0%
              green: 0%
              blue: 0%
              duration: 3000ms
      - strobe:
          name: Notify White
          colors:
            - state: True
              brightness: 100%
              red: 100%
              green: 100%
              blue: 100%
              duration: 100ms
            - state: True
              brightness: 0%
              red: 0%
              green: 0%
              blue: 0%
              duration: 100ms
            - state: True
              brightness: 100%
              red: 100%
              green: 100%
              blue: 100%
              duration: 100ms
            - state: True
              brightness: 0%
              red: 0%
              green: 0%
              blue: 0%
              duration: 3000ms
      - random:
          name: Random Colour
          transition_length: 5s
          update_interval: 7s
3 Likes

Thank you!!! That worked perfectly!

1 Like