Monitor 3d Printer using Home assistant and ESPHome

iot journy start here

Introduction

Recently I’ve made a poor guy 3d printer enclosure, But I don’t have confident to leave the room while the printer is working, I need to keep my eyes on it, So to make the solution more practical I’ve decided to rig a very basic system to monitor humidity and temperature just to take action whenever the temperature go out of normal, I also wanted to be able to switch light and camera on an off remotely which mean a WiFi capability is a must, and because the best system is the one you already have, I finally found a good use for my Raspberry pi 4, only needed ESP32-CAM and some sensors.

If you are a visual learner here is a youtube video with a recorded step by step instruction:

THINGS NEEDED:

Before diving into details, you may want to know the things needed for this project:

INSTALLING HOME ASSISTANT

Home Assistant is a central system to organize the communication between sensors, There is a whole lot of lessons out there explaining Home Assistant and ESPHome in much detail, but I wanted this lesson to be self contained with the least amount of external resources.

Home Assistant documentation offers multiple methods for installation process, but we will install it as a stand-alone operating system which is the easiest method and also recommended by the documentation itself.

For this we need balnea-etcher program, download and install the version that is compatible with your operating system, and follow the following steps:

  • from releases section on github, download the latest version for Raspberry pi 3 or 4, I’ve selected RaspberryPi 4 64 bit version
  • copy the link
  • open balena-etcher program click Flash from URL paste the link
  • attach a microSD to your computer
  • click select target and choose your microSD card,
  • click FLash
  • DONE

Wait until the process complete, when done, put the microSD back to its specific slot on Raspberry pi, connect ethernet cable, plug the PI to the power using USB Cable, wait until boot process complete then it’s ready to be accessed remotely.

Setup Wifi network (Optional)

if you have problem with cables laying around all over your home, then definitely you need to setup a wifi instead of using ethernet, for that you need a usb flash drive formatted as fat32, NTFS, or ext4, and labeled CONFIG with all capital letters, plug it to your computer, and:-

  • in the root folder create a folder named network
  • inside it create a text file with the name my-network
  • open this file with any text editor, and paste the following text inside it
[connection]
id=my-network
uuid=72111c67-4a5d-4d5c-925e-f8ee26efb3c3
type=802-11-wireless

[802-11-wireless]
mode=infrastructure
ssid=MY_SSID
# Uncomment below if your SSID is not broadcasted
#hidden=true

[802-11-wireless-security]
auth-alg=open
key-mgmt=wpa-psk
psk=MY_SECRET_KEY

[ipv4]
method=auto

[ipv6]
addr-gen-mode=stable-privacy
method=autoCP2102 (square chip): driver

change MY_SSID to the name of your network, and change MY_SECRET_KEY with the password of your network.

Plug the flash drive to one of usb port on Raspberry pi, power it, and if the configuration is good it will connect to the wifi network, it’s now ok to unplug the flash drive.

Starting/Configuring Home Assistant

Before trying to access Home Assistant using ip address we will try to use the hostname that usually associated for it, try either:

  • homeassistant.local:8123, or
  • homeassistant:8123

if Home assistant interface displayed, then it’s all good, otherwise we need to find the ip address of the raspberry pi in our home network, and that’s done either by logging to our network router if we have access to it, or using ip scanners such as AngryIP which will display ip addresses for all devices connected to the same network.

In the first run the Home Assistant will take some time to do background configuration, after it complete, it’s now our turn to configure it by providing some essential information such as username, password, unit system, and your location, and will also ask you to provide analytic information if you’d like to do so.

In the next step Home Assistant will explore your home network to find any smart devices that already connected, and ask you if you want to add them to Home Assistant, after finishing all steps click Finish.

Installing ESPHome platform in home assistant

