Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By swimmingmachine
#87205 Hi everyone,

I made a door sensor using Wemos D1 mini and a pair of magnetic sensor by following the schematic in this link

When the sensor is triggered, it will wake up from deep sleep mode, connect to WiFi and send a MQTT message to the cloud. The sensor is in deep sleep mode when it is not triggered (door open/close). The code is below.

However, the 3.7v 1800mAh lithium Ion battery that I used only could power the sensor for 1-2 days. This was when the door was closed most of the time. I only tested opening the door to trigger the sensor twice.

I am not sure if this is normal for a 1800mAh battery with only 1-2 triggers. If not, where could consume much power? Thank you for your help.

Code: Select all#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

const char* ssid = "XXX";
const char* password = "XXX";

#define ORG "XXX"
#define DEVICE_TYPE "door_sensor"
#define DEVICE_ID "door001"
#define TOKEN "XXX"

char server[] = ORG "XXX";
char topic[] = "iot-2/evt/status/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = TOKEN;
char clientId[] = "d:" ORG ":" DEVICE_TYPE ":" DEVICE_ID;

const char publishTopic[] = "iot-2/evt/status/fmt/json";
const char responseTopic[] = "iotdm-1/response";
const char manageTopic[] = "iotdevice-1/mgmt/manage";
const char updateTopic[] = "iotdm-1/device/update";
const char rebootTopic[] = "iotdm-1/mgmt/initiate/device/reboot";

void callback(char* topic, byte* payload, unsigned int payloadLength);

WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

void connectWIFI() {
  // Connect to WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.println("WiFi connected");

void mqttConnect() {
 if (!!!client.connected()) {
   Serial.print("Reconnecting MQTT client to "); Serial.println(server);
   while (!!!client.connect(clientId, authMethod, token)) {

void setup() {

  // Wait for serial to initialize.
  while(!Serial) { }

  Serial.println("I'm awake.");
  Serial.println("Door sensor setup! ");
  Serial.println("Going into deep sleep");


void loop() {

void publishData() {
 String payload = "{\"d\":{\"sensorID\":\"001\",\"detection\":\"1\"";
  //payload += millis() / 1000;
  payload += "}}";
  Serial.print("Sending payload: "); Serial.println(payload);
  if (client.publish(topic, (char*) payload.c_str())) {
    Serial.println("Publish ok");
  } else {
    Serial.println("Publish failed");


void callback(char* topic, byte* payload, unsigned int payloadLength) {
 Serial.print("callback invoked for topic: "); Serial.println(topic);



You do not have the required permissions to view the files attached to this post.
User avatar
By AcmeUK
#87211 The D1 mini is not a low power deep sleep device.
You need to use something like a ESP 12F.

To see how Board member btidey approached minimising battery drain in deep sleep have a look here:- ... -Watering/

His target was a very low quiescent current (< 20uA) for long battery life
User avatar
By btidey
#87216 It is certainly correct that if you want to maximise battery life you need to use a raw module like an ESP-12F without any extraneous circuitry like the USB serial chip or a less than optimal regulator.

The example given of the watering project gives over a year of use between charges from a single 2000mAH Lithium cell. I also have magnetic and PIR sensors running on 400mAh batteries for many months.

Having said that your numbers don't seem to add up even with a Wemos D1. If you are only getting 1 - 2days out of a 1800mAH battery then that implies an average current of say 50mA. I would expect a D1 to be taking something like 250uA in deep sleep (as opposed to 20uA with ESP-12F) which should give much more than 2 days usage. It sounds like it is not actually spending much time in deep sleep.

The first thing to do is to check you are really getting into deep sleep and the best way of doing that is to put a digital multimeter in current mode in line with the power feed. That way you can see what current is actually being drawn and work from there.
User avatar
By jesusdavidcab
#88413 :shock: :shock: Hi! Everybody says the same thing. The naked true is that wemos d1 is only for programming a first prototype and you will never get a low power device with it. The best you will get is 0,18 mA. Once you get your prototype working, you should make a custom pcb with some.of the pcb makers that you can find online on the internet. If you want to do it at home, the best for you is buying a esp-12F module and make a home pcb with the rest of circuit and female conector for the esp-12F. I recommend to you that don't try to solder your own esp8266x on a custom pcb, but, if you get it, please send a pic.

Here it is a pic of a home project using deep sleep mode with a wemos d1, I get 0,18mA as the best. It is calling device that send an msg on udp to other devices that ring an alarm, in order to take care of an elderly. I will take a esp12F and a Cr2032 battery of 100mah, and I expect to get 30uA with it so it would last about 100/0,03=3333 h= 120 days of continuous working. Can you guess if I use a 1700mah battery?

Best regards.
You do not have the required permissions to view the files attached to this post.