{"id":768,"date":"2019-08-11T10:48:30","date_gmt":"2019-08-11T00:18:30","guid":{"rendered":"http:\/\/parablog-wordpress.dockerbox.rei.moe\/?p=768"},"modified":"2019-08-11T10:49:21","modified_gmt":"2019-08-11T00:19:21","slug":"updating-my-last-arduino-based-esp-to-esphome","status":"publish","type":"post","link":"https:\/\/blog.lewys.eu\/?p=768","title":{"rendered":"Updating my last Arduino based ESP to ESPHome"},"content":{"rendered":"\n<p>I have most of my ESP based IoT devices running ESPHome by now, but there was one left that I hadn&#8217;t spent the time figuring out how to adapt.<\/p>\n\n\n\n<p>That was the one that lives inside my wall, and opens the buildings door by shorting two contacts on the intercom system.<\/p>\n\n\n\n<p>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!<\/p>\n\n\n\n<p>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.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;ESP8266WiFi.h>\n#include &lt;PubSubClient.h>\n\n#define CLIENT_ID \"buildingdoor-singlerelay\"\n\n#define RELAY_PIN 0\n\n\n\/\/ Update these with values suitable for your network.\nconst char* ssid = \"WiFiSSID\";\nconst char* password = \"WiFiPassword\";\nconst char* mqtt_server = \"MQTTServerIP\";\n\nWiFiClient espClient;\nPubSubClient client(espClient);\nlong lastMsg = 0;\n\nvoid setup()\n{\n  pinMode(LED_BUILTIN, OUTPUT);\n  pinMode(RELAY_PIN, OUTPUT);\n  Serial.begin(115200);\n  setup_wifi(); \n  client.setServer(mqtt_server, 1883);\n  client.setCallback(callback);\n  digitalWrite(RELAY_PIN, HIGH);\n  pinMode(D3, OUTPUT);\n  pinMode(D1, OUTPUT);\n  digitalWrite(D1, LOW);\n}\n\nvoid setup_wifi() {\n  delay(10);\n  \/\/ We start by connecting to a WiFi network\n  Serial.println();\n  Serial.print(\"Connecting to \");\n  Serial.println(ssid); \/\/We don't want the ESP to act as an AP\n  WiFi.mode(WIFI_STA);\n  WiFi.begin(ssid, password);\n\n  while (WiFi.status() != WL_CONNECTED) \n  {\n    delay(500);\n    Serial.print(\".\");\n  }\n  Serial.println(\"\");\n  Serial.println(\"WiFi connected\");\n  Serial.println(\"IP address: \");\n  Serial.println(WiFi.localIP());\n}\n\nvoid callback(char* topic, byte* payload, unsigned int length) {\n  Serial.print(\"Message arrived [\");\n  Serial.print(topic);\n  Serial.print(\"] \");\n  for (int i = 0; i &lt; length; i++) {\n    Serial.print((char)payload[i]);\n  }\n  Serial.println();\n\n  \/\/ Switch on the LED if an 1 was received as first character\n  if ((char)payload[0] == '1') {\n    client.publish(\"building\/door\/relay\", \"1\");\n    delay(100);\n    digitalWrite(RELAY_PIN, LOW);\n    delay(200);\n    digitalWrite(RELAY_PIN, HIGH);\n  }  } else {\n    digitalWrite(RELAY_PIN, HIGH);\n    delay(100);\n    client.publish(\"building\/door\/relay\", \"0\");\n  }\n\n}\n\nvoid reconnect() {\n  \/\/ Loop until we're reconnected\n  digitalWrite(LED_BUILTIN, LOW);\n  while (!client.connected()) {\n    Serial.print(\"Attempting MQTT connection...\");\n    \/\/ Attempt to connect\n    if (client.connect(CLIENT_ID)) {\n      Serial.println(\"connected\");\n      client.subscribe(\"building\/door\/relay\/set\");\n      digitalWrite(LED_BUILTIN, HIGH);\n    } else {\n      Serial.print(\"failed, rc=\");\n      Serial.print(client.state());\n      Serial.println(\" try again in 5 seconds\");\n      \/\/ Wait 5 seconds before retrying\n      delay(5000);\n    }\n  }\n}\n\nvoid loop()\n{\n  if (!client.connected()) {\n    reconnect();\n  }\n  client.loop();\n}<\/code><\/pre>\n\n\n\n<p>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! <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>esphome:\n  name: buildingdoor\n  platform: ESP8266\n  board: esp01_1m\n\nwifi:\n  ssid: 'WiFiSSID'\n  password: 'WiFiPassword'\n  domain: .local\n  fast_connect: true\n  manual_ip:\n    static_ip: 172.16.0.XX\n    gateway: 172.16.0.1\n    subnet: 255.255.255.0\nmqtt:\n  broker: 172.16.0.XX\n  username: MQTTUsername\n#  password: MyMQTTPassword\n  on_message:\n    topic: building\/door\/relay\/set\n    then:\n      - switch.turn_on: building_door_switch\napi:\n\n# Enable logging\nlogger:\n\nota:\n\nswitch:\n  - platform: gpio\n    pin:\n      number: 0\n      inverted: yes\n    icon: \"mdi:office-building\"\n    name: \"Building Door Open Switch\"\n    id: building_door_switch\n    retain: false\n    discovery: false\n    availability:\n      topic: building\/door\/status\n      payload_available: online\n      payload_not_available: offline\n    state_topic: building\/door\/relay\n    command_topic: building\/door\/relay\/set\n    on_turn_on:\n    - logger.log: \"Building Door Relay Activated!\"\n    - delay: 0.2s\n    - switch.turn_off: building_door_switch\n    on_turn_off:\n    - logger.log: \"Building Door Relay Deactivated!\"\n\nsensor:\n  - platform: wifi_signal\n    name: \"Building Door WiFi Signal\"\n    update_interval: 60s\ntext_sensor:\n  - platform: wifi_info\n    ip_address:\n      name: Building Door ESP IP Address\n    ssid:\n      name: Building Door ESP Connected SSID\n    bssid:\n      name: Building Door ESP Connected BSSID<\/code><\/pre>\n\n\n\n<p>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!<\/p>\n\n\n\n<p><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"1440\" height=\"1909\" src=\"https:\/\/i2.wp.com\/blog.lewys.eu\/wp-content\/uploads\/2019\/08\/Screenshot_20190811-100320_Photos.jpg?fit=660%2C875&amp;ssl=1\" alt=\"\" class=\"wp-image-772\" srcset=\"https:\/\/blog.lewys.eu\/wp-content\/uploads\/2019\/08\/Screenshot_20190811-100320_Photos.jpg 1440w, https:\/\/blog.lewys.eu\/wp-content\/uploads\/2019\/08\/Screenshot_20190811-100320_Photos-226x300.jpg 226w, https:\/\/blog.lewys.eu\/wp-content\/uploads\/2019\/08\/Screenshot_20190811-100320_Photos-768x1018.jpg 768w, https:\/\/blog.lewys.eu\/wp-content\/uploads\/2019\/08\/Screenshot_20190811-100320_Photos-772x1024.jpg 772w\" sizes=\"auto, (max-width: 1440px) 100vw, 1440px\" \/><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I have most of my ESP based IoT devices running ESPHome by now, but there was one left that I hadn&#8217;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 &hellip; <a href=\"https:\/\/blog.lewys.eu\/?p=768\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Updating my last Arduino based ESP to ESPHome<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20,23,44,41,19,22],"tags":[],"class_list":["post-768","post","type-post","status-publish","format-standard","hentry","category-arduino-2","category-esp8266","category-esphome","category-home-assistant","category-home-automation-2","category-reference"],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=768"}],"version-history":[{"count":4,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/768\/revisions"}],"predecessor-version":[{"id":773,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=\/wp\/v2\/posts\/768\/revisions\/773"}],"wp:attachment":[{"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.lewys.eu\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}