/!\ Vorläufig / Preliminary, work in progress /!\




ESP32 Door Sensor

In the German Computer magazine c't 2018-02 page 78 an interesting project with a ESP32 breakout board (WLAN and Bluetooth on board) was described:


/!\ A mouse click on a picture will expand the picture for better viewing.

/!\ A mouse click on a source program will show the listing in a new window with colour coding.

ESP32 Breakout Board

In May 2017 company WEMOS offered the ESP32 breakout board LoLin32. An ESP32 module (with FCC number) with micro-USB connector and a Lithium battery interface (usually 18650 type). The USB-UART converter is a Silicon Labs CP2104 chip (datasheet see at #Links). It is available in China for about 7 EUR.

A schematic is available.

The used voltage regulator ME6211 has just 0.1 V drop, datasheet see at #Links.

The used Linear Li-Ion Battery Charger LTC4054 is adjusted for 500 mA charge current, datasheet see at #Links.

Now Wemos is offering the LoLin32 Pro board with additional micro-SD slot, which may have a overheating problem in combination with software. The battery plug is there in parallel with the USB plug.


Also on the market aliexpress is the LoLin32 Light, which will be offered for about 4 EUR, but has no FCC number and the USB-UART converter is a cheap CH340 chip. The battery plug is there in parallel with the USB plug.

The ESP32 is supported in the Arduino IDE since September 2017, see here and for a review on Instructables.

The LoLin32 board contains the ESP-WROOM-32 module:

/!\ The pinout of the ESP32Dev Board and the Wemos LoLin32 are different.


Yes, the ESP32 has an 12 Bit Analog/Digital converter. But the accuracy is not so very good, see here, and a discussion with people from espressif.

The software API (Application Programming Interface) version 3.0 is described here.

Let's come to the details:


See on the right side the picture. The jumper can be replaced with an mA meter for measuring the standby current.

So, with 6 dB attenuation you get an input range of 0.1 V to 2.0 - 2.4 V. With a 1:3 resistor divider you get a 6.0 - 7.2 V range, which needs to be calibrated by software.

In order to scale down the LiPo battery voltage of maximum 4.2 V, you need a voltage divider 1:3. Because you have to calibrate anyway, the resistors need not to be very accurate. Additionally you need one resistor to compensate the 0.1 V offset. The schematic then looks like:

//  Voltage scaler, expand voltage range, eliminate 0.1 V offset, resistors 5% or better 
//          3.3 V --+3M3+--+--- ADC pin 34, 0.1 V...2.2 V, ADC -6dB
//                         |
// VBat 0 - 6.6 V --+220K+-+--+100K+---| GND
// 3.3 V ---+reed-contact+---Pin 33---+120K---| GND, pull down resistor

4.12 V input voltage, single values, without first value
Average: 2846, span: -33 (-0.8 %) - +22 (+0.54 %) percent of 4095 full scale
# That is not very good for a 12 bit resolution.
1,302 V = 2846 -> 1 step = 0,46 mV

A test for stability and offset showed the following values:

99.4 mV input voltage, each value was an average of 16 measurements
Average (n=16): 9
Average (n=16): 9
Average (n=16): 7
Average (n=16): 9
Average (n=16): 9
Average (n=16): 11
Average (n=16): 8
Average (n=16): 18
Average (n=16): 4
Average (n=16): 5
Average (n=16): 9
Average (n=16): 13
Average (n=16): 9
Average (n=16): 8
Average (n=16): 9
Average (n=16): 5
    Average = 9, span -5 (-0.12 %)  - 9 (+0.22 %) percent of 4095 full scale

Now the test for the battery voltage:

4.11 V input voltage, each value was an average of 16 measurements
Average (n=16): 2852
Average (n=16): 2849
Average (n=16): 2850
Average (n=16): 2848
Average (n=16): 2849
Average (n=16): 2848
Average (n=16): 2849
Average (n=16): 2851
Average (n=16): 2848
Average (n=16): 2849
Average (n=16): 2849
Average (n=16): 2848
Average (n=16): 2849
Average (n=16): 2849
Average (n=16): 2847
Average (n=16): 2848
    Average = 2949, span -2 (-0.05 %)  - +3 (+0.07 %) percent of 4095 full scale
# That looks much better now, about 11 bit resolution.

Regarding the conversion speed I got from forum postings a value of about 22 µs per conversion. With factor 16 averaging this will be a total measuring time of about 350 µs.

Software doorsensor

This software reacts on the opening and closing of a reed-contact mounted on a door or window. The reed-contact is mounted (adhesive) on the frame and the magnet on the door wing.

Upon the event opening or closing, the software sends a push telegram via MQTT broker (on a server) to the subscriber, usually a smartphone which could play a sound.

So, you could either check online via Internet if the door is closed, or get a notice if it is just being opened.

/!\ This is an interesting low cost demonstration for the MQTT push protocol, but does not replace a commercial alarm system.

The Arduino IDE original software doorsensor.ino is on github (advanced version).

The Arduino IDE (Integrated Development Environment) can be downloaded for Linux, MacOS and Windows, see at Links #1. Then you need the Hardware Extension ESP32, which can be installed with:

and the library basecamp from Merlin Shumacher (magazin c't):

With the Arduino Bibliothek/Library function also updates can be installed.

It needs an Arduino IDE library, installed in local folder Arduino/libraries/:

The extended program is here for download: doorsensor2.ino, it has the following extensions:

A typical 'door sensor switch event looks in the Arduino Serial Monitor like:

configsip: 0, SPIWP:0xee
mode:DIO, clock div:1
entry 0x40078a58
Basecamp V.0.1.6
MAC-Address: 30:ae:a4:26:62:64, Hardware MAC: 30:ae:a4:26:62:64
* ACCESS POINT PASSWORD: C:QZUJCR   <- since basecamp version 0.1.8

Wakeup caused by external signal using RTC_IO
ADC Average (n=16): 2892
Door open: 0
Battery full
Data published
Entering deep sleep



If you load the program for the first time, no personal data (WiFi, MQTT) are stored in the ESP32 flash-ROM. Therefore the program switches to an Access point mode (IP:, and offers entry fields for that data. See the screen shot above.

If you need to change the WLAN or MQTT data, reset the ESP32 5 times in an about one second interval, until it tells in the Arduino Serial Monitor the message Resetting the WiFi configuration and switches to a WLAN Access point mode, and you can change your data as explained before.

If there are unusual problems with the software, reset the ESP32 7 times in an about one second interval, then the ESP32 will be reset to factory settings.

Broker Mosquitto

As explained later you need a MQTT Broker. Because I want to keep the data at home, I use a small PC with Ubuntu 16.04 and software Mosquitto as MQTT Broker. You could use also a Raspberry Pi with Raspian for that job. A good guide for the Mosquitto installation and use can be found here.

In order to make the data flow easier understandable, see a GraphViz dot chart:


I will describe in short, what I did (Terminal):

# Installation of Mosquitto

# To get the actual Mosquitto version, add to the repository:
$ sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

# update repository
$ sudo apt-get update

# Install MQTT server
$ sudo apt-get install mosquitto

# Install MQTT client
$ sudo apt-get install mosquitto-clients

# test, if the server works:
$ ps -AF | grep mosquitto
mosquit+ 14823     1  0  1562  3876   0 Feb16 ?        00:02:38 /usr/sbin/mosquitto -c /etc/mosquitto/mosquitto.conf

# stop the server
$ sudo service mosquitto stop

# start the server
$ sudo service mosquitto start

# after a configuration change, restart
$ sudo service mosquitto restart

# See, if the IP ports 1883 and 8883 are open
$ netstat -at
tcp        0      0 *:8883                  *:*                     LISTEN
tcp        0      0 *:1883                  *:*                     LISTEN

# It works

The Mosquitto MQTT broker can be configured to require client authentication using a valid username and password before a connection is permitted.

The user name and password combination is transmitted in clear text, and is not secure without some form of transport encryption (TSL, SSH, https:).

The configuration of the Mosquitto server with user and password in detail (Terminal):

# create the password file for the first user
$ sudo mosquitto_passwd -c /etc/mosquitto/passwd <user name>
Password: <password>
Reenter password: <password>

# add additional users to the password file
$ sudo mosquitto_passwd -b /etc/mosquitto/passwd <user name> <password>

# You can also delete users from the password file
$ sudo mosquitto_passwd -D /etc/mosquitto/passwd <user name>

# Take care to mention the password file name in the config file:

# Add the lines:
  # you need <user> and <password> for access.
  allow_anonymous false
  password_file /etc/mosquitto/passwd

# Force mosquitto to read the new parameter
$ sudo service mosquitto reload

Security https:

In order to secure your sensible data you can use TLS (SSH, https:), see the description on a Raspberry Pi. I did the setup on Ubuntu 16.04 LTE.

Let's create TLS certificates with the help of Let's Encrypt. See here for the description.

# stop IP port 80 service
$ sudo service apache2 stop  

# add an actual repository
$ sudo add-apt-repository ppa:certbot/certbot

# update the repository
$ sudo apt-get update

# install the program
$ sudo apt-get install certbot

# get the certificates
$ sudo certbot certonly --standalone --preferred-challenges http-01 -d mqtt.rudiswiki.de
Saving debug log to /var/log/letsencrypt/letsencrypt.log
Plugins selected: Authenticator standalone, Installer None
Obtaining a new certificate
Performing the following challenges:
http-01 challenge for mqtt.rudiswiki.de
Waiting for verification...
Cleaning up challenges

 - Congratulations! Your certificate and chain have been saved at:
   Your key file has been saved at:
   Your cert will expire on 2018-05-16. To obtain a new or tweaked
   version of this certificate in the future, simply run certbot
   again. To non-interactively renew *all* of your certificates, run
   "certbot renew"
 - If you like Certbot, please consider supporting our work by:
   Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
   Donating to EFF:                    https://eff.org/donate-l

# start again the web server
$ sudo service apache2 start

# setup a regular refresh of the certificates
$ cat /etc/cron.d/certbot
# Eventually, this will be an opportunity to validate certificates
# haven't been revoked, etc.  Renewal will only occur if expiration
# is within 30 days.

0 */12 * * * root test -x /usr/bin/certbot -a \! -d /run/systemd/system && perl -e 'sleep int(rand(3600))' && certbot -q renew

Following the content of the mosquitto configuration files:

# Stop mosquitto
$ sudo service mosquitto stop

# Edit File /etc/mosquitto/mosquitto.conf
# add the following line
include_dir /etc/mosquitto/conf.d
# save the file

# Edit file /etc/mosquitto/conf.d/default.conf
# 2018-02-16 RR, use port 8883 for TLS security
# you need <user> and <password> for access.
allow_anonymous false

# remove the follpwing line comment # for test only
# allow_anonymous true

password_file /etc/mosquitto/passwd

# for unencrypted communication (http:)
listener 1883

# for encrypted TLS (https:, SSL) communication
listener 8883
certfile /etc/letsencrypt/live/mqtt.rudiswiki.de/cert.pem
cafile /etc/letsencrypt/live/mqtt.rudiswiki.de/chain.pem
keyfile /etc/letsencrypt/live/mqtt.rudiswiki.de/privkey.pem

# save the file

# Start mosquitto again
$ sudo service mosquitto start

# Setup in Androit App MQTT Snooper a new host:
Port number: 8883

ESP32 Deep Sleep

The ESP32 datasheet (esp32_datasheet_en.pdf, version 2.1) tells a current of about 150 uA in Table 4: Power consumption by Power Modes (page 21) for Power mode deep sleep under the condition ULP co-processor is powered down (Ultra Low Power). With the example program ExternalWakeUp2.ino I can reach a deep sleep current consumption of 0.16 mA (160 uA). So far, so good.

The current measurement was made with a digital meter Mastech MS8229, with a shunt resistor of about 2 Ohm in the mA range.

With the original program doorsensor.ino, the ESP32 in deep sleep mode, I see a current consumption of 1.6 mA, about 10 times more.

The trick is to power down the WiFi hardware before going to deep sleep, see:

#include <esp_wifi.h>
  esp_deep_sleep_start(); // 0.20 mA sleep current (battery, external pull down resistor 120K)

This is implemented in my extended program version doorsensor2.ino.

The sleep current can be further reduced by increasing the pull down resistor value.


MQTT Protocol

The MQTT protocol is very good described from company hivemq.

The Server software is the MQTT broker. A good free program for Linux, MacOS and Windows is mosquitto. A good guide can be found here.

The Clients are programs which can send data (Publisher) and receive data (Subscriber).


For Android a useful App is MQTT Snooper. Here you can play a sound when a telegram is received. It also list the events with time, which is important for the application doorsensor. See the screenshots on the right.


For Linux, MacOS and Windows a good MQTT client program is MQTT.fx, a guide can be found here.

In the client software you have to declare the names:


The time to get a signal from the doorsensor to the sound on the smartphone is:




MQTT Clients

Tested with the broker Mosquitto (version 1.4.15) on Ubuntu 16.04.




3D Housing

The 3D housing was designed with the program OpenSCAD (for Linux, MacOS and Windows). I have modified the original version from Merlin Schumacher to fit the LoLin32 board with ADC scaling resistors (3 mm higher), and added round edges to the housing.


You can download my OpenSCAD file and the .stl file for slicing (e.g. program Cura). See the grid model from OpenSCAD on the right side. You need to render (key F6) before you can see the grid model.

In order to screw the bottom plate, use countersunk head screws M3 x 30 mm and M3 nuts. Just use the two holes at the side of the outcut for the sensor cable.

The sides of the battery holder must be shortened by about 5 mm in order to fit into the cover. Then the LiPo 18650 battery cell is placed into the holder, that you can still read the polarity signs from the side. The holder is glued with hot glue to the cover (symmetric).

/!\ If you supply with 5 V via micro-USB plug, take care that the digital Ampere meter is switched ON, otherwise the battery voltage reading may not be correct.


Li-Ion Battery 18650


I bought MECO 4000mAh protected 18650 Li-Ion rechargeable batteries from https://banggood.com.

Unfortunately the measured capacity with the ELV HET20 meter shows at two batteries a real capacity (discharged down to 3.4 V @ 0.6 A) about 1250 - 1600 mAh, see the diagram above. That is far below the named capacity of 4000 mAh.

So, I can recommend batteries only with a well known brand name, like Samsung.

Parts List

  1. WeMos® LOLIN32 V1.0.0 WiFi + Bluetooth Board, https://www.banggood.com Product ID: 1164252, or https://de.aliexpress.com

  2. 100Pcs Mini Micro JST 2.0 PH 2Pin Connector Plug With 120mm Wires Cables, 3 EUR, https://www.banggood.com Product ID: 1147298

  3. 2pcs 3.7V ICR18650-26HM/FM 2600mah Button Top Protected 18650 Li-ion Battery , 8.08 + 2.74 EUR, https://www.banggood.com Product ID: 909142

  4. 5pcs DIY 1-Slot 18650 Battery Holder With Pins, 4 EUR, https://www.banggood.com Product ID: 1094146

  5. 5 stück Adhesived Sicherheit Tür Fenster Magnetkontakt Reed-schalter, 6 EUR, https://www.ebay.de, No.: 302434678197

  6. Resistors, wired, 1/4 W, 5%, 3.3 MOhm, 220 KOhm, 100 KOhm, 120 KOhm
  7. 3D printed housing, about 10 EUR
  8. 2 pieces countersunk head screws M3 x 30 mm
  9. 2 pieces M3 nuts

  1. Arduino Software IDE download (Integrated Development Environment), actual version 1.8.5.

  2. Hackaday - Wemos LoLin32 ESP32 module review

  3. Silicon Labs USB-UART converter CP2104 datasheet

  4. Datasheet Linear Technology LTC4054, Standalone Linear Li-Ion Battery Charger with Thermal Regulation

  5. Datasheet MicrOne ME6211, High Speed LDO Regulators

  6. ESP32: Neuer IoT-Chip von Espressif

  7. Instructables: IOT Made Simple: Playing With the ESP32 on Arduino IDE

  8. Smart-Home-Alarmanlagen selbst planen und einbauen (DE)

{i} Go to Top of the page

Contact Email

Please enter your Email address, if you expect an answer.

/!\ The entered Email address will not be published, or given away.

Comment this page
Are you human?

List of pages in this category:

-- RudolfReuter 2018-02-08 05:37:32

Go back to CategoryIoT or FrontPage

ESP32DoorSensor (last edited 2018-05-03 15:22:34 by RudolfReuter)