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 https://diyprojects.io/esp8266-deep-sleep-mode-test-wake-pir-motion-detector/#.XsRJHRNKjPB.

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.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println(WiFi.localIP());
}

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


void setup() {
  Serial.begin(115200);
  Serial.setTimeout(2000);

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

  Serial.println("I'm awake.");
  Serial.println("Door sensor setup! ");
 
  connectWIFI();
  mqttConnect();
  publishData();
  Serial.println(WiFi.status());
  delay(500);
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);
  delay(500);
  Serial.println(WiFi.status());
  delay(500);
 
  Serial.println("Going into deep sleep");
 

  [b]ESP.deepSleep(0);[/b]
}

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");
  }

  delay(5000);
}


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


IMG_9006.jpg

IMG_9007.jpg

IMG_9008.jpg
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:- https://www.instructables.com/id/Batter ... -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.