-->
Page 1 of 1

Using software serial on ESP8266

PostPosted: Fri Mar 29, 2019 6:37 pm
by Adame542
I am trying to interface the Adafruit fingerprint sensor with the ESP8266 module through the Arduino IDE. Originally I had the sensor connected to an Arduino Uno and set up with software serial at 9600 baud which worked fine. Now, I want to connect this project to Node-RED running on an Amazon webserver. My issue is when trying to move my code to the ESP8266 module the software serial on the ESP8266 module no longer detects the sensor. From what I have gathered online this could be because the baud rate on the module is set at 115200 baud by default and this is questionably supported, if at all, by the default software serial. I am wondering what the correct approach would be to implement 'software serial' on an ESP8266 module to work with my purposes. I have heard people using AT commands to change the baud rate down to 9600 but some of the commands bricked their devices so I have been hesitant to try this yet. Below I have pasted the code that I currently have. This is my first post so if you need anymore information to help, I'll add it as necessary. Thanks,

Code: Select all/***************************************************
This code runs on the MCU to allow or deny access via LED control signals
 ****************************************************/
//

#include <Adafruit_Fingerprint.h>                               //include Adafruit sensor libraries
#include <SoftwareSerial.h>
SoftwareSerial mySerial(12, 13, false, 256);                                  // Sets up software serial to be used by pins #12 (Rx) and #13 (Tx)

