-->
Page 1 of 1

ESP-12f to Fingerprint via Softserial

PostPosted: Thu Apr 22, 2021 2:06 pm
by Marc BOettinger
Hi,

I know softserial is unstable. Anyhow, I'm trying to connect my esp12f since many hours to connect to an fingerprint. I enrolled the fingerprints via an pro mini, did set an passwort, everything fine.

But connecting to the esp-12f doesn't work. It simply goes into the OTA Update. Here is my sketch:

Code: Select all/***************************************************
  This is an example sketch for our optical Fingerprint sensor

  Designed specifically to work with the Adafruit BMP085 Breakout
  ----> http://www.adafruit.com/products/751

  These displays use TTL Serial to communicate, 2 pins are required to
  interface
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries. 
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#include <Adafruit_Fingerprint.h>
#include <SPI.h>
#include <Wire.h>
#include <WiFiClient.h>
#include <PubSubClient.h> // Aus Wifi Mqtt
#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoOTA.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13);
//tx - mosi PIN 13  - rc503 grĂ¼n - rx
//rx - miso PIN 12  - rc503 gelb - tx


Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial, "myPasswordInDigits");

//************************************************************************
/* Set these to your desired credentials. */
const char *ssid = "ssid";  //ENTER YOUR WIFI SETTINGS
const char *password = "mypassword";
const char* mqttServer = "mymqtt"; // Broker PI
const int mqttPort = 1883; // Broker PI Port
const char* mqttUser = "FP"; // Broker PI Name
const char* mqttPassword = "pwd"; // Broker PI Password

WiFiClient espClient;
PubSubClient client(espClient);
char* mqttio = "12345"; // Broker PI Wert
char* confidence = "67890"; // Broker PI Wert

uint8_t id;


//************************************************************************


void setup() 
{
  // Serial.begin(115200);
  // Serial.println("\n\nConnect to Wifi");
  connectToWiFi();

    // mqtt
  client.setServer(mqttServer, mqttPort);
  client.setCallback(callback);
 
  while (!client.connected()) {
  // Serial.println("Connecting to MQTT...");
 
  if (client.connect("ESP8266Client", mqttUser, mqttPassword )) {
 
    // Serial.println("connected"); 
 
  } else {
 
    // Serial.print("failed with state ");
    // Serial.print(client.state());
    // Serial.print("looking for update ");

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
    ArduinoOTA.begin();
    while(1) {
      ArduinoOTA.handle();
      delay(50);
    }
 
  }
  }

 
 
  // Serial.println("\n\nAdafruit finger detect test");
  // set the data rate for the sensor serial port

  delay(1000);

  mySerial.begin(57600, SWSERIAL_8N1, 12, 13, false, 256, 256);
 
  finger.begin(57600);
 
  if (finger.verifyPassword()) {
    // Serial.println("Found fingerprint sensor!");
    // send command to turn on R503 led
    finger.led_control(1,100,2,1); // code(1-6),speed(1-255),color(1-R/2-B/3_P),time(1-255)

  } else {
    // Serial.println("Did not find fingerprint sensor :(");

   client.subscribe("Error");
   client.publish("Error", "No FP");

   
    delay(60000);
   
    // Serial.print("failed with state ");
    // Serial.print(client.state());
    // Serial.print("looking for update ");

  ArduinoOTA.onStart([]() {
    String type;
    if (ArduinoOTA.getCommand() == U_FLASH) {
      type = "sketch";
    } else { // U_FS
      type = "filesystem";
    }

    // NOTE: if updating FS this would be the place to unmount FS using FS.end()
    Serial.println("Start updating " + type);
  });
    ArduinoOTA.begin();
    while(1) {
      ArduinoOTA.handle();
      delay(50);
    }
   
  }

  finger.getTemplateCount();
  // Serial.print("Sensor contains "); // Serial.print(finger.templateCount); // Serial.println(" templates");
  // Serial.println("Waiting for valid finger...");

 
  client.subscribe("Fingerprint");
  client.publish("Fingerprint", "FP1");
 
}


void callback(char* topic, byte* payload, unsigned int length) {

// Serial.print("Message arrived in topic: ");
// Serial.println(topic);

// Serial.print("Message:");
for (int i = 0; i < length; i++) {
// Serial.print((char)payload[i]);
}

// Serial.println();
// Serial.println("-----------------------");

}


void loop()                     // run over and over again
{
  //check if there's a connection to WiFi or not
  if(WiFi.status() != WL_CONNECTED){
    connectToWiFi();
  }

  if (!client.connected()) {
    client.connect("ESP8266Client", mqttUser, mqttPassword );
  }
   
  if (getFingerprintIDez() == 9)  {
    delay(2000);
    getFingerprintEnroll();
  }
  delay(50);            //don't ned to run this at full speed.
}


