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.
#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);
}