Introduction: ESP8266 Based Temperature Sensor Connected to Home Assistant and MQTT

About: All round geek, love tinkering with microcontrollers. pcs. and just making stuff

In this instructable I will show you how to make room sensor to monitor temperature and humidity and with an additional sensor to monitor water temperature in an aquarium.

We will then set it up to work with Home assistant through MQTT

Step 1: What You're Going to Need

Wemos DI Mini

DHT22 Temperature and Humidity Sensor

DS180B Waterproof Temperature Sensor

Wire wrapping wire

Perf Board

Project Case

3 Pin straight header

2 Pin Header shorting block

3K3 Resistor.

Soldering Iron and Solder

Big blob of Blutack

Hot glue gun

Small Combination Square


Craft Knife

Small Phillips screwdriver

Arduino IDE install and ready to go
Overview of Arduino install for ESP8266 by TrakerJ

The following libraries will also be needed to be installed in your Arduino IDE

WifiManager by tzapu

PubSubClient by Nick O'Leary

DHT Sensor Library by AdaFruit

OneWire by Jim Studt et al

Installing Arduino Libraries by MertArduino

Step 2: Hook Up the DHT22 Sensor

Cut about 3 inches of Red, Black and Orange wire wrapping wire.

Strip about 1/2" off of each end

Carefully wrap the exposed ends round the pins, Red for VCC, Black for GND and finally Orange for DATA

Heat up your iron and carefully solder the wire in place on each pin.

Once the solder has cooled, carefully twist the three wires their entire length to help keep them tidy.

Step 3: Make a Cut Out on the Case for the DHT22

Using you Combination square and the pencil, strike a centre line down the case from the top downwards.

Now centre the DHT22 over the centre line on the case and carefully mark the outline of the sensor itself

Using the square again, draw up the outline of the sensor more accurately

With the craft knife cut out the rectangle you have marked up, trying to be as accurate as possible and tying to keep the piece of material you remove in one piece as we will be using it later on.

Once you have cleaned up the hole, flip the case over and fit the DHT22 in the freshly cut hole. Hopefully it will be a nice snug fit.

If you happy with the fit, apply a small blob of hot melt glue to the pins of the sensor making sure to cover them completely so as to provide a bit of insulation and some relief on the soldered connections.

Now take the piece of material you removed from the hole and place it over the back of the sensor covering up where the pins are coming through the back of the pcb. Again stick it in place with some hot glue.

Step 4: Assemble the WeMos Interface

Remove the WeMos from its packaging and insert the straight mail headers into the WeMos. With the Wireless module on the underside of the board and the long pins protruding down

Hold the pins in place on a breadboard and solder them up.

Cut your perf board to size, make it 14 wide and 15 longs, perforations that is.

I use a pair of scissors to cut my boards, just take it slowly and you should get a relatively clean cut.

Now take the two female headers, the ones with the longer pins.

Plug them onto the WeMos and then slide the pins into the piece of perf board

Flip the whole lot over and solder up the headers, first of all solder the 4 corner pins as this should hold things tight while you solder up the rest of the pins. Once the solder has cooled, cut about 1/8" or 3MM off the long legs of the female headers.

Now we need to add the four screw terminals and the 3 pin header.

Insert the screw terminals onto the board so that they sit centrally below the WeMos, solder them up and once the solder has cooled insert the 3 way male header in the remaining space besides the screw terminal and solder up. If they are a bit loose and wobbly when soldering hold them in place with the Blutack

Now we need to solder up the board.

I apologize for the tatty looking bit of board in the picture, it was the only bit I had available at the time and had been previously used for another project. Also I got the Orange and Yellow locations mixed up in the image.

You need to wire up the board in the following layout.

WeMos Pin Colour Location.

D0 White Pin 1 Male header (Pin nearest screw terminals)

RST White Pin 2 Male Header (Middle pin of header)

5V Red Pin 1 screw terminals (Furthest from male header)

GND Black Pin 2 Screw terminals

D1 Yellow Pin 3 Screw terminals

D2 Orange Pin 4 Screw terminals

Try and cut your wires to a decent length that will allow you to dress them tidily once you have finished you can tack them in place with a little bit of hot glue if you wish.

Step 5: Hookup the Sensors.

Twist the VCC, Red, Wires from each sensor and insert into screw terminal 1, with 1 leg of the 3k3 resistor and tighten down the screw.

Twist the GND, Black, wires for each sensor and insert into screw terminal 2 and tighten the screw

Insert the other leg of the resistor into terminal three along with the yellow wire from the DB180S sensor and tighten the screw.

Insert the orange wire from the DHT22 sensor and tighten the screw.

Dress the wires from the DHT22 over the back of the board and in the centre so as to not foul up the pins of the WeMos module when you place it in the case.

You should find that the pins just fit over the DHT22 and will help to hold it in place quite well.

If it sits a bit proud so as to stop the back going back on snip off a little bit more from the pin on the female headers

Before you push the WeMos all the way into the board, carefully dress the DB180S cable underneath the WeMo wrapping round the outside of the board as you can see in the picture above.