ESPHome come in two parts, a platform and a firmware, the firmware is the part that installed in the ESP32-cam itself and the platform is the part that is used to receive the signal from the ESP32-cam and also used to configure and push update to it, one way of installing ESPHome platform is to install it as an addon inside Home Assistant, and this exactly what we are going to do in this lesson.

  • From home-assistant interface click Supervisor, then Add-on Store.
  • in the search section, search for ESPHome, then click Install.
  • Wait till installation complete, enable watchdog then click start.
  • when started click OPEN WEB UI

Install ESPHome firmware to ESP32-CAM

Because esp32-cam has no on-board programmer, it need an external programmer to burn the esphome firmware, we could use the widely available programmers ics such as (CH341, CP2102), but in this lesson we will use an Arduino Uno, as it is more likely that you have one around. Using jumper cable, you need to make several connections between Arduino and ESP32-cam, as it appear in the following diagram.

ESP32-CAM arduino wiring

From ESPHome interface click the green button ADD NODE, in the dialogue type whatever name you like I’ve used 3dp, enter your WiFi name, and its password, then click Next.

From board selection screen leave it on ESP32 choice, then click Close.

A new Entry appeared, which has a long red line, indicating no connection between ESPHome platform and ESP32-CAM, To convert this line to online we need to burn the firmware to esp32-cam board.

Plug a usb cable between Arduino and Raspberry pi, click Install, choose the third option, which state Plug into the computer running ESPHome Dashboard, if all connections is good click on Arduino port that show in the screen and the installation process will start.

In Case firmware installation process failed you may want to press RST button in esp32-cam board.

When the installation process completed, be sure to disconnect the GPIO0 pin from GND in ESP32-Cam board, then press RST button, give it a while and you will see the red line in esphome platform converted into online indicating that your ess32-cam is now online and you will notice the notification icon in bottom left has a notification that a new device is discovered.

To integrate the newly added device to HOME ASSISTANT, go to Configuration in the left bar which has gear icon, then go to Integrations you will find new discovered device with the name 3dp, click CONFIGURE button, then click SUBMIT, choose the area if you would like then click FINISH.

From now on Arduino is no longer needed because any update can be pushed to esp32-cam remotely using wifi only, and it can be disconnected from arduino, just give it 5V and GND from any external power source, but for the sake of simplicity we will keep Arduino as a power source only.

Add Temperature and Humidity sensor (DHT11)

The good thing about ESPHome its comprehensive library of sensors, just a little search in their website and definitely will find what you want at least if it’s one of the popular sensors, in our case we need to integrate DHT11 temperature and humidity sensor in our project, a detailed instruction already available at their website with a sample of configuration.

Before doing any configuration we need to connect DHT11 sensor to ESP32-CAM as shown in the diagram, we simply connect the right-most pin of the sensor to GND pin in ESP32-CAM and the left-most pin of the sensor to 5V pin, connect data pin which is the second pin from left to the pin labeled IO13 in ESP32-CAM, we don’t need to forget the pull-up resistor with value 4.7k ohm between data pin and power pin of DHT11 sensor, and leave the remaining pin of unconnected.

ESP32-CAM DHT11 wiring

Now it’s time to do some configuration, from ESPHome official website search for DHT11, multiple search results will appear, click the link related to the same sensor, copy the example configuration code which also shown bellow, go to ESPHome platform and click EDIT, paste the code to the bottom of configuration, change the PIN: D2 to match the pin that DHT11 connected to, in our case it’s GPIO13, click SAVE then INSTALL, from the installation method menu, choose Wirelessly, and the installation process will start, just wait till it finish.

# Example configuration entry
sensor:
  - platform: dht
    pin: GPIO13
    temperature:
      name: "Living Room Temperature"
    humidity:
      name: "Living Room Humidity"
    update_interval: 10s

if everything goes well, you will see a green SUCCESS message and Uploading Progress bar, after it finish click STOP.

Now we need to add our newly created sensor to Home Assistant interface, from left bar select Overview which is the icon with four squares, click the three dot icon at upper right corner, then click Edit Dashboard, click + ADD CARD, choose BY ENTITY tab, from the menu choose the two entries related to DHT11 sensor, which is Living Room Temperature, and Living Room Humidity, click Continue, then select ADD TO LOVELACE UI, exit the dashboard editing mode by clicking the X at upper left corner.

