0

I've developed a fingerprint sensor using NodeMCU D1 mini powered by a 1000mah battery.

Everything seems working correctly except for battery energy consumption. I have read several topic where user says that using deepsleep fuction on NodeMCU 1000mah battery should last more than 3 month but mine doesn't reach 2 days.

This is my circuit schematic

I also report here my code snipped that I'm currently using. The circuit should wake up from deepsleep when the button is pressed and start reading the fingerprint sensor. If it detect a valid fingerprint, a message is sent to MQTT broker.

#include <Adafruit_Fingerprint.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>


const char* _ssid = "ZZZXXXYYY";
const char* _password = "pass";
const char* mqttServer = "IPADDRESS";
const int mqttPort = 1883;
const char* mqttUser = "USER";
const char* mqttPassword = "PASS";
long initialMillis = 0;
unsigned int raw=0;

// On Leonardo/Micro or others with hardware serial, use those! #0 is green wire, #1 is white
// uncomment this line:
//#define mySerial Serial

// For UNO and others without hardware serial, we must use software serial...
// pin #2 is IN from sensor (GREEN wire)
// pin #3 is OUT from arduino  (WHITE wire)
// comment these two lines if using hardware serial
SoftwareSerial mySerial(4, 5);

WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);

void setup()  
{
  Serial.begin(9600);
  pinMode(A0, INPUT);
  // Connect to Fingerprint. Set the data rate for the sensor serial port
  finger.begin(57600);
  delay(5);
  if (finger.verifyPassword()) {
    Serial.println("Found fingerprint sensor!");
  } else {
    Serial.println("Did not find fingerprint sensor :(");
    ESP.deepSleep(10 * 1000000);
  }

  finger.getTemplateCount();

  // Connect to WIFI
  WiFi.mode(WIFI_STA);
  WiFi.begin(_ssid, _password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");

  // Connect to MQTT
  client.setServer(mqttServer, mqttPort); 
  while (!client.connected()) {
    Serial.println("Connecting to MQTT...");

    if (client.connect("ESPFingerprint", mqttUser, mqttPassword )) { 
      Serial.println("Connected to MQTT server");   
    } else { 
      Serial.print("failed with state ");
      Serial.print(client.state());
      delay(2000); 
    }
  }

  // Calculate battery percentage
  raw = analogRead(A0);
  String bLevel = "{\"Battery\":" + String(raw) + "}";
  Serial.print("Battery: ");
  Serial.print(raw);
  Serial.print(" -> ");
  Serial.println(bLevel);

  initialMillis = millis();
  client.publish("tele/fingerprint/LWT", "Online");
  client.publish("tele/fingerprint/STATE", (char*)bLevel.c_str(), true);

  Serial.print("MQTT subscribed: ");
  Serial.println(WiFi.localIP());
}

void loop()
{
  unsigned long currentMillis = millis();
  int fid = getFingerprintIDez();
  if(fid != -1) {
    String fpIdstr = String(fid);
    client.publish("cmnd/fingerprint/RESULT", (char*)fpIdstr.c_str());
    Serial.println("Fingerprint correct. Going to sleep.");
    client.publish("tele/fingerprint/LWT", "Offline");
    // trigger disconnection from MQTT broker to correctly send the message before going to sleep
    client.disconnect(); 
    espClient.flush();
    // wait until connection is closed completely
    while(client.state() != -1){  
      delay(10);
    }
    ESP.deepSleep(0);
  }
  else if(currentMillis - initialMillis >= 15000){
    Serial.println("Timeout expired. Going to sleep.");
    client.publish("tele/fingerprint/LWT", "Offline");
    ESP.deepSleep(0);
  }
  delay(50);            //don't ned to run this at full speed.
}

uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      Serial.println("No finger detected");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_IMAGEFAIL:
      Serial.println("Imaging error");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK success!

  p = finger.image2Tz();
  switch (p) {
    case FINGERPRINT_OK:
      Serial.println("Image converted");
      break;
    case FINGERPRINT_IMAGEMESS:
      Serial.println("Image too messy");
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:
      Serial.println("Communication error");
      return p;
    case FINGERPRINT_FEATUREFAIL:
      Serial.println("Could not find fingerprint features");
      return p;
    case FINGERPRINT_INVALIDIMAGE:
      Serial.println("Could not find fingerprint features");
      return p;
    default:
      Serial.println("Unknown error");
      return p;
  }

  // OK converted!
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {
    Serial.println("Found a print match!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {
    Serial.println("Did not find a match");
    return p;
  } else {
    Serial.println("Unknown error");
    return p;
  }   

  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence); 

  return finger.fingerID;
}

// returns -1 if failed, otherwise returns ID #
int getFingerprintIDez() {
  uint8_t p = finger.getImage();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.image2Tz();
  if (p != FINGERPRINT_OK)  return -1;

  p = finger.fingerFastSearch();
  if (p != FINGERPRINT_OK)  return -1;

  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID); 
  Serial.print(" with confidence of "); Serial.println(finger.confidence);
  return finger.fingerID; 
}

Can somebody let me know if there is something wrong in my code or circuit?

CrazYoshi
  • 1
  • 1
  • 1
    use a esp8266 module without USB chip and voltage regulator. btw: "NodeMcu" and "Wemos/Lolin D1 mini" are two different development boards. "NodeMCU D1 mini" doesn't exist. – Juraj Mar 20 '20 at 16:40
  • Did you measure how much current the finger print module consume? You put ESP8266 to sleep but the finger print module will still consume power. – hcheung Mar 21 '20 at 05:31
  • If your 1000mAh last about two days, it means that the overall circuit take up about 20mA steady current throughout the two days, and it is likely consumed by the finger print module. – hcheung Mar 21 '20 at 05:34
  • How do you suggest to switch off the fingerprint sensor when the esp8266 is in deepsleep mode? – CrazYoshi Mar 21 '20 at 17:15

0 Answers0