Now place the header shorting block over pins 1 and 2 of the 3 way header.

Connect the USB cable to the WeMos and pop the back on the case. it should fit quite snugly but you should need to force the back on crushing down the WeMos board inside.

Step 6: The Sketch...

Download the envsens.ino sketch and save it in a folder called envsens

Open it up in the Arduino IDE and edit the following lines.

Line 27:

const PROGMEM char* MQTT_CLIENT_ID = "EnvRoom";

Change EnvRoom to something fitting with your system, EnvLounge for example.

Line 28:


Change MQTT_USER to your MQTT Servers username.

Line 29:


Change MQTT_PASSWORD to your MQTT Servers password.

For lines 30 to 32

const char* MQTT_HUMIDITY_TOPIC = "sensor/room/humid";
const char* MQTT_TEMPERATURE_TOPIC = "sensor/room/temp";

const char* MQTT_AQUARIUM_TOPIC = "sensor/room/aqua";

Set each topic so it is inline with your naming convention.

Finally Line 102:

WiFi.hostByName("", MQTT_SERVER_IP);

Change to the fully qualified domain name of your MQTT server.

Save the sketch and give it the once over and verify the sketch correcting any issues.

Once the sket passes verification we can send the sketch to the WeMos, though we must remember to move the header shorting bock from pins 1-2 to 2-3 otherwise the upload will fail.

Step 7: Connecting to the Wireless Network

Open up the Serial Monitor in the Arduino IDE, change the baud to 115200

If this Sonoff has never had a sketch uploaded to it using the WiFiManager library you should be seeing something similar to this...

Ver: 1.3


*WM: AutoConnect

*WM: Connecting as wifi client...

*WM: Using last saved values, should be faster

*WM: Connection result:

*WM: 0



*WM: Configuring access point...

*WM: ThingMaBob

*WM: AP IP address:


*WM: HTTP server started

If not and you would like to start from scratch, you will need to uncomment line 63: //wifiManager.resetSettings();

resend the sketch to the WeMos, then comment the line out again and resend once more.

Now we are set to connect to your local wireless network.

Using a smartphone or a PC with a WiFi adapter bring up the list of available networks.

Click on the network named ThingMaBob and connect to it.

You will then need to Authenticate, on Android clicking the Authenticate icon at the top of the screen will take you to the pages built into the WiFiManager, you can now select your local network and then enter your wifi key.

When you click save the settings will be written back to the Sonoff so that it will automatically connect on boot. The WeMos will now reboot and connect to your local WLAN and attempt to connect to the MQTT server.

Once the WeMos has rebooted you should see similar output to final image.

Step 8: Adding Your Sensor to HomeAssistant.

I separate all of my devices into separate files which I then include in the main configuration.yaml.

Please add the device to wherever you keep yours.

- platform: mqtt
state_topic: "sensor/lounge/temp"

name: "Temp Lounge"

qos: 0

unit_of_measurement: "ºC"

- platform: mqtt

name: "Humid Lounge"

state_topic: "sensor/lounge/humid"

qos: 0

unit_of_measurement: "%"

- platform: mqtt

name: "Aqurium Lounge"

state_topic: "sensor/lounge/aqua"

qos: 0

unit_of_measurement: "ºC"

You can alter the names to whatever you like, but you must make sure state_topic and command_topic match what you entered in the sonoff sketch on lines 30 to 32.

Finally run a test to make sure everything is ok with your new entry

/usr/local/bin/hass -c ./ --script check_config

If all appears ok restart hass

service hass-daemon restart

Place your sensor out of direct sunlight and with the waterproof sensor, if used, in your aquarium and you should now get updates every 15 minutes on your room temp and humidity alongside your aquariums temperature

Step 9: Some Optimisations.

Some of you may wonder what the 3 pin jumper is for and why you need to move it when you upload to the WeMos.

The sketch I have provided is designed to work off of battery power, hence the unit going into deep sleep for 15 minutes. This works by setting a timer which continues to run while the unit is in deep sleep.

Once the timer has timed out D0, goes high, which is connected to RST via the shorting block on pins 1 and 2.

The Wemos will then wake up take new readings and submit them to your MQTT server.

If RST is hooked up to D0 when you try to upload a new sketch it will invariably fail, hence the inclusion of the jumpers and the shorting blocks.

However, this sketch is not fully optimised to run on battery as the sensors are permanently connected to VCC. To optimise battery life, remove the VCC wire from the VCC pin on the WeMos and connect it to D3.

Add the following lines to the script.

Line 20:

#define POWERPIN D3
Line 48 and 49

digitalWrite(POWERPIN, HIGH);

Insert a line after Line 131:

digitalWrite(POWERPIN, HIGH);

Now power to the sensors will be cut while in deepsleep mode, dramatically increasing battery run time.

I currently run a DHT22 sensor in my back garden on and it has been running now on a single 3v7 1200MaH LiPo batery for well over a month.(Obviously in a suitable box so things don't get wet when it rains)

Internet of Things Contest 2017

Participated in the
Internet of Things Contest 2017