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:
Battery powered Sensor for detecting opening/closing of your house door and pushing this event to your smart phone via Internet with the MQTT protocol.
Open/close event -> MQTT broker (server) -> MQTT client (Smartphone APP)
All Software free available (mostly via github).
- Low cost (about 24 EUR, if more than one is build)
- Small size (3D printed housing)
Battery powered (LiPo 18650 cell)
- Integrated charge circuit
- Development under Arduino IDE
If you allow a push signal in a MQTT App on a Smartphone, it empties quickly your battery.
- If you want to have a small housing, you need a 3D printer
- You need a lot of background knowledge about software
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.
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 LoLin32 board contains the ESP-WROOM-32 module:
- CPU clock: Standard 80 MHz
- RAM: 520 KB RAM
- Flash: external 4 MB
- I/O: 26 pins
- Temperature sensor: range of -40°C to 125°C
- Hall sensor
The pinout of the ESP32Dev Board and the Wemos LoLin32 are different.
The software API (Application Programming Interface) version 3.0 is described here.
Let's come to the details:
Supply voltage, analog: 3.3 V
Reference voltage: nominal 1.1 V, span: 1.0 - 1.2 Volt, see here
Offset: the ADC has an offset voltage of 0.1 V.
Voltage Scaling: Espressif tell about attenuation. The highest usable attenuation is 6 dB = divider factor 2. The maximum attenuation is 11 dB (software default), but it is not linear, and needs a lookup table for using it.
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.
Resolution: The maximum is 12 bit (0..4095, software default).
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
Noise: Another problem is noise. I tried to reduce that by averaging, factor 16. Another problem is the deviation of the first ADC value. Therefore I just swallow that in the software. Let's see the measurement values:
4.12 V input voltage, single values, without first value 2855 2845 2841 2844 2868 2843 2842 2843 2847 2846 2842 2813 2863 2842 2843 2862 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.
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:
Arduino Menu ->Sketch->Bibliothek/Library->Bibliothek/Library verwalten/manage->ESP32
and the library basecamp from Merlin Shumacher (magazin c't):
Arduino Menu ->Sketch->Bibliothek/Library->Bibliothek/Library verwalten/manage->Basecamp
With the Arduino Bibliothek/Library function also updates can be installed.
It needs an Arduino IDE library, installed in local folder Arduino/libraries/:
Basecamp on github. This library has a few dependencies, also installed in Arduino/libraries/:
The extended program is here for download: doorsensor2.ino, it has the following extensions:
- Analog calibration with resistor voltage divider
1.65 -> 0.20 mA battery current consumption in deep sleep mode (with external pull down resistor 120 KOhm)
- avoid sleep while in configuration setup
- print wakeup reason for debug reason
A typical 'door sensor switch event looks in the Arduino Serial Monitor like:
rst:0x5 (DEEPSLEEP_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT) configsip: 0, SPIWP:0xee clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00 mode:DIO, clock div:1 load:0x3fff0018,len:4 load:0x3fff001c,len:956 load:0x40078000,len:0 load:0x40078000,len:13076 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: 192.168.4.1), and offers entry fields for that data. See the screen shot above.
First you have to connect your tablet (smartphone) to the ESP32 Access point (SSID: ESP32_<MAC-number>, password: C:QZUJCR). The password is needed since basecamp version 0.1.8.
Next enter in your web browser the URL 192.168.4.1, and you will see the data entry mask. See the screen shot on the right.
- Key in the needed data.
The error message in the screen shot does come from Arduino program MQTT snooper, because the tablet is no longer connected to the internet. That will be restored when you tap on the button: Save. Then the ESP32 switches back to Station mode, and the tablet connects back to the WLAN router, with Internet access.
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.
Serial Monitor print: Resetting the WiFi configuration
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.
Serial Monitor print: Factory reset was forced
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: /etc/mosquitto/conf.d/default.conf # 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
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 IMPORTANT NOTES: - Congratulations! Your certificate and chain have been saved at: /etc/letsencrypt/live/mqtt.rudiswiki.de/fullchain.pem Your key file has been saved at: /etc/letsencrypt/live/mqtt.rudiswiki.de/privkey.pem 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. SHELL=/bin/sh PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin 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_wifi_stop(); 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.
The build in ESP32 pull down resistor value is about 41 KOhm: 3.3 V / 41K = about 80 µA.
If you use an external pull down resistor with for example 120 KOhm: 3.3 V / 120K = 27 µA.
That could lower the deep sleep current: 250 µA to about 200 µA.
Calculated in standby time: 2300 mAh (90%) battery capacity / 0.25 mAh = 9.200 h = 383 days
Calculated in standby time: 2300 mAh (90%) battery capacity / 0.20 mAh = 11,500 h = 479 days
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.
Screenshot #1: It shows the subscribed MQTT topics
Screenshot #2: It shows the input messages
Screenshot #3: It shows the Host names and the next possible action
Secure connection: means connection to the broker via TLS (SSH, https:)
Screenshot #4: It shows the status of the message (close, open) and the MQTT service (connected, disconnected)
In the client software you have to declare the names:
MQTT topic name: sensor-irh
MQTT input message: stat/sensor-irh/status
state: open / closed
MQTT input message: stat/sensor-irh/batteryvalue
value: e.g. 405 = 4.05 V
MQTT input message: stat/sensor-irh/battery
state: full / empty (about 90% battery capacity = 3.4 V)
The time to get a signal from the doorsensor to the sound on the smartphone is:
open: about 5 seconds
close: about 3 seconds
Tested with the broker Mosquitto (version 1.4.15) on Ubuntu 16.04.
Android: (version 7.1.1)
MQTT Snooper: In the Android Play store I found this MQTT Client. The setup is shown in the previous chapter.
On the event of a message you can play a sound.
The Smartphone battery load is very high with 8%, see the screenshot on the right.
In the messages list the time is shown, but the date is missing.
iOS Clients: (version 10.3.3) I tested 5 client apps. Just two of them work .
MQTT Inspector: (Version 5.0.3, 2,29 €)
Drawback: If the iPhone switches to standby the connection is lost. If the iPhone is switched on again, you have to renew manually the SUBscription.
MQTTool: (version 2.2)
Drawback: If the iPhone switches to standby the connection is lost, but automatically renewed when you switch the iPhone on again.
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.
Size: Length x Width x height: 99 x 30 + 5 (plug housing) x 34 mm
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.
100Pcs Mini Micro JST 2.0 PH 2Pin Connector Plug With 120mm Wires Cables, 3 EUR, https://www.banggood.com Product ID: 1147298
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
5pcs DIY 1-Slot 18650 Battery Holder With Pins, 4 EUR, https://www.banggood.com Product ID: 1094146
5 stück Adhesived Sicherheit Tür Fenster Magnetkontakt Reed-schalter, 6 EUR, https://www.ebay.de, No.: 302434678197
- Resistors, wired, 1/4 W, 5%, 3.3 MOhm, 220 KOhm, 100 KOhm, 120 KOhm
- 3D printed housing, about 10 EUR
- 2 pieces countersunk head screws M3 x 30 mm
- 2 pieces M3 nuts
Arduino Software IDE download (Integrated Development Environment), actual version 1.8.5.
Please enter your Email address, if you expect an answer.
The entered Email address will not be published, or given away.
List of pages in this category:
-- RudolfReuter 2018-02-08 05:37:32
Go back to CategoryIoT or FrontPage