// 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) {
    // send command to turn on R503 led
    finger.led_control(1,100,1,3); // code(1-6),speed(1-255),color(1-R/2-B/3_P),time(1-255)

    return -1;
  }
 
  // found a match!
  // Serial.print("Found ID #"); // Serial.print(finger.fingerID);
  // Serial.print(" with confidence of "); // Serial.println(finger.confidence);
  // send command to turn on R503 led
  finger.led_control(1,100,2,3); // code(1-6),speed(1-255),color(1-R/2-B/3_P),time(1-255)

  // mqtt
 
  sprintf(mqttio,"%d",finger.fingerID);
  sprintf(confidence,"%d",finger.confidence);
   
  client.subscribe("Confidence");
  client.publish("Confidence", confidence);
   
  client.subscribe("FingerprintNr");
  client.publish("FingerprintNr", mqttio);

  return finger.fingerID;
}

void connectToWiFi(){
    WiFi.mode(WIFI_OFF);        //Prevents reconnection issue (taking too long to connect)
    delay(1000);
    WiFi.mode(WIFI_STA);
    // Serial.print("Connecting to ");
    // Serial.println(ssid);
    WiFi.begin(ssid, password);
 
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      // Serial.print(".");
    }
    // Serial.println("");
    // Serial.println("Connected");
   
   
    // Serial.print("IP address: ");
    // Serial.println(WiFi.localIP());  //IP address assigned to your ESP
}







uint8_t getFingerprintEnroll() {

  // send command to turn on R503 led
  finger.led_control(1,100,3,3); // code(1-6),speed(1-255),color(1-R/2-B/3_P),time(1-255)

  id = min(finger.templateCount + 1, 127);
  int p = -1;
  // Serial.print("Waiting for valid finger to enroll as #"); // Serial.println(id);
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      // Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      // Serial.println(".");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      // Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      // Serial.println("Imaging error");
      break;
    default:
      // Serial.println("Unknown error");
      break;
    }
  }


 
  // OK success!

  p = finger.image2Tz(1);
  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;
  }

  // send command to turn on R503 led
  finger.led_control(1,100,3,3); // code(1-6),speed(1-255),color(1-R/2-B/3_P),time(1-255)

 
  // Serial.println("Remove finger");
  delay(2000);
  p = 0;
  while (p != FINGERPRINT_NOFINGER) {
    p = finger.getImage();
  }
  // Serial.print("ID "); // Serial.println(id);
  p = -1;
  // Serial.println("Place same finger again");
  while (p != FINGERPRINT_OK) {
    p = finger.getImage();
    switch (p) {
    case FINGERPRINT_OK:
      // Serial.println("Image taken");
      break;
    case FINGERPRINT_NOFINGER:
      // Serial.print(".");
      break;
    case FINGERPRINT_PACKETRECIEVEERR:
      // Serial.println("Communication error");
      break;
    case FINGERPRINT_IMAGEFAIL:
      // Serial.println("Imaging error");
      break;
    default:
      // Serial.println("Unknown error");
      break;
    }
  }

  // OK success!

  // send command to turn on R503 led
  finger.led_control(1,100,3,3); // code(1-6),speed(1-255),color(1-R/2-B/3_P),time(1-255)

  p = finger.image2Tz(2);
  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!
  // Serial.print("Creating model for #");  // Serial.println(id);
 
  p = finger.createModel();
  if (p == FINGERPRINT_OK) {
    // Serial.println("Prints matched!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    // Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_ENROLLMISMATCH) {
    // Serial.println("Fingerprints did not match");
    return p;
  } else {
    // Serial.println("Unknown error");
    return p;
  }   
 
  // Serial.print("ID "); // Serial.println(id);
  p = finger.storeModel(id);
  if (p == FINGERPRINT_OK) {
    // Serial.println("Stored!");
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {
    // Serial.println("Communication error");
    return p;
  } else if (p == FINGERPRINT_BADLOCATION) {
    // Serial.println("Could not store in that location");
    return p;
  } else if (p == FINGERPRINT_FLASHERR) {
    // Serial.println("Error writing to flash");
    return p;
  } else {
    // Serial.println("Unknown error");
    return p;
  }   
}


Any help is appreciated.

Marc

Re: ESP-12f to Fingerprint via Softserial

PostPosted: Sat Apr 24, 2021 8:23 am
by schufti
then disable OTA to see if the softserial works...

Re: ESP-12f to Fingerprint via Softserial

PostPosted: Mon Apr 26, 2021 3:52 am
by Marc BOettinger
Disabled wifi and mqtt completely. No success. Very strange.