You’ll see a card showing current temperature and humidity, and updated every 10 seconds.

Add LED Strip

The process of adding LED Strip is almost similar to the process we follow when adding DHT11, here we have and LED Strip of kind WS2812B, as shown in the diagram we make a connection between ESP32-CAM and LED Strip, 5V to 5V, GND to GND, Data line to pin GPIO12 in.

ESP32-CAM WS2812B wiring

Search for WS2812B in ESPHome Website, multiple results will show, choose FastLED Light, copy the configuration example form its page, then paste it at the bottom of our device configuration entry, the only thing we need to change is the pin number which we set it here to be GPIO12, we prefer to change name to match our LED Strip kind FastLED WS2812B Light, Click SAVE then INSTALL, also choose wirelessly to do installation through WiFi.

# Example configuration entry
light:
  - platform: fastled_clockless
    chipset: WS2812B
    pin: GPIO12
    num_leds: 30
    rgb_order: BRG
    name: "FastLED WS2811 Light"

To add the LED Strip control card to Home Assistant, we do the same thing again, Overview -> Edit Dashboard -> ADD CARD -> BY ENTITY -> select FastLED WS2812B Light -> CONTINUE -> ADD TO LOVELACE UI, done.

From this control card, you can turn LED on/off, change the brightness, change the color, and also there is a plenty of light effect available at ESPHome website under the Light Component page, but I’ll leave it as a home-work for you to figure it out, or watch the video above to see how.

Add ESP32-CAM Camera

It’s time now to add camera, because we only need the camera whenever we want to investigate the 3d printer it will remain idle, and only activated when we log to our Home Assistant and click on it’s thumbnail, since the camera already integrated on ESP32 board itself no need to do any connection,just setting some configuration entry.

From the link, or searching for camera on ESPHome website, will take you to the same page, from that page copy the configuration for AI-Thinker model, which is the model that we have.

# Example configuration entry
esp32_camera:
  external_clock:
    pin: GPIO0
    frequency: 20MHz
  i2c_pins:
    sda: GPIO26
    scl: GPIO27
  data_pins: [GPIO5, GPIO18, GPIO19, GPIO21, GPIO36, GPIO39, GPIO34, GPIO35]
  vsync_pin: GPIO25
  href_pin: GPIO23
  pixel_clock_pin: GPIO22
  power_down_pin: GPIO32

  # Image settings
  name: My Camera

Paste it to the bottom of configuration file, SAVE and INSTALL, then add the camera to Home Assistant Lovelace using the same previous method, Overview -> Edit Dashboard -> ADD CARD -> BY ENTITY -> select My Camera -> CONTINUE -> ADD TO LOVELACE UI, done.

Add esp32-cam on-board Flash LED

ESP32-CAM has on-board flash LED, it can be used as an extra light source when needed it will give a considerable amount of light when needed but should be used briefly because of the extra amount of heat it will give, just add the following configuration entry at the bottom of your configuration file:

# Flash Switch configuration entry
output:
  - platform: gpio
    pin: 4
    id: 'generic_out'
switch:
  - platform: output
    name: "Flash Switch"
    output: 'generic_out'

and no need to explain how to add it to Home Assistant Lovelace, only add the new entry with the name Flash Switch.

Design and 3d print an enclosure

To this point the project should be done, but I won’t be satisfied without adding some aesthetic touches to it. I’ve designed a simple 3d printed enclosure to fit all components in one place, most of the parts are designed to be friction fit. To avoid any overheat issue I’ve attached a heat sink to the ESP32 chip, and also a hole on the lid for a cooling fan, (there is a model without fan mount) To guarantee stability and flexibility the enclosure attached to an articulated arm make to positioning easier, and it has a base that could be stabilized with a weight. You can download the stl files for enclosure from printables

Finished Project


اقرأها بالعربية

مراقبة طابعة الثري دي