Temp-Humidity Sensor on the Cheap - DHT22, ESPHOME, Wemos D-1 Mini
I picked up some DHT22 and Wemos D-1 Minis for a bathroom-vent project - if it gets too humid from the shower, turn on the fans. When the humidity drops, turn off the fans.
This is the story on how they were actually used. A kitten-based project.
Frida developed dry coughs this past week. Environmentally, nothing had changed. Food was normal, litter box normal, no chaos. We took her to the vet - no lungworm, no asthma, no cancer in the xrays. Maybe the house is just too dry?
Step One: Find the components
DHT-22 Sensors - I bought some "HiLetgo 2pcs DHT22/AM2302 Digital Temperature And Humidity Sensor Module" from Amazon. These have three connections: power, sensor, and ground. Some other DHT-22's have 4...one apparently is just not connected. The AM2302's have an advantage later as we will discover, but will an extra line of code. Came with appropriate female-female wires.
Wemos D-1 Mini: I found 5 for $16 on Amazon. Not the actual Wemos brand, but they worked. They came with pins which needed to be soldered. (Use the set that will allow the female wire to plug into the male header pins.)
You will also need a USB-USB-micro cable, and some inexpensive USB plug to power the device after the install.
Step Two: Install ESPHome
Go to Settings>Add-Ons>Add-On Store. Once it's installed, I moved all of the sliders over - Show in sidebar, Auto update, Watchdog.
Step Three: Create the Sensor in ESPHome
Click on the "NEW DEVICE" button, and click continue. I named my sensor d1-mini-th-1, as I was going to be moving it around, instead of tied to one place. The required "Network name" and password are required, but may be deleted later, depending on your HA instance. Go ahead and tell it the truth. On the next screen "Pick specific board" and select "Wemos D1 and Wemos D1 mini". We're not ready to install quite yet, so click "Skip".
Step Four: Wire up the Wemos D1 mini
You've soldered the pins to the D1 mini, so that the longer pins are sticking away from the board. Attach the three wires to the sensor, then wire the "+" from the sensor to the 3.3V (3V3) on the D1 mini. Wire the "-" from the sensor to the G(round) on the D1 mini. Wire the middle "out" from the sensor to the D2 position on the D1 mini.
The DHT-22, model AM2302 |
Red wire from the sensor "+" to 3V3 on the D1 Mini |
From the "-" on sensor to "G"; from the "out" on the sensor to "D2" pin |
Step Five: Clean up Secrets
In ESPHome, click on Secrets, in the upper left. For me, it shows two wifi_ssid & password entries. The top one, is an entry I made for the secrets.yaml file for my earlier projects. The second one was generated when I added that sensor in Step Three - the mandatory one I could not skip.
If you didn't already use secrets, you'll just have the one - great. If you have more than one entry, it will cause errors when you try to install to the D1 mini.
Step Six: Edit the D1 Mini sensor in ESPHome
"EDIT" on the unused sensor. Under "wifi", make sure it's using whatever wifi_ssid and wifi_psk/password/etc from that previous secrets screen. Next, add the DHT-22 sensor. At the end of the file, add these lines of code. (Most of it is found at esphome.io, for the DHT-22.) https://esphome.io/components/sensor/dht.html
sensor:
- platform: dht
pin: D2
model: AM2302
temperature:
name: "Temperature-1"
humidity:
name: "Humidity-1"
update_interval: 60s
The pin is where the data is coming in from your sensor. The model ties back into Step One - it has a built-in resistor which means your wiring is simpler, but if you don't include the model line, the temperatures will be incorrect. (My sensors reported 40 degrees Fahrenheit colder, without that line.)
You'll need the encryption key on line 13 later, for Home Assistant.
Click SAVE.
Step Seven: Install the file to the D1 Mini
Click INSTALL from that same page, or the page where the sensor is listed. As of this writing, there are four options. Wirelessly (which we can't do yet), Plug into this computer (which can be challenging), Plug into the computer running ESPHome Dashboard (which in this case was my RPi3 running Home Assistant), or manual download.
I attached the D1 mini to the RPi3, after several failed attempts at installing from my Ubuntu laptop. Clicking on "Plug into the computer running ESPHome Dashboard", it saw the D1 Mini as "Serial-2", and started the install process.
And it failed. At the time, I was doing some other projects on home assistant, so I stopped using those, and used re-try. (If you have anything else plugged into the USB ports on the your RPi, consider removing those.) It would have ALSO failed to even start installing, if you have overlapping wifi_ssid entries in your secrets file, but that was fixed in Step Five.
It installed successfully on my second attempt.
Step Eight: Enable the D1 Mini in Home Assistant
Grab the encryption key from your D1 mini sensor in ESPHome. Under Settings>Devices & Services>Integrations, you should see your new sensor, with a "CONFIGURE" box. Click Configure, then paste your encryption key in the single field on the next page.
Step Nine: Tying it in
On the Integrations page, your sensor is now under the "ESPHome" integration. Listed as one device with two entities, you can drop those into automations or a dashboard.
The sticking points: Installing to D1 and Incorrect Temperatures
Install to D1 was messy. First, creating new entries in my secrets.yaml file, which then caused the install to choke - not an ideal situation. Second, it wouldn't install from my laptop, and the ESPHome webpage option would not work with Firefox (and failed to install the time I attempted it with Chrome).
Comments
Post a Comment