Adafruit_Fingerprint finger = Adafruit_Fingerprint(&mySerial);
//Buzzer
const int buzzPin = 16;                                          //Pin for buzzer
const int RLEDPin = 0;                                           //Pin for Red LED
const int GLEDPin = 13;                                         //Pin for green LED
const int YLEDPin = 15;                                         //Pin for yellow LED
const int ConfThres = 300;                                      //Confidence threshold (Increasing this will cause sensor to be more secure)
void setup() 
{
  pinMode(0,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(15,OUTPUT);
  Serial.begin(9600);                                           //Begins serial communication at 9600 baud
  while (!Serial); 
  delay(100);
  Serial.println("\n\nFingerprint Sensor detection test");      //Prints sensor detect test                                                       
  finger.begin(57600);                                          // set the data rate for the sensor serial port
                                                                //Scans to see if the fingerprint sensor can be found
  if (finger.verifyPassword()) {                                //Verifies password set in fingerprint sensor
    Serial.println("Fingerprint sensor has been found");        //if fingerprint sensor is found
  } else {
    Serial.println("Did not find fingerprint sensor, please reset");  //Print error if fingerprint sensor is not found
      digitalWrite(RLEDPin,HIGH);                               //RED LED on if no sensor found
      while (1) { delay(1); }
  }

  finger.getTemplateCount();                                    //Function to get the number of fingerprints stored(up to 162)
  Serial.print("Sensor contains ");                             //Print the number of fingerprints stored
  Serial.print(finger.templateCount);
  Serial.println(" fingerprints"); 
 
  Serial.println("Waiting for valid fingerprint...");           //Next loop waits for a valid fingerprint
}

void loop()                                                     //Continue Running loop
{
  if(getFingerprintIDez()>=ConfThres){                          //if valid fingerprint confidence is above threshold, buzz 'correct sound'
     buzzer(true);
    }
  delay(50);                                                    //Slows down the loop from running full speed
}
//This function captures the fingerprint image
uint8_t getFingerprintID() {
  uint8_t p = finger.getImage();                                //.getImage() is the function used by the DSP chip to capture the fingerprint
  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!

//This function converts the image and looks for features in the scanned print to be compared with the fingerprints stored in the data base
  p = finger.image2Tz();                                        //.image2Tz is the function used by the DSP chip
  switch (p) {                                                  //Case test to test if the image was correctly decoded or tests a set of errors
    case FINGERPRINT_OK:                                        //Tests case if the fingerprint was ok
      Serial.println("Image converted");                        //Print corresponding message
      break;
    case FINGERPRINT_IMAGEMESS:                                 //Tests case if the image was to noisy
      Serial.println("Image too noisy");                        //Print corresponding message
      return p;
    case FINGERPRINT_PACKETRECIEVEERR:                          //Tests case if there was a communication error with the sensor
      Serial.println("Communication error");                    //Print corresponding message
      return p;
    case FINGERPRINT_FEATUREFAIL:                               //Tests case if no valid features were found with scanned fingerprint
      Serial.println("Could not find fingerprint features");    //Print corresponding message
      return p;
    case FINGERPRINT_INVALIDIMAGE:                              //Tests case if no valid features were found with scanned fingerprint
      Serial.println("Could not find fingerprint features");    //Print corresponding message
      return p;
    default:                                                    //Else:
      Serial.println("Unknown error");                          //Print corresponding message
      return p;
  }
 
                                                                //Continue if "Image Converted"
  p = finger.fingerFastSearch();
  if (p == FINGERPRINT_OK) {                                    //If scanned fingerprint features match that a saved fingerprint
    Serial.println("Found a print match!");                     //Print that a match was found
  } else if (p == FINGERPRINT_PACKETRECIEVEERR) {               //If there was a communication error
    Serial.println("Communication error");                      //Print there was a comm error
    return p;
  } else if (p == FINGERPRINT_NOTFOUND) {                       //If scanned fingerprint does not match a saved fingerprint
    Serial.println("Did not find a match");                     //Print that no match was found
    return p;
  } else {
    Serial.println("Unknown error");                            //Else: Print unknown error
    return p;
  }   
 
  // found a match!
  Serial.print("Found ID #"); Serial.print(finger.fingerID);    //If a match was found, print the ID of the fingerprint
  Serial.print(" with confidence of ");
  Serial.println(finger.confidence);                            //Calculates the confidence of the compared images

  return finger.fingerID;
}

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

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

  p = finger.fingerFastSearch();                                 //Calls .fingerFastSearch function
  if (p != FINGERPRINT_OK)  return -1;
 
  // found a match!
if(finger.confidence<ConfThres)                                  //if confidence of fingerprint is less than set threshold, buzz 'false tone'
  buzzer(false);
  digitalWrite(YLEDPin,LOW);                                      //Turns off Yellow LED when match is found                                 
  delay(1000);
  digitalWrite(YLEDPin,HIGH);                                     //Turn on yellow LED signaling Fingerprint sensor is ready to scan fingerprint
  Serial.print("Found ID #");
  Serial.print(finger.fingerID);                                  //Print fingerprint ID
  Serial.print(" with confidence of ");
  Serial.println(finger.confidence);                              //Print confidence of fingerprint scan
  return finger.confidence;                                       //return from function
}
void buzzer(boolean valid){
  if(valid){                                                      //if fingerprints matched,
    digitalWrite(GLEDPin,HIGH);                                     //Turns on Green LED when match is found
    delay(1000);                                                 
    digitalWrite(GLEDPin,LOW); 
    tone(buzzPin, 523, 200);                                      //buzz 'correct tone'
  }
  else if(!valid){                                                //else if invalid
    digitalWrite(RLEDPin,HIGH);                                   //turn on red LED for some time
    delay(1000);
    digitalWrite(RLEDPin,LOW);                                   
    tone(buzzPin, 123, 600);                                      //buzz 'incorrect tone'
  }
}

Re: Using software serial on ESP8266

PostPosted: Sat Apr 13, 2019 2:01 pm
by tgv_boost
Hi there,
had serious difficulties with Software Serial on a Wemos D1 and compatible together with NeoGPS, OLED Display and WLAN webserver. I tried all sort of Timings and tools, but failed. Today it works, but what did the trick? In the end it was hardware serial, no other choice. Software serial apparently has some weakness when it comes to Timing. As soon as you constantly need to feed hungry objects simultanously to other tasks , you most likely will fail with software serial.
So, maybe an idea for you to give it a try with hardware serial.
Good luck